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