]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
ipcs: clean up do_shm()
authorKarel Zak <kzak@redhat.com>
Mon, 5 Nov 2012 15:48:12 +0000 (16:48 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 23 Nov 2012 13:58:21 +0000 (14:58 +0100)
 - don't expect maxid as argument in ipc_shm_get_info()
 - if there is @id argument then use it everywhere in ipc_shm_get_info()
 - don't call shmctl() if not necessary in do_shm()

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/ipcs.c
sys-utils/ipcutils.c
sys-utils/ipcutils.h

index 496fc334c1d19e925042ec860dd308c6bbd7cc37..d0ad42fcd8a3913a5f8285ffc9111dff44cc5db5 100644 (file)
@@ -192,20 +192,14 @@ static void print_perms (int id, struct ipc_perm *ipcp)
 
 static void do_shm (char format)
 {
-       int maxid;
-       struct shm_info shm_info;
        struct passwd *pw;
-       struct ipc_limits lim;
        struct shm_data *shmds, *shmdsp;
 
-       maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
-       if (maxid < 0) {
-               printf (_("kernel not configured for shared memory\n"));
-               return;
-       }
-
        switch (format) {
        case LIMITS:
+       {
+               struct ipc_limits lim;
+
                printf (_("------ Shared Memory Limits --------\n"));
                if (ipc_shm_get_limits(&lim))
                        return;
@@ -215,8 +209,18 @@ static void do_shm (char format)
                                        (lim.shmall / 1024) * getpagesize());
                printf (_("min seg size (bytes) = %ju\n"), lim.shmmin);
                return;
-
+       }
        case STATUS:
+       {
+               int maxid;
+               struct shm_info shm_info;
+
+               maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) &shm_info);
+               if (maxid < 0) {
+                       printf (_("kernel not configured for shared memory\n"));
+                       return;
+               }
+
                printf (_("------ Shared Memory Status --------\n"));
                /*
                 * TRANSLATORS: This output format is maintained for backward
@@ -240,7 +244,11 @@ static void do_shm (char format)
                        shm_info.shm_swp,
                        shm_info.swap_attempts, shm_info.swap_successes);
                return;
+       }
 
+       /*
+        * Headers only
+        */
        case CREATOR:
                printf (_("------ Shared Memory Segment Creators/Owners --------\n"));
                printf ("%-10s %-10s %-10s %-10s %-10s %-10s\n",
@@ -268,7 +276,10 @@ static void do_shm (char format)
                break;
        }
 
-       if (ipc_shm_get_info(maxid, -1, &shmds) < 1)
+       /*
+        * Print data
+        */
+       if (ipc_shm_get_info(-1, &shmds) < 1)
                return;
        shmdsp = shmds;
 
index c266ad8680ced489a576d816c42b43ec9a2f0af3..959b70c63fa3583dc725d060fb553e76f79dc7f5 100644 (file)
@@ -91,11 +91,12 @@ int ipc_shm_get_limits(struct ipc_limits *lim)
        return 0;
 }
 
-int ipc_shm_get_info(int maxid, int id, struct shm_data **shmds)
+int ipc_shm_get_info(int id, struct shm_data **shmds)
 {
        FILE *f;
-       int i;
+       int i, maxid;
        struct shm_data *p;
+       struct shm_info dummy;
 
        p = *shmds = xmalloc(sizeof(struct shm_data));
        p->next = NULL;
@@ -129,6 +130,10 @@ int ipc_shm_get_info(int maxid, int id, struct shm_data **shmds)
                           &p->shm_swp) != 16)
                        continue;
 
+               if (id > -1 && id != p->shm_perm.id) {
+                       i--;
+                       continue;       /* id specified, but does not match */
+               }
                if (id < 0) {
                        p->next = xmalloc(sizeof(struct shm_data));
                        p = p->next;
@@ -145,6 +150,10 @@ int ipc_shm_get_info(int maxid, int id, struct shm_data **shmds)
 fallback:
        i = id < 0 ? 0 : id;
 
+       maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) &dummy);
+       if (maxid < 0)
+               return 0;
+
        while (i <= maxid) {
                int shmid;
                struct shmid_ds shmseg;
index 8e2bfddfb8fdeb055917b465c5a8b08a63b7c210..45a75d84f80da6ce38008cbfd23519777abc1669 100644 (file)
@@ -129,7 +129,7 @@ struct shm_data {
        struct shm_data  *next;
 };
 
-extern int ipc_shm_get_info(int maxid, int id, struct shm_data **shmds);
+extern int ipc_shm_get_info(int id, struct shm_data **shmds);
 extern void ipc_shm_free_info(struct shm_data *shmds);
 
 #endif /* UTIL_LINUX_IPCUTILS_H */