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. */
118 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
121 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
142 option |= OUTSTANDING;
160 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
167 uid = user2uid(user);
177 * If paths to the exec file or core file were specified, we
178 * aren't operating on the running kernel, so we can't use
181 if (namelist != NULL || core != NULL)
185 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
187 errx(1, "kvm_openfiles: %s", kvmoferr);
188 switch (kvm_nlist(kd, symbols)) {
192 errx(1, "unable to read kernel symbol table");
198 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
199 if ((display & (MSGINFO | MSGTOTAL))) {
200 if (display & MSGTOTAL)
201 print_kmsqtotal(msginfo);
203 if (display & MSGINFO) {
204 struct msqid_kernel *kxmsqids;
208 sizeof(struct msqid_kernel) * msginfo.msgmni;
209 kxmsqids = malloc(kxmsqids_len);
210 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
212 print_kmsqheader(option);
214 for (i = 0; i < msginfo.msgmni; i += 1) {
215 if (kxmsqids[i].u.msg_qbytes != 0) {
217 uid != kxmsqids[i].u.msg_perm.uid)
220 print_kmsqptr(i, option, &kxmsqids[i]);
228 if (display & (MSGINFO | MSGTOTAL)) {
230 "SVID messages facility "
231 "not configured in the system\n");
234 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
235 if ((display & (SHMINFO | SHMTOTAL))) {
237 if (display & SHMTOTAL)
238 print_kshmtotal(shminfo);
240 if (display & SHMINFO) {
241 struct shmid_kernel *kxshmids;
245 sizeof(struct shmid_kernel) * shminfo.shmmni;
246 kxshmids = malloc(kxshmids_len);
247 kget(X_SHMSEGS, kxshmids, kxshmids_len);
249 print_kshmheader(option);
251 for (shmidx = 0; shmidx < shminfo.shmmni; shmidx += 1) {
252 if (kxshmids[shmidx].u.shm_perm.mode & 0x0800) {
254 uid != kxshmids[shmidx].u.shm_perm.uid)
257 print_kshmptr(shmidx, option, &kxshmids[shmidx]);
263 if (display & (SHMINFO | SHMTOTAL)) {
265 "SVID shared memory facility "
266 "not configured in the system\n");
269 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
270 if ((display & (SEMINFO | SEMTOTAL))) {
271 struct semid_kernel *kxsema;
274 if (display & SEMTOTAL)
275 print_ksemtotal(seminfo);
277 if (display & SEMINFO) {
279 sizeof(struct semid_kernel) * seminfo.semmni;
280 kxsema = malloc(kxsema_len);
281 kget(X_SEMA, kxsema, kxsema_len);
283 print_ksemheader(option);
285 for (i = 0; i < seminfo.semmni; i += 1) {
286 if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
289 uid != kxsema[i].u.sem_perm.uid)
292 print_ksemptr(i, option, &kxsema[i]);
300 if (display & (SEMINFO | SEMTOTAL)) {
302 "SVID semaphores facility "
303 "not configured in the system\n");
313 print_kmsqtotal(struct msginfo local_msginfo)
316 printf("msginfo:\n");
317 printf("\tmsgmax: %12d\t(max characters in a message)\n",
318 local_msginfo.msgmax);
319 printf("\tmsgmni: %12d\t(# of message queues)\n",
320 local_msginfo.msgmni);
321 printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
322 local_msginfo.msgmnb);
323 printf("\tmsgtql: %12d\t(max # of messages in system)\n",
324 local_msginfo.msgtql);
325 printf("\tmsgssz: %12d\t(size of a message segment)\n",
326 local_msginfo.msgssz);
327 printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
328 local_msginfo.msgseg);
331 void print_kmsqheader(int option)
334 printf("Message Queues:\n");
335 printf("T %12s %12s %-11s %-8s %-8s",
336 "ID", "KEY", "MODE", "OWNER", "GROUP");
337 if (option & CREATOR)
338 printf(" %-8s %-8s", "CREATOR", "CGROUP");
339 if (option & OUTSTANDING)
340 printf(" %20s %20s", "CBYTES", "QNUM");
341 if (option & BIGGEST)
342 printf(" %20s", "QBYTES");
344 printf(" %12s %12s", "LSPID", "LRPID");
346 printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
351 print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
353 char stime_buf[100], rtime_buf[100], ctime_buf[100];
355 cvt_time(kmsqptr->u.msg_stime, stime_buf);
356 cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
357 cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
359 printf("q %12d %12d %s %-8s %-8s",
360 IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
361 (int)kmsqptr->u.msg_perm.key,
362 fmt_perm(kmsqptr->u.msg_perm.mode),
363 user_from_uid(kmsqptr->u.msg_perm.uid, 0),
364 group_from_gid(kmsqptr->u.msg_perm.gid, 0));
366 if (option & CREATOR)
368 user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
369 group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
371 if (option & OUTSTANDING)
372 printf(" %12lu %12lu",
373 kmsqptr->u.msg_cbytes,
374 kmsqptr->u.msg_qnum);
376 if (option & BIGGEST)
377 printf(" %20lu", kmsqptr->u.msg_qbytes);
381 kmsqptr->u.msg_lspid,
382 kmsqptr->u.msg_lrpid);
394 print_kshmtotal(struct shminfo local_shminfo)
397 printf("shminfo:\n");
398 printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
399 local_shminfo.shmmax);
400 printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
401 local_shminfo.shmmin);
402 printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
403 local_shminfo.shmmni);
404 printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
405 local_shminfo.shmseg);
406 printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
407 local_shminfo.shmall);
411 print_kshmheader(int option)
414 printf("Shared Memory:\n");
415 printf("T %12s %12s %-11s %-8s %-8s",
416 "ID", "KEY", "MODE", "OWNER", "GROUP");
417 if (option & CREATOR)
418 printf(" %-8s %-8s", "CREATOR", "CGROUP");
419 if (option & OUTSTANDING)
420 printf(" %12s", "NATTCH");
421 if (option & BIGGEST)
422 printf(" %12s", "SEGSZ");
424 printf(" %12s %12s", "CPID", "LPID");
426 printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
431 print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
433 char atime_buf[100], dtime_buf[100], ctime_buf[100];
435 cvt_time(kshmptr->u.shm_atime, atime_buf);
436 cvt_time(kshmptr->u.shm_dtime, dtime_buf);
437 cvt_time(kshmptr->u.shm_ctime, ctime_buf);
439 printf("m %12d %12d %s %-8s %-8s",
440 IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
441 (int)kshmptr->u.shm_perm.key,
442 fmt_perm(kshmptr->u.shm_perm.mode),
443 user_from_uid(kshmptr->u.shm_perm.uid, 0),
444 group_from_gid(kshmptr->u.shm_perm.gid, 0));
446 if (option & CREATOR)
448 user_from_uid(kshmptr->u.shm_perm.cuid, 0),
449 group_from_gid(kshmptr->u.shm_perm.cgid, 0));
451 if (option & OUTSTANDING)
453 kshmptr->u.shm_nattch);
455 if (option & BIGGEST)
457 kshmptr->u.shm_segsz);
462 kshmptr->u.shm_lpid);
474 print_ksemtotal(struct seminfo local_seminfo)
477 printf("seminfo:\n");
478 printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
479 local_seminfo.semmni);
480 printf("\tsemmns: %12d\t(# of semaphores in system)\n",
481 local_seminfo.semmns);
482 printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
483 local_seminfo.semmnu);
484 printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
485 local_seminfo.semmsl);
486 printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
487 local_seminfo.semopm);
488 printf("\tsemume: %12d\t(max # of undo entries per process)\n",
489 local_seminfo.semume);
490 printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
491 local_seminfo.semusz);
492 printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
493 local_seminfo.semvmx);
494 printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
495 local_seminfo.semaem);
499 print_ksemheader(int option)
502 printf("Semaphores:\n");
503 printf("T %12s %12s %-11s %-8s %-8s",
504 "ID", "KEY", "MODE", "OWNER", "GROUP");
505 if (option & CREATOR)
506 printf(" %-8s %-8s", "CREATOR", "CGROUP");
507 if (option & BIGGEST)
508 printf(" %12s", "NSEMS");
510 printf(" %-8s %-8s", "OTIME", "CTIME");
515 print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
517 char ctime_buf[100], otime_buf[100];
519 cvt_time(ksemaptr->u.sem_otime, otime_buf);
520 cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
522 printf("s %12d %12d %s %-8s %-8s",
523 IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
524 (int)ksemaptr->u.sem_perm.key,
525 fmt_perm(ksemaptr->u.sem_perm.mode),
526 user_from_uid(ksemaptr->u.sem_perm.uid, 0),
527 group_from_gid(ksemaptr->u.sem_perm.gid, 0));
529 if (option & CREATOR)
531 user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
532 group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
534 if (option & BIGGEST)
536 ksemaptr->u.sem_nsems);
547 user2uid(char *username)
553 uid = strtoul(username, &r, 0);
554 if (!*r && r != username)
556 if ((pwd = getpwnam(username)) == NULL)
557 errx(1, "getpwnam failed: No such user");
559 return (pwd->pw_uid);
568 "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");