public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb.base/info-os.c: clean up SysV IPC on error
@ 2015-01-30 20:16 Don Breazeal
  2015-01-31 22:22 ` Maciej W. Rozycki
  0 siblings, 1 reply; 5+ messages in thread
From: Don Breazeal @ 2015-01-30 20:16 UTC (permalink / raw)
  To: gdb-patches

We have noticed here that over time some of our test systems accumulate
stale System V IPC resources.  At least some of this is due to a GDB test
program, gdb.base/info-os.c.  The program doesn't clean up any allocated
IPC objects when an error occurs.  System V IPC objects will stay around
forever unless they are explicitly removed (or until a reboot).

This patch puts the IPC cleanup code into a function and calls the
function everywhere that a fatal error can occur, as well as at 
successful termination.

I looked at changing the SysV IPC key for allocating the IPC objects
to IPC_PRIVATE.  That would prevent a namespace conflict with the key.
However, the test needs to read the actual key number from the 'info os'
command output, and IPC_PRIVATE won't work for that.

One can clean up stale resources allocated by this test on Linux systems
by running 'ipcs' and looking for shared memory objects with keys in
the range 0xf55-0x133d, semaphore objects with keys in the range
0x1d04-20ec, and message queue objects in the range 0x14ae-0x1896.  Use
the 'ipcrm' command to delete the IPC objects.  Note that other programs
(like vncserver) use these objects, so make sure that the object is stale
before deleting it.

Tested on x86_64 Ubuntu.

OK?

thanks
--Don

gdb/testsuite/
2015-01-29  Don Breazeal  <dbreazea@my.domain.org>

	* gdb.base/info-os.c (shmid, semid, msqid): Make variables static
	and initialize them.
	(ipc_cleanup): New function.
	(main): Don't declare shmid, semid, and msqid.  Call ipc_cleanup
	on error, or on exit in place of making 'remove' calls directly.

---
 gdb/testsuite/gdb.base/info-os.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/gdb/testsuite/gdb.base/info-os.c b/gdb/testsuite/gdb.base/info-os.c
index ae2606a..d6e3e50 100644
--- a/gdb/testsuite/gdb.base/info-os.c
+++ b/gdb/testsuite/gdb.base/info-os.c
@@ -26,6 +26,22 @@
 
 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
+/* System V IPC identifiers.  */
+static int shmid = -1, semid = -1, msqid = -1;
+
+/* Delete any System V IPC resources that were allocated.  */
+
+static void
+ipc_cleanup (void)
+{
+  if (shmid >= 0)
+    shmctl (shmid, IPC_RMID, NULL);
+  if (semid >= 0)
+    semctl (semid, 0, IPC_RMID, NULL);
+  if (msqid >= 0)
+    msgctl (msqid, IPC_RMID, NULL);
+}
+
 void *
 thread_proc (void *args)
 {
@@ -38,7 +54,6 @@ main (void)
 {
   const int flags = IPC_CREAT | 0666;
   key_t shmkey = 3925, semkey = 7428, msgkey = 5294;
-  int shmid, semid, msqid;
   FILE *fd;
   pthread_t thread;
   struct sockaddr_in sock_addr;
@@ -74,6 +89,7 @@ main (void)
   if (semid < 0)
     {
       printf ("Cannot create semaphore after %d tries.\n", retries);
+      ipc_cleanup ();
       return 1;
     }
 
@@ -89,6 +105,7 @@ main (void)
   if (msqid < 0)
     {
       printf ("Cannot create message queue after %d tries.\n", retries);
+      ipc_cleanup ();
       return 1;
     }
 
@@ -102,6 +119,7 @@ main (void)
   if (sock < 0)
     {
       printf ("Cannot create socket.\n");
+      ipc_cleanup ();
       return 1;
     }
  
@@ -113,6 +131,7 @@ main (void)
   if (status < 0)
     {
       printf ("Cannot bind socket.\n");
+      ipc_cleanup ();
       return 1;
     }
 
@@ -122,6 +141,7 @@ main (void)
   if (status < 0)
     {
       printf ("Cannot find name of socket.\n");
+      ipc_cleanup ();
       return 1;
     }
   port = ntohs (sock_addr.sin_port);
@@ -130,14 +150,13 @@ main (void)
   if (status < 0)
     {
       printf ("Cannot listen on socket.\n");
+      ipc_cleanup ();
       return 1;
     }
 
   /* Set breakpoint here.  */
 
-  shmctl (shmid, IPC_RMID, NULL);
-  semctl (semid, 0, IPC_RMID, NULL);
-  msgctl (msqid, IPC_RMID, NULL);
+  ipc_cleanup ();
   fclose (fd);
   close (sock);
 
-- 
1.8.1.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] gdb.base/info-os.c: clean up SysV IPC on error
  2015-01-30 20:16 [PATCH] gdb.base/info-os.c: clean up SysV IPC on error Don Breazeal
@ 2015-01-31 22:22 ` Maciej W. Rozycki
  2015-02-02 22:37   ` [PATCH v2] gdb.base/info-os.c: Clean " Don Breazeal
  0 siblings, 1 reply; 5+ messages in thread
From: Maciej W. Rozycki @ 2015-01-31 22:22 UTC (permalink / raw)
  To: Don Breazeal; +Cc: gdb-patches

Don,

> We have noticed here that over time some of our test systems accumulate
> stale System V IPC resources.  At least some of this is due to a GDB test
> program, gdb.base/info-os.c.  The program doesn't clean up any allocated
> IPC objects when an error occurs.  System V IPC objects will stay around
> forever unless they are explicitly removed (or until a reboot).
> 
> This patch puts the IPC cleanup code into a function and calls the
> function everywhere that a fatal error can occur, as well as at 
> successful termination.

 Use `atexit' to run `ipc_cleanup' automagically instead maybe?  This way 
