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>
54 char *fmt_perm(u_short);
55 void cvt_time(time_t, char *);
57 uid_t user2uid(char *username);
59 void print_kmsqtotal(struct msginfo msginfo);
60 void print_kmsqheader(int option);
61 void print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr);
62 void print_kshmtotal(struct shminfo shminfo);
63 void print_kshmheader(int option);
64 void print_kshmptr(int i, int option, struct shmid_kernel *kshmptr);
65 void print_ksemtotal(struct seminfo seminfo);
66 void print_ksemheader(int option);
67 void print_ksemptr(int i, int option, struct semid_kernel *ksemaptr);
70 fmt_perm(u_short mode)
72 static char buffer[100];
76 buffer[2] = ((mode & 0400) ? 'r' : '-');
77 buffer[3] = ((mode & 0200) ? 'w' : '-');
78 buffer[4] = ((mode & 0100) ? 'a' : '-');
79 buffer[5] = ((mode & 0040) ? 'r' : '-');
80 buffer[6] = ((mode & 0020) ? 'w' : '-');
81 buffer[7] = ((mode & 0010) ? 'a' : '-');
82 buffer[8] = ((mode & 0004) ? 'r' : '-');
83 buffer[9] = ((mode & 0002) ? 'w' : '-');
84 buffer[10] = ((mode & 0001) ? 'a' : '-');
90 cvt_time(time_t t, char *buf)
95 strcpy(buf, "no-entry");
98 sprintf(buf, "%2d:%02d:%02d",
99 tm->tm_hour, tm->tm_min, tm->tm_sec);
105 #define OUTSTANDING 4
110 main(int argc, char *argv[])
112 int display = SHMINFO | MSGINFO | SEMINFO;
114 char *core = NULL, *user = NULL, *namelist = NULL;
115 char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */
120 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
123 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
144 option |= OUTSTANDING;
162 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
169 uid = user2uid(user);
179 * If paths to the exec file or core file were specified, we
180 * aren't operating on the running kernel, so we can't use
183 if (namelist != NULL || core != NULL)
187 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
189 errx(1, "kvm_openfiles: %s", kvmoferr);
190 switch (kvm_nlist(kd, symbols)) {
194 errx(1, "unable to read kernel symbol table");
200 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
201 if (display & (MSGINFO | MSGTOTAL)) {
202 if (display & MSGTOTAL)
203 print_kmsqtotal(msginfo);
205 if (display & MSGINFO) {
206 struct msqid_kernel *kxmsqids;
210 sizeof(struct msqid_kernel) * msginfo.msgmni;
211 kxmsqids = malloc(kxmsqids_len);
212 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
214 print_kmsqheader(option);
216 for (i = 0; i < msginfo.msgmni; i += 1) {
217 if (kxmsqids[i].u.msg_qbytes != 0) {
219 uid != kxmsqids[i].u.msg_perm.uid)
222 print_kmsqptr(i, option, &kxmsqids[i]);
231 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
232 if (display & (SHMINFO | SHMTOTAL)) {
234 if (display & SHMTOTAL)
235 print_kshmtotal(shminfo);
237 if (display & SHMINFO) {
238 struct shmid_kernel *kxshmids;
242 sizeof(struct shmid_kernel) * shminfo.shmmni;
243 kxshmids = malloc(kxshmids_len);
244 kget(X_SHMSEGS, kxshmids, kxshmids_len);
246 print_kshmheader(option);
248 for (shmidx = 0; shmidx < shminfo.shmmni; shmidx += 1) {
249 if (kxshmids[shmidx].u.shm_perm.mode & 0x0800) {
251 uid != kxshmids[shmidx].u.shm_perm.uid)
254 print_kshmptr(shmidx, option, &kxshmids[shmidx]);
261 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
262 if (display & (SEMINFO | SEMTOTAL)) {
263 struct semid_kernel *kxsema;
266 if (display & SEMTOTAL)
267 print_ksemtotal(seminfo);
269 if (display & SEMINFO) {
271 sizeof(struct semid_kernel) * seminfo.semmni;
272 kxsema = malloc(kxsema_len);
273 kget(X_SEMA, kxsema, kxsema_len);
275 print_ksemheader(option);
277 for (i = 0; i < seminfo.semmni; i += 1) {
278 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
281 uid != kxsema[i].u.sem_perm.uid)
284 print_ksemptr(i, option, &kxsema[i]);
300 print_kmsqtotal(struct msginfo local_msginfo)
303 printf("msginfo:\n");
304 printf("\tmsgmax: %12d\t(max characters in a message)\n",
305 local_msginfo.msgmax);
306 printf("\tmsgmni: %12d\t(# of message queues)\n",
307 local_msginfo.msgmni);
308 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
309 local_msginfo.msgmnb);
310 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
311 local_msginfo.msgtql);
312 printf("\tmsgssz: %12d\t(size of a message segment)\n",
313 local_msginfo.msgssz);
314 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
315 local_msginfo.msgseg);
318 void print_kmsqheader(int option)
321 printf("Message Queues:\n");
322 printf("T %12s %12s %-11s %-8s %-8s",
323 "ID", "KEY", "MODE", "OWNER", "GROUP");
324 if (option & CREATOR)
325 printf(" %-8s %-8s", "CREATOR", "CGROUP");
326 if (option & OUTSTANDING)
327 printf(" %20s %20s", "CBYTES", "QNUM");
328 if (option & BIGGEST)
329 printf(" %20s", "QBYTES");
331 printf(" %12s %12s", "LSPID", "LRPID");
333 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
338 print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
340 char stime_buf[100], rtime_buf[100], ctime_buf[100];
342 cvt_time(kmsqptr->u.msg_stime, stime_buf);
343 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
344 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
346 printf("q %12d %12d %s %-8s %-8s",
347 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
348 (int)kmsqptr->u.msg_perm.key,
349 fmt_perm(kmsqptr->u.msg_perm.mode),
350 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
351 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
353 if (option & CREATOR)
355 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
356 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
358 if (option & OUTSTANDING)
359 printf(" %12lu %12lu",
360 kmsqptr->u.msg_cbytes,
361 kmsqptr->u.msg_qnum);
363 if (option & BIGGEST)
364 printf(" %20lu", kmsqptr->u.msg_qbytes);
368 kmsqptr->u.msg_lspid,
369 kmsqptr->u.msg_lrpid);
381 print_kshmtotal(struct shminfo local_shminfo)
384 printf("shminfo:\n");
385 printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
386 local_shminfo.shmmax);
387 printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
388 local_shminfo.shmmin);
389 printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
390 local_shminfo.shmmni);
391 printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
392 local_shminfo.shmseg);
393 printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
394 local_shminfo.shmall);
398 print_kshmheader(int option)
401 printf("Shared Memory:\n");
402 printf("T %12s %12s %-11s %-8s %-8s",
403 "ID", "KEY", "MODE", "OWNER", "GROUP");
404 if (option & CREATOR)
405 printf(" %-8s %-8s", "CREATOR", "CGROUP");
406 if (option & OUTSTANDING)
407 printf(" %12s", "NATTCH");
408 if (option & BIGGEST)
409 printf(" %12s", "SEGSZ");
411 printf(" %12s %12s", "CPID", "LPID");
413 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
418 print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
420 char atime_buf[100], dtime_buf[100], ctime_buf[100];
422 cvt_time(kshmptr->u.shm_atime, atime_buf);
423 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
424 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
426 printf("m %12d %12d %s %-8s %-8s",
427 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
428 (int)kshmptr->u.shm_perm.key,
429 fmt_perm(kshmptr->u.shm_perm.mode),
430 user_from_uid(kshmptr->u.shm_perm.uid, 0),
431 group_from_gid(kshmptr->u.shm_perm.gid, 0));
433 if (option & CREATOR)
435 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
436 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
438 if (option & OUTSTANDING)
440 kshmptr->u.shm_nattch);
442 if (option & BIGGEST)
444 kshmptr->u.shm_segsz);
449 kshmptr->u.shm_lpid);
461 print_ksemtotal(struct seminfo local_seminfo)
464 printf("seminfo:\n");
465 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
466 local_seminfo.semmni);
467 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
468 local_seminfo.semmns);
469 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
470 local_seminfo.semmnu);
471 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
472 local_seminfo.semmsl);
473 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
474 local_seminfo.semopm);
475 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
476 local_seminfo.semume);
477 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
478 local_seminfo.semusz);
479 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
480 local_seminfo.semvmx);
481 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
482 local_seminfo.semaem);
486 print_ksemheader(int option)
489 printf("Semaphores:\n");
490 printf("T %12s %12s %-11s %-8s %-8s",
491 "ID", "KEY", "MODE", "OWNER", "GROUP");
492 if (option & CREATOR)
493 printf(" %-8s %-8s", "CREATOR", "CGROUP");
494 if (option & BIGGEST)
495 printf(" %12s", "NSEMS");
497 printf(" %-8s %-8s", "OTIME", "CTIME");
502 print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
504 char ctime_buf[100], otime_buf[100];
506 cvt_time(ksemaptr->u.sem_otime, otime_buf);
507 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
509 printf("s %12d %12d %s %-8s %-8s",
510 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
511 (int)ksemaptr->u.sem_perm.key,
512 fmt_perm(ksemaptr->u.sem_perm.mode),
513 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
514 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
516 if (option & CREATOR)
518 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
519 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
521 if (option & BIGGEST)
523 ksemaptr->u.sem_nsems);
534 user2uid(char *username)
540 uid = strtoul(username, &r, 0);
541 if (!*r && r != username)
543 if ((pwd = getpwnam(username)) == NULL)
544 errx(1, "getpwnam failed: No such user");
546 return (pwd->pw_uid);
555 "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");