Home | History | Annotate | Line # | Download | only in ipcs
ipcs.c revision 1.13
      1 /*	$NetBSD: ipcs.c,v 1.13 1997/02/11 08:43:23 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo (at) sigmasoft.com>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by SigmaSoft, Th.  Lockert.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     23  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     24  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     30  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/types.h>
     34 #include <sys/param.h>
     35 #include <sys/time.h>
     36 #include <sys/proc.h>
     37 #define _KERNEL
     38 #include <sys/ipc.h>
     39 #include <sys/sem.h>
     40 #include <sys/shm.h>
     41 #include <sys/msg.h>
     42 #undef _KERNEL
     43 
     44 #include <err.h>
     45 #include <fcntl.h>
     46 #include <kvm.h>
     47 #include <limits.h>
     48 #include <nlist.h>
     49 #include <paths.h>
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 
     55 int	semconfig __P((int, ...));
     56 void	usage __P((void));
     57 
     58 extern	char *__progname;		/* from crt0.o */
     59 
     60 static struct nlist symbols[] = {
     61 	{"_sema"},
     62 #define X_SEMA		0
     63 	{"_seminfo"},
     64 #define X_SEMINFO	1
     65 	{"_semu"},
     66 #define X_SEMU		2
     67 	{"_msginfo"},
     68 #define X_MSGINFO	3
     69 	{"_msqids"},
     70 #define X_MSQIDS	4
     71 	{"_shminfo"},
     72 #define X_SHMINFO	5
     73 	{"_shmsegs"},
     74 #define X_SHMSEGS	6
     75 	{NULL}
     76 };
     77 
     78 static kvm_t *kd;
     79 
     80 char   *
     81 fmt_perm(mode)
     82 	u_short mode;
     83 {
     84 	static char buffer[100];
     85 
     86 	buffer[0] = '-';
     87 	buffer[1] = '-';
     88 	buffer[2] = ((mode & 0400) ? 'r' : '-');
     89 	buffer[3] = ((mode & 0200) ? 'w' : '-');
     90 	buffer[4] = ((mode & 0100) ? 'a' : '-');
     91 	buffer[5] = ((mode & 0040) ? 'r' : '-');
     92 	buffer[6] = ((mode & 0020) ? 'w' : '-');
     93 	buffer[7] = ((mode & 0010) ? 'a' : '-');
     94 	buffer[8] = ((mode & 0004) ? 'r' : '-');
     95 	buffer[9] = ((mode & 0002) ? 'w' : '-');
     96 	buffer[10] = ((mode & 0001) ? 'a' : '-');
     97 	buffer[11] = '\0';
     98 	return (&buffer[0]);
     99 }
    100 
    101 void
    102 cvt_time(t, buf, buflen)
    103 	time_t  t;
    104 	char   *buf;
    105 	int	buflen;
    106 {
    107 	struct tm *tm;
    108 
    109 	if (t == 0) {
    110 		(void)strncpy(buf, "no-entry", buflen - 1);
    111 	} else {
    112 		tm = localtime(&t);
    113 		(void)snprintf(buf, buflen, "%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 	char	errbuf[_POSIX2_LINE_MAX];
    139 	int     i;
    140 
    141 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != EOF)
    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 privelidges if not the running kernel so that
    194 	 * bad guys can't print interesting stuff from kernel memory.
    195 	 */
    196 	if (namelist != NULL || core != NULL)
    197 		setgid(getgid());
    198 
    199 	if ((kd = kvm_openfiles(namelist, core, NULL, O_RDONLY,
    200 	    errbuf)) == NULL)
    201 		errx(1, "can't open kvm: %s", errbuf);
    202 
    203 	switch (kvm_nlist(kd, symbols)) {
    204 	case 0:
    205 		break;
    206 	case -1:
    207 		errx(1, "%s: unable to read symbol table.",
    208 		    namelist == NULL ? _PATH_UNIX : namelist);
    209 	default:
    210 #ifdef notdef		/* they'll be told more civilly later */
    211 		warnx("nlist failed");
    212 		for (i = 0; symbols[i].n_name != NULL; i++)
    213 			if (symbols[i].n_value == 0)
    214 				warnx("symbol %s not found",
    215 				    symbols[i].n_name);
    216 		break;
    217 #endif
    218 	}
    219 
    220 	if ((display & (MSGINFO | MSGTOTAL)) &&
    221 	    (kvm_read(kd, symbols[X_MSGINFO].n_value,
    222 	     &msginfo, sizeof(msginfo)) == sizeof(msginfo))) {
    223 
    224 		if (display & MSGTOTAL) {
    225 			printf("msginfo:\n");
    226 			printf("\tmsgmax: %6d\t(max characters in a message)\n",
    227 			    msginfo.msgmax);
    228 			printf("\tmsgmni: %6d\t(# of message queues)\n",
    229 			    msginfo.msgmni);
    230 			printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
    231 			    msginfo.msgmnb);
    232 			printf("\tmsgtql: %6d\t(max # of messages in system)\n",
    233 			    msginfo.msgtql);
    234 			printf("\tmsgssz: %6d\t(size of a message segment)\n",
    235 			    msginfo.msgssz);
    236 			printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
    237 			    msginfo.msgseg);
    238 		}
    239 		if (display & MSGINFO) {
    240 			struct msqid_ds *xmsqids;
    241 
    242 			if (kvm_read(kd, symbols[X_MSQIDS].n_value,
    243 			    &msqids, sizeof(msqids)) != sizeof(msqids))
    244 				errx(1, "kvm_read (%s): %s",
    245 				    symbols[X_MSQIDS].n_name, kvm_geterr(kd));
    246 
    247 			xmsqids = malloc(sizeof(struct msqid_ds) *
    248 			    msginfo.msgmni);
    249 
    250 			if (kvm_read(kd, (u_long)msqids, xmsqids,
    251 			    sizeof(struct msqid_ds) * msginfo.msgmni) !=
    252 			    sizeof(struct msqid_ds) * msginfo.msgmni)
    253 				errx(1, "kvm_read (msqids): %s",
    254 				    kvm_geterr(kd));
    255 
    256 			printf("Message Queues:\n");
    257 			printf("T     ID     KEY        MODE       OWNER    GROUP");
    258 			if (option & CREATOR)
    259 				printf("  CREATOR   CGROUP");
    260 			if (option & OUTSTANDING)
    261 				printf(" CBYTES  QNUM");
    262 			if (option & BIGGEST)
    263 				printf(" QBYTES");
    264 			if (option & PID)
    265 				printf(" LSPID LRPID");
    266 			if (option & TIME)
    267 				printf("   STIME    RTIME    CTIME");
    268 			printf("\n");
    269 			for (i = 0; i < msginfo.msgmni; i += 1) {
    270 				if (xmsqids[i].msg_qbytes != 0) {
    271 					char    stime_buf[100], rtime_buf[100],
    272 					        ctime_buf[100];
    273 					struct msqid_ds *msqptr = &xmsqids[i];
    274 
    275 					cvt_time(msqptr->msg_stime, stime_buf,
    276 					    sizeof stime_buf);
    277 					cvt_time(msqptr->msg_rtime, rtime_buf,
    278 					    sizeof rtime_buf);
    279 					cvt_time(msqptr->msg_ctime, ctime_buf,
    280 					    sizeof ctime_buf);
    281 
    282 					printf("q %6d %10d %s %8s %8s",
    283 					    IXSEQ_TO_IPCID(i, msqptr->msg_perm),
    284 					    msqptr->msg_perm.key,
    285 					    fmt_perm(msqptr->msg_perm.mode),
    286 					    user_from_uid(msqptr->msg_perm.uid, 0),
    287 					    group_from_gid(msqptr->msg_perm.gid, 0));
    288 
    289 					if (option & CREATOR)
    290 						printf(" %8s %8s",
    291 						    user_from_uid(msqptr->msg_perm.cuid, 0),
    292 						    group_from_gid(msqptr->msg_perm.cgid, 0));
    293 
    294 					if (option & OUTSTANDING)
    295 						printf(" %6d %6d",
    296 						    msqptr->msg_cbytes,
    297 						    msqptr->msg_qnum);
    298 
    299 					if (option & BIGGEST)
    300 						printf(" %6d",
    301 						    msqptr->msg_qbytes);
    302 
    303 					if (option & PID)
    304 						printf(" %6d %6d",
    305 						    msqptr->msg_lspid,
    306 						    msqptr->msg_lrpid);
    307 
    308 					if (option & TIME)
    309 						printf("%s %s %s",
    310 						    stime_buf,
    311 						    rtime_buf,
    312 						    ctime_buf);
    313 
    314 					printf("\n");
    315 				}
    316 			}
    317 			printf("\n");
    318 		}
    319 	} else
    320 		if (display & (MSGINFO | MSGTOTAL)) {
    321 			fprintf(stderr,
    322 			    "SVID messages facility not configured in the system\n");
    323 		}
    324 	if ((display & (SHMINFO | SHMTOTAL)) &&
    325 	    (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo,
    326 	     sizeof(shminfo)) == sizeof(shminfo))) {
    327 
    328 		if (display & SHMTOTAL) {
    329 			printf("shminfo:\n");
    330 			printf("\tshmmax: %7d\t(max shared memory segment size)\n",
    331 			    shminfo.shmmax);
    332 			printf("\tshmmin: %7d\t(min shared memory segment size)\n",
    333 			    shminfo.shmmin);
    334 			printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
    335 			    shminfo.shmmni);
    336 			printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
    337 			    shminfo.shmseg);
    338 			printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
    339 			    shminfo.shmall);
    340 		}
    341 		if (display & SHMINFO) {
    342 			struct shmid_ds *xshmids;
    343 
    344 			if (kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs,
    345 			    sizeof(shmsegs)) != sizeof(shmsegs))
    346 				errx(1, "kvm_read (%s): %s",
    347 				    symbols[X_SHMSEGS].n_name, kvm_geterr(kd));
    348 
    349 			xshmids = malloc(sizeof(struct shmid_ds) *
    350 			    shminfo.shmmni);
    351 
    352 			if (kvm_read(kd, (u_long)shmsegs, xshmids,
    353 			    sizeof(struct shmid_ds) * shminfo.shmmni) !=
    354 			    sizeof(struct shmid_ds) * shminfo.shmmni)
    355 				errx(1, "kvm_read (shmsegs): %s",
    356 				    kvm_geterr(kd));
    357 
    358 			printf("Shared Memory:\n");
    359 			printf("T     ID     KEY        MODE       OWNER    GROUP");
    360 			if (option & CREATOR)
    361 				printf("  CREATOR   CGROUP");
    362 			if (option & OUTSTANDING)
    363 				printf(" NATTCH");
    364 			if (option & BIGGEST)
    365 				printf("  SEGSZ");
    366 			if (option & PID)
    367 				printf("  CPID  LPID");
    368 			if (option & TIME)
    369 				printf("   ATIME    DTIME    CTIME");
    370 			printf("\n");
    371 			for (i = 0; i < shminfo.shmmni; i += 1) {
    372 				if (xshmids[i].shm_perm.mode & 0x0800) {
    373 					char    atime_buf[100], dtime_buf[100],
    374 					        ctime_buf[100];
    375 					struct shmid_ds *shmptr = &xshmids[i];
    376 
    377 					cvt_time(shmptr->shm_atime, atime_buf,
    378 					    sizeof atime_buf);
    379 					cvt_time(shmptr->shm_dtime, dtime_buf,
    380 					    sizeof dtime_buf);
    381 					cvt_time(shmptr->shm_ctime, ctime_buf,
    382 					    sizeof ctime_buf);
    383 
    384 					printf("m %6d %10d %s %8s %8s",
    385 					    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
    386 					    shmptr->shm_perm.key,
    387 					    fmt_perm(shmptr->shm_perm.mode),
    388 					    user_from_uid(shmptr->shm_perm.uid, 0),
    389 					    group_from_gid(shmptr->shm_perm.gid, 0));
    390 
    391 					if (option & CREATOR)
    392 						printf(" %8s %8s",
    393 						    user_from_uid(shmptr->shm_perm.cuid, 0),
    394 						    group_from_gid(shmptr->shm_perm.cgid, 0));
    395 
    396 					if (option & OUTSTANDING)
    397 						printf(" %6d",
    398 						    shmptr->shm_nattch);
    399 
    400 					if (option & BIGGEST)
    401 						printf(" %6d",
    402 						    shmptr->shm_segsz);
    403 
    404 					if (option & PID)
    405 						printf(" %6d %6d",
    406 						    shmptr->shm_cpid,
    407 						    shmptr->shm_lpid);
    408 
    409 					if (option & TIME)
    410 						printf("%s %s %s",
    411 						    atime_buf,
    412 						    dtime_buf,
    413 						    ctime_buf);
    414 
    415 					printf("\n");
    416 				}
    417 			}
    418 			printf("\n");
    419 		}
    420 	} else
    421 		if (display & (SHMINFO | SHMTOTAL)) {
    422 			fprintf(stderr,
    423 			    "SVID shared memory facility not configured in the system\n");
    424 		}
    425 	if ((display & (SEMINFO | SEMTOTAL)) &&
    426 	    (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo,
    427 	     sizeof(seminfo)) == sizeof(seminfo))) {
    428 		struct semid_ds *xsema;
    429 
    430 		if (display & SEMTOTAL) {
    431 			printf("seminfo:\n");
    432 			printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
    433 			    seminfo.semmap);
    434 			printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
    435 			    seminfo.semmni);
    436 			printf("\tsemmns: %6d\t(# of semaphores in system)\n",
    437 			    seminfo.semmns);
    438 			printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
    439 			    seminfo.semmnu);
    440 			printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
    441 			    seminfo.semmsl);
    442 			printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
    443 			    seminfo.semopm);
    444 			printf("\tsemume: %6d\t(max # of undo entries per process)\n",
    445 			    seminfo.semume);
    446 			printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
    447 			    seminfo.semusz);
    448 			printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
    449 			    seminfo.semvmx);
    450 			printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
    451 			    seminfo.semaem);
    452 		}
    453 		if (display & SEMINFO) {
    454 			if (semconfig(SEM_CONFIG_FREEZE) != 0) {
    455 				perror("semconfig");
    456 				fprintf(stderr,
    457 				    "Can't lock semaphore facility - winging it...\n");
    458 			}
    459 			if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
    460 			    sizeof(sema)) != sizeof(sema))
    461 				errx(1, "kvm_read (%s): %s",
    462 				    symbols[X_SEMA].n_name, kvm_geterr(kd));
    463 
    464 			xsema = malloc(sizeof(struct semid_ds) *
    465 			    seminfo.semmni);
    466 
    467 			if (kvm_read(kd, (u_long)sema, xsema,
    468 			    sizeof(struct semid_ds) * seminfo.semmni) !=
    469 			    sizeof(struct semid_ds) * seminfo.semmni)
    470 				errx(1, "kvm_read (sema): %s",
    471 				    kvm_geterr(kd));
    472 
    473 			printf("Semaphores:\n");
    474 			printf("T     ID     KEY        MODE       OWNER    GROUP");
    475 			if (option & CREATOR)
    476 				printf("  CREATOR   CGROUP");
    477 			if (option & BIGGEST)
    478 				printf(" NSEMS");
    479 			if (option & TIME)
    480 				printf("   OTIME    CTIME");
    481 			printf("\n");
    482 			for (i = 0; i < seminfo.semmni; i += 1) {
    483 				if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
    484 					char    ctime_buf[100], otime_buf[100];
    485 					struct semid_ds *semaptr = &xsema[i];
    486 					int     j, value;
    487 					union semun junk;
    488 
    489 					cvt_time(semaptr->sem_otime, otime_buf,
    490 					    sizeof otime_buf);
    491 					cvt_time(semaptr->sem_ctime, ctime_buf,
    492 					    sizeof ctime_buf);
    493 
    494 					printf("s %6d %10d %s %8s %8s",
    495 					    IXSEQ_TO_IPCID(i, semaptr->sem_perm),
    496 					    semaptr->sem_perm.key,
    497 					    fmt_perm(semaptr->sem_perm.mode),
    498 					    user_from_uid(semaptr->sem_perm.uid, 0),
    499 					    group_from_gid(semaptr->sem_perm.gid, 0));
    500 
    501 					if (option & CREATOR)
    502 						printf(" %8s %8s",
    503 						    user_from_uid(semaptr->sem_perm.cuid, 0),
    504 						    group_from_gid(semaptr->sem_perm.cgid, 0));
    505 
    506 					if (option & BIGGEST)
    507 						printf(" %6d",
    508 						    semaptr->sem_nsems);
    509 
    510 					if (option & TIME)
    511 						printf("%s %s",
    512 						    otime_buf,
    513 						    ctime_buf);
    514 
    515 					printf("\n");
    516 				}
    517 			}
    518 
    519 			(void) semconfig(SEM_CONFIG_THAW);
    520 
    521 			printf("\n");
    522 		}
    523 	} else
    524 		if (display & (SEMINFO | SEMTOTAL)) {
    525 			fprintf(stderr, "SVID semaphores facility not configured in the system\n");
    526 		}
    527 	kvm_close(kd);
    528 
    529 	exit(0);
    530 }
    531 
    532 void
    533 usage()
    534 {
    535 
    536 	fprintf(stderr,
    537 	    "usage: %s [-abcmopqst] [-C corefile] [-N namelist]\n",
    538 	    __progname);
    539 	exit(1);
    540 }
    541