you won't have to run the function manually at each exit point and also it 
won't be missed on any future updates.

 Just a thought if you want to experiment with it, there's nothing really 
wrong with your proposal as it stands.

  Maciej

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] gdb.base/info-os.c: Clean up SysV IPC on error.
  2015-01-31 22:22 ` Maciej W. Rozycki
@ 2015-02-02 22:37   ` Don Breazeal
  2015-02-03 15:32     ` Pedro Alves
  0 siblings, 1 reply; 5+ messages in thread
From: Don Breazeal @ 2015-02-02 22:37 UTC (permalink / raw)
  To: gdb-patches

On 1/31/2015 3:55 AM, Maciej W. Rozycki wrote:
> Use `atexit' to run `ipc_cleanup' automagically instead maybe?  This way 
> you won't have to run the function manually at each exit point and also it 
> won't be missed on any future updates.

Maciej,
That makes sense to me.  Here is the updated patch.
Thanks!
--Don

We have noticed here that over time some of our test systems accumulate
stale System V IPC resources.  At least some of this is due to a GDB test
program, gdb.base/info-os.c.  The program doesn't clean up any allocated
IPC objects when an error occurs.  System V IPC objects will stay around
forever unless they are explicitly removed (or until a reboot).

This patch puts the IPC cleanup code into a function and calls atexit
so that the function will be called for any exit, not just successful
completion.

I looked at changing the SysV IPC key for allocating the IPC objects
to IPC_PRIVATE.  That would prevent a namespace conflict with the key.
However, the test needs to read the actual key number from the 'info os'
command output, and IPC_PRIVATE won't work for that.

One can clean up stale resources allocated by this test on Linux systems
by running 'ipcs' and looking for shared memory objects with keys in
the range 0xf55-0x133d, semaphore objects with keys in the range
0x1d04-20ec, and message queue objects in the range 0x14ae-0x1896.  Use
the 'ipcrm' command to delete the IPC objects.  Note that other programs
(like vncserver) use System V IPC, so make sure that the object is stale
before deleting it.

Tested on x86_64 Ubuntu.

OK?

thanks
--Don

