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 $
30 #define _KERNEL_STRUCTURES
44 #include <sys/types.h>
45 #include <sys/param.h>
52 struct semid_pool *sema;
53 struct seminfo seminfo;
54 struct msginfo msginfo;
55 struct msqid_ds *msqids;
56 struct shminfo shminfo;
57 struct shmid_ds *shmsegs;
59 static void usage(void);
60 static uid_t user2uid(const char *);
61 static gid_t grp2gid(const char *);
63 static struct nlist symbols[] = {
64 { .n_name = "_sema" },
66 { .n_name = "_seminfo" },
68 { .n_name = "_msginfo" },
70 { .n_name = "_msqids" },
72 { .n_name = "_shminfo" },
74 { .n_name = "_shmsegs" },
82 fmt_perm(u_short mode)
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' : '-');
102 cvt_time(time_t t, char *buf)
107 strcpy(buf, "noentry");
110 sprintf(buf, "%2d:%02d:%02d",
111 tm->tm_hour, tm->tm_min, tm->tm_sec);
123 #define OUTSTANDING 4
128 main(int argc, char **argv)
130 int display = SHMINFO | MSGINFO | SEMINFO;
132 char *core, *namelist;
138 core = namelist = NULL;
141 while ((i = getopt(argc, argv, "MmQqSsabC:cN:ou:g:ptT")) != -1)
162 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
165 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
180 option |= OUTSTANDING;
191 useruid = user2uid(optarg);
196 grpgid = grp2gid(optarg);
203 * Discard setgid privileges if not the running kernel so that bad
204 * guys can't print interesting stuff from kernel memory.
206 if (namelist != NULL || core != NULL)
209 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
212 switch (kvm_nlist(kd, symbols)) {
216 errx(1, "unable to read kernel symbol table");
218 #ifdef notdef /* they'll be told more civilly later */
219 warnx("nlist failed");
220 for (i = 0; symbols[i].n_name != NULL; i++)
221 if (symbols[i].n_value == 0)
222 warnx("symbol %s not found",
228 if ((display & (MSGINFO | MSGTOTAL)) &&
229 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
231 if (display & MSGTOTAL) {
232 printf("msginfo:\n");
233 printf("\tmsgmax: %6d\t(max characters in a message)\n",
235 printf("\tmsgmni: %6d\t(# of message queues)\n",
237 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
239 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
241 printf("\tmsgssz: %6d\t(size of a message segment)\n",
243 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
246 if (display & MSGINFO) {
247 struct msqid_ds *xmsqids;
249 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
250 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
251 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
253 printf("Message Queues:\n");
254 printf("T ID KEY MODE OWNER GROUP");
255 if (option & CREATOR)
256 printf(" CREATOR CGROUP");
257 if (option & OUTSTANDING)
258 printf(" CBYTES QNUM");
259 if (option & BIGGEST)
262 printf(" LSPID LRPID");
264 printf(" STIME RTIME CTIME");
266 for (i = 0; i < msginfo.msgmni; i += 1) {
267 if (xmsqids[i].msg_qbytes != 0) {
268 char stime_buf[100], rtime_buf[100],
270 struct msqid_ds *msqptr = &xmsqids[i];
272 if (user && useruid != msqptr->msg_perm.uid)
274 if (grp && grpgid != msqptr->msg_perm.gid)
277 cvt_time(msqptr->msg_stime, stime_buf);
278 cvt_time(msqptr->msg_rtime, rtime_buf);
279 cvt_time(msqptr->msg_ctime, ctime_buf);
281 printf("q %6d %10ld %s %8s %8s",
282 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
283 (long)msqptr->msg_perm.key,
284 fmt_perm(msqptr->msg_perm.mode),
285 user_from_uid(msqptr->msg_perm.uid, 0),
286 group_from_gid(msqptr->msg_perm.gid, 0));
288 if (option & CREATOR)
290 user_from_uid(msqptr->msg_perm.cuid, 0),
291 group_from_gid(msqptr->msg_perm.cgid, 0));
293 if (option & OUTSTANDING)
298 if (option & BIGGEST)
304 (int)msqptr->msg_lspid,
305 (int)msqptr->msg_lrpid);
319 if (display & (MSGINFO | MSGTOTAL)) {
321 "SVID messages facility not configured in the system\n");
323 if ((display & (SHMINFO | SHMTOTAL)) &&
324 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
325 if (display & SHMTOTAL) {
326 printf("shminfo:\n");
327 printf("\tshmmax: %12ld\t(max shared memory segment size)\n",
329 printf("\tshmmin: %12ld\t(min shared memory segment size)\n",
331 printf("\tshmmni: %12ld\t(max number of shared memory identifiers)\n",
333 printf("\tshmseg: %12ld\t(max shared memory segments per process)\n",
335 printf("\tshmall: %12ld\t(max amount of shared memory in pages)\n\n",
338 if (display & SHMINFO) {
339 struct shmid_ds *xshmids;
341 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
342 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
343 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
346 printf("Shared Memory:\n");
347 printf("T ID KEY MODE OWNER GROUP");
348 if (option & CREATOR)
349 printf(" CREATOR CGROUP");
350 if (option & OUTSTANDING)
352 if (option & BIGGEST)
355 printf(" CPID LPID");
357 printf(" ATIME DTIME CTIME");
359 for (i = 0; i < shminfo.shmmni; i += 1) {
360 if (xshmids[i].shm_perm.mode & 0x0800) {
361 char atime_buf[100], dtime_buf[100],
363 struct shmid_ds *shmptr = &xshmids[i];
365 if (user && useruid != shmptr->shm_perm.uid)
368 if (grp && grpgid != shmptr->shm_perm.gid)
371 cvt_time(shmptr->shm_atime, atime_buf);
372 cvt_time(shmptr->shm_dtime, dtime_buf);
373 cvt_time(shmptr->shm_ctime, ctime_buf);
375 printf("m %6d %10ld %s %8s %8s",
376 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
377 (long)shmptr->shm_perm.key,
378 fmt_perm(shmptr->shm_perm.mode),
379 user_from_uid(shmptr->shm_perm.uid, 0),
380 group_from_gid(shmptr->shm_perm.gid, 0));
382 if (option & CREATOR)
384 user_from_uid(shmptr->shm_perm.cuid, 0),
385 group_from_gid(shmptr->shm_perm.cgid, 0));
387 if (option & OUTSTANDING)
391 if (option & BIGGEST)
412 if (display & (SHMINFO | SHMTOTAL)) {
414 "SVID shared memory facility not configured in the system\n");
416 if ((display & (SEMINFO | SEMTOTAL)) &&
417 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
418 struct semid_pool *xsema;
420 if (display & SEMTOTAL) {
421 printf("seminfo:\n");
422 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
424 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
426 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
428 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
430 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
432 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
434 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
436 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
438 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
440 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
443 if (display & SEMINFO) {
444 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
445 xsema = malloc(sizeof(struct semid_pool) * seminfo.semmni);
446 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_pool) * seminfo.semmni);
448 printf("Semaphores:\n");
449 printf("T ID KEY MODE OWNER GROUP");
450 if (option & CREATOR)
451 printf(" CREATOR CGROUP");
452 if (option & BIGGEST)
455 printf(" OTIME CTIME");
457 for (i = 0; i < seminfo.semmni; i += 1) {
458 if ((xsema[i].ds.sem_perm.mode & SEM_ALLOC) != 0) {
459 char ctime_buf[100], otime_buf[100];
460 struct semid_ds *semaptr = &xsema[i].ds;
462 if (user && useruid != semaptr->sem_perm.uid)
465 if (grp && grpgid != semaptr->sem_perm.gid)
468 cvt_time(semaptr->sem_otime, otime_buf);
469 cvt_time(semaptr->sem_ctime, ctime_buf);
471 printf("s %6d %10ld %s %8s %8s",
472 IXSEQ_TO_IPCID(i, semaptr->sem_perm),
473 (long)semaptr->sem_perm.key,
474 fmt_perm(semaptr->sem_perm.mode),
475 user_from_uid(semaptr->sem_perm.uid, 0),
476 group_from_gid(semaptr->sem_perm.gid, 0));
478 if (option & CREATOR)
480 user_from_uid(semaptr->sem_perm.cuid, 0),
481 group_from_gid(semaptr->sem_perm.cgid, 0));
483 if (option & BIGGEST)
499 if (display & (SEMINFO | SEMTOTAL)) {
500 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
508 user2uid(const char *username)
514 uid = strtoul(username, &r, 0);
515 if (!*r && r != username)
517 if ((pwd = getpwnam(username)) == NULL)
518 errx(1, "No such user");
520 return (pwd->pw_uid);
524 grp2gid(const char *groupname)
530 gid = strtol(groupname, &r, 0);
531 if (!*r && r != groupname)
533 if ((grp = getgrnam(groupname)) == NULL)
534 errx(1, "No such group");
536 return (grp->gr_gid);
544 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist] [-u user] [-g group]\n");