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