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 <err.h> 34 #include <fcntl.h> 35 #include <kvm.h> 36 #include <nlist.h> 37 #include <paths.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 42 #include <sys/types.h> 43 #include <sys/param.h> 44 #include <sys/time.h> 45 #include <sys/proc.h> 46 #define KERNEL 47 #include <sys/ipc.h> 48 #include <sys/sem.h> 49 #include <sys/shm.h> 50 #include <sys/msg.h> 51 52 struct semid_ds *sema; 53 struct seminfo seminfo; 54 struct msginfo msginfo; 55 struct msqid_ds *msqids; 56 struct shminfo shminfo; 57 struct shmid_ds *shmsegs; 58 59 int semconfig __P((int,...)); 60 void usage __P((void)); 61 62 static struct nlist symbols[] = { 63 {"_sema"}, 64 #define X_SEMA 0 65 {"_seminfo"}, 66 #define X_SEMINFO 1 67 {"_semu"}, 68 #define X_SEMU 2 69 {"_msginfo"}, 70 #define X_MSGINFO 3 71 {"_msqids"}, 72 #define X_MSQIDS 4 73 {"_shminfo"}, 74 #define X_SHMINFO 5 75 {"_shmsegs"}, 76 #define X_SHMSEGS 6 77 {NULL} 78 }; 79 80 static kvm_t *kd; 81 82 char * 83 fmt_perm(mode) 84 u_short mode; 85 { 86 static char buffer[100]; 87 88 buffer[0] = '-'; 89 buffer[1] = '-'; 90 buffer[2] = ((mode & 0400) ? 'r' : '-'); 91 buffer[3] = ((mode & 0200) ? 'w' : '-'); 92 buffer[4] = ((mode & 0100) ? 'a' : '-'); 93 buffer[5] = ((mode & 0040) ? 'r' : '-'); 94 buffer[6] = ((mode & 0020) ? 'w' : '-'); 95 buffer[7] = ((mode & 0010) ? 'a' : '-'); 96 buffer[8] = ((mode & 0004) ? 'r' : '-'); 97 buffer[9] = ((mode & 0002) ? 'w' : '-'); 98 buffer[10] = ((mode & 0001) ? 'a' : '-'); 99 buffer[11] = '\0'; 100 return (&buffer[0]); 101 } 102 103 void 104 cvt_time(t, buf) 105 time_t t; 106 char *buf; 107 { 108 struct tm *tm; 109 110 if (t == 0) { 111 strcpy(buf, "no-entry"); 112 } else { 113 tm = localtime(&t); 114 sprintf(buf, "%2d:%02d:%02d", 115 tm->tm_hour, tm->tm_min, tm->tm_sec); 116 } 117 } 118 #define SHMINFO 1 119 #define SHMTOTAL 2 120 #define MSGINFO 4 121 #define MSGTOTAL 8 122 #define SEMINFO 16 123 #define SEMTOTAL 32 124 125 #define BIGGEST 1 126 #define CREATOR 2 127 #define OUTSTANDING 4 128 #define PID 8 129 #define TIME 16 130 131 int 132 main(argc, argv) 133 int argc; 134 char *argv[]; 135 { 136 int display = SHMINFO | MSGINFO | SEMINFO; 137 int option = 0; 138 char *core = NULL, *namelist = NULL; 139 int i; 140 141 while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1) 142 switch (i) { 143 case 'M': 144 display = SHMTOTAL; 145 break; 146 case 'm': 147 display = SHMINFO; 148 break; 149 case 'Q': 150 display = MSGTOTAL; 151 break; 152 case 'q': 153 display = MSGINFO; 154 break; 155 case 'S': 156 display = SEMTOTAL; 157 break; 158 case 's': 159 display = SEMINFO; 160 break; 161 case 'T': 162 display = SHMTOTAL | MSGTOTAL | SEMTOTAL; 163 break; 164 case 'a': 165 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME; 166 break; 167 case 'b': 168 option |= BIGGEST; 169 break; 170 case 'C': 171 core = optarg; 172 break; 173 case 'c': 174 option |= CREATOR; 175 break; 176 case 'N': 177 namelist = optarg; 178 break; 179 case 'o': 180 option |= OUTSTANDING; 181 break; 182 case 'p': 183 option |= PID; 184 break; 185 case 't': 186 option |= TIME; 187 break; 188 default: 189 usage(); 190 } 191 192 /* 193 * Discard setgid privileges if not the running kernel so that bad 194 * guys can't print interesting stuff from kernel memory. 195 */ 196 if (namelist != NULL || core != NULL) 197 setgid(getgid()); 198 199 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL) 200 exit(1); 201 202 switch (kvm_nlist(kd, symbols)) { 203 case 0: 204 break; 205 case -1: 206 errx(1, "unable to read kernel symbol table"); 207 default: 208 #ifdef notdef /* they'll be told more civilly later */ 209 warnx("nlist failed"); 210 for (i = 0; symbols[i].n_name != NULL; i++) 211 if (symbols[i].n_value == 0) 212 warnx("symbol %s not found", 213 symbols[i].n_name); 214 break; 215 #endif 216 } 217 218 if ((display & (MSGINFO | MSGTOTAL)) && 219 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))) { 220 221 if (display & MSGTOTAL) { 222 printf("msginfo:\n"); 223 printf("\tmsgmax: %6d\t(max characters in a message)\n", 224 msginfo.msgmax); 225 printf("\tmsgmni: %6d\t(# of message queues)\n", 226 msginfo.msgmni); 227 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n", 228 msginfo.msgmnb); 229 printf("\tmsgtql: %6d\t(max # of messages in system)\n", 230 msginfo.msgtql); 231 printf("\tmsgssz: %6d\t(size of a message segment)\n", 232 msginfo.msgssz); 233 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n", 234 msginfo.msgseg); 235 } 236 if (display & MSGINFO) { 237 struct msqid_ds *xmsqids; 238 239 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids)); 240 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni); 241 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni); 242 243 printf("Message Queues:\n"); 244 printf("T ID KEY MODE OWNER GROUP"); 245 if (option & CREATOR) 246 printf(" CREATOR CGROUP"); 247 if (option & OUTSTANDING) 248 printf(" CBYTES QNUM"); 249 if (option & BIGGEST) 250 printf(" QBYTES"); 251 if (option & PID) 252 printf(" LSPID LRPID"); 253 if (option & TIME) 254 printf(" STIME RTIME CTIME"); 255 printf("\n"); 256 for (i = 0; i < msginfo.msgmni; i += 1) { 257 if (xmsqids[i].msg_qbytes != 0) { 258 char stime_buf[100], rtime_buf[100], 259 ctime_buf[100]; 260 struct msqid_ds *msqptr = &xmsqids[i]; 261 262 cvt_time(msqptr->msg_stime, stime_buf); 263 cvt_time(msqptr->msg_rtime, rtime_buf); 264 cvt_time(msqptr->msg_ctime, ctime_buf); 265 266 printf("q %6d %10d %s %8s %8s", 267 IXSEQ_TO_IPCID(i, msqptr->msg_perm), 268 msqptr->msg_perm.key, 269 fmt_perm(msqptr->msg_perm.mode), 270 user_from_uid(msqptr->msg_perm.uid, 0), 271 group_from_gid(msqptr->msg_perm.gid, 0)); 272 273 if (option & CREATOR) 274 printf(" %8s %8s", 275 user_from_uid(msqptr->msg_perm.cuid, 0), 276 group_from_gid(msqptr->msg_perm.cgid, 0)); 277 278 if (option & OUTSTANDING) 279 printf(" %6d %6d", 280 msqptr->msg_cbytes, 281 msqptr->msg_qnum); 282 283 if (option & BIGGEST) 284 printf(" %6d", 285 msqptr->msg_qbytes); 286 287 if (option & PID) 288 printf(" %6d %6d", 289 msqptr->msg_lspid, 290 msqptr->msg_lrpid); 291 292 if (option & TIME) 293 printf("%s %s %s", 294 stime_buf, 295 rtime_buf, 296 ctime_buf); 297 298 printf("\n"); 299 } 300 } 301 printf("\n"); 302 } 303 } else 304 if (display & (MSGINFO | MSGTOTAL)) { 305 fprintf(stderr, 306 "SVID messages facility not configured in the system\n"); 307 } 308 if ((display & (SHMINFO | SHMTOTAL)) && 309 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) { 310 if (display & SHMTOTAL) { 311 printf("shminfo:\n"); 312 printf("\tshmmax: %7d\t(max shared memory segment size)\n", 313 shminfo.shmmax); 314 printf("\tshmmin: %7d\t(min shared memory segment size)\n", 315 shminfo.shmmin); 316 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n", 317 shminfo.shmmni); 318 printf("\tshmseg: %7d\t(max shared memory segments per process)\n", 319 shminfo.shmseg); 320 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n", 321 shminfo.shmall); 322 } 323 if (display & SHMINFO) { 324 struct shmid_ds *xshmids; 325 326 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs)); 327 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni); 328 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) * 329 shminfo.shmmni); 330 331 printf("Shared Memory:\n"); 332 printf("T ID KEY MODE OWNER GROUP"); 333 if (option & CREATOR) 334 printf(" CREATOR CGROUP"); 335 if (option & OUTSTANDING) 336 printf(" NATTCH"); 337 if (option & BIGGEST) 338 printf(" SEGSZ"); 339 if (option & PID) 340 printf(" CPID LPID"); 341 if (option & TIME) 342 printf(" ATIME DTIME CTIME"); 343 printf("\n"); 344 for (i = 0; i < shminfo.shmmni; i += 1) { 345 if (xshmids[i].shm_perm.mode & 0x0800) { 346 char atime_buf[100], dtime_buf[100], 347 ctime_buf[100]; 348 struct shmid_ds *shmptr = &xshmids[i]; 349 350 cvt_time(shmptr->shm_atime, atime_buf); 351 cvt_time(shmptr->shm_dtime, dtime_buf); 352 cvt_time(shmptr->shm_ctime, ctime_buf); 353 354 printf("m %6d %10d %s %8s %8s", 355 IXSEQ_TO_IPCID(i, shmptr->shm_perm), 356 shmptr->shm_perm.key, 357 fmt_perm(shmptr->shm_perm.mode), 358 user_from_uid(shmptr->shm_perm.uid, 0), 359 group_from_gid(shmptr->shm_perm.gid, 0)); 360 361 if (option & CREATOR) 362 printf(" %8s %8s", 363 user_from_uid(shmptr->shm_perm.cuid, 0), 364 group_from_gid(shmptr->shm_perm.cgid, 0)); 365 366 if (option & OUTSTANDING) 367 printf(" %6d", 368 shmptr->shm_nattch); 369 370 if (option & BIGGEST) 371 printf(" %6d", 372 shmptr->shm_segsz); 373 374 if (option & PID) 375 printf(" %6d %6d", 376 shmptr->shm_cpid, 377 shmptr->shm_lpid); 378 379 if (option & TIME) 380 printf("%s %s %s", 381 atime_buf, 382 dtime_buf, 383 ctime_buf); 384 385 printf("\n"); 386 } 387 } 388 printf("\n"); 389 } 390 } else 391 if (display & (SHMINFO | SHMTOTAL)) { 392 fprintf(stderr, 393 "SVID shared memory facility not configured in the system\n"); 394 } 395 if ((display & (SEMINFO | SEMTOTAL)) && 396 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) { 397 struct semid_ds *xsema; 398 399 if (display & SEMTOTAL) { 400 printf("seminfo:\n"); 401 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n", 402 seminfo.semmap); 403 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n", 404 seminfo.semmni); 405 printf("\tsemmns: %6d\t(# of semaphores in system)\n", 406 seminfo.semmns); 407 printf("\tsemmnu: %6d\t(# of undo structures in system)\n", 408 seminfo.semmnu); 409 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n", 410 seminfo.semmsl); 411 printf("\tsemopm: %6d\t(max # of operations per semop call)\n", 412 seminfo.semopm); 413 printf("\tsemume: %6d\t(max # of undo entries per process)\n", 414 seminfo.semume); 415 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n", 416 seminfo.semusz); 417 printf("\tsemvmx: %6d\t(semaphore maximum value)\n", 418 seminfo.semvmx); 419 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n", 420 seminfo.semaem); 421 } 422 if (display & SEMINFO) { 423 if (semconfig(SEM_CONFIG_FREEZE) != 0) { 424 perror("semconfig"); 425 fprintf(stderr, 426 "Can't lock semaphore facility - winging it...\n"); 427 } 428 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema)); 429 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni); 430 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni); 431 432 printf("Semaphores:\n"); 433 printf("T ID KEY MODE OWNER GROUP"); 434 if (option & CREATOR) 435 printf(" CREATOR CGROUP"); 436 if (option & BIGGEST) 437 printf(" NSEMS"); 438 if (option & TIME) 439 printf(" OTIME CTIME"); 440 printf("\n"); 441 for (i = 0; i < seminfo.semmni; i += 1) { 442 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) { 443 char ctime_buf[100], otime_buf[100]; 444 struct semid_ds *semaptr = &xsema[i]; 445 446 cvt_time(semaptr->sem_otime, otime_buf); 447 cvt_time(semaptr->sem_ctime, ctime_buf); 448 449 printf("s %6d %10d %s %8s %8s", 450 IXSEQ_TO_IPCID(i, semaptr->sem_perm), 451 semaptr->sem_perm.key, 452 fmt_perm(semaptr->sem_perm.mode), 453 user_from_uid(semaptr->sem_perm.uid, 0), 454 group_from_gid(semaptr->sem_perm.gid, 0)); 455 456 if (option & CREATOR) 457 printf(" %8s %8s", 458 user_from_uid(semaptr->sem_perm.cuid, 0), 459 group_from_gid(semaptr->sem_perm.cgid, 0)); 460 461 if (option & BIGGEST) 462 printf(" %6d", 463 semaptr->sem_nsems); 464 465 if (option & TIME) 466 printf("%s %s", 467 otime_buf, 468 ctime_buf); 469 470 printf("\n"); 471 } 472 } 473 474 (void) semconfig(SEM_CONFIG_THAW); 475 476 printf("\n"); 477 } 478 } else 479 if (display & (SEMINFO | SEMTOTAL)) { 480 fprintf(stderr, "SVID semaphores facility not configured in the system\n"); 481 } 482 kvm_close(kd); 483 484 exit(0); 485 } 486 487 void 488 usage() 489 { 490 491 fprintf(stderr, 492 "usage: ipcs [-abcmopqst] [-C corefile] [-N namelist]\n"); 493 exit(1); 494 } 495