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