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