2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #define _WANT_SYSVMSG_INTERNALS
37 #define _WANT_SYSVSEM_INTERNALS
39 #define _WANT_SYSVSHM_INTERNALS
55 char *fmt_perm(u_short);
56 void cvt_time(time_t, char *);
58 uid_t user2uid(char *username);
60 void print_kmsqtotal(struct msginfo msginfo);
61 void print_kmsqheader(int option);
62 void print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr);
63 void print_kshmtotal(struct shminfo shminfo);
64 void print_kshmheader(int option);
65 void print_kshmptr(int i, int option, struct shmid_kernel *kshmptr);
66 void print_ksemtotal(struct seminfo seminfo);
67 void print_ksemheader(int option);
68 void print_ksemptr(int i, int option, struct semid_kernel *ksemaptr);
71 fmt_perm(u_short mode)
73 static char buffer[100];
77 buffer[2] = ((mode & 0400) ? 'r' : '-');
78 buffer[3] = ((mode & 0200) ? 'w' : '-');
79 buffer[4] = ((mode & 0100) ? 'a' : '-');
80 buffer[5] = ((mode & 0040) ? 'r' : '-');
81 buffer[6] = ((mode & 0020) ? 'w' : '-');
82 buffer[7] = ((mode & 0010) ? 'a' : '-');
83 buffer[8] = ((mode & 0004) ? 'r' : '-');
84 buffer[9] = ((mode & 0002) ? 'w' : '-');
85 buffer[10] = ((mode & 0001) ? 'a' : '-');
91 cvt_time(time_t t, char *buf)
96 strcpy(buf, "no-entry");
99 sprintf(buf, "%2d:%02d:%02d",
100 tm->tm_hour, tm->tm_min, tm->tm_sec);
106 #define OUTSTANDING 4
111 main(int argc, char *argv[])
113 int display = SHMINFO | MSGINFO | SEMINFO;
115 char *core = NULL, *user = NULL, *namelist = NULL;
116 char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */
121 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
124 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
145 option |= OUTSTANDING;
163 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
170 uid = user2uid(user);
180 * If paths to the exec file or core file were specified, we
181 * aren't operating on the running kernel, so we can't use
184 if (namelist != NULL || core != NULL)
188 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
190 errx(1, "kvm_openfiles: %s", kvmoferr);
191 switch (kvm_nlist(kd, symbols)) {
195 errx(1, "unable to read kernel symbol table");
201 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
202 if (display & (MSGINFO | MSGTOTAL)) {
203 if (display & MSGTOTAL)
204 print_kmsqtotal(msginfo);
206 if (display & MSGINFO) {
207 struct msqid_kernel *kxmsqids;
211 sizeof(struct msqid_kernel) * msginfo.msgmni;
212 kxmsqids = malloc(kxmsqids_len);
213 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
215 print_kmsqheader(option);
217 for (i = 0; i < msginfo.msgmni; i += 1) {
218 if (kxmsqids[i].u.msg_qbytes != 0) {
220 uid != kxmsqids[i].u.msg_perm.uid)
223 print_kmsqptr(i, option, &kxmsqids[i]);
232 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
233 if (display & (SHMINFO | SHMTOTAL)) {
235 if (display & SHMTOTAL)
236 print_kshmtotal(shminfo);
238 if (display & SHMINFO) {
239 struct shmid_kernel *kxshmids;
243 sizeof(struct shmid_kernel) * shminfo.shmmni;
244 kxshmids = malloc(kxshmids_len);
245 kget(X_SHMSEGS, kxshmids, kxshmids_len);
247 print_kshmheader(option);
249 for (shmidx = 0; shmidx < shminfo.shmmni; shmidx += 1) {
250 if (kxshmids[shmidx].u.shm_perm.mode & 0x0800) {
252 uid != kxshmids[shmidx].u.shm_perm.uid)
255 print_kshmptr(shmidx, option, &kxshmids[shmidx]);
262 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
263 if (display & (SEMINFO | SEMTOTAL)) {
264 struct semid_kernel *kxsema;
267 if (display & SEMTOTAL)
268 print_ksemtotal(seminfo);
270 if (display & SEMINFO) {
272 sizeof(struct semid_kernel) * seminfo.semmni;
273 kxsema = malloc(kxsema_len);
274 kget(X_SEMA, kxsema, kxsema_len);
276 print_ksemheader(option);
278 for (i = 0; i < seminfo.semmni; i += 1) {
279 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
282 uid != kxsema[i].u.sem_perm.uid)
285 print_ksemptr(i, option, &kxsema[i]);
301 print_kmsqtotal(struct msginfo local_msginfo)
304 printf("msginfo:\n");
305 printf("\tmsgmax: %12d\t(max characters in a message)\n",
306 local_msginfo.msgmax);
307 printf("\tmsgmni: %12d\t(# of message queues)\n",
308 local_msginfo.msgmni);
309 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
310 local_msginfo.msgmnb);
311 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
312 local_msginfo.msgtql);
313 printf("\tmsgssz: %12d\t(size of a message segment)\n",
314 local_msginfo.msgssz);
315 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
316 local_msginfo.msgseg);
319 void print_kmsqheader(int option)
322 printf("Message Queues:\n");
323 printf("T %12s %12s %-11s %-8s %-8s",
324 "ID", "KEY", "MODE", "OWNER", "GROUP");
325 if (option & CREATOR)
326 printf(" %-8s %-8s", "CREATOR", "CGROUP");
327 if (option & OUTSTANDING)
328 printf(" %20s %20s", "CBYTES", "QNUM");
329 if (option & BIGGEST)
330 printf(" %20s", "QBYTES");
332 printf(" %12s %12s", "LSPID", "LRPID");
334 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
339 print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
341 char stime_buf[100], rtime_buf[100], ctime_buf[100];
343 cvt_time(kmsqptr->u.msg_stime, stime_buf);
344 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
345 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
347 printf("q %12d %12d %s %-8s %-8s",
348 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
349 (int)kmsqptr->u.msg_perm.key,
350 fmt_perm(kmsqptr->u.msg_perm.mode),
351 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
352 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
354 if (option & CREATOR)
356 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
357 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
359 if (option & OUTSTANDING)
360 printf(" %12lu %12lu",
361 kmsqptr->u.msg_cbytes,
362 kmsqptr->u.msg_qnum);
364 if (option & BIGGEST)
365 printf(" %20lu", kmsqptr->u.msg_qbytes);
369 kmsqptr->u.msg_lspid,
370 kmsqptr->u.msg_lrpid);
382 print_kshmtotal(struct shminfo local_shminfo)
385 printf("shminfo:\n");
386 printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
387 local_shminfo.shmmax);
388 printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
389 local_shminfo.shmmin);
390 printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
391 local_shminfo.shmmni);
392 printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
393 local_shminfo.shmseg);
394 printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
395 local_shminfo.shmall);
399 print_kshmheader(int option)
402 printf("Shared Memory:\n");
403 printf("T %12s %12s %-11s %-8s %-8s",
404 "ID", "KEY", "MODE", "OWNER", "GROUP");
405 if (option & CREATOR)
406 printf(" %-8s %-8s", "CREATOR", "CGROUP");
407 if (option & OUTSTANDING)
408 printf(" %12s", "NATTCH");
409 if (option & BIGGEST)
410 printf(" %12s", "SEGSZ");
412 printf(" %12s %12s", "CPID", "LPID");
414 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
419 print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
421 char atime_buf[100], dtime_buf[100], ctime_buf[100];
423 cvt_time(kshmptr->u.shm_atime, atime_buf);
424 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
425 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
427 printf("m %12d %12d %s %-8s %-8s",
428 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
429 (int)kshmptr->u.shm_perm.key,
430 fmt_perm(kshmptr->u.shm_perm.mode),
431 user_from_uid(kshmptr->u.shm_perm.uid, 0),
432 group_from_gid(kshmptr->u.shm_perm.gid, 0));
434 if (option & CREATOR)
436 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
437 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
439 if (option & OUTSTANDING)
441 kshmptr->u.shm_nattch);
443 if (option & BIGGEST)
445 kshmptr->u.shm_segsz);
450 kshmptr->u.shm_lpid);
462 print_ksemtotal(struct seminfo local_seminfo)
465 printf("seminfo:\n");
466 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
467 local_seminfo.semmni);
468 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
469 local_seminfo.semmns);
470 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
471 local_seminfo.semmnu);
472 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
473 local_seminfo.semmsl);
474 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
475 local_seminfo.semopm);
476 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
477 local_seminfo.semume);
478 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
479 local_seminfo.semusz);
480 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
481 local_seminfo.semvmx);
482 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
483 local_seminfo.semaem);
487 print_ksemheader(int option)
490 printf("Semaphores:\n");
491 printf("T %12s %12s %-11s %-8s %-8s",
492 "ID", "KEY", "MODE", "OWNER", "GROUP");
493 if (option & CREATOR)
494 printf(" %-8s %-8s", "CREATOR", "CGROUP");
495 if (option & BIGGEST)
496 printf(" %12s", "NSEMS");
498 printf(" %-8s %-8s", "OTIME", "CTIME");
503 print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
505 char ctime_buf[100], otime_buf[100];
507 cvt_time(ksemaptr->u.sem_otime, otime_buf);
508 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
510 printf("s %12d %12d %s %-8s %-8s",
511 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
512 (int)ksemaptr->u.sem_perm.key,
513 fmt_perm(ksemaptr->u.sem_perm.mode),
514 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
515 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
517 if (option & CREATOR)
519 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
520 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
522 if (option & BIGGEST)
524 ksemaptr->u.sem_nsems);
535 user2uid(char *username)
541 uid = strtoul(username, &r, 0);
542 if (!*r && r != username)
544 if ((pwd = getpwnam(username)) == NULL)
545 errx(1, "getpwnam failed: No such user");
547 return (pwd->pw_uid);
556 "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");