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