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