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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
52 char *fmt_perm(u_short);
53 void cvt_time(time_t, char *);
55 uid_t user2uid(char *username);
57 void print_kmsqtotal(struct msginfo msginfo);
58 void print_kmsqheader(int option);
59 void print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr);
60 void print_kshmtotal(struct shminfo shminfo);
61 void print_kshmheader(int option);
62 void print_kshmptr(int i, int option, struct shmid_kernel *kshmptr);
63 void print_ksemtotal(struct seminfo seminfo);
64 void print_ksemheader(int option);
65 void print_ksemptr(int i, int option, struct semid_kernel *ksemaptr);
68 fmt_perm(u_short mode)
70 static char buffer[100];
74 buffer[2] = ((mode & 0400) ? 'r' : '-');
75 buffer[3] = ((mode & 0200) ? 'w' : '-');
76 buffer[4] = ((mode & 0100) ? 'a' : '-');
77 buffer[5] = ((mode & 0040) ? 'r' : '-');
78 buffer[6] = ((mode & 0020) ? 'w' : '-');
79 buffer[7] = ((mode & 0010) ? 'a' : '-');
80 buffer[8] = ((mode & 0004) ? 'r' : '-');
81 buffer[9] = ((mode & 0002) ? 'w' : '-');
82 buffer[10] = ((mode & 0001) ? 'a' : '-');
88 cvt_time(time_t t, char *buf)
93 strcpy(buf, "no-entry");
96 sprintf(buf, "%2d:%02d:%02d",
97 tm->tm_hour, tm->tm_min, tm->tm_sec);
103 #define OUTSTANDING 4
108 main(int argc, char *argv[])
110 int display = SHMINFO | MSGINFO | SEMINFO;
112 char *core = NULL, *user = NULL, *namelist = NULL;
113 char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */
117 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
120 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
141 option |= OUTSTANDING;
159 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
166 uid = user2uid(user);
176 * If paths to the exec file or core file were specified, we
177 * aren't operating on the running kernel, so we can't use
180 if (namelist != NULL || core != NULL)
184 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
186 errx(1, "kvm_openfiles: %s", kvmoferr);
187 switch (kvm_nlist(kd, symbols)) {
191 errx(1, "unable to read kernel symbol table");
197 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
198 if ((display & (MSGINFO | MSGTOTAL))) {
199 if (display & MSGTOTAL)
200 print_kmsqtotal(msginfo);
202 if (display & MSGINFO) {
203 struct msqid_kernel *kxmsqids;
207 sizeof(struct msqid_kernel) * msginfo.msgmni;
208 kxmsqids = malloc(kxmsqids_len);
209 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
211 print_kmsqheader(option);
213 for (i = 0; i < msginfo.msgmni; i += 1) {
214 if (kxmsqids[i].u.msg_qbytes != 0) {
216 uid != kxmsqids[i].u.msg_perm.uid)
219 print_kmsqptr(i, option, &kxmsqids[i]);
227 if (display & (MSGINFO | MSGTOTAL)) {
229 "SVID messages facility "
230 "not configured in the system\n");
233 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
234 if ((display & (SHMINFO | SHMTOTAL))) {
236 if (display & SHMTOTAL)
237 print_kshmtotal(shminfo);
239 if (display & SHMINFO) {
240 struct shmid_kernel *kxshmids;
244 sizeof(struct shmid_kernel) * shminfo.shmmni;
245 kxshmids = malloc(kxshmids_len);
246 kget(X_SHMSEGS, kxshmids, kxshmids_len);
248 print_kshmheader(option);
250 for (i = 0; i < shminfo.shmmni; i += 1) {
251 if (kxshmids[i].u.shm_perm.mode & 0x0800) {
253 uid != kxshmids[i].u.shm_perm.uid)
256 print_kshmptr(i, option, &kxshmids[i]);
262 if (display & (SHMINFO | SHMTOTAL)) {
264 "SVID shared memory facility "
265 "not configured in the system\n");
268 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
269 if ((display & (SEMINFO | SEMTOTAL))) {
270 struct semid_kernel *kxsema;
273 if (display & SEMTOTAL)
274 print_ksemtotal(seminfo);
276 if (display & SEMINFO) {
278 sizeof(struct semid_kernel) * seminfo.semmni;
279 kxsema = malloc(kxsema_len);
280 kget(X_SEMA, kxsema, kxsema_len);
282 print_ksemheader(option);
284 for (i = 0; i < seminfo.semmni; i += 1) {
285 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
288 uid != kxsema[i].u.sem_perm.uid)
291 print_ksemptr(i, option, &kxsema[i]);
299 if (display & (SEMINFO | SEMTOTAL)) {
301 "SVID semaphores facility "
302 "not configured in the system\n");
312 print_kmsqtotal(struct msginfo msginfo)
315 printf("msginfo:\n");
316 printf("\tmsgmax: %12d\t(max characters in a message)\n",
318 printf("\tmsgmni: %12d\t(# of message queues)\n",
320 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
322 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
324 printf("\tmsgssz: %12d\t(size of a message segment)\n",
326 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
330 void print_kmsqheader(int option)
333 printf("Message Queues:\n");
334 printf("T %12s %12s %-11s %-8s %-8s",
335 "ID", "KEY", "MODE", "OWNER", "GROUP");
336 if (option & CREATOR)
337 printf(" %-8s %-8s", "CREATOR", "CGROUP");
338 if (option & OUTSTANDING)
339 printf(" %20s %20s", "CBYTES", "QNUM");
340 if (option & BIGGEST)
341 printf(" %20s", "QBYTES");
343 printf(" %12s %12s", "LSPID", "LRPID");
345 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
350 print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
352 char stime_buf[100], rtime_buf[100], ctime_buf[100];
354 cvt_time(kmsqptr->u.msg_stime, stime_buf);
355 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
356 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
358 printf("q %12d %12d %s %-8s %-8s",
359 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
360 (int)kmsqptr->u.msg_perm.key,
361 fmt_perm(kmsqptr->u.msg_perm.mode),
362 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
363 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
365 if (option & CREATOR)
367 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
368 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
370 if (option & OUTSTANDING)
371 printf(" %12lu %12lu",
372 kmsqptr->u.msg_cbytes,
373 kmsqptr->u.msg_qnum);
375 if (option & BIGGEST)
376 printf(" %20lu", kmsqptr->u.msg_qbytes);
380 kmsqptr->u.msg_lspid,
381 kmsqptr->u.msg_lrpid);
393 print_kshmtotal(struct shminfo shminfo)
396 printf("shminfo:\n");
397 printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
399 printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
401 printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
403 printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
405 printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
410 print_kshmheader(int option)
413 printf("Shared Memory:\n");
414 printf("T %12s %12s %-11s %-8s %-8s",
415 "ID", "KEY", "MODE", "OWNER", "GROUP");
416 if (option & CREATOR)
417 printf(" %-8s %-8s", "CREATOR", "CGROUP");
418 if (option & OUTSTANDING)
419 printf(" %12s", "NATTCH");
420 if (option & BIGGEST)
421 printf(" %12s", "SEGSZ");
423 printf(" %12s %12s", "CPID", "LPID");
425 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
430 print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
432 char atime_buf[100], dtime_buf[100], ctime_buf[100];
434 cvt_time(kshmptr->u.shm_atime, atime_buf);
435 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
436 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
438 printf("m %12d %12d %s %-8s %-8s",
439 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
440 (int)kshmptr->u.shm_perm.key,
441 fmt_perm(kshmptr->u.shm_perm.mode),
442 user_from_uid(kshmptr->u.shm_perm.uid, 0),
443 group_from_gid(kshmptr->u.shm_perm.gid, 0));
445 if (option & CREATOR)
447 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
448 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
450 if (option & OUTSTANDING)
452 kshmptr->u.shm_nattch);
454 if (option & BIGGEST)
456 kshmptr->u.shm_segsz);
461 kshmptr->u.shm_lpid);
473 print_ksemtotal(struct seminfo seminfo)
476 printf("seminfo:\n");
477 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
479 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
481 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
483 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
485 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
487 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
489 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
491 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
493 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
498 print_ksemheader(int option)
501 printf("Semaphores:\n");
502 printf("T %12s %12s %-11s %-8s %-8s",
503 "ID", "KEY", "MODE", "OWNER", "GROUP");
504 if (option & CREATOR)
505 printf(" %-8s %-8s", "CREATOR", "CGROUP");
506 if (option & BIGGEST)
507 printf(" %12s", "NSEMS");
509 printf(" %-8s %-8s", "OTIME", "CTIME");
514 print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
516 char ctime_buf[100], otime_buf[100];
518 cvt_time(ksemaptr->u.sem_otime, otime_buf);
519 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
521 printf("s %12d %12d %s %-8s %-8s",
522 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
523 (int)ksemaptr->u.sem_perm.key,
524 fmt_perm(ksemaptr->u.sem_perm.mode),
525 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
526 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
528 if (option & CREATOR)
530 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
531 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
533 if (option & BIGGEST)
535 ksemaptr->u.sem_nsems);
546 user2uid(char *username)
552 uid = strtoul(username, &r, 0);
553 if (!*r && r != username)
555 if ((pwd = getpwnam(username)) == NULL)
556 errx(1, "getpwnam failed: No such user");
558 return (pwd->pw_uid);
567 "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");