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.7 2004/11/02 19:38:49 liamfoy Exp $
31 #define _KERNEL_STRUCTURES
45 #include <sys/types.h>
46 #include <sys/param.h>
53 struct semid_ds *sema;
54 struct seminfo seminfo;
55 struct msginfo msginfo;
56 struct msqid_ds *msqids;
57 struct shminfo shminfo;
58 struct shmid_ds *shmsegs;
60 static void usage(void);
61 static uid_t user2uid(const char *);
62 static gid_t grp2gid(const char *);
64 static struct nlist symbols[] = {
85 fmt_perm(u_short mode)
87 static char buffer[100];
91 buffer[2] = ((mode & 0400) ? 'r' : '-');
92 buffer[3] = ((mode & 0200) ? 'w' : '-');
93 buffer[4] = ((mode & 0100) ? 'a' : '-');
94 buffer[5] = ((mode & 0040) ? 'r' : '-');
95 buffer[6] = ((mode & 0020) ? 'w' : '-');
96 buffer[7] = ((mode & 0010) ? 'a' : '-');
97 buffer[8] = ((mode & 0004) ? 'r' : '-');
98 buffer[9] = ((mode & 0002) ? 'w' : '-');
99 buffer[10] = ((mode & 0001) ? 'a' : '-');
105 cvt_time(time_t t, char *buf)
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
131 main(int argc, char **argv)
133 int display = SHMINFO | MSGINFO | SEMINFO;
135 char *core, *namelist;
136 const char *user, *grp;
141 core = namelist = NULL;
143 while ((i = getopt(argc, argv, "MmQqSsabC:cN:ou:g:ptT")) != -1)
164 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
167 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
182 option |= OUTSTANDING;
193 useruid = user2uid(optarg);
198 grpgid = grp2gid(optarg);
205 * Discard setgid privileges if not the running kernel so that bad
206 * guys can't print interesting stuff from kernel memory.
208 if (namelist != NULL || core != NULL)
211 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
214 switch (kvm_nlist(kd, symbols)) {
218 errx(1, "unable to read kernel symbol table");
220 #ifdef notdef /* they'll be told more civilly later */
221 warnx("nlist failed");
222 for (i = 0; symbols[i].n_name != NULL; i++)
223 if (symbols[i].n_value == 0)
224 warnx("symbol %s not found",
230 if ((display & (MSGINFO | MSGTOTAL)) &&
231 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
233 if (display & MSGTOTAL) {
234 printf("msginfo:\n");
235 printf("\tmsgmax: %6d\t(max characters in a message)\n",
237 printf("\tmsgmni: %6d\t(# of message queues)\n",
239 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
241 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
243 printf("\tmsgssz: %6d\t(size of a message segment)\n",
245 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
248 if (display & MSGINFO) {
249 struct msqid_ds *xmsqids;
251 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
252 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
253 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
255 printf("Message Queues:\n");
256 printf("T ID KEY MODE OWNER GROUP");
257 if (option & CREATOR)
258 printf(" CREATOR CGROUP");
259 if (option & OUTSTANDING)
260 printf(" CBYTES QNUM");
261 if (option & BIGGEST)
264 printf(" LSPID LRPID");
266 printf(" STIME RTIME CTIME");
268 for (i = 0; i < msginfo.msgmni; i += 1) {
269 if (xmsqids[i].msg_qbytes != 0) {
270 char stime_buf[100], rtime_buf[100],
272 struct msqid_ds *msqptr = &xmsqids[i];
274 if (user && useruid != msqptr->msg_perm.uid)
276 if (grp && grpgid != msqptr->msg_perm.gid)
279 cvt_time(msqptr->msg_stime, stime_buf);
280 cvt_time(msqptr->msg_rtime, rtime_buf);
281 cvt_time(msqptr->msg_ctime, ctime_buf);
283 printf("q %6d %10d %s %8s %8s",
284 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
285 msqptr->msg_perm.key,
286 fmt_perm(msqptr->msg_perm.mode),
287 user_from_uid(msqptr->msg_perm.uid, 0),
288 group_from_gid(msqptr->msg_perm.gid, 0));
290 if (option & CREATOR)
292 user_from_uid(msqptr->msg_perm.cuid, 0),
293 group_from_gid(msqptr->msg_perm.cgid, 0));
295 if (option & OUTSTANDING)
300 if (option & BIGGEST)
321 if (display & (MSGINFO | MSGTOTAL)) {
323 "SVID messages facility not configured in the system\n");
325 if ((display & (SHMINFO | SHMTOTAL)) &&
326 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
327 if (display & SHMTOTAL) {
328 printf("shminfo:\n");
329 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
331 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
333 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
335 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
337 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
340 if (display & SHMINFO) {
341 struct shmid_ds *xshmids;
343 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
344 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
345 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
348 printf("Shared Memory:\n");
349 printf("T ID KEY MODE OWNER GROUP");
350 if (option & CREATOR)
351 printf(" CREATOR CGROUP");
352 if (option & OUTSTANDING)
354 if (option & BIGGEST)
357 printf(" CPID LPID");
359 printf(" ATIME DTIME CTIME");
361 for (i = 0; i < shminfo.shmmni; i += 1) {
362 if (xshmids[i].shm_perm.mode & 0x0800) {
363 char atime_buf[100], dtime_buf[100],
365 struct shmid_ds *shmptr = &xshmids[i];
367 if (user && useruid != shmptr->shm_perm.uid)
370 if (grp && grpgid != shmptr->shm_perm.gid)
373 cvt_time(shmptr->shm_atime, atime_buf);
374 cvt_time(shmptr->shm_dtime, dtime_buf);
375 cvt_time(shmptr->shm_ctime, ctime_buf);
377 printf("m %6d %10d %s %8s %8s",
378 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
379 shmptr->shm_perm.key,
380 fmt_perm(shmptr->shm_perm.mode),
381 user_from_uid(shmptr->shm_perm.uid, 0),
382 group_from_gid(shmptr->shm_perm.gid, 0));
384 if (option & CREATOR)
386 user_from_uid(shmptr->shm_perm.cuid, 0),
387 group_from_gid(shmptr->shm_perm.cgid, 0));
389 if (option & OUTSTANDING)
393 if (option & BIGGEST)
414 if (display & (SHMINFO | SHMTOTAL)) {
416 "SVID shared memory facility not configured in the system\n");
418 if ((display & (SEMINFO | SEMTOTAL)) &&
419 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
420 struct semid_ds *xsema;
422 if (display & SEMTOTAL) {
423 printf("seminfo:\n");
424 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
426 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
428 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
430 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
432 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
434 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
436 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
438 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
440 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
442 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
445 if (display & SEMINFO) {
446 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
447 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
448 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
450 printf("Semaphores:\n");
451 printf("T ID KEY MODE OWNER GROUP");
452 if (option & CREATOR)
453 printf(" CREATOR CGROUP");
454 if (option & BIGGEST)
457 printf(" OTIME CTIME");
459 for (i = 0; i < seminfo.semmni; i += 1) {
460 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
461 char ctime_buf[100], otime_buf[100];
462 struct semid_ds *semaptr = &xsema[i];
464 if (user && useruid != semaptr->sem_perm.uid)
467 if (grp && grpgid != semaptr->sem_perm.gid)
470 cvt_time(semaptr->sem_otime, otime_buf);
471 cvt_time(semaptr->sem_ctime, ctime_buf);
473 printf("s %6d %10d %s %8s %8s",
474 IXSEQ_TO_IPCID(i, semaptr->sem_perm),
475 semaptr->sem_perm.key,
476 fmt_perm(semaptr->sem_perm.mode),
477 user_from_uid(semaptr->sem_perm.uid, 0),
478 group_from_gid(semaptr->sem_perm.gid, 0));
480 if (option & CREATOR)
482 user_from_uid(semaptr->sem_perm.cuid, 0),
483 group_from_gid(semaptr->sem_perm.cgid, 0));
485 if (option & BIGGEST)
501 if (display & (SEMINFO | SEMTOTAL)) {
502 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
510 user2uid(const char *username)
516 uid = strtoul(username, &r, 0);
517 if (!*r && r != username)
519 if ((pwd = getpwnam(username)) == NULL)
520 errx(1, "No such user");
522 return (pwd->pw_uid);
526 grp2gid(const char *groupname)
532 gid = strtol(groupname, &r, 0);
533 if (!*r && r != groupname)
535 if ((grp = getgrnam(groupname)) == NULL)
536 errx(1, "No such group");
538 return (grp->gr_gid);
546 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist] [-u user] [-g group]\n");