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 #include <sys/param.h>
33 #define _WANT_SYSVMSG_INTERNALS
35 #define _WANT_SYSVSEM_INTERNALS
37 #define _WANT_SYSVSHM_INTERNALS
53 char *fmt_perm(u_short);
54 void cvt_time(time_t, char *);
56 uid_t user2uid(char *username);
58 void print_kmsqtotal(struct msginfo msginfo);
59 void print_kmsqheader(int option);
60 void print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr);
61 void print_kshmtotal(struct shminfo shminfo);
62 void print_kshmheader(int option);
63 void print_kshmptr(int i, int option, struct shmid_kernel *kshmptr);
64 void print_ksemtotal(struct seminfo seminfo);
65 void print_ksemheader(int option);
66 void print_ksemptr(int i, int option, struct semid_kernel *ksemaptr);
69 fmt_perm(u_short mode)
71 static char buffer[100];
75 buffer[2] = ((mode & 0400) ? 'r' : '-');
76 buffer[3] = ((mode & 0200) ? 'w' : '-');
77 buffer[4] = ((mode & 0100) ? 'a' : '-');
78 buffer[5] = ((mode & 0040) ? 'r' : '-');
79 buffer[6] = ((mode & 0020) ? 'w' : '-');
80 buffer[7] = ((mode & 0010) ? 'a' : '-');
81 buffer[8] = ((mode & 0004) ? 'r' : '-');
82 buffer[9] = ((mode & 0002) ? 'w' : '-');
83 buffer[10] = ((mode & 0001) ? 'a' : '-');
89 cvt_time(time_t t, char *buf)
94 strcpy(buf, "no-entry");
97 sprintf(buf, "%2d:%02d:%02d",
98 tm->tm_hour, tm->tm_min, tm->tm_sec);
104 #define OUTSTANDING 4
109 main(int argc, char *argv[])
111 int display = SHMINFO | MSGINFO | SEMINFO;
113 char *core = NULL, *user = NULL, *namelist = NULL;
114 char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */
119 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
122 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
143 option |= OUTSTANDING;
161 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
168 uid = user2uid(user);
178 * If paths to the exec file or core file were specified, we
179 * aren't operating on the running kernel, so we can't use
182 if (namelist != NULL || core != NULL)
186 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
188 errx(1, "kvm_openfiles: %s", kvmoferr);
189 switch (kvm_nlist(kd, symbols)) {
193 errx(1, "unable to read kernel symbol table");
199 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
200 if (display & (MSGINFO | MSGTOTAL)) {
201 if (display & MSGTOTAL)
202 print_kmsqtotal(msginfo);
204 if (display & MSGINFO) {
205 struct msqid_kernel *kxmsqids;
209 sizeof(struct msqid_kernel) * msginfo.msgmni;
210 kxmsqids = malloc(kxmsqids_len);
211 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
213 print_kmsqheader(option);
215 for (i = 0; i < msginfo.msgmni; i += 1) {
216 if (kxmsqids[i].u.msg_qbytes != 0) {
218 uid != kxmsqids[i].u.msg_perm.uid)
221 print_kmsqptr(i, option, &kxmsqids[i]);
230 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
231 if (display & (SHMINFO | SHMTOTAL)) {
233 if (display & SHMTOTAL)
234 print_kshmtotal(shminfo);
236 if (display & SHMINFO) {
237 struct shmid_kernel *kxshmids;
241 sizeof(struct shmid_kernel) * shminfo.shmmni;
242 kxshmids = malloc(kxshmids_len);
243 kget(X_SHMSEGS, kxshmids, kxshmids_len);
245 print_kshmheader(option);
247 for (shmidx = 0; shmidx < shminfo.shmmni; shmidx += 1) {
248 if (kxshmids[shmidx].u.shm_perm.mode & 0x0800) {
250 uid != kxshmids[shmidx].u.shm_perm.uid)
253 print_kshmptr(shmidx, option, &kxshmids[shmidx]);
260 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
261 if (display & (SEMINFO | SEMTOTAL)) {
262 struct semid_kernel *kxsema;
265 if (display & SEMTOTAL)
266 print_ksemtotal(seminfo);
268 if (display & SEMINFO) {
270 sizeof(struct semid_kernel) * seminfo.semmni;
271 kxsema = malloc(kxsema_len);
272 kget(X_SEMA, kxsema, kxsema_len);
274 print_ksemheader(option);
276 for (i = 0; i < seminfo.semmni; i += 1) {
277 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
280 uid != kxsema[i].u.sem_perm.uid)
283 print_ksemptr(i, option, &kxsema[i]);
299 print_kmsqtotal(struct msginfo local_msginfo)
302 printf("msginfo:\n");
303 printf("\tmsgmax: %12d\t(max characters in a message)\n",
304 local_msginfo.msgmax);
305 printf("\tmsgmni: %12d\t(# of message queues)\n",
306 local_msginfo.msgmni);
307 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
308 local_msginfo.msgmnb);
309 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
310 local_msginfo.msgtql);
311 printf("\tmsgssz: %12d\t(size of a message segment)\n",
312 local_msginfo.msgssz);
313 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
314 local_msginfo.msgseg);
317 void print_kmsqheader(int option)
320 printf("Message Queues:\n");
321 printf("T %12s %12s %-11s %-8s %-8s",
322 "ID", "KEY", "MODE", "OWNER", "GROUP");
323 if (option & CREATOR)
324 printf(" %-8s %-8s", "CREATOR", "CGROUP");
325 if (option & OUTSTANDING)
326 printf(" %20s %20s", "CBYTES", "QNUM");
327 if (option & BIGGEST)
328 printf(" %20s", "QBYTES");
330 printf(" %12s %12s", "LSPID", "LRPID");
332 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
337 print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
339 char stime_buf[100], rtime_buf[100], ctime_buf[100];
341 cvt_time(kmsqptr->u.msg_stime, stime_buf);
342 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
343 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
345 printf("q %12d %12d %s %-8s %-8s",
346 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
347 (int)kmsqptr->u.msg_perm.key,
348 fmt_perm(kmsqptr->u.msg_perm.mode),
349 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
350 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
352 if (option & CREATOR)
354 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
355 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
357 if (option & OUTSTANDING)
358 printf(" %12lu %12lu",
359 kmsqptr->u.msg_cbytes,
360 kmsqptr->u.msg_qnum);
362 if (option & BIGGEST)
363 printf(" %20lu", kmsqptr->u.msg_qbytes);
367 kmsqptr->u.msg_lspid,
368 kmsqptr->u.msg_lrpid);
380 print_kshmtotal(struct shminfo local_shminfo)
383 printf("shminfo:\n");
384 printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
385 local_shminfo.shmmax);
386 printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
387 local_shminfo.shmmin);
388 printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
389 local_shminfo.shmmni);
390 printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
391 local_shminfo.shmseg);
392 printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
393 local_shminfo.shmall);
397 print_kshmheader(int option)
400 printf("Shared Memory:\n");
401 printf("T %12s %12s %-11s %-8s %-8s",
402 "ID", "KEY", "MODE", "OWNER", "GROUP");
403 if (option & CREATOR)
404 printf(" %-8s %-8s", "CREATOR", "CGROUP");
405 if (option & OUTSTANDING)
406 printf(" %12s", "NATTCH");
407 if (option & BIGGEST)
408 printf(" %12s", "SEGSZ");
410 printf(" %12s %12s", "CPID", "LPID");
412 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
417 print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
419 char atime_buf[100], dtime_buf[100], ctime_buf[100];
421 cvt_time(kshmptr->u.shm_atime, atime_buf);
422 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
423 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
425 printf("m %12d %12d %s %-8s %-8s",
426 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
427 (int)kshmptr->u.shm_perm.key,
428 fmt_perm(kshmptr->u.shm_perm.mode),
429 user_from_uid(kshmptr->u.shm_perm.uid, 0),
430 group_from_gid(kshmptr->u.shm_perm.gid, 0));
432 if (option & CREATOR)
434 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
435 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
437 if (option & OUTSTANDING)
439 kshmptr->u.shm_nattch);
441 if (option & BIGGEST)
443 kshmptr->u.shm_segsz);
448 kshmptr->u.shm_lpid);
460 print_ksemtotal(struct seminfo local_seminfo)
463 printf("seminfo:\n");
464 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
465 local_seminfo.semmni);
466 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
467 local_seminfo.semmns);
468 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
469 local_seminfo.semmnu);
470 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
471 local_seminfo.semmsl);
472 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
473 local_seminfo.semopm);
474 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
475 local_seminfo.semume);
476 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
477 local_seminfo.semusz);
478 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
479 local_seminfo.semvmx);
480 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
481 local_seminfo.semaem);
485 print_ksemheader(int option)
488 printf("Semaphores:\n");
489 printf("T %12s %12s %-11s %-8s %-8s",
490 "ID", "KEY", "MODE", "OWNER", "GROUP");
491 if (option & CREATOR)
492 printf(" %-8s %-8s", "CREATOR", "CGROUP");
493 if (option & BIGGEST)
494 printf(" %12s", "NSEMS");
496 printf(" %-8s %-8s", "OTIME", "CTIME");
501 print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
503 char ctime_buf[100], otime_buf[100];
505 cvt_time(ksemaptr->u.sem_otime, otime_buf);
506 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
508 printf("s %12d %12d %s %-8s %-8s",
509 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
510 (int)ksemaptr->u.sem_perm.key,
511 fmt_perm(ksemaptr->u.sem_perm.mode),
512 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
513 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
515 if (option & CREATOR)
517 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
518 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
520 if (option & BIGGEST)
522 ksemaptr->u.sem_nsems);
533 user2uid(char *username)
539 uid = strtoul(username, &r, 0);
540 if (!*r && r != username)
542 if ((pwd = getpwnam(username)) == NULL)
543 errx(1, "getpwnam failed: No such user");
545 return (pwd->pw_uid);
554 "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");