1 /* $NetBSD: ipcs.c,v 1.43 2014/06/11 14:57:55 joerg Exp $ */
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
46 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
47 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
48 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
50 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
51 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
52 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
53 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
54 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 #include <sys/cdefs.h>
58 #include <sys/param.h>
59 #include <sys/sysctl.h>
60 #include <sys/inttypes.h>
91 static int display
= 0;
92 static int option
= 0;
94 static void cvt_time(time_t, char *, size_t);
95 static char *fmt_perm(u_short
);
96 static void msg_sysctl(void);
97 static void sem_sysctl(void);
98 static void shm_sysctl(void);
99 static void show_msginfo(time_t, time_t, time_t, int, u_int64_t
, mode_t
,
100 uid_t
, gid_t
, uid_t
, gid_t
, u_int64_t
, u_int64_t
, u_int64_t
, pid_t
, pid_t
);
101 static void show_msginfo_hdr(void);
102 static void show_msgtotal(struct msginfo
*);
103 static void show_seminfo_hdr(void);
104 static void show_seminfo(time_t, time_t, int, u_int64_t
, mode_t
, uid_t
,
105 gid_t
, uid_t
, gid_t
, int16_t);
106 static void show_semtotal(struct seminfo
*);
107 static void show_shminfo(time_t, time_t, time_t, int, u_int64_t
, mode_t
,
108 uid_t
, gid_t
, uid_t
, gid_t
, u_int32_t
, u_int64_t
, pid_t
, pid_t
);
109 static void show_shminfo_hdr(void);
110 static void show_shmtotal(struct shminfo
*);
111 static void usage(void) __dead
;
112 static void unconfsem(void);
113 static void unconfmsg(void);
114 static void unconfshm(void);
119 warnx("SVID semaphores facility not configured in the system");
125 warnx("SVID messages facility not configured in the system");
131 warnx("SVID shared memory facility not configured in the system");
135 fmt_perm(u_short mode
)
137 static char buffer
[12];
141 buffer
[2] = ((mode
& 0400) ? 'r' : '-');
142 buffer
[3] = ((mode
& 0200) ? 'w' : '-');
143 buffer
[4] = ((mode
& 0100) ? 'a' : '-');
144 buffer
[5] = ((mode
& 0040) ? 'r' : '-');
145 buffer
[6] = ((mode
& 0020) ? 'w' : '-');
146 buffer
[7] = ((mode
& 0010) ? 'a' : '-');
147 buffer
[8] = ((mode
& 0004) ? 'r' : '-');
148 buffer
[9] = ((mode
& 0002) ? 'w' : '-');
149 buffer
[10] = ((mode
& 0001) ? 'a' : '-');
155 cvt_time(time_t t
, char *buf
, size_t buflen
)
160 (void)strlcpy(buf
, "no-entry", buflen
);
163 (void)snprintf(buf
, buflen
, "%2d:%02d:%02d",
164 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
168 main(int argc
, char *argv
[])
173 while ((i
= getopt(argc
, argv
, "MmQqSsabcoptT")) != -1)
194 display
|= SHMTOTAL
| MSGTOTAL
| SEMTOTAL
;
197 option
|= BIGGEST
| CREATOR
| OUTSTANDING
| PID
| TIME
;
206 option
|= OUTSTANDING
;
218 if (argc
- optind
> 0)
222 (void)printf("IPC status from <running system> as of %s\n",
223 /* and extra \n from ctime(3) */
227 display
= SHMINFO
| MSGINFO
| SEMINFO
;
229 if (display
& (MSGINFO
| MSGTOTAL
))
231 if (display
& (SHMINFO
| SHMTOTAL
))
233 if (display
& (SEMINFO
| SEMTOTAL
))
239 show_msgtotal(struct msginfo
*msginfo
)
241 (void)printf("msginfo:\n");
242 (void)printf("\tmsgmax: %6d\t(max characters in a message)\n",
244 (void)printf("\tmsgmni: %6d\t(# of message queues)\n",
246 (void)printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
248 (void)printf("\tmsgtql: %6d\t(max # of messages in system)\n",
250 (void)printf("\tmsgssz: %6d\t(size of a message segment)\n",
252 (void)printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
257 show_shmtotal(struct shminfo
*shminfo
)
259 (void)printf("shminfo:\n");
260 (void)printf("\tshmmax: %" PRIu64
"\t(max shared memory segment size)\n",
262 (void)printf("\tshmmin: %7d\t(min shared memory segment size)\n",
264 (void)printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
266 (void)printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
268 (void)printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
273 show_semtotal(struct seminfo
*seminfo
)
275 (void)printf("seminfo:\n");
276 (void)printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
278 (void)printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
280 (void)printf("\tsemmns: %6d\t(# of semaphores in system)\n",
282 (void)printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
284 (void)printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
286 (void)printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
288 (void)printf("\tsemume: %6d\t(max # of undo entries per process)\n",
290 (void)printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
292 (void)printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
294 (void)printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
299 show_msginfo_hdr(void)
301 (void)printf("Message Queues:\n");
302 (void)printf("T ID KEY MODE OWNER GROUP");
303 if (option
& CREATOR
)
304 (void)printf(" CREATOR CGROUP");
305 if (option
& OUTSTANDING
)
306 (void)printf(" CBYTES QNUM");
307 if (option
& BIGGEST
)
308 (void)printf(" QBYTES");
310 (void)printf(" LSPID LRPID");
312 (void)printf(" STIME RTIME CTIME");
317 show_msginfo(time_t s_time
, time_t r_time
, time_t c_time
, int ipcid
,
319 mode_t mode
, uid_t uid
, gid_t gid
, uid_t cuid
, gid_t cgid
,
320 u_int64_t cbytes
, u_int64_t qnum
, u_int64_t qbytes
, pid_t lspid
,
323 char s_time_buf
[100], r_time_buf
[100], c_time_buf
[100];
326 cvt_time(s_time
, s_time_buf
, sizeof(s_time_buf
));
327 cvt_time(r_time
, r_time_buf
, sizeof(r_time_buf
));
328 cvt_time(c_time
, c_time_buf
, sizeof(c_time_buf
));
331 (void)printf("q %9d %10lld %s %8s %8s", ipcid
, (long long)key
, fmt_perm(mode
),
332 user_from_uid(uid
, 0), group_from_gid(gid
, 0));
334 if (option
& CREATOR
)
335 (void)printf(" %8s %8s", user_from_uid(cuid
, 0),
336 group_from_gid(cgid
, 0));
338 if (option
& OUTSTANDING
)
339 (void)printf(" %6lld %5lld", (long long)cbytes
, (long long)qnum
);
341 if (option
& BIGGEST
)
342 (void)printf(" %6lld", (long long)qbytes
);
345 (void)printf(" %5d %5d", lspid
, lrpid
);
348 (void)printf(" %s %s %s", s_time_buf
, r_time_buf
, c_time_buf
);
354 show_shminfo_hdr(void)
356 (void)printf("Shared Memory:\n");
357 (void)printf("T ID KEY MODE OWNER GROUP");
358 if (option
& CREATOR
)
359 (void)printf(" CREATOR CGROUP");
360 if (option
& OUTSTANDING
)
361 (void)printf(" NATTCH");
362 if (option
& BIGGEST
)
363 (void)printf(" SEGSZ");
365 (void)printf(" CPID LPID");
367 (void)printf(" ATIME DTIME CTIME");
372 show_shminfo(time_t atime
, time_t dtime
, time_t c_time
, int ipcid
, u_int64_t key
,
373 mode_t mode
, uid_t uid
, gid_t gid
, uid_t cuid
, gid_t cgid
,
374 u_int32_t nattch
, u_int64_t segsz
, pid_t cpid
, pid_t lpid
)
376 char atime_buf
[100], dtime_buf
[100], c_time_buf
[100];
379 cvt_time(atime
, atime_buf
, sizeof(atime_buf
));
380 cvt_time(dtime
, dtime_buf
, sizeof(dtime_buf
));
381 cvt_time(c_time
, c_time_buf
, sizeof(c_time_buf
));
384 (void)printf("m %9d %10lld %s %8s %8s", ipcid
, (long long)key
, fmt_perm(mode
),
385 user_from_uid(uid
, 0), group_from_gid(gid
, 0));
387 if (option
& CREATOR
)
388 (void)printf(" %8s %8s", user_from_uid(cuid
, 0),
389 group_from_gid(cgid
, 0));
391 if (option
& OUTSTANDING
)
392 (void)printf(" %6d", nattch
);
394 if (option
& BIGGEST
)
395 (void)printf(" %7llu", (long long)segsz
);
398 (void)printf(" %5d %5d", cpid
, lpid
);
401 (void)printf(" %s %s %s",
410 show_seminfo_hdr(void)
412 (void)printf("Semaphores:\n");
413 (void)printf("T ID KEY MODE OWNER GROUP");
414 if (option
& CREATOR
)
415 (void)printf(" CREATOR CGROUP");
416 if (option
& BIGGEST
)
417 (void)printf(" NSEMS");
419 (void)printf(" OTIME CTIME");
424 show_seminfo(time_t otime
, time_t c_time
, int ipcid
, u_int64_t key
, mode_t mode
,
425 uid_t uid
, gid_t gid
, uid_t cuid
, gid_t cgid
, int16_t nsems
)
427 char c_time_buf
[100], otime_buf
[100];
430 cvt_time(otime
, otime_buf
, sizeof(otime_buf
));
431 cvt_time(c_time
, c_time_buf
, sizeof(c_time_buf
));
434 (void)printf("s %9d %10lld %s %8s %8s", ipcid
, (long long)key
, fmt_perm(mode
),
435 user_from_uid(uid
, 0), group_from_gid(gid
, 0));
437 if (option
& CREATOR
)
438 (void)printf(" %8s %8s", user_from_uid(cuid
, 0),
439 group_from_gid(cgid
, 0));
441 if (option
& BIGGEST
)
442 (void)printf(" %5d", nsems
);
445 (void)printf(" %s %s", otime_buf
, c_time_buf
);
453 struct msg_sysctl_info
*msgsi
;
460 mib
[1] = KERN_SYSVIPC
;
461 mib
[2] = KERN_SYSVIPC_MSG
;
463 if (sysctl(mib
, 3, &valid
, &len
, NULL
, 0) < 0) {
464 warn("sysctl(KERN_SYSVIPC_MSG)");
473 mib
[1] = KERN_SYSVIPC
;
474 mib
[2] = KERN_SYSVIPC_INFO
;
475 mib
[3] = KERN_SYSVIPC_MSG_INFO
;
477 if (!(display
& MSGINFO
)) {
479 len
= sizeof(struct msginfo
);
481 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) < 0) {
482 warn("sysctl(KERN_SYSVIPC_MSG_INFO)");
487 if ((buf
= malloc(len
)) == NULL
)
489 msgsi
= (struct msg_sysctl_info
*)buf
;
490 if (sysctl(mib
, 4, msgsi
, &len
, NULL
, 0) < 0) {
491 warn("sysctl(KERN_SYSVIPC_MSG_INFO)");
495 if (display
& MSGTOTAL
)
496 show_msgtotal(&msgsi
->msginfo
);
498 if (display
& MSGINFO
) {
500 for (i
= 0; i
< msgsi
->msginfo
.msgmni
; i
++) {
501 struct msgid_ds_sysctl
*msqptr
= &msgsi
->msgids
[i
];
502 if (msqptr
->msg_qbytes
!= 0)
503 show_msginfo(msqptr
->msg_stime
,
506 IXSEQ_TO_IPCID(i
, msqptr
->msg_perm
),
507 msqptr
->msg_perm
._key
,
508 msqptr
->msg_perm
.mode
,
509 msqptr
->msg_perm
.uid
,
510 msqptr
->msg_perm
.gid
,
511 msqptr
->msg_perm
.cuid
,
512 msqptr
->msg_perm
.cgid
,
528 struct shm_sysctl_info
*shmsi
;
536 mib
[1] = KERN_SYSVIPC
;
537 mib
[2] = KERN_SYSVIPC_SHM
;
539 if (sysctl(mib
, 3, &valid
, &len
, NULL
, 0) < 0) {
540 warn("sysctl(KERN_SYSVIPC_SHM)");
549 mib
[1] = KERN_SYSVIPC
;
550 mib
[2] = KERN_SYSVIPC_INFO
;
551 mib
[3] = KERN_SYSVIPC_SHM_INFO
;
553 if (!(display
& SHMINFO
)) {
555 len
= sizeof(struct shminfo
);
557 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) < 0) {
558 warn("sysctl(KERN_SYSVIPC_SHM_INFO)");
563 if ((buf
= malloc(len
)) == NULL
)
565 shmsi
= (struct shm_sysctl_info
*)buf
;
566 if (sysctl(mib
, 4, shmsi
, &len
, NULL
, 0) < 0) {
567 warn("sysctl(KERN_SYSVIPC_SHM_INFO)");
571 if (display
& SHMTOTAL
)
572 show_shmtotal(&shmsi
->shminfo
);
574 if (display
& SHMINFO
) {
576 for (i
= 0; i
< shmsi
->shminfo
.shmmni
; i
++) {
577 struct shmid_ds_sysctl
*shmptr
= &shmsi
->shmids
[i
];
578 if (shmptr
->shm_perm
.mode
& 0x0800)
579 show_shminfo(shmptr
->shm_atime
,
582 IXSEQ_TO_IPCID(i
, shmptr
->shm_perm
),
583 shmptr
->shm_perm
._key
,
584 shmptr
->shm_perm
.mode
,
585 shmptr
->shm_perm
.uid
,
586 shmptr
->shm_perm
.gid
,
587 shmptr
->shm_perm
.cuid
,
588 shmptr
->shm_perm
.cgid
,
603 struct sem_sysctl_info
*semsi
;
610 mib
[1] = KERN_SYSVIPC
;
611 mib
[2] = KERN_SYSVIPC_SEM
;
613 if (sysctl(mib
, 3, &valid
, &len
, NULL
, 0) < 0) {
614 warn("sysctl(KERN_SYSVIPC_SEM)");
623 mib
[1] = KERN_SYSVIPC
;
624 mib
[2] = KERN_SYSVIPC_INFO
;
625 mib
[3] = KERN_SYSVIPC_SEM_INFO
;
627 if (!(display
& SEMINFO
)) {
629 len
= sizeof(struct seminfo
);
631 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) < 0) {
632 warn("sysctl(KERN_SYSVIPC_SEM_INFO)");
637 if ((buf
= malloc(len
)) == NULL
)
639 semsi
= (struct sem_sysctl_info
*)buf
;
640 if (sysctl(mib
, 4, semsi
, &len
, NULL
, 0) < 0) {
641 warn("sysctl(KERN_SYSVIPC_SEM_INFO)");
645 if (display
& SEMTOTAL
)
646 show_semtotal(&semsi
->seminfo
);
648 if (display
& SEMINFO
) {
650 for (i
= 0; i
< semsi
->seminfo
.semmni
; i
++) {
651 struct semid_ds_sysctl
*semaptr
= &semsi
->semids
[i
];
652 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) != 0)
653 show_seminfo(semaptr
->sem_otime
,
655 IXSEQ_TO_IPCID(i
, semaptr
->sem_perm
),
656 semaptr
->sem_perm
._key
,
657 semaptr
->sem_perm
.mode
,
658 semaptr
->sem_perm
.uid
,
659 semaptr
->sem_perm
.gid
,
660 semaptr
->sem_perm
.cuid
,
661 semaptr
->sem_perm
.cgid
,
674 (void)fprintf(stderr
,
675 "Usage: %s [-abcmopqstMQST]\n",