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.3 2003/08/27 03:21:49 dillon Exp $
31 #define _KERNEL_STRUCTURES
43 #include <sys/types.h>
44 #include <sys/param.h>
52 struct semid_ds *sema;
53 struct seminfo seminfo;
54 struct msginfo msginfo;
55 struct msqid_ds *msqids;
56 struct shminfo shminfo;
57 struct shmid_ds *shmsegs;
59 void usage __P((void));
61 static struct nlist symbols[] = {
85 static char buffer[100];
89 buffer[2] = ((mode & 0400) ? 'r' : '-');
90 buffer[3] = ((mode & 0200) ? 'w' : '-');
91 buffer[4] = ((mode & 0100) ? 'a' : '-');
92 buffer[5] = ((mode & 0040) ? 'r' : '-');
93 buffer[6] = ((mode & 0020) ? 'w' : '-');
94 buffer[7] = ((mode & 0010) ? 'a' : '-');
95 buffer[8] = ((mode & 0004) ? 'r' : '-');
96 buffer[9] = ((mode & 0002) ? 'w' : '-');
97 buffer[10] = ((mode & 0001) ? 'a' : '-');
110 strcpy(buf, "no-entry");
113 sprintf(buf, "%2d:%02d:%02d",
114 tm->tm_hour, tm->tm_min, tm->tm_sec);
126 #define OUTSTANDING 4
135 int display = SHMINFO | MSGINFO | SEMINFO;
137 char *core = NULL, *namelist = NULL;
140 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
161 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
164 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
179 option |= OUTSTANDING;
192 * Discard setgid privileges if not the running kernel so that bad
193 * guys can't print interesting stuff from kernel memory.
195 if (namelist != NULL || core != NULL)
198 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
201 switch (kvm_nlist(kd, symbols)) {
205 errx(1, "unable to read kernel symbol table");
207 #ifdef notdef /* they'll be told more civilly later */
208 warnx("nlist failed");
209 for (i = 0; symbols[i].n_name != NULL; i++)
210 if (symbols[i].n_value == 0)
211 warnx("symbol %s not found",
217 if ((display & (MSGINFO | MSGTOTAL)) &&
218 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
220 if (display & MSGTOTAL) {
221 printf("msginfo:\n");
222 printf("\tmsgmax: %6d\t(max characters in a message)\n",
224 printf("\tmsgmni: %6d\t(# of message queues)\n",
226 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
228 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
230 printf("\tmsgssz: %6d\t(size of a message segment)\n",
232 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
235 if (display & MSGINFO) {
236 struct msqid_ds *xmsqids;
238 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
239 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
240 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
242 printf("Message Queues:\n");
243 printf("T ID KEY MODE OWNER GROUP");
244 if (option & CREATOR)
245 printf(" CREATOR CGROUP");
246 if (option & OUTSTANDING)
247 printf(" CBYTES QNUM");
248 if (option & BIGGEST)
251 printf(" LSPID LRPID");
253 printf(" STIME RTIME CTIME");
255 for (i = 0; i < msginfo.msgmni; i += 1) {
256 if (xmsqids[i].msg_qbytes != 0) {
257 char stime_buf[100], rtime_buf[100],
259 struct msqid_ds *msqptr = &xmsqids[i];
261 cvt_time(msqptr->msg_stime, stime_buf);
262 cvt_time(msqptr->msg_rtime, rtime_buf);
263 cvt_time(msqptr->msg_ctime, ctime_buf);
265 printf("q %6d %10d %s %8s %8s",
266 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
267 msqptr->msg_perm.key,
268 fmt_perm(msqptr->msg_perm.mode),
269 user_from_uid(msqptr->msg_perm.uid, 0),
270 group_from_gid(msqptr->msg_perm.gid, 0));
272 if (option & CREATOR)
274 user_from_uid(msqptr->msg_perm.cuid, 0),
275 group_from_gid(msqptr->msg_perm.cgid, 0));
277 if (option & OUTSTANDING)
282 if (option & BIGGEST)
303 if (display & (MSGINFO | MSGTOTAL)) {
305 "SVID messages facility not configured in the system\n");
307 if ((display & (SHMINFO | SHMTOTAL)) &&
308 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
309 if (display & SHMTOTAL) {
310 printf("shminfo:\n");
311 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
313 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
315 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
317 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
319 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
322 if (display & SHMINFO) {
323 struct shmid_ds *xshmids;
325 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
326 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
327 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
330 printf("Shared Memory:\n");
331 printf("T ID KEY MODE OWNER GROUP");
332 if (option & CREATOR)
333 printf(" CREATOR CGROUP");
334 if (option & OUTSTANDING)
336 if (option & BIGGEST)
339 printf(" CPID LPID");
341 printf(" ATIME DTIME CTIME");
343 for (i = 0; i < shminfo.shmmni; i += 1) {
344 if (xshmids[i].shm_perm.mode & 0x0800) {
345 char atime_buf[100], dtime_buf[100],
347 struct shmid_ds *shmptr = &xshmids[i];
349 cvt_time(shmptr->shm_atime, atime_buf);
350 cvt_time(shmptr->shm_dtime, dtime_buf);
351 cvt_time(shmptr->shm_ctime, ctime_buf);
353 printf("m %6d %10d %s %8s %8s",
354 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
355 shmptr->shm_perm.key,
356 fmt_perm(shmptr->shm_perm.mode),
357 user_from_uid(shmptr->shm_perm.uid, 0),
358 group_from_gid(shmptr->shm_perm.gid, 0));
360 if (option & CREATOR)
362 user_from_uid(shmptr->shm_perm.cuid, 0),
363 group_from_gid(shmptr->shm_perm.cgid, 0));
365 if (option & OUTSTANDING)
369 if (option & BIGGEST)
390 if (display & (SHMINFO | SHMTOTAL)) {
392 "SVID shared memory facility not configured in the system\n");
394 if ((display & (SEMINFO | SEMTOTAL)) &&
395 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
396 struct semid_ds *xsema;
398 if (display & SEMTOTAL) {
399 printf("seminfo:\n");
400 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
402 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
404 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
406 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
408 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
410 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
412 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
414 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
416 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
418 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
421 if (display & SEMINFO) {
422 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
423 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
424 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
426 printf("Semaphores:\n");
427 printf("T ID KEY MODE OWNER GROUP");
428 if (option & CREATOR)
429 printf(" CREATOR CGROUP");
430 if (option & BIGGEST)
433 printf(" OTIME CTIME");
435 for (i = 0; i < seminfo.semmni; i += 1) {
436 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
437 char ctime_buf[100], otime_buf[100];
438 struct semid_ds *semaptr = &xsema[i];
440 cvt_time(semaptr->sem_otime, otime_buf);
441 cvt_time(semaptr->sem_ctime, ctime_buf);
443 printf("s %6d %10d %s %8s %8s",
444 IXSEQ_TO_IPCID(i, semaptr->sem_perm),
445 semaptr->sem_perm.key,
446 fmt_perm(semaptr->sem_perm.mode),
447 user_from_uid(semaptr->sem_perm.uid, 0),
448 group_from_gid(semaptr->sem_perm.gid, 0));
450 if (option & CREATOR)
452 user_from_uid(semaptr->sem_perm.cuid, 0),
453 group_from_gid(semaptr->sem_perm.cgid, 0));
455 if (option & BIGGEST)
471 if (display & (SEMINFO | SEMTOTAL)) {
472 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
484 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist]\n");