hexedit: new applet
[oweals/busybox.git] / util-linux / ipcs.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ipcs.c -- provides information on allocated ipc resources.
4  *
5  * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
6  * Adapted for busybox from util-linux-2.12a.
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 //config:config IPCS
11 //config:       bool "ipcs (11 kb)"
12 //config:       default y
13 //config:       select PLATFORM_LINUX
14 //config:       help
15 //config:       The ipcs utility is used to provide information on the currently
16 //config:       allocated System V interprocess (IPC) objects in the system.
17
18 //applet:IF_IPCS(APPLET_NOEXEC(ipcs, ipcs, BB_DIR_USR_BIN, BB_SUID_DROP, ipcs))
19
20 //kbuild:lib-$(CONFIG_IPCS) += ipcs.o
21
22 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
23 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
24 /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
25 #include <sys/types.h>
26 #include <sys/ipc.h>
27 #include <sys/sem.h>
28 #include <sys/msg.h>
29 #include <sys/shm.h>
30
31 #include "libbb.h"
32
33 /*-------------------------------------------------------------------*/
34 /* SHM_DEST and SHM_LOCKED are defined in kernel headers,
35    but inside #ifdef __KERNEL__ ... #endif */
36 #ifndef SHM_DEST
37 /* shm_mode upper byte flags */
38 #define SHM_DEST        01000   /* segment will be destroyed on last detach */
39 #define SHM_LOCKED      02000   /* segment will not be swapped */
40 #endif
41
42 /* For older kernels the same holds for the defines below */
43 #ifndef MSG_STAT
44 #define MSG_STAT        11
45 #define MSG_INFO        12
46 #endif
47
48 #ifndef SHM_STAT
49 #define SHM_STAT        13
50 #define SHM_INFO        14
51 struct shm_info {
52         int used_ids;
53         unsigned long shm_tot;          /* total allocated shm */
54         unsigned long shm_rss;          /* total resident shm */
55         unsigned long shm_swp;          /* total swapped shm */
56         unsigned long swap_attempts;
57         unsigned long swap_successes;
58 };
59 #endif
60
61 #ifndef SEM_STAT
62 #define SEM_STAT        18
63 #define SEM_INFO        19
64 #endif
65
66 /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
67 #ifndef IPC_INFO
68 #define IPC_INFO        3
69 #endif
70 /*-------------------------------------------------------------------*/
71
72 /* The last arg of semctl is a union semun, but where is it defined?
73    X/OPEN tells us to define it ourselves, but until recently
74    Linux include files would also define it. */
75 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
76 /* union semun is defined by including <sys/sem.h> */
77 #else
78 /* according to X/OPEN we have to define it ourselves */
79 union semun {
80         int val;
81         struct semid_ds *buf;
82         unsigned short *array;
83         struct seminfo *__buf;
84 };
85 #endif
86
87 /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
88    libc 4/5 does not mention struct ipc_term at all, but includes
89    <linux/ipc.h>, which defines a struct ipc_perm with such fields.
90    glibc-1.09 has no support for sysv ipc.
91    glibc 2 uses __key, __seq */
92 #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
93 #define KEY __key
94 #else
95 #define KEY key
96 #endif
97
98 #define LIMITS 1
99 #define STATUS 2
100 #define CREATOR 3
101 #define TIME 4
102 #define PID 5
103
104 static char format;
105
106 static void print_perms(int id, struct ipc_perm *ipcp)
107 {
108         struct passwd *pw;
109         struct group *gr;
110
111         printf("%-10d %-10o", id, ipcp->mode & 0777);
112
113         pw = getpwuid(ipcp->cuid);
114         if (pw) printf(" %-10s", pw->pw_name);
115         else    printf(" %-10d", ipcp->cuid);
116         gr = getgrgid(ipcp->cgid);
117         if (gr) printf(" %-10s", gr->gr_name);
118         else    printf(" %-10d", ipcp->cgid);
119
120         pw = getpwuid(ipcp->uid);
121         if (pw) printf(" %-10s", pw->pw_name);
122         else    printf(" %-10d", ipcp->uid);
123         gr = getgrgid(ipcp->gid);
124         if (gr) printf(" %-10s\n", gr->gr_name);
125         else    printf(" %-10d\n", ipcp->gid);
126 }
127
128
129 static NOINLINE void do_shm(void)
130 {
131         int maxid, shmid, id;
132         struct shmid_ds shmseg;
133         struct shm_info shm_info;
134         struct shminfo shminfo;
135         struct ipc_perm *ipcp = &shmseg.shm_perm;
136         struct passwd *pw;
137
138         maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
139         if (maxid < 0) {
140                 printf("kernel not configured for %s\n", "shared memory");
141                 return;
142         }
143
144         switch (format) {
145         case LIMITS:
146                 printf("------ Shared Memory %s --------\n", "Limits");
147                 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
148                         return;
149                 /* glibc 2.1.3 and all earlier libc's have ints as fields
150                  * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
151                 printf("max number of segments = %lu\n"
152                                 "max seg size (kbytes) = %lu\n"
153                                 "max total shared memory (pages) = %lu\n"
154                                 "min seg size (bytes) = %lu\n",
155                                 (unsigned long) shminfo.shmmni,
156                                 (unsigned long) (shminfo.shmmax >> 10),
157                                 (unsigned long) shminfo.shmall,
158                                 (unsigned long) shminfo.shmmin);
159                 return;
160
161         case STATUS:
162                 printf("------ Shared Memory %s --------\n", "Status");
163                 printf("segments allocated %d\n"
164                                 "pages allocated %lu\n"
165                                 "pages resident  %lu\n"
166                                 "pages swapped   %lu\n"
167                                 "Swap performance: %lu attempts\t%lu successes\n",
168                                 shm_info.used_ids,
169                                 shm_info.shm_tot,
170                                 shm_info.shm_rss,
171                                 shm_info.shm_swp,
172                                 shm_info.swap_attempts, shm_info.swap_successes);
173                 return;
174
175         case CREATOR:
176                 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
177                 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
178                                 "shmid", "perms", "cuid", "cgid", "uid", "gid");
179                 break;
180
181         case TIME:
182                 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
183                 printf("%-10s %-10s %-20s %-20s %-20s\n",
184                                 "shmid", "owner", "attached", "detached", "changed");
185                 break;
186
187         case PID:
188                 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
189                 printf("%-10s %-10s %-10s %-10s\n",
190                                 "shmid", "owner", "cpid", "lpid");
191                 break;
192
193         default:
194                 printf("------ Shared Memory %s --------\n", "Segments");
195                 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
196                                 "key", "shmid", "owner", "perms", "bytes", "nattch",
197                                 "status");
198                 break;
199         }
200
201         for (id = 0; id <= maxid; id++) {
202                 shmid = shmctl(id, SHM_STAT, &shmseg);
203                 if (shmid < 0)
204                         continue;
205                 if (format == CREATOR) {
206                         print_perms(shmid, ipcp);
207                         continue;
208                 }
209                 pw = getpwuid(ipcp->uid);
210                 switch (format) {
211                 case TIME:
212                         if (pw)
213                                 printf("%-10d %-10.10s", shmid, pw->pw_name);
214                         else
215                                 printf("%-10d %-10d", shmid, ipcp->uid);
216                         /* ctime uses static buffer: use separate calls */
217                         printf(" %-20.16s", shmseg.shm_atime
218                                         ? ctime(&shmseg.shm_atime) + 4 : "Not set");
219                         printf(" %-20.16s", shmseg.shm_dtime
220                                         ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
221                         printf(" %-20.16s\n", shmseg.shm_ctime
222                                         ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
223                         break;
224                 case PID:
225                         if (pw)
226                                 printf("%-10d %-10.10s", shmid, pw->pw_name);
227                         else
228                                 printf("%-10d %-10d", shmid, ipcp->uid);
229                         printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
230                         break;
231
232                 default:
233                         printf("0x%08x ", ipcp->KEY);
234                         if (pw)
235                                 printf("%-10d %-10.10s", shmid, pw->pw_name);
236                         else
237                                 printf("%-10d %-10d", shmid, ipcp->uid);
238                         printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
239                                         /*
240                                          * earlier: int, Austin has size_t
241                                          */
242                                         (unsigned long) shmseg.shm_segsz,
243                                         /*
244                                          * glibc-2.1.3 and earlier has unsigned short;
245                                          * Austin has shmatt_t
246                                          */
247                                         (long) shmseg.shm_nattch,
248                                         ipcp->mode & SHM_DEST ? "dest" : " ",
249                                         ipcp->mode & SHM_LOCKED ? "locked" : " ");
250                         break;
251                 }
252         }
253 }
254
255
256 static NOINLINE void do_sem(void)
257 {
258         int maxid, semid, id;
259         struct semid_ds semary;
260         struct seminfo seminfo;
261         struct ipc_perm *ipcp = &semary.sem_perm;
262         struct passwd *pw;
263         union semun arg;
264
265         arg.array = (unsigned short *) (void *) &seminfo;
266         maxid = semctl(0, 0, SEM_INFO, arg);
267         if (maxid < 0) {
268                 printf("kernel not configured for %s\n", "semaphores");
269                 return;
270         }
271
272         switch (format) {
273         case LIMITS:
274                 printf("------ Semaphore %s --------\n", "Limits");
275                 arg.array = (unsigned short *) (void *) &seminfo;       /* damn union */
276                 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
277                         return;
278                 printf("max number of arrays = %d\n"
279                                 "max semaphores per array = %d\n"
280                                 "max semaphores system wide = %d\n"
281                                 "max ops per semop call = %d\n"
282                                 "semaphore max value = %d\n",
283                                 seminfo.semmni,
284                                 seminfo.semmsl,
285                                 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
286                 return;
287
288         case STATUS:
289                 printf("------ Semaphore %s --------\n", "Status");
290                 printf("used arrays = %d\n"
291                                 "allocated semaphores = %d\n",
292                                 seminfo.semusz, seminfo.semaem);
293                 return;
294
295         case CREATOR:
296                 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
297                 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
298                                 "semid", "perms", "cuid", "cgid", "uid", "gid");
299                 break;
300
301         case TIME:
302                 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
303                 printf("%-8s %-10s %-26.24s %-26.24s\n",
304                                 "shmid", "owner", "last-op", "last-changed");
305                 break;
306
307         case PID:
308                 break;
309
310         default:
311                 printf("------ Semaphore %s --------\n", "Arrays");
312                 printf("%-10s %-10s %-10s %-10s %-10s\n",
313                                 "key", "semid", "owner", "perms", "nsems");
314                 break;
315         }
316
317         for (id = 0; id <= maxid; id++) {
318                 arg.buf = (struct semid_ds *) &semary;
319                 semid = semctl(id, 0, SEM_STAT, arg);
320                 if (semid < 0)
321                         continue;
322                 if (format == CREATOR) {
323                         print_perms(semid, ipcp);
324                         continue;
325                 }
326                 pw = getpwuid(ipcp->uid);
327                 switch (format) {
328                 case TIME:
329                         if (pw)
330                                 printf("%-8d %-10.10s", semid, pw->pw_name);
331                         else
332                                 printf("%-8d %-10d", semid, ipcp->uid);
333                         /* ctime uses static buffer: use separate calls */
334                         printf("  %-26.24s", semary.sem_otime
335                                         ? ctime(&semary.sem_otime) : "Not set");
336                         printf(" %-26.24s\n", semary.sem_ctime
337                                         ? ctime(&semary.sem_ctime) : "Not set");
338                         break;
339                 case PID:
340                         break;
341
342                 default:
343                         printf("0x%08x ", ipcp->KEY);
344                         if (pw)
345                                 printf("%-10d %-10.9s", semid, pw->pw_name);
346                         else
347                                 printf("%-10d %-9d", semid, ipcp->uid);
348                         printf(" %-10o %-10ld\n", ipcp->mode & 0777,
349                                         /*
350                                          * glibc-2.1.3 and earlier has unsigned short;
351                                          * glibc-2.1.91 has variation between
352                                          * unsigned short and unsigned long
353                                          * Austin prescribes unsigned short.
354                                          */
355                                         (long) semary.sem_nsems);
356                         break;
357                 }
358         }
359 }
360
361
362 static NOINLINE void do_msg(void)
363 {
364         int maxid, msqid, id;
365         struct msqid_ds msgque;
366         struct msginfo msginfo;
367         struct ipc_perm *ipcp = &msgque.msg_perm;
368         struct passwd *pw;
369
370         maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
371         if (maxid < 0) {
372                 printf("kernel not configured for %s\n", "message queues");
373                 return;
374         }
375
376         switch (format) {
377         case LIMITS:
378                 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
379                         return;
380                 printf("------ Message%s --------\n", "s: Limits");
381                 printf("max queues system wide = %d\n"
382                                 "max size of message (bytes) = %d\n"
383                                 "default max size of queue (bytes) = %d\n",
384                                 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
385                 return;
386
387         case STATUS:
388                 printf("------ Message%s --------\n", "s: Status");
389                 printf("allocated queues = %d\n"
390                                 "used headers = %d\n"
391                                 "used space = %d bytes\n",
392                                 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
393                 return;
394
395         case CREATOR:
396                 printf("------ Message%s --------\n", " Queues: Creators/Owners");
397                 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
398                                 "msqid", "perms", "cuid", "cgid", "uid", "gid");
399                 break;
400
401         case TIME:
402                 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
403                 printf("%-8s %-10s %-20s %-20s %-20s\n",
404                                 "msqid", "owner", "send", "recv", "change");
405                 break;
406
407         case PID:
408                 printf("------ Message%s --------\n", " Queues PIDs");
409                 printf("%-10s %-10s %-10s %-10s\n",
410                                 "msqid", "owner", "lspid", "lrpid");
411                 break;
412
413         default:
414                 printf("------ Message%s --------\n", " Queues");
415                 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
416                                 "key", "msqid", "owner", "perms", "used-bytes", "messages");
417                 break;
418         }
419
420         for (id = 0; id <= maxid; id++) {
421                 msqid = msgctl(id, MSG_STAT, &msgque);
422                 if (msqid < 0)
423                         continue;
424                 if (format == CREATOR) {
425                         print_perms(msqid, ipcp);
426                         continue;
427                 }
428                 pw = getpwuid(ipcp->uid);
429                 switch (format) {
430                 case TIME:
431                         if (pw)
432                                 printf("%-8d %-10.10s", msqid, pw->pw_name);
433                         else
434                                 printf("%-8d %-10d", msqid, ipcp->uid);
435                         printf(" %-20.16s", msgque.msg_stime
436                                         ? ctime(&msgque.msg_stime) + 4 : "Not set");
437                         printf(" %-20.16s", msgque.msg_rtime
438                                         ? ctime(&msgque.msg_rtime) + 4 : "Not set");
439                         printf(" %-20.16s\n", msgque.msg_ctime
440                                         ? ctime(&msgque.msg_ctime) + 4 : "Not set");
441                         break;
442                 case PID:
443                         if (pw)
444                                 printf("%-8d %-10.10s", msqid, pw->pw_name);
445                         else
446                                 printf("%-8d %-10d", msqid, ipcp->uid);
447                         printf("  %5d     %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
448                         break;
449
450                 default:
451                         printf("0x%08x ", ipcp->KEY);
452                         if (pw)
453                                 printf("%-10d %-10.10s", msqid, pw->pw_name);
454                         else
455                                 printf("%-10d %-10d", msqid, ipcp->uid);
456                         printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
457                                         /*
458                                          * glibc-2.1.3 and earlier has unsigned short;
459                                          * glibc-2.1.91 has variation between
460                                          * unsigned short, unsigned long
461                                          * Austin has msgqnum_t
462                                          */
463                                         (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
464                         break;
465                 }
466         }
467 }
468
469
470 static void print_shm(int shmid)
471 {
472         struct shmid_ds shmds;
473         struct ipc_perm *ipcp = &shmds.shm_perm;
474
475         if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
476                 bb_perror_msg("shmctl");
477                 return;
478         }
479
480         printf("\nShared memory Segment shmid=%d\n"
481                         "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
482                         "mode=%#o\taccess_perms=%#o\n"
483                         "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
484                         shmid,
485                         ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
486                         ipcp->mode, ipcp->mode & 0777,
487                         (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
488                         (long) shmds.shm_nattch);
489         printf("att_time=%-26.24s\n",
490                         shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
491         printf("det_time=%-26.24s\n",
492                         shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
493         printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
494 }
495
496
497 static void print_msg(int msqid)
498 {
499         struct msqid_ds buf;
500         struct ipc_perm *ipcp = &buf.msg_perm;
501
502         if (msgctl(msqid, IPC_STAT, &buf) == -1) {
503                 bb_perror_msg("msgctl");
504                 return;
505         }
506
507         printf("\nMessage Queue msqid=%d\n"
508                         "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
509                         "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
510                         msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
511                         /*
512                          * glibc-2.1.3 and earlier has unsigned short;
513                          * glibc-2.1.91 has variation between
514                          * unsigned short, unsigned long
515                          * Austin has msgqnum_t (for msg_qbytes)
516                          */
517                         (long) buf.msg_cbytes, (long) buf.msg_qbytes,
518                         (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
519
520         printf("send_time=%-26.24s\n",
521                         buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
522         printf("rcv_time=%-26.24s\n",
523                         buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
524         printf("change_time=%-26.24s\n\n",
525                         buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
526 }
527
528 static void print_sem(int semid)
529 {
530         struct semid_ds semds;
531         struct ipc_perm *ipcp = &semds.sem_perm;
532         union semun arg;
533         unsigned int i;
534
535         arg.buf = &semds;
536         if (semctl(semid, 0, IPC_STAT, arg)) {
537                 bb_perror_msg("semctl");
538                 return;
539         }
540
541         printf("\nSemaphore Array semid=%d\n"
542                         "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
543                         "mode=%#o, access_perms=%#o\n"
544                         "nsems = %ld\n"
545                         "otime = %-26.24s\n",
546                         semid,
547                         ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
548                         ipcp->mode, ipcp->mode & 0777,
549                         (long) semds.sem_nsems,
550                         semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
551         printf("ctime = %-26.24s\n"
552                         "%-10s %-10s %-10s %-10s %-10s\n",
553                         ctime(&semds.sem_ctime),
554                         "semnum", "value", "ncount", "zcount", "pid");
555
556         arg.val = 0;
557         for (i = 0; i < semds.sem_nsems; i++) {
558                 int val, ncnt, zcnt, pid;
559
560                 val = semctl(semid, i, GETVAL, arg);
561                 ncnt = semctl(semid, i, GETNCNT, arg);
562                 zcnt = semctl(semid, i, GETZCNT, arg);
563                 pid = semctl(semid, i, GETPID, arg);
564                 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
565                         bb_perror_msg_and_die("semctl");
566                 }
567                 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
568         }
569         bb_putchar('\n');
570 }
571
572 //usage:#define ipcs_trivial_usage
573 //usage:       "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
574 //usage:#define ipcs_full_usage "\n\n"
575 //usage:       "        -i      Show specific resource"
576 //usage:     "\nResource specification:"
577 //usage:     "\n        -m      Shared memory segments"
578 //usage:     "\n        -q      Message queues"
579 //usage:     "\n        -s      Semaphore arrays"
580 //usage:     "\n        -a      All (default)"
581 //usage:     "\nOutput format:"
582 //usage:     "\n        -t      Time"
583 //usage:     "\n        -c      Creator"
584 //usage:     "\n        -p      Pid"
585 //usage:     "\n        -l      Limits"
586 //usage:     "\n        -u      Summary"
587
588 int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
589 int ipcs_main(int argc UNUSED_PARAM, char **argv)
590 {
591         int id = 0;
592         unsigned flags = 0;
593         unsigned opt;
594         char *opt_i;
595 #define flag_print      (1<<0)
596 #define flag_msg        (1<<1)
597 #define flag_sem        (1<<2)
598 #define flag_shm        (1<<3)
599
600         opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
601         if (opt & 0x1) { // -i
602                 id = xatoi(opt_i);
603                 flags |= flag_print;
604         }
605         if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
606         if (opt & 0x4) flags |= flag_msg; // -q
607         if (opt & 0x8) flags |= flag_sem; // -s
608         if (opt & 0x10) flags |= flag_shm; // -m
609         if (opt & 0x20) format = TIME; // -t
610         if (opt & 0x40) format = CREATOR; // -c
611         if (opt & 0x80) format = PID; // -p
612         if (opt & 0x100) format = LIMITS; // -l
613         if (opt & 0x200) format = STATUS; // -u
614
615         if (flags & flag_print) {
616                 if (flags & flag_shm) {
617                         print_shm(id);
618                         fflush_stdout_and_exit(EXIT_SUCCESS);
619                 }
620                 if (flags & flag_sem) {
621                         print_sem(id);
622                         fflush_stdout_and_exit(EXIT_SUCCESS);
623                 }
624                 if (flags & flag_msg) {
625                         print_msg(id);
626                         fflush_stdout_and_exit(EXIT_SUCCESS);
627                 }
628                 bb_show_usage();
629         }
630
631         if (!(flags & (flag_shm | flag_msg | flag_sem)))
632                 flags |= flag_msg | flag_shm | flag_sem;
633         bb_putchar('\n');
634
635         if (flags & flag_msg) {
636                 do_msg();
637                 bb_putchar('\n');
638         }
639         if (flags & flag_shm) {
640                 do_shm();
641                 bb_putchar('\n');
642         }
643         if (flags & flag_sem) {
644                 do_sem();
645                 bb_putchar('\n');
646         }
647         fflush_stdout_and_exit(EXIT_SUCCESS);
648 }