Annotation of src/usr.bin/ipcs/ipcs.c, Revision 1.8
1.8 ! deraadt 1: /* $OpenBSD: ipcs.c,v 1.7 1997/01/15 23:42:37 millert 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);
1.8 ! deraadt 201:
! 202: setegid(getgid());
! 203: setgid(getgid());
1.1 deraadt 204:
205: switch (kvm_nlist(kd, symbols)) {
206: case 0:
207: break;
208: case -1:
209: errx(1, "unable to read kernel symbol table.");
210: default:
211: #ifdef notdef /* they'll be told more civilly later */
212: warnx("nlist failed");
213: for (i = 0; symbols[i].n_name != NULL; i++)
214: if (symbols[i].n_value == 0)
215: warnx("symbol %s not found",
216: symbols[i].n_name);
217: break;
218: #endif
219: }
220:
221: if ((display & (MSGINFO | MSGTOTAL)) &&
1.3 deraadt 222: (kvm_read(kd, symbols[X_MSGINFO].n_value,
223: &msginfo, sizeof(msginfo)) == sizeof(msginfo))) {
1.1 deraadt 224:
225: if (display & MSGTOTAL) {
226: printf("msginfo:\n");
227: printf("\tmsgmax: %6d\t(max characters in a message)\n",
228: msginfo.msgmax);
229: printf("\tmsgmni: %6d\t(# of message queues)\n",
230: msginfo.msgmni);
231: printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
232: msginfo.msgmnb);
233: printf("\tmsgtql: %6d\t(max # of messages in system)\n",
234: msginfo.msgtql);
235: printf("\tmsgssz: %6d\t(size of a message segment)\n",
236: msginfo.msgssz);
237: printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
238: msginfo.msgseg);
239: }
240: if (display & MSGINFO) {
241: struct msqid_ds *xmsqids;
242:
1.3 deraadt 243: if (kvm_read(kd, symbols[X_MSQIDS].n_value,
244: &msqids, sizeof(msqids)) != sizeof(msqids))
245: errx(1, "kvm_read (%s): %s",
246: symbols[X_MSQIDS].n_name, kvm_geterr(kd));
247:
248: xmsqids = malloc(sizeof(struct msqid_ds) *
249: msginfo.msgmni);
250:
251: if (kvm_read(kd, (u_long)msqids, xmsqids,
252: sizeof(struct msqid_ds) * msginfo.msgmni) !=
253: sizeof(struct msqid_ds) * msginfo.msgmni)
254: errx(1, "kvm_read (msqids): %s",
255: kvm_geterr(kd));
1.1 deraadt 256:
257: printf("Message Queues:\n");
258: printf("T ID KEY MODE OWNER GROUP");
259: if (option & CREATOR)
260: printf(" CREATOR CGROUP");
261: if (option & OUTSTANDING)
262: printf(" CBYTES QNUM");
263: if (option & BIGGEST)
264: printf(" QBYTES");
265: if (option & PID)
266: printf(" LSPID LRPID");
267: if (option & TIME)
268: printf(" STIME RTIME CTIME");
269: printf("\n");
270: for (i = 0; i < msginfo.msgmni; i += 1) {
271: if (xmsqids[i].msg_qbytes != 0) {
272: char stime_buf[100], rtime_buf[100],
273: ctime_buf[100];
274: struct msqid_ds *msqptr = &xmsqids[i];
275:
276: cvt_time(msqptr->msg_stime, stime_buf);
277: cvt_time(msqptr->msg_rtime, rtime_buf);
278: cvt_time(msqptr->msg_ctime, ctime_buf);
279:
280: printf("q %6d %10d %s %8s %8s",
281: IXSEQ_TO_IPCID(i, msqptr->msg_perm),
282: msqptr->msg_perm.key,
283: fmt_perm(msqptr->msg_perm.mode),
284: user_from_uid(msqptr->msg_perm.uid, 0),
285: group_from_gid(msqptr->msg_perm.gid, 0));
286:
287: if (option & CREATOR)
288: printf(" %8s %8s",
289: user_from_uid(msqptr->msg_perm.cuid, 0),
290: group_from_gid(msqptr->msg_perm.cgid, 0));
291:
292: if (option & OUTSTANDING)
293: printf(" %6d %6d",
294: msqptr->msg_cbytes,
295: msqptr->msg_qnum);
296:
297: if (option & BIGGEST)
298: printf(" %6d",
299: msqptr->msg_qbytes);
300:
301: if (option & PID)
302: printf(" %6d %6d",
303: msqptr->msg_lspid,
304: msqptr->msg_lrpid);
305:
306: if (option & TIME)
307: printf("%s %s %s",
308: stime_buf,
309: rtime_buf,
310: ctime_buf);
311:
312: printf("\n");
313: }
314: }
315: printf("\n");
316: }
317: } else
318: if (display & (MSGINFO | MSGTOTAL)) {
319: fprintf(stderr,
320: "SVID messages facility not configured in the system\n");
321: }
322: if ((display & (SHMINFO | SHMTOTAL)) &&
1.3 deraadt 323: (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo,
324: sizeof(shminfo)) == sizeof(shminfo))) {
325:
1.1 deraadt 326: if (display & SHMTOTAL) {
327: printf("shminfo:\n");
328: printf("\tshmmax: %7d\t(max shared memory segment size)\n",
329: shminfo.shmmax);
330: printf("\tshmmin: %7d\t(min shared memory segment size)\n",
331: shminfo.shmmin);
332: printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
333: shminfo.shmmni);
334: printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
335: shminfo.shmseg);
336: printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
337: shminfo.shmall);
338: }
339: if (display & SHMINFO) {
340: struct shmid_ds *xshmids;
341:
1.3 deraadt 342: if (kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs,
343: sizeof(shmsegs)) != sizeof(shmsegs))
344: errx(1, "kvm_read (%s): %s",
345: symbols[X_SHMSEGS].n_name, kvm_geterr(kd));
346:
347: xshmids = malloc(sizeof(struct shmid_ds) *
1.5 deraadt 348: shminfo.shmmni);
1.3 deraadt 349:
350: if (kvm_read(kd, (u_long)shmsegs, xshmids,
351: sizeof(struct shmid_ds) * shminfo.shmmni) !=
352: sizeof(struct shmid_ds) * shminfo.shmmni)
353: errx(1, "kvm_read (shmsegs): %s",
354: kvm_geterr(kd));
1.1 deraadt 355:
356: printf("Shared Memory:\n");
357: printf("T ID KEY MODE OWNER GROUP");
358: if (option & CREATOR)
359: printf(" CREATOR CGROUP");
360: if (option & OUTSTANDING)
361: printf(" NATTCH");
362: if (option & BIGGEST)
363: printf(" SEGSZ");
364: if (option & PID)
365: printf(" CPID LPID");
366: if (option & TIME)
367: printf(" ATIME DTIME CTIME");
368: printf("\n");
369: for (i = 0; i < shminfo.shmmni; i += 1) {
370: if (xshmids[i].shm_perm.mode & 0x0800) {
371: char atime_buf[100], dtime_buf[100],
372: ctime_buf[100];
373: struct shmid_ds *shmptr = &xshmids[i];
374:
375: cvt_time(shmptr->shm_atime, atime_buf);
376: cvt_time(shmptr->shm_dtime, dtime_buf);
377: cvt_time(shmptr->shm_ctime, ctime_buf);
378:
379: printf("m %6d %10d %s %8s %8s",
380: IXSEQ_TO_IPCID(i, shmptr->shm_perm),
381: shmptr->shm_perm.key,
382: fmt_perm(shmptr->shm_perm.mode),
383: user_from_uid(shmptr->shm_perm.uid, 0),
384: group_from_gid(shmptr->shm_perm.gid, 0));
385:
386: if (option & CREATOR)
387: printf(" %8s %8s",
388: user_from_uid(shmptr->shm_perm.cuid, 0),
389: group_from_gid(shmptr->shm_perm.cgid, 0));
390:
391: if (option & OUTSTANDING)
392: printf(" %6d",
393: shmptr->shm_nattch);
394:
395: if (option & BIGGEST)
396: printf(" %6d",
397: shmptr->shm_segsz);
398:
399: if (option & PID)
400: printf(" %6d %6d",
401: shmptr->shm_cpid,
402: shmptr->shm_lpid);
403:
404: if (option & TIME)
405: printf("%s %s %s",
406: atime_buf,
407: dtime_buf,
408: ctime_buf);
409:
410: printf("\n");
411: }
412: }
413: printf("\n");
414: }
415: } else
416: if (display & (SHMINFO | SHMTOTAL)) {
417: fprintf(stderr,
418: "SVID shared memory facility not configured in the system\n");
419: }
420: if ((display & (SEMINFO | SEMTOTAL)) &&
1.3 deraadt 421: (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo,
422: sizeof(seminfo)) == sizeof(seminfo))) {
1.1 deraadt 423: struct semid_ds *xsema;
424:
425: if (display & SEMTOTAL) {
426: printf("seminfo:\n");
427: printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
428: seminfo.semmap);
429: printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
430: seminfo.semmni);
431: printf("\tsemmns: %6d\t(# of semaphores in system)\n",
432: seminfo.semmns);
433: printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
434: seminfo.semmnu);
435: printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
436: seminfo.semmsl);
437: printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
438: seminfo.semopm);
439: printf("\tsemume: %6d\t(max # of undo entries per process)\n",
440: seminfo.semume);
441: printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
442: seminfo.semusz);
443: printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
444: seminfo.semvmx);
445: printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
446: seminfo.semaem);
447: }
448: if (display & SEMINFO) {
449: if (semconfig(SEM_CONFIG_FREEZE) != 0) {
450: perror("semconfig");
451: fprintf(stderr,
452: "Can't lock semaphore facility - winging it...\n");
453: }
1.3 deraadt 454: if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
455: sizeof(sema)) != sizeof(sema))
456: errx(1, "kvm_read (%s): %s",
457: symbols[X_SEMA].n_name, kvm_geterr(kd));
458:
459: xsema = malloc(sizeof(struct semid_ds) *
460: seminfo.semmni);
461:
462: if (kvm_read(kd, (u_long)sema, xsema,
463: sizeof(struct semid_ds) * seminfo.semmni) !=
464: sizeof(struct semid_ds) * seminfo.semmni)
465: errx(1, "kvm_read (sema): %s",
466: kvm_geterr(kd));
1.1 deraadt 467:
468: printf("Semaphores:\n");
469: printf("T ID KEY MODE OWNER GROUP");
470: if (option & CREATOR)
471: printf(" CREATOR CGROUP");
472: if (option & BIGGEST)
473: printf(" NSEMS");
474: if (option & TIME)
475: printf(" OTIME CTIME");
476: printf("\n");
477: for (i = 0; i < seminfo.semmni; i += 1) {
478: if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
479: char ctime_buf[100], otime_buf[100];
480: struct semid_ds *semaptr = &xsema[i];
481: int j, value;
482: union semun junk;
483:
484: cvt_time(semaptr->sem_otime, otime_buf);
485: cvt_time(semaptr->sem_ctime, ctime_buf);
486:
487: printf("s %6d %10d %s %8s %8s",
488: IXSEQ_TO_IPCID(i, semaptr->sem_perm),
489: semaptr->sem_perm.key,
490: fmt_perm(semaptr->sem_perm.mode),
491: user_from_uid(semaptr->sem_perm.uid, 0),
492: group_from_gid(semaptr->sem_perm.gid, 0));
493:
494: if (option & CREATOR)
495: printf(" %8s %8s",
496: user_from_uid(semaptr->sem_perm.cuid, 0),
497: group_from_gid(semaptr->sem_perm.cgid, 0));
498:
499: if (option & BIGGEST)
500: printf(" %6d",
501: semaptr->sem_nsems);
502:
503: if (option & TIME)
504: printf("%s %s",
505: otime_buf,
506: ctime_buf);
507:
508: printf("\n");
509: }
510: }
511:
512: (void) semconfig(SEM_CONFIG_THAW);
513:
514: printf("\n");
515: }
516: } else
517: if (display & (SEMINFO | SEMTOTAL)) {
518: fprintf(stderr, "SVID semaphores facility not configured in the system\n");
519: }
520: kvm_close(kd);
521:
522: exit(0);
523: }
524:
525: void
526: usage()
527: {
528:
529: fprintf(stderr,
1.3 deraadt 530: "usage: %s [-abcmopqst] [-C corefile] [-N namelist]\n",
531: __progname);
1.1 deraadt 532: exit(1);
533: }