1 /* 2 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 15 * 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. 26 */ 27 28 #ifndef lint 29 static const char rcsid[] = 30 "$FreeBSD$"; 31 #endif /* not lint */ 32 33 #include <assert.h> 34 #include <err.h> 35 #include <fcntl.h> 36 #include <grp.h> 37 #include <kvm.h> 38 #include <nlist.h> 39 #include <limits.h> 40 #include <paths.h> 41 #include <pwd.h> 42 #include <stddef.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <unistd.h> 47 48 #include <sys/types.h> 49 #include <sys/param.h> 50 #include <sys/time.h> 51 #include <sys/proc.h> 52 #include <sys/sysctl.h> 53 #define _KERNEL 54 #include <sys/ipc.h> 55 #include <sys/sem.h> 56 #include <sys/shm.h> 57 #include <sys/msg.h> 58 59 /* SysCtlGatherStruct structure. */ 60 struct scgs_vector { 61 const char *sysctl; 62 off_t offset; 63 size_t size; 64 }; 65 66 int use_sysctl = 1; 67 struct semid_ds *sema; 68 struct seminfo seminfo; 69 struct msginfo msginfo; 70 struct msqid_ds *msqids; 71 struct shminfo shminfo; 72 struct shmid_ds *shmsegs; 73 74 void sysctlgatherstruct __P((void *addr, size_t size, 75 struct scgs_vector *vec)); 76 void kget __P((int idx, void *addr, size_t size)); 77 void usage __P((void)); 78 79 static struct nlist symbols[] = { 80 {"sema"}, 81 #define X_SEMA 0 82 {"seminfo"}, 83 #define X_SEMINFO 1 84 {"msginfo"}, 85 #define X_MSGINFO 2 86 {"msqids"}, 87 #define X_MSQIDS 3 88 {"shminfo"}, 89 #define X_SHMINFO 4 90 {"shmsegs"}, 91 #define X_SHMSEGS 5 92 {NULL} 93 }; 94 95 #define SHMINFO_XVEC \ 96 X(shmmax, sizeof(int)) \ 97 X(shmmin, sizeof(int)) \ 98 X(shmmni, sizeof(int)) \ 99 X(shmseg, sizeof(int)) \ 100 X(shmall, sizeof(int)) 101 102 #define SEMINFO_XVEC \ 103 X(semmap, sizeof(int)) \ 104 X(semmni, sizeof(int)) \ 105 X(semmns, sizeof(int)) \ 106 X(semmnu, sizeof(int)) \ 107 X(semmsl, sizeof(int)) \ 108 X(semopm, sizeof(int)) \ 109 X(semume, sizeof(int)) \ 110 X(semusz, sizeof(int)) \ 111 X(semvmx, sizeof(int)) \ 112 X(semaem, sizeof(int)) 113 114 #define MSGINFO_XVEC \ 115 X(msgmax, sizeof(int)) \ 116 X(msgmni, sizeof(int)) \ 117 X(msgmnb, sizeof(int)) \ 118 X(msgtql, sizeof(int)) \ 119 X(msgssz, sizeof(int)) \ 120 X(msgseg, sizeof(int)) 121 122 #define X(a, b) { "kern.ipc." #a, offsetof(TYPEC, a), (b) }, 123 #define TYPEC struct shminfo 124 struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { NULL } }; 125 #undef TYPEC 126 #define TYPEC struct seminfo 127 struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { NULL } }; 128 #undef TYPEC 129 #define TYPEC struct msginfo 130 struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { NULL } }; 131 #undef TYPEC 132 #undef X 133 134 static kvm_t *kd; 135 136 char * 137 fmt_perm(mode) 138 u_short mode; 139 { 140 static char buffer[100]; 141 142 buffer[0] = '-'; 143 buffer[1] = '-'; 144 buffer[2] = ((mode & 0400) ? 'r' : '-'); 145 buffer[3] = ((mode & 0200) ? 'w' : '-'); 146 buffer[4] = ((mode & 0100) ? 'a' : '-'); 147 buffer[5] = ((mode & 0040) ? 'r' : '-'); 148 buffer[6] = ((mode & 0020) ? 'w' : '-'); 149 buffer[7] = ((mode & 0010) ? 'a' : '-'); 150 buffer[8] = ((mode & 0004) ? 'r' : '-'); 151 buffer[9] = ((mode & 0002) ? 'w' : '-'); 152 buffer[10] = ((mode & 0001) ? 'a' : '-'); 153 buffer[11] = '\0'; 154 return (&buffer[0]); 155 } 156 157 void 158 cvt_time(t, buf) 159 time_t t; 160 char *buf; 161 { 162 struct tm *tm; 163 164 if (t == 0) { 165 strcpy(buf, "no-entry"); 166 } else { 167 tm = localtime(&t); 168 sprintf(buf, "%2d:%02d:%02d", 169 tm->tm_hour, tm->tm_min, tm->tm_sec); 170 } 171 } 172 #define SHMINFO 1 173 #define SHMTOTAL 2 174 #define MSGINFO 4 175 #define MSGTOTAL 8 176 #define SEMINFO 16 177 #define SEMTOTAL 32 178 179 #define BIGGEST 1 180 #define CREATOR 2 181 #define OUTSTANDING 4 182 #define PID 8 183 #define TIME 16 184 185 int 186 main(argc, argv) 187 int argc; 188 char *argv[]; 189 { 190 int display = SHMINFO | MSGINFO | SEMINFO; 191 int option = 0; 192 char *core = NULL, *namelist = NULL; 193 char kvmoferr[_POSIX2_LINE_MAX]; /* Error buf for kvm_openfiles. */ 194 int i; 195 196 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTy")) != -1) 197 switch (i) { 198 case 'M': 199 display = SHMTOTAL; 200 break; 201 case 'm': 202 display = SHMINFO; 203 break; 204 case 'Q': 205 display = MSGTOTAL; 206 break; 207 case 'q': 208 display = MSGINFO; 209 break; 210 case 'S': 211 display = SEMTOTAL; 212 break; 213 case 's': 214 display = SEMINFO; 215 break; 216 case 'T': 217 display = SHMTOTAL | MSGTOTAL | SEMTOTAL; 218 break; 219 case 'a': 220 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME; 221 break; 222 case 'b': 223 option |= BIGGEST; 224 break; 225 case 'C': 226 core = optarg; 227 break; 228 case 'c': 229 option |= CREATOR; 230 break; 231 case 'N': 232 namelist = optarg; 233 break; 234 case 'o': 235 option |= OUTSTANDING; 236 break; 237 case 'p': 238 option |= PID; 239 break; 240 case 't': 241 option |= TIME; 242 break; 243 case 'y': 244 use_sysctl = 0; 245 break; 246 default: 247 usage(); 248 } 249 250 /* 251 * If paths to the exec file or core file were specified, we 252 * aren't operating on the running kernel, so we can't use 253 * sysctl. 254 */ 255 if (namelist != NULL || core != NULL) 256 use_sysctl = 0; 257 258 if (!use_sysctl) { 259 kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr); 260 if (kd == NULL) 261 errx(1, "kvm_openfiles: %s", kvmoferr); 262 switch (kvm_nlist(kd, symbols)) { 263 case 0: 264 break; 265 case -1: 266 errx(1, "unable to read kernel symbol table"); 267 default: 268 #ifdef notdef /* they'll be told more civilly later */ 269 warnx("nlist failed"); 270 for (i = 0; symbols[i].n_name != NULL; i++) 271 if (symbols[i].n_value == 0) 272 warnx("symbol %s not found", 273 symbols[i].n_name); 274 break; 275 #endif 276 } 277 } 278 279 kget(X_MSGINFO, &msginfo, sizeof(msginfo)); 280 if ((display & (MSGINFO | MSGTOTAL))) { 281 if (display & MSGTOTAL) { 282 printf("msginfo:\n"); 283 printf("\tmsgmax: %6d\t(max characters in a message)\n", 284 msginfo.msgmax); 285 printf("\tmsgmni: %6d\t(# of message queues)\n", 286 msginfo.msgmni); 287 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n", 288 msginfo.msgmnb); 289 printf("\tmsgtql: %6d\t(max # of messages in system)\n", 290 msginfo.msgtql); 291 printf("\tmsgssz: %6d\t(size of a message segment)\n", 292 msginfo.msgssz); 293 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n", 294 msginfo.msgseg); 295 } 296 if (display & MSGINFO) { 297 struct msqid_ds *xmsqids; 298 size_t xmsqids_len; 299 300 301 xmsqids_len = sizeof(struct msqid_ds) * msginfo.msgmni; 302 xmsqids = malloc(xmsqids_len); 303 kget(X_MSQIDS, xmsqids, xmsqids_len); 304 305 printf("Message Queues:\n"); 306 printf("T ID KEY MODE OWNER GROUP"); 307 if (option & CREATOR) 308 printf(" CREATOR CGROUP"); 309 if (option & OUTSTANDING) 310 printf(" CBYTES QNUM"); 311 if (option & BIGGEST) 312 printf(" QBYTES"); 313 if (option & PID) 314 printf(" LSPID LRPID"); 315 if (option & TIME) 316 printf(" STIME RTIME CTIME"); 317 printf("\n"); 318 for (i = 0; i < msginfo.msgmni; i += 1) { 319 if (xmsqids[i].msg_qbytes != 0) { 320 char stime_buf[100], rtime_buf[100], 321 ctime_buf[100]; 322 struct msqid_ds *msqptr = &xmsqids[i]; 323 324 cvt_time(msqptr->msg_stime, stime_buf); 325 cvt_time(msqptr->msg_rtime, rtime_buf); 326 cvt_time(msqptr->msg_ctime, ctime_buf); 327 328 printf("q %6d %10d %s %8s %8s", 329 IXSEQ_TO_IPCID(i, msqptr->msg_perm), 330 msqptr->msg_perm.key, 331 fmt_perm(msqptr->msg_perm.mode), 332 user_from_uid(msqptr->msg_perm.uid, 0), 333 group_from_gid(msqptr->msg_perm.gid, 0)); 334 335 if (option & CREATOR) 336 printf(" %8s %8s", 337 user_from_uid(msqptr->msg_perm.cuid, 0), 338 group_from_gid(msqptr->msg_perm.cgid, 0)); 339 340 if (option & OUTSTANDING) 341 printf(" %6d %6d", 342 msqptr->msg_cbytes, 343 msqptr->msg_qnum); 344 345 if (option & BIGGEST) 346 printf(" %6d", 347 msqptr->msg_qbytes); 348 349 if (option & PID) 350 printf(" %6d %6d", 351 msqptr->msg_lspid, 352 msqptr->msg_lrpid); 353 354 if (option & TIME) 355 printf("%s %s %s", 356 stime_buf, 357 rtime_buf, 358 ctime_buf); 359 360 printf("\n"); 361 } 362 } 363 printf("\n"); 364 } 365 } else 366 if (display & (MSGINFO | MSGTOTAL)) { 367 fprintf(stderr, 368 "SVID messages facility not configured in the system\n"); 369 } 370 371 kget(X_SHMINFO, &shminfo, sizeof(shminfo)); 372 if ((display & (SHMINFO | SHMTOTAL))) { 373 if (display & SHMTOTAL) { 374 printf("shminfo:\n"); 375 printf("\tshmmax: %7d\t(max shared memory segment size)\n", 376 shminfo.shmmax); 377 printf("\tshmmin: %7d\t(min shared memory segment size)\n", 378 shminfo.shmmin); 379 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n", 380 shminfo.shmmni); 381 printf("\tshmseg: %7d\t(max shared memory segments per process)\n", 382 shminfo.shmseg); 383 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n", 384 shminfo.shmall); 385 } 386 if (display & SHMINFO) { 387 struct shmid_ds *xshmids; 388 size_t xshmids_len; 389 390 xshmids_len = sizeof(struct shmid_ds) * shminfo.shmmni; 391 xshmids = malloc(xshmids_len); 392 kget(X_SHMSEGS, xshmids, xshmids_len); 393 394 printf("Shared Memory:\n"); 395 printf("T ID KEY MODE OWNER GROUP"); 396 if (option & CREATOR) 397 printf(" CREATOR CGROUP"); 398 if (option & OUTSTANDING) 399 printf(" NATTCH"); 400 if (option & BIGGEST) 401 printf(" SEGSZ"); 402 if (option & PID) 403 printf(" CPID LPID"); 404 if (option & TIME) 405 printf(" ATIME DTIME CTIME"); 406 printf("\n"); 407 for (i = 0; i < shminfo.shmmni; i += 1) { 408 if (xshmids[i].shm_perm.mode & 0x0800) { 409 char atime_buf[100], dtime_buf[100], 410 ctime_buf[100]; 411 struct shmid_ds *shmptr = &xshmids[i]; 412 413 cvt_time(shmptr->shm_atime, atime_buf); 414 cvt_time(shmptr->shm_dtime, dtime_buf); 415 cvt_time(shmptr->shm_ctime, ctime_buf); 416 417 printf("m %6d %10d %s %8s %8s", 418 IXSEQ_TO_IPCID(i, shmptr->shm_perm), 419 shmptr->shm_perm.key, 420 fmt_perm(shmptr->shm_perm.mode), 421 user_from_uid(shmptr->shm_perm.uid, 0), 422 group_from_gid(shmptr->shm_perm.gid, 0)); 423 424 if (option & CREATOR) 425 printf(" %8s %8s", 426 user_from_uid(shmptr->shm_perm.cuid, 0), 427 group_from_gid(shmptr->shm_perm.cgid, 0)); 428 429 if (option & OUTSTANDING) 430 printf(" %6d", 431 shmptr->shm_nattch); 432 433 if (option & BIGGEST) 434 printf(" %6d", 435 shmptr->shm_segsz); 436 437 if (option & PID) 438 printf(" %6d %6d", 439 shmptr->shm_cpid, 440 shmptr->shm_lpid); 441 442 if (option & TIME) 443 printf("%s %s %s", 444 atime_buf, 445 dtime_buf, 446 ctime_buf); 447 448 printf("\n"); 449 } 450 } 451 printf("\n"); 452 } 453 } else 454 if (display & (SHMINFO | SHMTOTAL)) { 455 fprintf(stderr, 456 "SVID shared memory facility not configured in the system\n"); 457 } 458 459 kget(X_SEMINFO, &seminfo, sizeof(seminfo)); 460 if ((display & (SEMINFO | SEMTOTAL))) { 461 struct semid_ds *xsema; 462 size_t xsema_len; 463 464 if (display & SEMTOTAL) { 465 printf("seminfo:\n"); 466 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n", 467 seminfo.semmap); 468 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n", 469 seminfo.semmni); 470 printf("\tsemmns: %6d\t(# of semaphores in system)\n", 471 seminfo.semmns); 472 printf("\tsemmnu: %6d\t(# of undo structures in system)\n", 473 seminfo.semmnu); 474 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n", 475 seminfo.semmsl); 476 printf("\tsemopm: %6d\t(max # of operations per semop call)\n", 477 seminfo.semopm); 478 printf("\tsemume: %6d\t(max # of undo entries per process)\n", 479 seminfo.semume); 480 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n", 481 seminfo.semusz); 482 printf("\tsemvmx: %6d\t(semaphore maximum value)\n", 483 seminfo.semvmx); 484 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n", 485 seminfo.semaem); 486 } 487 if (display & SEMINFO) { 488 xsema_len = sizeof(struct semid_ds) * seminfo.semmni; 489 xsema = malloc(xsema_len); 490 kget(X_SEMA, xsema, xsema_len); 491 492 printf("Semaphores:\n"); 493 printf("T ID KEY MODE OWNER GROUP"); 494 if (option & CREATOR) 495 printf(" CREATOR CGROUP"); 496 if (option & BIGGEST) 497 printf(" NSEMS"); 498 if (option & TIME) 499 printf(" OTIME CTIME"); 500 printf("\n"); 501 for (i = 0; i < seminfo.semmni; i += 1) { 502 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) { 503 char ctime_buf[100], otime_buf[100]; 504 struct semid_ds *semaptr = &xsema[i]; 505 506 cvt_time(semaptr->sem_otime, otime_buf); 507 cvt_time(semaptr->sem_ctime, ctime_buf); 508 509 printf("s %6d %10d %s %8s %8s", 510 IXSEQ_TO_IPCID(i, semaptr->sem_perm), 511 semaptr->sem_perm.key, 512 fmt_perm(semaptr->sem_perm.mode), 513 user_from_uid(semaptr->sem_perm.uid, 0), 514 group_from_gid(semaptr->sem_perm.gid, 0)); 515 516 if (option & CREATOR) 517 printf(" %8s %8s", 518 user_from_uid(semaptr->sem_perm.cuid, 0), 519 group_from_gid(semaptr->sem_perm.cgid, 0)); 520 521 if (option & BIGGEST) 522 printf(" %6d", 523 semaptr->sem_nsems); 524 525 if (option & TIME) 526 printf("%s %s", 527 otime_buf, 528 ctime_buf); 529 530 printf("\n"); 531 } 532 } 533 534 printf("\n"); 535 } 536 } else 537 if (display & (SEMINFO | SEMTOTAL)) { 538 fprintf(stderr, "SVID semaphores facility not configured in the system\n"); 539 } 540 if (!use_sysctl) 541 kvm_close(kd); 542 543 exit(0); 544 } 545 546 void 547 sysctlgatherstruct(addr, size, vecarr) 548 void *addr; 549 size_t size; 550 struct scgs_vector *vecarr; 551 { 552 struct scgs_vector *xp; 553 size_t tsiz; 554 int rv; 555 556 for (xp = vecarr; xp->sysctl != NULL; xp++) { 557 assert(xp->offset <= size); 558 tsiz = xp->size; 559 rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset, 560 &tsiz, NULL, 0); 561 if (rv == -1) 562 errx(1, "sysctlbyname: %s", xp->sysctl); 563 if (tsiz != xp->size) 564 errx(1, "%s size mismatch (expected %d, got %d)", 565 xp->sysctl, xp->size, tsiz); 566 } 567 } 568 569 void 570 kget(idx, addr, size) 571 int idx; 572 void *addr; 573 size_t size; 574 { 575 char *symn; /* symbol name */ 576 size_t tsiz; 577 int rv; 578 unsigned long kaddr; 579 const char *sym2sysctl[] = { /* symbol to sysctl name table */ 580 "kern.ipc.sema", 581 "kern.ipc.seminfo", 582 "kern.ipc.msginfo", 583 "kern.ipc.msqids", 584 "kern.ipc.shminfo", 585 "kern.ipc.shmsegs" }; 586 587 assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl)); 588 if (!use_sysctl) { 589 symn = symbols[idx].n_name; 590 if (*symn == '_') 591 symn++; 592 if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0) 593 errx(1, "symbol %s undefined", symn); 594 /* 595 * For some symbols, the value we retreieve is 596 * actually a pointer; since we want the actual value, 597 * we have to manually dereference it. 598 */ 599 switch (idx) { 600 case X_MSQIDS: 601 tsiz = sizeof(msqids); 602 rv = kvm_read(kd, symbols[idx].n_value, 603 &msqids, tsiz); 604 kaddr = (u_long)msqids; 605 break; 606 case X_SHMSEGS: 607 tsiz = sizeof(shmsegs); 608 rv = kvm_read(kd, symbols[idx].n_value, 609 &shmsegs, tsiz); 610 kaddr = (u_long)shmsegs; 611 break; 612 case X_SEMA: 613 tsiz = sizeof(sema); 614 rv = kvm_read(kd, symbols[idx].n_value, 615 &sema, tsiz); 616 kaddr = (u_long)sema; 617 break; 618 default: 619 rv = tsiz = 0; 620 kaddr = symbols[idx].n_value; 621 break; 622 } 623 if ((unsigned)rv != tsiz) 624 errx(1, "%s: %s", symn, kvm_geterr(kd)); 625 if ((unsigned)kvm_read(kd, kaddr, addr, size) != size) 626 errx(1, "%s: %s", symn, kvm_geterr(kd)); 627 } else { 628 switch (idx) { 629 case X_SHMINFO: 630 sysctlgatherstruct(addr, size, shminfo_scgsv); 631 break; 632 case X_SEMINFO: 633 sysctlgatherstruct(addr, size, seminfo_scgsv); 634 break; 635 case X_MSGINFO: 636 sysctlgatherstruct(addr, size, msginfo_scgsv); 637 break; 638 default: 639 tsiz = size; 640 rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz, 641 NULL, 0); 642 if (rv == -1) 643 err(1, "sysctlbyname: %s", sym2sysctl[idx]); 644 if (tsiz != size) 645 errx(1, "%s size mismatch " 646 "(expected %d, got %d)", 647 sym2sysctl[idx], size, tsiz); 648 break; 649 } 650 } 651 } 652 653 void 654 usage() 655 { 656 657 fprintf(stderr, 658 "usage: ipcs [-abcmopqsty] [-C corefile] [-N namelist]\n"); 659 exit(1); 660 } 661