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]);
228 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
229 if (display & (SHMINFO | SHMTOTAL)) {
231 if (display & SHMTOTAL)
232 print_kshmtotal(shminfo);
234 if (display & SHMINFO) {
235 struct shmid_kernel *kxshmids;
239 sizeof(struct shmid_kernel) * shminfo.shmmni;
240 kxshmids = malloc(kxshmids_len);
241 kget(X_SHMSEGS, kxshmids, kxshmids_len);
243 print_kshmheader(option);
245 for (i = 0; i < shminfo.shmmni; i += 1) {
246 if (kxshmids[i].u.shm_perm.mode & 0x0800) {
248 uid != kxshmids[i].u.shm_perm.uid)
251 print_kshmptr(i, option, &kxshmids[i]);
258 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
259 if (display & (SEMINFO | SEMTOTAL)) {
260 struct semid_kernel *kxsema;
263 if (display & SEMTOTAL)
264 print_ksemtotal(seminfo);
266 if (display & SEMINFO) {
268 sizeof(struct semid_kernel) * seminfo.semmni;
269 kxsema = malloc(kxsema_len);
270 kget(X_SEMA, kxsema, kxsema_len);
272 print_ksemheader(option);
274 for (i = 0; i < seminfo.semmni; i += 1) {
275 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
278 uid != kxsema[i].u.sem_perm.uid)
281 print_ksemptr(i, option, &kxsema[i]);
297 print_kmsqtotal(struct msginfo msginfo)
300 printf("msginfo:\n");
301 printf("\tmsgmax: %12d\t(max characters in a message)\n",
303 printf("\tmsgmni: %12d\t(# of message queues)\n",
305 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
307 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
309 printf("\tmsgssz: %12d\t(size of a message segment)\n",
311 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
315 void print_kmsqheader(int option)
318 printf("Message Queues:\n");
319 printf("T %12s %12s %-11s %-8s %-8s",
320 "ID", "KEY", "MODE", "OWNER", "GROUP");
321 if (option & CREATOR)
322 printf(" %-8s %-8s", "CREATOR", "CGROUP");
323 if (option & OUTSTANDING)
324 printf(" %20s %20s", "CBYTES", "QNUM");
325 if (option & BIGGEST)
326 printf(" %20s", "QBYTES");
328 printf(" %12s %12s", "LSPID", "LRPID");
330 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
335 print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
337 char stime_buf[100], rtime_buf[100], ctime_buf[100];
339 cvt_time(kmsqptr->u.msg_stime, stime_buf);
340 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
341 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
343 printf("q %12d %12d %s %-8s %-8s",
344 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
345 (int)kmsqptr->u.msg_perm.key,
346 fmt_perm(kmsqptr->u.msg_perm.mode),
347 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
348 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
350 if (option & CREATOR)
352 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
353 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
355 if (option & OUTSTANDING)
356 printf(" %12lu %12lu",
357 kmsqptr->u.msg_cbytes,
358 kmsqptr->u.msg_qnum);
360 if (option & BIGGEST)
361 printf(" %20lu", kmsqptr->u.msg_qbytes);
365 kmsqptr->u.msg_lspid,
366 kmsqptr->u.msg_lrpid);
378 print_kshmtotal(struct shminfo shminfo)
381 printf("shminfo:\n");
382 printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
384 printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
386 printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
388 printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
390 printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
395 print_kshmheader(int option)
398 printf("Shared Memory:\n");
399 printf("T %12s %12s %-11s %-8s %-8s",
400 "ID", "KEY", "MODE", "OWNER", "GROUP");
401 if (option & CREATOR)
402 printf(" %-8s %-8s", "CREATOR", "CGROUP");
403 if (option & OUTSTANDING)
404 printf(" %12s", "NATTCH");
405 if (option & BIGGEST)
406 printf(" %12s", "SEGSZ");
408 printf(" %12s %12s", "CPID", "LPID");
410 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
415 print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
417 char atime_buf[100], dtime_buf[100], ctime_buf[100];
419 cvt_time(kshmptr->u.shm_atime, atime_buf);
420 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
421 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
423 printf("m %12d %12d %s %-8s %-8s",
424 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
425 (int)kshmptr->u.shm_perm.key,
426 fmt_perm(kshmptr->u.shm_perm.mode),
427 user_from_uid(kshmptr->u.shm_perm.uid, 0),
428 group_from_gid(kshmptr->u.shm_perm.gid, 0));
430 if (option & CREATOR)
432 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
433 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
435 if (option & OUTSTANDING)
437 kshmptr->u.shm_nattch);
439 if (option & BIGGEST)
441 kshmptr->u.shm_segsz);
446 kshmptr->u.shm_lpid);
458 print_ksemtotal(struct seminfo seminfo)
461 printf("seminfo:\n");
462 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
464 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
466 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
468 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
470 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
472 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
474 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
476 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
478 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
483 print_ksemheader(int option)
486 printf("Semaphores:\n");
487 printf("T %12s %12s %-11s %-8s %-8s",
488 "ID", "KEY", "MODE", "OWNER", "GROUP");
489 if (option & CREATOR)
490 printf(" %-8s %-8s", "CREATOR", "CGROUP");
491 if (option & BIGGEST)
492 printf(" %12s", "NSEMS");
494 printf(" %-8s %-8s", "OTIME", "CTIME");
499 print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
501 char ctime_buf[100], otime_buf[100];
503 cvt_time(ksemaptr->u.sem_otime, otime_buf);
504 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
506 printf("s %12d %12d %s %-8s %-8s",
507 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
508 (int)ksemaptr->u.sem_perm.key,
509 fmt_perm(ksemaptr->u.sem_perm.mode),
510 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
511 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
513 if (option & CREATOR)
515 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
516 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
518 if (option & BIGGEST)
520 ksemaptr->u.sem_nsems);
531 user2uid(char *username)
537 uid = strtoul(username, &r, 0);
538 if (!*r && r != username)
540 if ((pwd = getpwnam(username)) == NULL)
541 errx(1, "getpwnam failed: No such user");
543 return (pwd->pw_uid);
552 "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");