gdb/testsuite/
2015-02-02  Don Breazeal  <donb@codesourcery.com>

	* gdb.base/info-os.c (shmid, semid, msqid): Make variables static
	and initialize them.
	(ipc_cleanup): New function.
	(main): Don't declare shmid, semid, and msqid.  Add a call to
	atexit so that we call ipc_cleanup on exit.

---
 gdb/testsuite/gdb.base/info-os.c | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/gdb/testsuite/gdb.base/info-os.c b/gdb/testsuite/gdb.base/info-os.c
index ae2606a..8ceaaff 100644
--- a/gdb/testsuite/gdb.base/info-os.c
+++ b/gdb/testsuite/gdb.base/info-os.c
@@ -26,6 +26,22 @@
 
 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 
+/* System V IPC identifiers.  */
+static int shmid = -1, semid = -1, msqid = -1;
+
+/* Delete any System V IPC resources that were allocated.  */
+
+static void
+ipc_cleanup (void)
+{
+  if (shmid >= 0)
+    shmctl (shmid, IPC_RMID, NULL);
+  if (semid >= 0)
+    semctl (semid, 0, IPC_RMID, NULL);
+  if (msqid >= 0)
+    msgctl (msqid, IPC_RMID, NULL);
+}
+
 void *
 thread_proc (void *args)
 {
@@ -38,7 +54,6 @@ main (void)
 {
   const int flags = IPC_CREAT | 0666;
   key_t shmkey = 3925, semkey = 7428, msgkey = 5294;
-  int shmid, semid, msqid;
   FILE *fd;
   pthread_t thread;
   struct sockaddr_in sock_addr;
@@ -47,6 +62,8 @@ main (void)
   socklen_t size;
   int status, try, retries = 1000;
 
+  atexit (ipc_cleanup);
+
   for (try = 0; try < retries; ++try)
     {
       shmid = shmget (shmkey, 4096, flags | IPC_EXCL);
@@ -135,9 +152,6 @@ main (void)
 
   /* Set breakpoint here.  */
 
-  shmctl (shmid, IPC_RMID, NULL);
-  semctl (semid, 0, IPC_RMID, NULL);
-  msgctl (msqid, IPC_RMID, NULL);
   fclose (fd);
   close (sock);
 
-- 
1.8.1.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] gdb.base/info-os.c: Clean up SysV IPC on error.
  2015-02-02 22:37   ` [PATCH v2] gdb.base/info-os.c: Clean " Don Breazeal
@ 2015-02-03 15:32     ` Pedro Alves
  2015-02-04 21:33       ` [commit] " Breazeal, Don
  0 siblings, 1 reply; 5+ messages in thread
From: Pedro Alves @ 2015-02-03 15:32 UTC (permalink / raw)
  To: Don Breazeal, gdb-patches

On 02/02/2015 11:37 PM, Don Breazeal wrote:

> OK?

OK.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [commit] Re: [PATCH v2] gdb.base/info-os.c: Clean up SysV IPC on error.
  2015-02-03 15:32     ` Pedro Alves
@ 2015-02-04 21:33       ` Breazeal, Don
  0 siblings, 0 replies; 5+ messages in thread
From: Breazeal, Don @ 2015-02-04 21:33 UTC (permalink / raw)
  To: Pedro Alves, gdb-patches

On 2/3/2015 7:32 AM, Pedro Alves wrote:
> On 02/02/2015 11:37 PM, Don Breazeal wrote:
> 
>> OK?
> 
> OK.
> 
> Thanks,
> Pedro Alves
> 
This has been pushed.
Thanks,
-Don

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-02-04 21:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-30 20:16 [PATCH] gdb.base/info-os.c: clean up SysV IPC on error Don Breazeal
2015-01-31 22:22 ` Maciej W. Rozycki
2015-02-02 22:37   ` [PATCH v2] gdb.base/info-os.c: Clean " Don Breazeal
2015-02-03 15:32     ` Pedro Alves
2015-02-04 21:33       ` [commit] " Breazeal, Don

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).