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.4 2003/10/04 20:36:46 hmp 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;
61 static struct nlist symbols[] = {
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, "no-entry");
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 = NULL, *namelist = NULL;
135 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
156 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
159 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
174 option |= OUTSTANDING;
187 * Discard setgid privileges if not the running kernel so that bad
188 * guys can't print interesting stuff from kernel memory.
190 if (namelist != NULL || core != NULL)
193 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
196 switch (kvm_nlist(kd, symbols)) {
200 errx(1, "unable to read kernel symbol table");
202 #ifdef notdef /* they'll be told more civilly later */
203 warnx("nlist failed");
204 for (i = 0; symbols[i].n_name != NULL; i++)
205 if (symbols[i].n_value == 0)
206 warnx("symbol %s not found",
212 if ((display & (MSGINFO | MSGTOTAL)) &&
213 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
215 if (display & MSGTOTAL) {
216 printf("msginfo:\n");
217 printf("\tmsgmax: %6d\t(max characters in a message)\n",
219 printf("\tmsgmni: %6d\t(# of message queues)\n",
221 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
223 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
225 printf("\tmsgssz: %6d\t(size of a message segment)\n",
227 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
230 if (display & MSGINFO) {
231 struct msqid_ds *xmsqids;
233 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
234 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
235 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
237 printf("Message Queues:\n");
238 printf("T ID KEY MODE OWNER GROUP");
239 if (option & CREATOR)
240 printf(" CREATOR CGROUP");
241 if (option & OUTSTANDING)
242 printf(" CBYTES QNUM");
243 if (option & BIGGEST)
246 printf(" LSPID LRPID");
248 printf(" STIME RTIME CTIME");
250 for (i = 0; i < msginfo.msgmni; i += 1) {
251 if (xmsqids[i].msg_qbytes != 0) {
252 char stime_buf[100], rtime_buf[100],
254 struct msqid_ds *msqptr = &xmsqids[i];
256 cvt_time(msqptr->msg_stime, stime_buf);
257 cvt_time(msqptr->msg_rtime, rtime_buf);
258 cvt_time(msqptr->msg_ctime, ctime_buf);
260 printf("q %6d %10d %s %8s %8s",
261 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
262 msqptr->msg_perm.key,
263 fmt_perm(msqptr->msg_perm.mode),
264 user_from_uid(msqptr->msg_perm.uid, 0),
265 group_from_gid(msqptr->msg_perm.gid, 0));
267 if (option & CREATOR)
269 user_from_uid(msqptr->msg_perm.cuid, 0),
270 group_from_gid(msqptr->msg_perm.cgid, 0));
272 if (option & OUTSTANDING)
277 if (option & BIGGEST)
298 if (display & (MSGINFO | MSGTOTAL)) {
300 "SVID messages facility not configured in the system\n");
302 if ((display & (SHMINFO | SHMTOTAL)) &&
303 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
304 if (display & SHMTOTAL) {
305 printf("shminfo:\n");
306 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
308 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
310 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
312 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
314 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
317 if (display & SHMINFO) {
318 struct shmid_ds *xshmids;
320 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
321 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
322 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
325 printf("Shared Memory:\n");
326 printf("T ID KEY MODE OWNER GROUP");
327 if (option & CREATOR)
328 printf(" CREATOR CGROUP");
329 if (option & OUTSTANDING)
331 if (option & BIGGEST)
334 printf(" CPID LPID");
336 printf(" ATIME DTIME CTIME");
338 for (i = 0; i < shminfo.shmmni; i += 1) {
339 if (xshmids[i].shm_perm.mode & 0x0800) {
340 char atime_buf[100], dtime_buf[100],
342 struct shmid_ds *shmptr = &xshmids[i];
344 cvt_time(shmptr->shm_atime, atime_buf);
345 cvt_time(shmptr->shm_dtime, dtime_buf);
346 cvt_time(shmptr->shm_ctime, ctime_buf);
348 printf("m %6d %10d %s %8s %8s",
349 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
350 shmptr->shm_perm.key,
351 fmt_perm(shmptr->shm_perm.mode),
352 user_from_uid(shmptr->shm_perm.uid, 0),
353 group_from_gid(shmptr->shm_perm.gid, 0));
355 if (option & CREATOR)
357 user_from_uid(shmptr->shm_perm.cuid, 0),
358 group_from_gid(shmptr->shm_perm.cgid, 0));
360 if (option & OUTSTANDING)
364 if (option & BIGGEST)
385 if (display & (SHMINFO | SHMTOTAL)) {
387 "SVID shared memory facility not configured in the system\n");
389 if ((display & (SEMINFO | SEMTOTAL)) &&
390 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
391 struct semid_ds *xsema;
393 if (display & SEMTOTAL) {
394 printf("seminfo:\n");
395 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
397 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
399 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
401 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
403 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
405 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
407 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
409 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
411 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
413 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
416 if (display & SEMINFO) {
417 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
418 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
419 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
421 printf("Semaphores:\n");
422 printf("T ID KEY MODE OWNER GROUP");
423 if (option & CREATOR)
424 printf(" CREATOR CGROUP");
425 if (option & BIGGEST)
428 printf(" OTIME CTIME");
430 for (i = 0; i < seminfo.semmni; i += 1) {
431 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
432 char ctime_buf[100], otime_buf[100];
433 struct semid_ds *semaptr = &xsema[i];
435 cvt_time(semaptr->sem_otime, otime_buf);
436 cvt_time(semaptr->sem_ctime, ctime_buf);
438 printf("s %6d %10d %s %8s %8s",
439 IXSEQ_TO_IPCID(i, semaptr->sem_perm),
440 semaptr->sem_perm.key,
441 fmt_perm(semaptr->sem_perm.mode),
442 user_from_uid(semaptr->sem_perm.uid, 0),
443 group_from_gid(semaptr->sem_perm.gid, 0));
445 if (option & CREATOR)
447 user_from_uid(semaptr->sem_perm.cuid, 0),
448 group_from_gid(semaptr->sem_perm.cgid, 0));
450 if (option & BIGGEST)
466 if (display & (SEMINFO | SEMTOTAL)) {
467 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
479 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist]\n");