Annotation of src/usr.bin/ipcs/ipcs.c, Revision 1.7
1.7 ! millert 1: /* $OpenBSD: ipcs.c,v 1.6 1996/12/22 03:25:54 tholo 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.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by SigmaSoft, Th. Lockert.
19: * 4. The name of the author may not be used to endorse or promote products
20: * derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
33:
34: #include <sys/types.h>
35: #include <sys/param.h>
1.3 deraadt 36: #include <sys/time.h>
1.1 deraadt 37: #include <sys/proc.h>
38: #define _KERNEL
39: #include <sys/ipc.h>
40: #include <sys/sem.h>
41: #include <sys/shm.h>
42: #include <sys/msg.h>
1.3 deraadt 43: #undef _KERNEL
44:
45: #include <err.h>
46: #include <fcntl.h>
47: #include <kvm.h>
48: #include <limits.h>
49: #include <nlist.h>
50: #include <paths.h>
51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
54: #include <unistd.h>
1.1 deraadt 55:
1.3 deraadt 56: int semconfig __P((int, ...));
1.1 deraadt 57: void usage __P((void));
58:
1.3 deraadt 59: extern char *__progname; /* from crt0.o */
60:
1.1 deraadt 61: static struct nlist symbols[] = {
62: {"_sema"},
63: #define X_SEMA 0
64: {"_seminfo"},
65: #define X_SEMINFO 1
66: {"_semu"},
67: #define X_SEMU 2
68: {"_msginfo"},
69: #define X_MSGINFO 3
70: {"_msqids"},
71: #define X_MSQIDS 4
72: {"_shminfo"},
73: #define X_SHMINFO 5
74: {"_shmsegs"},
75: #define X_SHMSEGS 6
76: {NULL}
77: };
78:
79: static kvm_t *kd;
80:
81: char *
82: fmt_perm(mode)
83: u_short mode;
84: {
85: static char buffer[100];
86:
87: buffer[0] = '-';
88: buffer[1] = '-';
89: buffer[2] = ((mode & 0400) ? 'r' : '-');
90: buffer[3] = ((mode & 0200) ? 'w' : '-');
91: buffer[4] = ((mode & 0100) ? 'a' : '-');
92: buffer[5] = ((mode & 0040) ? 'r' : '-');
93: buffer[6] = ((mode & 0020) ? 'w' : '-');
94: buffer[7] = ((mode & 0010) ? 'a' : '-');
95: buffer[8] = ((mode & 0004) ? 'r' : '-');
96: buffer[9] = ((mode & 0002) ? 'w' : '-');
97: buffer[10] = ((mode & 0001) ? 'a' : '-');
98: buffer[11] = '\0';
99: return (&buffer[0]);
100: }
101:
102: void
103: cvt_time(t, buf)
104: time_t t;
105: char *buf;
106: {
107: struct tm *tm;
108:
109: if (t == 0) {
110: strcpy(buf, "no-entry");
111: } else {
112: tm = localtime(&t);
113: sprintf(buf, "%2d:%02d:%02d",
114: tm->tm_hour, tm->tm_min, tm->tm_sec);
115: }
116: }
117: #define SHMINFO 1
118: #define SHMTOTAL 2
119: #define MSGINFO 4
120: #define MSGTOTAL 8
121: #define SEMINFO 16
122: #define SEMTOTAL 32
123:
124: #define BIGGEST 1
125: #define CREATOR 2
126: #define OUTSTANDING 4
127: #define PID 8
128: #define TIME 16
129:
130: int
131: main(argc, argv)
132: int argc;
133: char *argv[];
134: {
135: int display = SHMINFO | MSGINFO | SEMINFO;
136: int option = 0;
137: char *core = NULL, *namelist = NULL;
1.3 deraadt 138: char errbuf[_POSIX2_LINE_MAX];
1.1 deraadt 139: int i;
140:
1.7 ! millert 141: while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
1.1 deraadt 142: switch (i) {
143: case 'M':
144: display = SHMTOTAL;
145: break;
146: case 'm':
147: display = SHMINFO;
148: break;
149: case 'Q':
150: display = MSGTOTAL;
151: break;
152: case 'q':
153: display = MSGINFO;
154: break;
155: case 'S':
156: display = SEMTOTAL;
157: break;
158: case 's':
159: display = SEMINFO;
160: break;
161: case 'T':
162: display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
163: break;
164: case 'a':
165: option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
166: break;
167: case 'b':
168: option |= BIGGEST;
169: break;
170: case 'C':
171: core = optarg;
172: break;
173: case 'c':
174: option |= CREATOR;
175: break;
176: case 'N':
177: namelist = optarg;
178: break;
179: case 'o':
180: option |= OUTSTANDING;
181: break;
182: case 'p':
183: option |= PID;
184: break;
185: case 't':
186: option |= TIME;
187: break;
188: default:
189: usage();
190: }
1.2 deraadt 191: /*
192: * Discard setgid privileges if not the running kernel so that bad
193: * guys can't print interesting stuff from kernel memory.
194: */
1.6 tholo 195: if (namelist != NULL || core != NULL) {
196: setegid(getgid());
1.2 deraadt 197: setgid(getgid());
1.6 tholo 198: }
1.1 deraadt 199: if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
200: exit(1);
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:
277: printf("q %6d %10d %s %8s %8s",
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)
290: printf(" %6d %6d",
291: msqptr->msg_cbytes,
292: msqptr->msg_qnum);
293:
294: if (option & BIGGEST)
295: printf(" %6d",
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)
304: printf("%s %s %s",
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:
376: printf("m %6d %10d %s %8s %8s",
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)
402: printf("%s %s %s",
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("\tsemmap: %6d\t(# of entries in semaphore map)\n",
425: seminfo.semmap);
426: printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
427: seminfo.semmni);
428: printf("\tsemmns: %6d\t(# of semaphores in system)\n",
429: seminfo.semmns);
430: printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
431: seminfo.semmnu);
432: printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
433: seminfo.semmsl);
434: printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
435: seminfo.semopm);
436: printf("\tsemume: %6d\t(max # of undo entries per process)\n",
437: seminfo.semume);
438: printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
439: seminfo.semusz);
440: printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
441: seminfo.semvmx);
442: printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
443: seminfo.semaem);
444: }
445: if (display & SEMINFO) {
446: if (semconfig(SEM_CONFIG_FREEZE) != 0) {
447: perror("semconfig");
448: fprintf(stderr,
449: "Can't lock semaphore facility - winging it...\n");
450: }
1.3 deraadt 451: if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
452: sizeof(sema)) != sizeof(sema))
453: errx(1, "kvm_read (%s): %s",
454: symbols[X_SEMA].n_name, kvm_geterr(kd));
455:
456: xsema = malloc(sizeof(struct semid_ds) *
457: seminfo.semmni);
458:
459: if (kvm_read(kd, (u_long)sema, xsema,
460: sizeof(struct semid_ds) * seminfo.semmni) !=
461: sizeof(struct semid_ds) * seminfo.semmni)
462: errx(1, "kvm_read (sema): %s",
463: kvm_geterr(kd));
1.1 deraadt 464:
465: printf("Semaphores:\n");
466: printf("T ID KEY MODE OWNER GROUP");
467: if (option & CREATOR)
468: printf(" CREATOR CGROUP");
469: if (option & BIGGEST)
470: printf(" NSEMS");
471: if (option & TIME)
472: printf(" OTIME CTIME");
473: printf("\n");
474: for (i = 0; i < seminfo.semmni; i += 1) {
475: if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
476: char ctime_buf[100], otime_buf[100];
477: struct semid_ds *semaptr = &xsema[i];
478: int j, value;
479: union semun junk;
480:
481: cvt_time(semaptr->sem_otime, otime_buf);
482: cvt_time(semaptr->sem_ctime, ctime_buf);
483:
484: printf("s %6d %10d %s %8s %8s",
485: IXSEQ_TO_IPCID(i, semaptr->sem_perm),
486: semaptr->sem_perm.key,
487: fmt_perm(semaptr->sem_perm.mode),
488: user_from_uid(semaptr->sem_perm.uid, 0),
489: group_from_gid(semaptr->sem_perm.gid, 0));
490:
491: if (option & CREATOR)
492: printf(" %8s %8s",
493: user_from_uid(semaptr->sem_perm.cuid, 0),
494: group_from_gid(semaptr->sem_perm.cgid, 0));
495:
496: if (option & BIGGEST)
497: printf(" %6d",
498: semaptr->sem_nsems);
499:
500: if (option & TIME)
501: printf("%s %s",
502: otime_buf,
503: ctime_buf);
504:
505: printf("\n");
506: }
507: }
508:
509: (void) semconfig(SEM_CONFIG_THAW);
510:
511: printf("\n");
512: }
513: } else
514: if (display & (SEMINFO | SEMTOTAL)) {
515: fprintf(stderr, "SVID semaphores facility not configured in the system\n");
516: }
517: kvm_close(kd);
518:
519: exit(0);
520: }
521:
522: void
523: usage()
524: {
525:
526: fprintf(stderr,
1.3 deraadt 527: "usage: %s [-abcmopqst] [-C corefile] [-N namelist]\n",
528: __progname);
1.1 deraadt 529: exit(1);
530: }