Annotation of src/usr.bin/ipcs/ipcs.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: ipcs.c,v 1.13 2001/07/12 05:17:11 deraadt Exp $ */
1.3 deraadt 2: /* $NetBSD: ipcs.c,v 1.10.6.1 1996/06/07 01:53:47 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.12 tholo 16: * 3. The name of the author may not be used to endorse or promote products
1.1 deraadt 17: * derived from this software without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30:
31: #include <sys/types.h>
32: #include <sys/param.h>
1.3 deraadt 33: #include <sys/time.h>
1.1 deraadt 34: #include <sys/proc.h>
35: #define _KERNEL
36: #include <sys/ipc.h>
37: #include <sys/sem.h>
38: #include <sys/shm.h>
39: #include <sys/msg.h>
1.3 deraadt 40: #undef _KERNEL
41:
42: #include <err.h>
43: #include <fcntl.h>
44: #include <kvm.h>
1.13 deraadt 45: #include <pwd.h>
46: #include <grp.h>
1.3 deraadt 47: #include <limits.h>
48: #include <nlist.h>
49: #include <paths.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <unistd.h>
1.1 deraadt 54:
55: void usage __P((void));
56:
1.3 deraadt 57: extern char *__progname; /* from crt0.o */
58:
1.1 deraadt 59: static struct nlist symbols[] = {
60: {"_sema"},
61: #define X_SEMA 0
62: {"_seminfo"},
63: #define X_SEMINFO 1
64: {"_semu"},
65: #define X_SEMU 2
66: {"_msginfo"},
67: #define X_MSGINFO 3
68: {"_msqids"},
69: #define X_MSQIDS 4
70: {"_shminfo"},
71: #define X_SHMINFO 5
72: {"_shmsegs"},
73: #define X_SHMSEGS 6
74: {NULL}
75: };
76:
77: static kvm_t *kd;
78:
79: char *
80: fmt_perm(mode)
81: u_short mode;
82: {
83: static char buffer[100];
84:
85: buffer[0] = '-';
86: buffer[1] = '-';
87: buffer[2] = ((mode & 0400) ? 'r' : '-');
88: buffer[3] = ((mode & 0200) ? 'w' : '-');
89: buffer[4] = ((mode & 0100) ? 'a' : '-');
90: buffer[5] = ((mode & 0040) ? 'r' : '-');
91: buffer[6] = ((mode & 0020) ? 'w' : '-');
92: buffer[7] = ((mode & 0010) ? 'a' : '-');
93: buffer[8] = ((mode & 0004) ? 'r' : '-');
94: buffer[9] = ((mode & 0002) ? 'w' : '-');
95: buffer[10] = ((mode & 0001) ? 'a' : '-');
96: buffer[11] = '\0';
97: return (&buffer[0]);
98: }
99:
100: void
101: cvt_time(t, buf)
102: time_t t;
103: char *buf;
104: {
105: struct tm *tm;
106:
107: if (t == 0) {
108: strcpy(buf, "no-entry");
109: } else {
110: tm = localtime(&t);
111: sprintf(buf, "%2d:%02d:%02d",
112: tm->tm_hour, tm->tm_min, tm->tm_sec);
113: }
114: }
115: #define SHMINFO 1
116: #define SHMTOTAL 2
117: #define MSGINFO 4
118: #define MSGTOTAL 8
119: #define SEMINFO 16
120: #define SEMTOTAL 32
121:
122: #define BIGGEST 1
123: #define CREATOR 2
124: #define OUTSTANDING 4
125: #define PID 8
126: #define TIME 16
127:
128: int
129: main(argc, argv)
130: int argc;
131: char *argv[];
132: {
133: int display = SHMINFO | MSGINFO | SEMINFO;
134: int option = 0;
135: char *core = NULL, *namelist = NULL;
136: int i;
137:
1.7 millert 138: while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
1.1 deraadt 139: switch (i) {
140: case 'M':
141: display = SHMTOTAL;
142: break;
143: case 'm':
144: display = SHMINFO;
145: break;
146: case 'Q':
147: display = MSGTOTAL;
148: break;
149: case 'q':
150: display = MSGINFO;
151: break;
152: case 'S':
153: display = SEMTOTAL;
154: break;
155: case 's':
156: display = SEMINFO;
157: break;
158: case 'T':
159: display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
160: break;
161: case 'a':
162: option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
163: break;
164: case 'b':
165: option |= BIGGEST;
166: break;
167: case 'C':
168: core = optarg;
169: break;
170: case 'c':
171: option |= CREATOR;
172: break;
173: case 'N':
174: namelist = optarg;
175: break;
176: case 'o':
177: option |= OUTSTANDING;
178: break;
179: case 'p':
180: option |= PID;
181: break;
182: case 't':
183: option |= TIME;
184: break;
185: default:
186: usage();
187: }
1.2 deraadt 188: /*
189: * Discard setgid privileges if not the running kernel so that bad
190: * guys can't print interesting stuff from kernel memory.
191: */
1.6 tholo 192: if (namelist != NULL || core != NULL) {
193: setegid(getgid());
1.2 deraadt 194: setgid(getgid());
1.6 tholo 195: }
1.1 deraadt 196: if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
197: exit(1);
1.8 deraadt 198:
199: setegid(getgid());
200: setgid(getgid());
1.1 deraadt 201:
202: switch (kvm_nlist(kd, symbols)) {
203: case 0:
204: break;
205: case -1:
206: errx(1, "unable to read kernel symbol table.");
207: default:
208: #ifdef notdef /* they'll be told more civilly later */
209: warnx("nlist failed");
210: for (i = 0; symbols[i].n_name != NULL; i++)
211: if (symbols[i].n_value == 0)
212: warnx("symbol %s not found",
213: symbols[i].n_name);
214: break;
215: #endif
216: }
217:
218: if ((display & (MSGINFO | MSGTOTAL)) &&
1.3 deraadt 219: (kvm_read(kd, symbols[X_MSGINFO].n_value,
220: &msginfo, sizeof(msginfo)) == sizeof(msginfo))) {
1.1 deraadt 221:
222: if (display & MSGTOTAL) {
223: printf("msginfo:\n");
224: printf("\tmsgmax: %6d\t(max characters in a message)\n",
225: msginfo.msgmax);
226: printf("\tmsgmni: %6d\t(# of message queues)\n",
227: msginfo.msgmni);
228: printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
229: msginfo.msgmnb);
230: printf("\tmsgtql: %6d\t(max # of messages in system)\n",
231: msginfo.msgtql);
232: printf("\tmsgssz: %6d\t(size of a message segment)\n",
233: msginfo.msgssz);
234: printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
235: msginfo.msgseg);
236: }
237: if (display & MSGINFO) {
238: struct msqid_ds *xmsqids;
239:
1.3 deraadt 240: if (kvm_read(kd, symbols[X_MSQIDS].n_value,
241: &msqids, sizeof(msqids)) != sizeof(msqids))
242: errx(1, "kvm_read (%s): %s",
243: symbols[X_MSQIDS].n_name, kvm_geterr(kd));
244:
245: xmsqids = malloc(sizeof(struct msqid_ds) *
246: msginfo.msgmni);
247:
248: if (kvm_read(kd, (u_long)msqids, xmsqids,
249: sizeof(struct msqid_ds) * msginfo.msgmni) !=
250: sizeof(struct msqid_ds) * msginfo.msgmni)
251: errx(1, "kvm_read (msqids): %s",
252: kvm_geterr(kd));
1.1 deraadt 253:
254: printf("Message Queues:\n");
255: printf("T ID KEY MODE OWNER GROUP");
256: if (option & CREATOR)
257: printf(" CREATOR CGROUP");
258: if (option & OUTSTANDING)
259: printf(" CBYTES QNUM");
260: if (option & BIGGEST)
261: printf(" QBYTES");
262: if (option & PID)
263: printf(" LSPID LRPID");
264: if (option & TIME)
265: printf(" STIME RTIME CTIME");
266: printf("\n");
267: for (i = 0; i < msginfo.msgmni; i += 1) {
268: if (xmsqids[i].msg_qbytes != 0) {
269: char stime_buf[100], rtime_buf[100],
270: ctime_buf[100];
271: struct msqid_ds *msqptr = &xmsqids[i];
272:
273: cvt_time(msqptr->msg_stime, stime_buf);
274: cvt_time(msqptr->msg_rtime, rtime_buf);
275: cvt_time(msqptr->msg_ctime, ctime_buf);
276:
1.13 deraadt 277: printf("q %6d %10ld %s %8s %8s",
1.1 deraadt 278: IXSEQ_TO_IPCID(i, msqptr->msg_perm),
279: msqptr->msg_perm.key,
280: fmt_perm(msqptr->msg_perm.mode),
281: user_from_uid(msqptr->msg_perm.uid, 0),
282: group_from_gid(msqptr->msg_perm.gid, 0));
283:
284: if (option & CREATOR)
285: printf(" %8s %8s",
286: user_from_uid(msqptr->msg_perm.cuid, 0),
287: group_from_gid(msqptr->msg_perm.cgid, 0));
288:
289: if (option & OUTSTANDING)
1.13 deraadt 290: printf(" %6lu %6lu",
1.1 deraadt 291: msqptr->msg_cbytes,
292: msqptr->msg_qnum);
293:
294: if (option & BIGGEST)
1.13 deraadt 295: printf(" %6lu",
1.1 deraadt 296: msqptr->msg_qbytes);
297:
298: if (option & PID)
299: printf(" %6d %6d",
300: msqptr->msg_lspid,
301: msqptr->msg_lrpid);
302:
303: if (option & TIME)
1.11 angelos 304: printf(" %s %s %s",
1.1 deraadt 305: stime_buf,
306: rtime_buf,
307: ctime_buf);
308:
309: printf("\n");
310: }
311: }
312: printf("\n");
313: }
314: } else
315: if (display & (MSGINFO | MSGTOTAL)) {
316: fprintf(stderr,
317: "SVID messages facility not configured in the system\n");
318: }
319: if ((display & (SHMINFO | SHMTOTAL)) &&
1.3 deraadt 320: (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo,
321: sizeof(shminfo)) == sizeof(shminfo))) {
322:
1.1 deraadt 323: if (display & SHMTOTAL) {
324: printf("shminfo:\n");
325: printf("\tshmmax: %7d\t(max shared memory segment size)\n",
326: shminfo.shmmax);
327: printf("\tshmmin: %7d\t(min shared memory segment size)\n",
328: shminfo.shmmin);
329: printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
330: shminfo.shmmni);
331: printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
332: shminfo.shmseg);
333: printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
334: shminfo.shmall);
335: }
336: if (display & SHMINFO) {
337: struct shmid_ds *xshmids;
338:
1.3 deraadt 339: if (kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs,
340: sizeof(shmsegs)) != sizeof(shmsegs))
341: errx(1, "kvm_read (%s): %s",
342: symbols[X_SHMSEGS].n_name, kvm_geterr(kd));
343:
344: xshmids = malloc(sizeof(struct shmid_ds) *
1.5 deraadt 345: shminfo.shmmni);
1.3 deraadt 346:
347: if (kvm_read(kd, (u_long)shmsegs, xshmids,
348: sizeof(struct shmid_ds) * shminfo.shmmni) !=
349: sizeof(struct shmid_ds) * shminfo.shmmni)
350: errx(1, "kvm_read (shmsegs): %s",
351: kvm_geterr(kd));
1.1 deraadt 352:
353: printf("Shared Memory:\n");
354: printf("T ID KEY MODE OWNER GROUP");
355: if (option & CREATOR)
356: printf(" CREATOR CGROUP");
357: if (option & OUTSTANDING)
358: printf(" NATTCH");
359: if (option & BIGGEST)
360: printf(" SEGSZ");
361: if (option & PID)
362: printf(" CPID LPID");
363: if (option & TIME)
364: printf(" ATIME DTIME CTIME");
365: printf("\n");
366: for (i = 0; i < shminfo.shmmni; i += 1) {
367: if (xshmids[i].shm_perm.mode & 0x0800) {
368: char atime_buf[100], dtime_buf[100],
369: ctime_buf[100];
370: struct shmid_ds *shmptr = &xshmids[i];
371:
372: cvt_time(shmptr->shm_atime, atime_buf);
373: cvt_time(shmptr->shm_dtime, dtime_buf);
374: cvt_time(shmptr->shm_ctime, ctime_buf);
375:
1.13 deraadt 376: printf("m %6d %10ld %s %8s %8s",
1.1 deraadt 377: IXSEQ_TO_IPCID(i, shmptr->shm_perm),
378: shmptr->shm_perm.key,
379: fmt_perm(shmptr->shm_perm.mode),
380: user_from_uid(shmptr->shm_perm.uid, 0),
381: group_from_gid(shmptr->shm_perm.gid, 0));
382:
383: if (option & CREATOR)
384: printf(" %8s %8s",
385: user_from_uid(shmptr->shm_perm.cuid, 0),
386: group_from_gid(shmptr->shm_perm.cgid, 0));
387:
388: if (option & OUTSTANDING)
389: printf(" %6d",
390: shmptr->shm_nattch);
391:
392: if (option & BIGGEST)
393: printf(" %6d",
394: shmptr->shm_segsz);
395:
396: if (option & PID)
397: printf(" %6d %6d",
398: shmptr->shm_cpid,
399: shmptr->shm_lpid);
400:
401: if (option & TIME)
1.11 angelos 402: printf(" %s %s %s",
1.1 deraadt 403: atime_buf,
404: dtime_buf,
405: ctime_buf);
406:
407: printf("\n");
408: }
409: }
410: printf("\n");
411: }
412: } else
413: if (display & (SHMINFO | SHMTOTAL)) {
414: fprintf(stderr,
415: "SVID shared memory facility not configured in the system\n");
416: }
417: if ((display & (SEMINFO | SEMTOTAL)) &&
1.3 deraadt 418: (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo,
419: sizeof(seminfo)) == sizeof(seminfo))) {
1.1 deraadt 420: struct semid_ds *xsema;
421:
422: if (display & SEMTOTAL) {
423: printf("seminfo:\n");
424: printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
425: seminfo.semmni);
426: printf("\tsemmns: %6d\t(# of semaphores in system)\n",
427: seminfo.semmns);
428: printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
429: seminfo.semmnu);
430: printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
431: seminfo.semmsl);
432: printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
433: seminfo.semopm);
434: printf("\tsemume: %6d\t(max # of undo entries per process)\n",
435: seminfo.semume);
436: printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
437: seminfo.semusz);
438: printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
439: seminfo.semvmx);
440: printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
441: seminfo.semaem);
442: }
443: if (display & SEMINFO) {
1.3 deraadt 444: if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
445: sizeof(sema)) != sizeof(sema))
446: errx(1, "kvm_read (%s): %s",
447: symbols[X_SEMA].n_name, kvm_geterr(kd));
448:
449: xsema = malloc(sizeof(struct semid_ds) *
450: seminfo.semmni);
451:
452: if (kvm_read(kd, (u_long)sema, xsema,
453: sizeof(struct semid_ds) * seminfo.semmni) !=
454: sizeof(struct semid_ds) * seminfo.semmni)
455: errx(1, "kvm_read (sema): %s",
456: kvm_geterr(kd));
1.1 deraadt 457:
458: printf("Semaphores:\n");
459: printf("T ID KEY MODE OWNER GROUP");
460: if (option & CREATOR)
461: printf(" CREATOR CGROUP");
462: if (option & BIGGEST)
463: printf(" NSEMS");
464: if (option & TIME)
465: printf(" OTIME CTIME");
466: printf("\n");
467: for (i = 0; i < seminfo.semmni; i += 1) {
468: if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
469: char ctime_buf[100], otime_buf[100];
470: struct semid_ds *semaptr = &xsema[i];
471:
472: cvt_time(semaptr->sem_otime, otime_buf);
473: cvt_time(semaptr->sem_ctime, ctime_buf);
474:
1.13 deraadt 475: printf("s %6d %10ld %s %8s %8s",
1.1 deraadt 476: IXSEQ_TO_IPCID(i, semaptr->sem_perm),
477: semaptr->sem_perm.key,
478: fmt_perm(semaptr->sem_perm.mode),
479: user_from_uid(semaptr->sem_perm.uid, 0),
480: group_from_gid(semaptr->sem_perm.gid, 0));
481:
482: if (option & CREATOR)
483: printf(" %8s %8s",
484: user_from_uid(semaptr->sem_perm.cuid, 0),
485: group_from_gid(semaptr->sem_perm.cgid, 0));
486:
487: if (option & BIGGEST)
488: printf(" %6d",
489: semaptr->sem_nsems);
490:
491: if (option & TIME)
1.11 angelos 492: printf(" %s %s",
1.1 deraadt 493: otime_buf,
494: ctime_buf);
495:
496: printf("\n");
497: }
498: }
499:
500: printf("\n");
501: }
502: } else
503: if (display & (SEMINFO | SEMTOTAL)) {
504: fprintf(stderr, "SVID semaphores facility not configured in the system\n");
505: }
506: kvm_close(kd);
507:
508: exit(0);
509: }
510:
511: void
512: usage()
513: {
514:
515: fprintf(stderr,
1.3 deraadt 516: "usage: %s [-abcmopqst] [-C corefile] [-N namelist]\n",
517: __progname);
1.1 deraadt 518: exit(1);
519: }