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