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.6 2004/06/19 18:55:48 joerg Exp $
31 #define _KERNEL_STRUCTURES
43 #include <sys/types.h>
44 #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;
60 static struct nlist symbols[] = {
81 fmt_perm(u_short mode)
83 static char buffer[100];
87 buffer[2] = ((mode & 0400) ? 'r' : '-');
88 buffer[3] = ((mode & 0200) ? 'w' : '-');
89 buffer[4] = ((mode & 0100) ? 'a' : '-');
90 buffer[5] = ((mode & 0040) ? 'r' : '-');
91 buffer[6] = ((mode & 0020) ? 'w' : '-');
92 buffer[7] = ((mode & 0010) ? 'a' : '-');
93 buffer[8] = ((mode & 0004) ? 'r' : '-');
94 buffer[9] = ((mode & 0002) ? 'w' : '-');
95 buffer[10] = ((mode & 0001) ? 'a' : '-');
101 cvt_time(time_t t, char *buf)
106 strcpy(buf, "no-entry");
109 sprintf(buf, "%2d:%02d:%02d",
110 tm->tm_hour, tm->tm_min, tm->tm_sec);
122 #define OUTSTANDING 4
127 main(int argc, char **argv)
129 int display = SHMINFO | MSGINFO | SEMINFO;
131 char *core = NULL, *namelist = NULL;
134 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
155 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
158 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
173 option |= OUTSTANDING;
186 * Discard setgid privileges if not the running kernel so that bad
187 * guys can't print interesting stuff from kernel memory.
189 if (namelist != NULL || core != NULL)
192 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
195 switch (kvm_nlist(kd, symbols)) {
199 errx(1, "unable to read kernel symbol table");
201 #ifdef notdef /* they'll be told more civilly later */
202 warnx("nlist failed");
203 for (i = 0; symbols[i].n_name != NULL; i++)
204 if (symbols[i].n_value == 0)
205 warnx("symbol %s not found",
211 if ((display & (MSGINFO | MSGTOTAL)) &&
212 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
214 if (display & MSGTOTAL) {
215 printf("msginfo:\n");
216 printf("\tmsgmax: %6d\t(max characters in a message)\n",
218 printf("\tmsgmni: %6d\t(# of message queues)\n",
220 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
222 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
224 printf("\tmsgssz: %6d\t(size of a message segment)\n",
226 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
229 if (display & MSGINFO) {
230 struct msqid_ds *xmsqids;
232 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
233 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
234 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
236 printf("Message Queues:\n");
237 printf("T ID KEY MODE OWNER GROUP");
238 if (option & CREATOR)
239 printf(" CREATOR CGROUP");
240 if (option & OUTSTANDING)
241 printf(" CBYTES QNUM");
242 if (option & BIGGEST)
245 printf(" LSPID LRPID");
247 printf(" STIME RTIME CTIME");
249 for (i = 0; i < msginfo.msgmni; i += 1) {
250 if (xmsqids[i].msg_qbytes != 0) {
251 char stime_buf[100], rtime_buf[100],
253 struct msqid_ds *msqptr = &xmsqids[i];
255 cvt_time(msqptr->msg_stime, stime_buf);
256 cvt_time(msqptr->msg_rtime, rtime_buf);
257 cvt_time(msqptr->msg_ctime, ctime_buf);
259 printf("q %6d %10d %s %8s %8s",
260 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
261 msqptr->msg_perm.key,
262 fmt_perm(msqptr->msg_perm.mode),
263 user_from_uid(msqptr->msg_perm.uid, 0),
264 group_from_gid(msqptr->msg_perm.gid, 0));
266 if (option & CREATOR)
268 user_from_uid(msqptr->msg_perm.cuid, 0),
269 group_from_gid(msqptr->msg_perm.cgid, 0));
271 if (option & OUTSTANDING)
276 if (option & BIGGEST)
297 if (display & (MSGINFO | MSGTOTAL)) {
299 "SVID messages facility not configured in the system\n");
301 if ((display & (SHMINFO | SHMTOTAL)) &&
302 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
303 if (display & SHMTOTAL) {
304 printf("shminfo:\n");
305 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
307 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
309 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
311 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
313 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
316 if (display & SHMINFO) {
317 struct shmid_ds *xshmids;
319 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
320 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
321 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
324 printf("Shared Memory:\n");
325 printf("T ID KEY MODE OWNER GROUP");
326 if (option & CREATOR)
327 printf(" CREATOR CGROUP");
328 if (option & OUTSTANDING)
330 if (option & BIGGEST)
333 printf(" CPID LPID");
335 printf(" ATIME DTIME CTIME");
337 for (i = 0; i < shminfo.shmmni; i += 1) {
338 if (xshmids[i].shm_perm.mode & 0x0800) {
339 char atime_buf[100], dtime_buf[100],
341 struct shmid_ds *shmptr = &xshmids[i];
343 cvt_time(shmptr->shm_atime, atime_buf);
344 cvt_time(shmptr->shm_dtime, dtime_buf);
345 cvt_time(shmptr->shm_ctime, ctime_buf);
347 printf("m %6d %10d %s %8s %8s",
348 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
349 shmptr->shm_perm.key,
350 fmt_perm(shmptr->shm_perm.mode),
351 user_from_uid(shmptr->shm_perm.uid, 0),
352 group_from_gid(shmptr->shm_perm.gid, 0));
354 if (option & CREATOR)
356 user_from_uid(shmptr->shm_perm.cuid, 0),
357 group_from_gid(shmptr->shm_perm.cgid, 0));
359 if (option & OUTSTANDING)
363 if (option & BIGGEST)
384 if (display & (SHMINFO | SHMTOTAL)) {
386 "SVID shared memory facility not configured in the system\n");
388 if ((display & (SEMINFO | SEMTOTAL)) &&
389 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
390 struct semid_ds *xsema;
392 if (display & SEMTOTAL) {
393 printf("seminfo:\n");
394 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
396 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
398 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
400 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
402 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
404 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
406 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
408 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
410 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
412 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
415 if (display & SEMINFO) {
416 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
417 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
418 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
420 printf("Semaphores:\n");
421 printf("T ID KEY MODE OWNER GROUP");
422 if (option & CREATOR)
423 printf(" CREATOR CGROUP");
424 if (option & BIGGEST)
427 printf(" OTIME CTIME");
429 for (i = 0; i < seminfo.semmni; i += 1) {
430 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
431 char ctime_buf[100], otime_buf[100];
432 struct semid_ds *semaptr = &xsema[i];
434 cvt_time(semaptr->sem_otime, otime_buf);
435 cvt_time(semaptr->sem_ctime, ctime_buf);
437 printf("s %6d %10d %s %8s %8s",
438 IXSEQ_TO_IPCID(i, semaptr->sem_perm),
439 semaptr->sem_perm.key,
440 fmt_perm(semaptr->sem_perm.mode),
441 user_from_uid(semaptr->sem_perm.uid, 0),
442 group_from_gid(semaptr->sem_perm.gid, 0));
444 if (option & CREATOR)
446 user_from_uid(semaptr->sem_perm.cuid, 0),
447 group_from_gid(semaptr->sem_perm.cgid, 0));
449 if (option & BIGGEST)
465 if (display & (SEMINFO | SEMTOTAL)) {
466 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
478 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist]\n");