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.8 2005/04/19 20:38:57 dillon 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
;
141 core
= namelist
= NULL
;
144 while ((i
= getopt(argc
, argv
, "MmQqSsabC:cN:ou:g:ptT")) != -1)
165 display
= SHMTOTAL
| MSGTOTAL
| SEMTOTAL
;
168 option
|= BIGGEST
| CREATOR
| OUTSTANDING
| PID
| TIME
;
183 option
|= OUTSTANDING
;
194 useruid
= user2uid(optarg
);
199 grpgid
= grp2gid(optarg
);
206 * Discard setgid privileges if not the running kernel so that bad
207 * guys can't print interesting stuff from kernel memory.
209 if (namelist
!= NULL
|| core
!= NULL
)
212 if ((kd
= kvm_open(namelist
, core
, NULL
, O_RDONLY
, "ipcs")) == NULL
)
215 switch (kvm_nlist(kd
, symbols
)) {
219 errx(1, "unable to read kernel symbol table");
221 #ifdef notdef /* they'll be told more civilly later */
222 warnx("nlist failed");
223 for (i
= 0; symbols
[i
].n_name
!= NULL
; i
++)
224 if (symbols
[i
].n_value
== 0)
225 warnx("symbol %s not found",
231 if ((display
& (MSGINFO
| MSGTOTAL
)) &&
232 kvm_read(kd
, symbols
[X_MSGINFO
].n_value
, &msginfo
, sizeof(msginfo
))== sizeof(msginfo
)) {
234 if (display
& MSGTOTAL
) {
235 printf("msginfo:\n");
236 printf("\tmsgmax: %6d\t(max characters in a message)\n",
238 printf("\tmsgmni: %6d\t(# of message queues)\n",
240 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
242 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
244 printf("\tmsgssz: %6d\t(size of a message segment)\n",
246 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
249 if (display
& MSGINFO
) {
250 struct msqid_ds
*xmsqids
;
252 kvm_read(kd
, symbols
[X_MSQIDS
].n_value
, &msqids
, sizeof(msqids
));
253 xmsqids
= malloc(sizeof(struct msqid_ds
) * msginfo
.msgmni
);
254 kvm_read(kd
, (u_long
) msqids
, xmsqids
, sizeof(struct msqid_ds
) * msginfo
.msgmni
);
256 printf("Message Queues:\n");
257 printf("T ID KEY MODE OWNER GROUP");
258 if (option
& CREATOR
)
259 printf(" CREATOR CGROUP");
260 if (option
& OUTSTANDING
)
261 printf(" CBYTES QNUM");
262 if (option
& BIGGEST
)
265 printf(" LSPID LRPID");
267 printf(" STIME RTIME CTIME");
269 for (i
= 0; i
< msginfo
.msgmni
; i
+= 1) {
270 if (xmsqids
[i
].msg_qbytes
!= 0) {
271 char stime_buf
[100], rtime_buf
[100],
273 struct msqid_ds
*msqptr
= &xmsqids
[i
];
275 if (user
&& useruid
!= msqptr
->msg_perm
.uid
)
277 if (grp
&& grpgid
!= msqptr
->msg_perm
.gid
)
280 cvt_time(msqptr
->msg_stime
, stime_buf
);
281 cvt_time(msqptr
->msg_rtime
, rtime_buf
);
282 cvt_time(msqptr
->msg_ctime
, ctime_buf
);
284 printf("q %6d %10d %s %8s %8s",
285 IXSEQ_TO_IPCID(i
, msqptr
->msg_perm
),
286 msqptr
->msg_perm
.key
,
287 fmt_perm(msqptr
->msg_perm
.mode
),
288 user_from_uid(msqptr
->msg_perm
.uid
, 0),
289 group_from_gid(msqptr
->msg_perm
.gid
, 0));
291 if (option
& CREATOR
)
293 user_from_uid(msqptr
->msg_perm
.cuid
, 0),
294 group_from_gid(msqptr
->msg_perm
.cgid
, 0));
296 if (option
& OUTSTANDING
)
301 if (option
& BIGGEST
)
322 if (display
& (MSGINFO
| MSGTOTAL
)) {
324 "SVID messages facility not configured in the system\n");
326 if ((display
& (SHMINFO
| SHMTOTAL
)) &&
327 kvm_read(kd
, symbols
[X_SHMINFO
].n_value
, &shminfo
, sizeof(shminfo
))) {
328 if (display
& SHMTOTAL
) {
329 printf("shminfo:\n");
330 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
332 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
334 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
336 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
338 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
341 if (display
& SHMINFO
) {
342 struct shmid_ds
*xshmids
;
344 kvm_read(kd
, symbols
[X_SHMSEGS
].n_value
, &shmsegs
, sizeof(shmsegs
));
345 xshmids
= malloc(sizeof(struct shmid_ds
) * shminfo
.shmmni
);
346 kvm_read(kd
, (u_long
) shmsegs
, xshmids
, sizeof(struct shmid_ds
) *
349 printf("Shared Memory:\n");
350 printf("T ID KEY MODE OWNER GROUP");
351 if (option
& CREATOR
)
352 printf(" CREATOR CGROUP");
353 if (option
& OUTSTANDING
)
355 if (option
& BIGGEST
)
358 printf(" CPID LPID");
360 printf(" ATIME DTIME CTIME");
362 for (i
= 0; i
< shminfo
.shmmni
; i
+= 1) {
363 if (xshmids
[i
].shm_perm
.mode
& 0x0800) {
364 char atime_buf
[100], dtime_buf
[100],
366 struct shmid_ds
*shmptr
= &xshmids
[i
];
368 if (user
&& useruid
!= shmptr
->shm_perm
.uid
)
371 if (grp
&& grpgid
!= shmptr
->shm_perm
.gid
)
374 cvt_time(shmptr
->shm_atime
, atime_buf
);
375 cvt_time(shmptr
->shm_dtime
, dtime_buf
);
376 cvt_time(shmptr
->shm_ctime
, ctime_buf
);
378 printf("m %6d %10d %s %8s %8s",
379 IXSEQ_TO_IPCID(i
, shmptr
->shm_perm
),
380 shmptr
->shm_perm
.key
,
381 fmt_perm(shmptr
->shm_perm
.mode
),
382 user_from_uid(shmptr
->shm_perm
.uid
, 0),
383 group_from_gid(shmptr
->shm_perm
.gid
, 0));
385 if (option
& CREATOR
)
387 user_from_uid(shmptr
->shm_perm
.cuid
, 0),
388 group_from_gid(shmptr
->shm_perm
.cgid
, 0));
390 if (option
& OUTSTANDING
)
394 if (option
& BIGGEST
)
415 if (display
& (SHMINFO
| SHMTOTAL
)) {
417 "SVID shared memory facility not configured in the system\n");
419 if ((display
& (SEMINFO
| SEMTOTAL
)) &&
420 kvm_read(kd
, symbols
[X_SEMINFO
].n_value
, &seminfo
, sizeof(seminfo
))) {
421 struct semid_ds
*xsema
;
423 if (display
& SEMTOTAL
) {
424 printf("seminfo:\n");
425 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
427 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
429 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
431 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
433 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
435 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
437 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
439 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
441 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
443 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
446 if (display
& SEMINFO
) {
447 kvm_read(kd
, symbols
[X_SEMA
].n_value
, &sema
, sizeof(sema
));
448 xsema
= malloc(sizeof(struct semid_ds
) * seminfo
.semmni
);
449 kvm_read(kd
, (u_long
) sema
, xsema
, sizeof(struct semid_ds
) * seminfo
.semmni
);
451 printf("Semaphores:\n");
452 printf("T ID KEY MODE OWNER GROUP");
453 if (option
& CREATOR
)
454 printf(" CREATOR CGROUP");
455 if (option
& BIGGEST
)
458 printf(" OTIME CTIME");
460 for (i
= 0; i
< seminfo
.semmni
; i
+= 1) {
461 if ((xsema
[i
].sem_perm
.mode
& SEM_ALLOC
) != 0) {
462 char ctime_buf
[100], otime_buf
[100];
463 struct semid_ds
*semaptr
= &xsema
[i
];
465 if (user
&& useruid
!= semaptr
->sem_perm
.uid
)
468 if (grp
&& grpgid
!= semaptr
->sem_perm
.gid
)
471 cvt_time(semaptr
->sem_otime
, otime_buf
);
472 cvt_time(semaptr
->sem_ctime
, ctime_buf
);
474 printf("s %6d %10d %s %8s %8s",
475 IXSEQ_TO_IPCID(i
, semaptr
->sem_perm
),
476 semaptr
->sem_perm
.key
,
477 fmt_perm(semaptr
->sem_perm
.mode
),
478 user_from_uid(semaptr
->sem_perm
.uid
, 0),
479 group_from_gid(semaptr
->sem_perm
.gid
, 0));
481 if (option
& CREATOR
)
483 user_from_uid(semaptr
->sem_perm
.cuid
, 0),
484 group_from_gid(semaptr
->sem_perm
.cgid
, 0));
486 if (option
& BIGGEST
)
502 if (display
& (SEMINFO
| SEMTOTAL
)) {
503 fprintf(stderr
, "SVID semaphores facility not configured in the system\n");
511 user2uid(const char *username
)
517 uid
= strtoul(username
, &r
, 0);
518 if (!*r
&& r
!= username
)
520 if ((pwd
= getpwnam(username
)) == NULL
)
521 errx(1, "No such user");
523 return (pwd
->pw_uid
);
527 grp2gid(const char *groupname
)
533 gid
= strtol(groupname
, &r
, 0);
534 if (!*r
&& r
!= groupname
)
536 if ((grp
= getgrnam(groupname
)) == NULL
)
537 errx(1, "No such group");
539 return (grp
->gr_gid
);
547 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist] [-u user] [-g group]\n");