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