2 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * $FreeBSD: src/usr.bin/ipcs/ipcs.c,v 1.12.2.4 2003/04/08 11:01:34 tjr Exp $
28 * $DragonFly: src/usr.bin/ipcs/ipcs.c,v 1.2 2003/06/17 04:29:27 dillon Exp $
41 #include <sys/types.h>
42 #include <sys/param.h>
51 struct semid_ds *sema;
52 struct seminfo seminfo;
53 struct msginfo msginfo;
54 struct msqid_ds *msqids;
55 struct shminfo shminfo;
56 struct shmid_ds *shmsegs;
58 void usage __P((void));
60 static struct nlist symbols[] = {
84 static char buffer[100];
88 buffer[2] = ((mode & 0400) ? 'r' : '-');
89 buffer[3] = ((mode & 0200) ? 'w' : '-');
90 buffer[4] = ((mode & 0100) ? 'a' : '-');
91 buffer[5] = ((mode & 0040) ? 'r' : '-');
92 buffer[6] = ((mode & 0020) ? 'w' : '-');
93 buffer[7] = ((mode & 0010) ? 'a' : '-');
94 buffer[8] = ((mode & 0004) ? 'r' : '-');
95 buffer[9] = ((mode & 0002) ? 'w' : '-');
96 buffer[10] = ((mode & 0001) ? 'a' : '-');
109 strcpy(buf, "no-entry");
112 sprintf(buf, "%2d:%02d:%02d",
113 tm->tm_hour, tm->tm_min, tm->tm_sec);
125 #define OUTSTANDING 4
134 int display = SHMINFO | MSGINFO | SEMINFO;
136 char *core = NULL, *namelist = NULL;
139 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
160 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
163 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
178 option |= OUTSTANDING;
191 * Discard setgid privileges if not the running kernel so that bad
192 * guys can't print interesting stuff from kernel memory.
194 if (namelist != NULL || core != NULL)
197 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
200 switch (kvm_nlist(kd, symbols)) {
204 errx(1, "unable to read kernel symbol table");
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",
216 if ((display & (MSGINFO | MSGTOTAL)) &&
217 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
219 if (display & MSGTOTAL) {
220 printf("msginfo:\n");
221 printf("\tmsgmax: %6d\t(max characters in a message)\n",
223 printf("\tmsgmni: %6d\t(# of message queues)\n",
225 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
227 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
229 printf("\tmsgssz: %6d\t(size of a message segment)\n",
231 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
234 if (display & MSGINFO) {
235 struct msqid_ds *xmsqids;
237 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
238 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
239 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
241 printf("Message Queues:\n");
242 printf("T ID KEY MODE OWNER GROUP");
243 if (option & CREATOR)
244 printf(" CREATOR CGROUP");
245 if (option & OUTSTANDING)
246 printf(" CBYTES QNUM");
247 if (option & BIGGEST)
250 printf(" LSPID LRPID");
252 printf(" STIME RTIME CTIME");
254 for (i = 0; i < msginfo.msgmni; i += 1) {
255 if (xmsqids[i].msg_qbytes != 0) {
256 char stime_buf[100], rtime_buf[100],
258 struct msqid_ds *msqptr = &xmsqids[i];
260 cvt_time(msqptr->msg_stime, stime_buf);
261 cvt_time(msqptr->msg_rtime, rtime_buf);
262 cvt_time(msqptr->msg_ctime, ctime_buf);
264 printf("q %6d %10d %s %8s %8s",
265 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
266 msqptr->msg_perm.key,
267 fmt_perm(msqptr->msg_perm.mode),
268 user_from_uid(msqptr->msg_perm.uid, 0),
269 group_from_gid(msqptr->msg_perm.gid, 0));
271 if (option & CREATOR)
273 user_from_uid(msqptr->msg_perm.cuid, 0),
274 group_from_gid(msqptr->msg_perm.cgid, 0));
276 if (option & OUTSTANDING)
281 if (option & BIGGEST)
302 if (display & (MSGINFO | MSGTOTAL)) {
304 "SVID messages facility not configured in the system\n");
306 if ((display & (SHMINFO | SHMTOTAL)) &&
307 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
308 if (display & SHMTOTAL) {
309 printf("shminfo:\n");
310 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
312 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
314 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
316 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
318 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
321 if (display & SHMINFO) {
322 struct shmid_ds *xshmids;
324 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
325 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
326 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
329 printf("Shared Memory:\n");
330 printf("T ID KEY MODE OWNER GROUP");
331 if (option & CREATOR)
332 printf(" CREATOR CGROUP");
333 if (option & OUTSTANDING)
335 if (option & BIGGEST)
338 printf(" CPID LPID");
340 printf(" ATIME DTIME CTIME");
342 for (i = 0; i < shminfo.shmmni; i += 1) {
343 if (xshmids[i].shm_perm.mode & 0x0800) {
344 char atime_buf[100], dtime_buf[100],
346 struct shmid_ds *shmptr = &xshmids[i];
348 cvt_time(shmptr->shm_atime, atime_buf);
349 cvt_time(shmptr->shm_dtime, dtime_buf);
350 cvt_time(shmptr->shm_ctime, ctime_buf);
352 printf("m %6d %10d %s %8s %8s",
353 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
354 shmptr->shm_perm.key,
355 fmt_perm(shmptr->shm_perm.mode),
356 user_from_uid(shmptr->shm_perm.uid, 0),
357 group_from_gid(shmptr->shm_perm.gid, 0));
359 if (option & CREATOR)
361 user_from_uid(shmptr->shm_perm.cuid, 0),
362 group_from_gid(shmptr->shm_perm.cgid, 0));
364 if (option & OUTSTANDING)
368 if (option & BIGGEST)
389 if (display & (SHMINFO | SHMTOTAL)) {
391 "SVID shared memory facility not configured in the system\n");
393 if ((display & (SEMINFO | SEMTOTAL)) &&
394 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
395 struct semid_ds *xsema;
397 if (display & SEMTOTAL) {
398 printf("seminfo:\n");
399 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
401 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
403 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
405 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
407 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
409 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
411 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
413 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
415 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
417 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
420 if (display & SEMINFO) {
421 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
422 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
423 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
425 printf("Semaphores:\n");
426 printf("T ID KEY MODE OWNER GROUP");
427 if (option & CREATOR)
428 printf(" CREATOR CGROUP");
429 if (option & BIGGEST)
432 printf(" OTIME CTIME");
434 for (i = 0; i < seminfo.semmni; i += 1) {
435 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
436 char ctime_buf[100], otime_buf[100];
437 struct semid_ds *semaptr = &xsema[i];
439 cvt_time(semaptr->sem_otime, otime_buf);
440 cvt_time(semaptr->sem_ctime, ctime_buf);
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));
449 if (option & CREATOR)
451 user_from_uid(semaptr->sem_perm.cuid, 0),
452 group_from_gid(semaptr->sem_perm.cgid, 0));
454 if (option & BIGGEST)
470 if (display & (SEMINFO | SEMTOTAL)) {
471 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
483 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist]\n");