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