2 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
46 #include <sys/types.h>
47 #include <sys/param.h>
50 #include <sys/sysctl.h>
57 /* SysCtlGatherStruct structure. */
65 struct semid_kernel *sema;
66 struct seminfo seminfo;
67 struct msginfo msginfo;
68 struct msqid_kernel *msqids;
69 struct shminfo shminfo;
70 struct shmid_kernel *shmsegs;
72 char *fmt_perm(u_short);
73 void cvt_time(time_t, char *);
74 void sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vec);
75 void kget(int idx, void *addr, size_t size);
77 uid_t user2uid(char *username);
79 static struct nlist symbols[] = {
95 #define SHMINFO_XVEC \
96 X(shmmax, sizeof(int)) \
97 X(shmmin, sizeof(int)) \
98 X(shmmni, sizeof(int)) \
99 X(shmseg, sizeof(int)) \
100 X(shmall, sizeof(int))
102 #define SEMINFO_XVEC \
103 X(semmap, sizeof(int)) \
104 X(semmni, sizeof(int)) \
105 X(semmns, sizeof(int)) \
106 X(semmnu, sizeof(int)) \
107 X(semmsl, sizeof(int)) \
108 X(semopm, sizeof(int)) \
109 X(semume, sizeof(int)) \
110 X(semusz, sizeof(int)) \
111 X(semvmx, sizeof(int)) \
112 X(semaem, sizeof(int))
114 #define MSGINFO_XVEC \
115 X(msgmax, sizeof(int)) \
116 X(msgmni, sizeof(int)) \
117 X(msgmnb, sizeof(int)) \
118 X(msgtql, sizeof(int)) \
119 X(msgssz, sizeof(int)) \
120 X(msgseg, sizeof(int))
122 #define X(a, b) { "kern.ipc." #a, offsetof(TYPEC, a), (b) },
123 #define TYPEC struct shminfo
124 struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { NULL } };
126 #define TYPEC struct seminfo
127 struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { NULL } };
129 #define TYPEC struct msginfo
130 struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { NULL } };
137 fmt_perm(u_short mode)
139 static char buffer[100];
143 buffer[2] = ((mode & 0400) ? 'r' : '-');
144 buffer[3] = ((mode & 0200) ? 'w' : '-');
145 buffer[4] = ((mode & 0100) ? 'a' : '-');
146 buffer[5] = ((mode & 0040) ? 'r' : '-');
147 buffer[6] = ((mode & 0020) ? 'w' : '-');
148 buffer[7] = ((mode & 0010) ? 'a' : '-');
149 buffer[8] = ((mode & 0004) ? 'r' : '-');
150 buffer[9] = ((mode & 0002) ? 'w' : '-');
151 buffer[10] = ((mode & 0001) ? 'a' : '-');
157 cvt_time(time_t t, char *buf)
162 strcpy(buf, "no-entry");
165 sprintf(buf, "%2d:%02d:%02d",
166 tm->tm_hour, tm->tm_min, tm->tm_sec);
178 #define OUTSTANDING 4
183 main(int argc, char *argv[])
185 int display = SHMINFO | MSGINFO | SEMINFO;
187 char *core = NULL, *user = NULL, *namelist = NULL;
188 char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */
192 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
213 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
216 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
231 option |= OUTSTANDING;
244 uid = user2uid(user);
251 * If paths to the exec file or core file were specified, we
252 * aren't operating on the running kernel, so we can't use
255 if (namelist != NULL || core != NULL)
259 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
261 errx(1, "kvm_openfiles: %s", kvmoferr);
262 switch (kvm_nlist(kd, symbols)) {
266 errx(1, "unable to read kernel symbol table");
268 #ifdef notdef /* they'll be told more civilly later */
269 warnx("nlist failed");
270 for (i = 0; symbols[i].n_name != NULL; i++)
271 if (symbols[i].n_value == 0)
272 warnx("symbol %s not found",
279 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
280 if ((display & (MSGINFO | MSGTOTAL))) {
281 if (display & MSGTOTAL) {
282 printf("msginfo:\n");
283 printf("\tmsgmax: %12d\t(max characters in a message)\n",
285 printf("\tmsgmni: %12d\t(# of message queues)\n",
287 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
289 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
291 printf("\tmsgssz: %12d\t(size of a message segment)\n",
293 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
296 if (display & MSGINFO) {
297 struct msqid_kernel *kxmsqids;
301 kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
302 kxmsqids = malloc(kxmsqids_len);
303 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
305 printf("Message Queues:\n");
306 printf("T %12s %12s %-11s %-8s %-8s", "ID", "KEY", "MODE",
308 if (option & CREATOR)
309 printf(" %-8s %-8s", "CREATOR", "CGROUP");
310 if (option & OUTSTANDING)
311 printf(" %20s %20s", "CBYTES", "QNUM");
312 if (option & BIGGEST)
313 printf(" %20s", "QBYTES");
315 printf(" %12s %12s", "LSPID", "LRPID");
317 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
319 for (i = 0; i < msginfo.msgmni; i += 1) {
320 if (kxmsqids[i].u.msg_qbytes != 0) {
321 char stime_buf[100], rtime_buf[100],
323 struct msqid_kernel *kmsqptr = &kxmsqids[i];
326 if (uid != kmsqptr->u.msg_perm.uid)
328 cvt_time(kmsqptr->u.msg_stime, stime_buf);
329 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
330 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
332 printf("q %12d %12d %s %8s %8s",
333 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
334 (int)kmsqptr->u.msg_perm.key,
335 fmt_perm(kmsqptr->u.msg_perm.mode),
336 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
337 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
339 if (option & CREATOR)
341 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
342 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
344 if (option & OUTSTANDING)
345 printf(" %12lu %12lu",
346 kmsqptr->u.msg_cbytes,
347 kmsqptr->u.msg_qnum);
349 if (option & BIGGEST)
351 kmsqptr->u.msg_qbytes);
355 kmsqptr->u.msg_lspid,
356 kmsqptr->u.msg_lrpid);
370 if (display & (MSGINFO | MSGTOTAL)) {
372 "SVID messages facility not configured in the system\n");
375 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
376 if ((display & (SHMINFO | SHMTOTAL))) {
377 if (display & SHMTOTAL) {
378 printf("shminfo:\n");
379 printf("\tshmmax: %12d\t(max shared memory segment size)\n",
381 printf("\tshmmin: %12d\t(min shared memory segment size)\n",
383 printf("\tshmmni: %12d\t(max number of shared memory identifiers)\n",
385 printf("\tshmseg: %12d\t(max shared memory segments per process)\n",
387 printf("\tshmall: %12d\t(max amount of shared memory in pages)\n\n",
390 if (display & SHMINFO) {
391 struct shmid_kernel *kxshmids;
394 kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
395 kxshmids = malloc(kxshmids_len);
396 kget(X_SHMSEGS, kxshmids, kxshmids_len);
398 printf("Shared Memory:\n");
399 printf("T %12s %12s %-11s %-8s %-8s", "ID", "KEY", "MODE",
401 if (option & CREATOR)
402 printf(" %-8s %-8s", "CREATOR", "CGROUP");
403 if (option & OUTSTANDING)
404 printf(" %12s", "NATTCH");
405 if (option & BIGGEST)
406 printf(" %12s", "SEGSZ");
408 printf(" %12s %12s", "CPID", "LPID");
410 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
412 for (i = 0; i < shminfo.shmmni; i += 1) {
413 if (kxshmids[i].u.shm_perm.mode & 0x0800) {
414 char atime_buf[100], dtime_buf[100],
416 struct shmid_kernel *kshmptr = &kxshmids[i];
419 if (uid != kshmptr->u.shm_perm.uid)
421 cvt_time(kshmptr->u.shm_atime, atime_buf);
422 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
423 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
425 printf("m %12d %12d %s %8s %8s",
426 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
427 (int)kshmptr->u.shm_perm.key,
428 fmt_perm(kshmptr->u.shm_perm.mode),
429 user_from_uid(kshmptr->u.shm_perm.uid, 0),
430 group_from_gid(kshmptr->u.shm_perm.gid, 0));
432 if (option & CREATOR)
434 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
435 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
437 if (option & OUTSTANDING)
439 kshmptr->u.shm_nattch);
441 if (option & BIGGEST)
443 kshmptr->u.shm_segsz);
448 kshmptr->u.shm_lpid);
462 if (display & (SHMINFO | SHMTOTAL)) {
464 "SVID shared memory facility not configured in the system\n");
467 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
468 if ((display & (SEMINFO | SEMTOTAL))) {
469 struct semid_kernel *kxsema;
472 if (display & SEMTOTAL) {
473 printf("seminfo:\n");
474 printf("\tsemmap: %12d\t(# of entries in semaphore map)\n",
476 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
478 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
480 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
482 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
484 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
486 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
488 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
490 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
492 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
495 if (display & SEMINFO) {
496 kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
497 kxsema = malloc(kxsema_len);
498 kget(X_SEMA, kxsema, kxsema_len);
500 printf("Semaphores:\n");
501 printf("T %12s %12s %-11s %-8s %-8s", "ID", "KEY", "MODE",
503 if (option & CREATOR)
504 printf(" %-8s %-8s", "CREATOR", "CGROUP");
505 if (option & BIGGEST)
506 printf(" %12s", "NSEMS");
508 printf(" %-8s %-8s", "OTIME", "CTIME");
510 for (i = 0; i < seminfo.semmni; i += 1) {
511 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC) != 0) {
512 char ctime_buf[100], otime_buf[100];
513 struct semid_kernel *ksemaptr = &kxsema[i];
516 if (uid != ksemaptr->u.sem_perm.uid)
518 cvt_time(ksemaptr->u.sem_otime, otime_buf);
519 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
521 printf("s %12d %12d %s %8s %8s",
522 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
523 (int)ksemaptr->u.sem_perm.key,
524 fmt_perm(ksemaptr->u.sem_perm.mode),
525 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
526 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
528 if (option & CREATOR)
530 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
531 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
533 if (option & BIGGEST)
535 ksemaptr->u.sem_nsems);
549 if (display & (SEMINFO | SEMTOTAL)) {
550 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
559 sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vecarr)
561 struct scgs_vector *xp;
565 for (xp = vecarr; xp->sysctl != NULL; xp++) {
566 assert(xp->offset <= size);
568 rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset,
571 err(1, "sysctlbyname: %s", xp->sysctl);
572 if (tsiz != xp->size)
573 errx(1, "%s size mismatch (expected %d, got %d)",
574 xp->sysctl, xp->size, tsiz);
579 kget(int idx, void *addr, size_t size)
581 char *symn; /* symbol name */
585 const char *sym2sysctl[] = { /* symbol to sysctl name table */
591 "kern.ipc.shmsegs" };
593 assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl));
595 symn = symbols[idx].n_name;
598 if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0)
599 errx(1, "symbol %s undefined", symn);
601 * For some symbols, the value we retreieve is
602 * actually a pointer; since we want the actual value,
603 * we have to manually dereference it.
607 tsiz = sizeof(msqids);
608 rv = kvm_read(kd, symbols[idx].n_value,
610 kaddr = (u_long)msqids;
613 tsiz = sizeof(shmsegs);
614 rv = kvm_read(kd, symbols[idx].n_value,
616 kaddr = (u_long)shmsegs;
620 rv = kvm_read(kd, symbols[idx].n_value,
622 kaddr = (u_long)sema;
626 kaddr = symbols[idx].n_value;
629 if ((unsigned)rv != tsiz)
630 errx(1, "%s: %s", symn, kvm_geterr(kd));
631 if ((unsigned)kvm_read(kd, kaddr, addr, size) != size)
632 errx(1, "%s: %s", symn, kvm_geterr(kd));
636 sysctlgatherstruct(addr, size, shminfo_scgsv);
639 sysctlgatherstruct(addr, size, seminfo_scgsv);
642 sysctlgatherstruct(addr, size, msginfo_scgsv);
646 rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz,
649 err(1, "sysctlbyname: %s", sym2sysctl[idx]);
651 errx(1, "%s size mismatch "
652 "(expected %d, got %d)",
653 sym2sysctl[idx], size, tsiz);
660 user2uid(char *username)
666 uid = strtoul(username, &r, 0);
667 if (!*r && r != username)
669 if ((pwd = getpwnam(username)) == NULL)
670 errx(1, "getpwnam failed: No such user");
672 return (pwd->pw_uid);
680 "usage: ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");