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