Home | History | Annotate | Line # | Download | only in ipcs
ipcs.c revision 1.16
      1  1.16       mrg /*	$NetBSD: ipcs.c,v 1.16 1998/07/06 07:50:19 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.1       cgd 	static char buffer[100];
     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.4       cgd 	} else {
    118   1.4       cgd 		tm = localtime(&t);
    119  1.13       mrg 		(void)snprintf(buf, buflen, "%2d:%02d:%02d",
    120   1.4       cgd 			tm->tm_hour, tm->tm_min, tm->tm_sec);
    121   1.1       cgd 	}
    122   1.1       cgd }
    123   1.4       cgd #define	SHMINFO		1
    124   1.4       cgd #define	SHMTOTAL	2
    125   1.4       cgd #define	MSGINFO		4
    126   1.4       cgd #define	MSGTOTAL	8
    127   1.4       cgd #define	SEMINFO		16
    128   1.4       cgd #define	SEMTOTAL	32
    129   1.4       cgd 
    130   1.4       cgd #define BIGGEST		1
    131   1.4       cgd #define CREATOR		2
    132   1.4       cgd #define OUTSTANDING	4
    133   1.4       cgd #define PID		8
    134   1.4       cgd #define TIME		16
    135   1.4       cgd 
    136   1.4       cgd int
    137   1.4       cgd main(argc, argv)
    138   1.4       cgd 	int     argc;
    139   1.4       cgd 	char   *argv[];
    140   1.1       cgd {
    141   1.4       cgd 	int     display = SHMINFO | MSGINFO | SEMINFO;
    142   1.4       cgd 	int     option = 0;
    143   1.4       cgd 	char   *core = NULL, *namelist = NULL;
    144  1.11   thorpej 	char	errbuf[_POSIX2_LINE_MAX];
    145   1.4       cgd 	int     i;
    146  1.16       mrg 	gid_t	egid = getegid();
    147   1.4       cgd 
    148  1.14     lukem 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
    149   1.4       cgd 		switch (i) {
    150   1.4       cgd 		case 'M':
    151   1.4       cgd 			display = SHMTOTAL;
    152   1.4       cgd 			break;
    153   1.4       cgd 		case 'm':
    154   1.4       cgd 			display = SHMINFO;
    155   1.4       cgd 			break;
    156   1.4       cgd 		case 'Q':
    157   1.4       cgd 			display = MSGTOTAL;
    158   1.4       cgd 			break;
    159   1.4       cgd 		case 'q':
    160   1.4       cgd 			display = MSGINFO;
    161   1.4       cgd 			break;
    162   1.4       cgd 		case 'S':
    163   1.4       cgd 			display = SEMTOTAL;
    164   1.4       cgd 			break;
    165   1.4       cgd 		case 's':
    166   1.4       cgd 			display = SEMINFO;
    167   1.4       cgd 			break;
    168   1.4       cgd 		case 'T':
    169   1.4       cgd 			display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
    170   1.4       cgd 			break;
    171   1.4       cgd 		case 'a':
    172   1.4       cgd 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
    173   1.4       cgd 			break;
    174   1.4       cgd 		case 'b':
    175   1.4       cgd 			option |= BIGGEST;
    176   1.4       cgd 			break;
    177   1.4       cgd 		case 'C':
    178   1.4       cgd 			core = optarg;
    179   1.4       cgd 			break;
    180   1.4       cgd 		case 'c':
    181   1.4       cgd 			option |= CREATOR;
    182   1.4       cgd 			break;
    183   1.4       cgd 		case 'N':
    184   1.4       cgd 			namelist = optarg;
    185   1.4       cgd 			break;
    186   1.4       cgd 		case 'o':
    187   1.4       cgd 			option |= OUTSTANDING;
    188   1.4       cgd 			break;
    189   1.4       cgd 		case 'p':
    190   1.4       cgd 			option |= PID;
    191   1.4       cgd 			break;
    192   1.4       cgd 		case 't':
    193   1.4       cgd 			option |= TIME;
    194   1.4       cgd 			break;
    195   1.4       cgd 		default:
    196   1.5       cgd 			usage();
    197   1.4       cgd 		}
    198  1.11   thorpej 
    199  1.11   thorpej 	/*
    200  1.16       mrg 	 * Discard setgid privileges.  If not the running kernel, we toss
    201  1.16       mrg 	 * them away totally so that bad guys can't print interesting stuff
    202  1.16       mrg 	 * from kernel memory, otherwise switch back to kmem for the
    203  1.16       mrg 	 * duration of the kvm_openfiles() call.
    204  1.11   thorpej 	 */
    205  1.11   thorpej 	if (namelist != NULL || core != NULL)
    206  1.16       mrg 		(void)setgid(getgid());
    207  1.16       mrg 	else
    208  1.16       mrg 		(void)setegid(egid);
    209  1.11   thorpej 
    210  1.11   thorpej 	if ((kd = kvm_openfiles(namelist, core, NULL, O_RDONLY,
    211  1.11   thorpej 	    errbuf)) == NULL)
    212  1.11   thorpej 		errx(1, "can't open kvm: %s", errbuf);
    213  1.16       mrg 
    214  1.16       mrg 	/* get rid of it now anyway */
    215  1.16       mrg 	if (namelist == NULL && core == NULL)
    216  1.16       mrg 		(void)setgid(getgid());
    217   1.1       cgd 
    218   1.4       cgd 	switch (kvm_nlist(kd, symbols)) {
    219   1.1       cgd 	case 0:
    220   1.1       cgd 		break;
    221   1.1       cgd 	case -1:
    222  1.11   thorpej 		errx(1, "%s: unable to read symbol table.",
    223  1.11   thorpej 		    namelist == NULL ? _PATH_UNIX : namelist);
    224   1.1       cgd 	default:
    225   1.5       cgd #ifdef notdef		/* they'll be told more civilly later */
    226   1.5       cgd 		warnx("nlist failed");
    227   1.5       cgd 		for (i = 0; symbols[i].n_name != NULL; i++)
    228   1.5       cgd 			if (symbols[i].n_value == 0)
    229   1.5       cgd 				warnx("symbol %s not found",
    230   1.5       cgd 				    symbols[i].n_name);
    231   1.1       cgd 		break;
    232   1.5       cgd #endif
    233   1.1       cgd 	}
    234   1.1       cgd 
    235   1.4       cgd 	if ((display & (MSGINFO | MSGTOTAL)) &&
    236  1.11   thorpej 	    (kvm_read(kd, symbols[X_MSGINFO].n_value,
    237  1.11   thorpej 	     &msginfo, sizeof(msginfo)) == sizeof(msginfo))) {
    238   1.1       cgd 
    239   1.4       cgd 		if (display & MSGTOTAL) {
    240   1.4       cgd 			printf("msginfo:\n");
    241   1.4       cgd 			printf("\tmsgmax: %6d\t(max characters in a message)\n",
    242   1.4       cgd 			    msginfo.msgmax);
    243   1.4       cgd 			printf("\tmsgmni: %6d\t(# of message queues)\n",
    244   1.4       cgd 			    msginfo.msgmni);
    245   1.4       cgd 			printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
    246   1.4       cgd 			    msginfo.msgmnb);
    247   1.4       cgd 			printf("\tmsgtql: %6d\t(max # of messages in system)\n",
    248   1.4       cgd 			    msginfo.msgtql);
    249   1.4       cgd 			printf("\tmsgssz: %6d\t(size of a message segment)\n",
    250   1.4       cgd 			    msginfo.msgssz);
    251   1.4       cgd 			printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
    252   1.4       cgd 			    msginfo.msgseg);
    253   1.4       cgd 		}
    254   1.4       cgd 		if (display & MSGINFO) {
    255   1.4       cgd 			struct msqid_ds *xmsqids;
    256   1.1       cgd 
    257  1.11   thorpej 			if (kvm_read(kd, symbols[X_MSQIDS].n_value,
    258  1.11   thorpej 			    &msqids, sizeof(msqids)) != sizeof(msqids))
    259  1.11   thorpej 				errx(1, "kvm_read (%s): %s",
    260  1.11   thorpej 				    symbols[X_MSQIDS].n_name, kvm_geterr(kd));
    261  1.11   thorpej 
    262  1.11   thorpej 			xmsqids = malloc(sizeof(struct msqid_ds) *
    263  1.11   thorpej 			    msginfo.msgmni);
    264  1.11   thorpej 
    265  1.11   thorpej 			if (kvm_read(kd, (u_long)msqids, xmsqids,
    266  1.11   thorpej 			    sizeof(struct msqid_ds) * msginfo.msgmni) !=
    267  1.11   thorpej 			    sizeof(struct msqid_ds) * msginfo.msgmni)
    268  1.11   thorpej 				errx(1, "kvm_read (msqids): %s",
    269  1.11   thorpej 				    kvm_geterr(kd));
    270   1.4       cgd 
    271   1.4       cgd 			printf("Message Queues:\n");
    272   1.4       cgd 			printf("T     ID     KEY        MODE       OWNER    GROUP");
    273   1.4       cgd 			if (option & CREATOR)
    274   1.4       cgd 				printf("  CREATOR   CGROUP");
    275   1.4       cgd 			if (option & OUTSTANDING)
    276   1.4       cgd 				printf(" CBYTES  QNUM");
    277   1.4       cgd 			if (option & BIGGEST)
    278   1.4       cgd 				printf(" QBYTES");
    279   1.4       cgd 			if (option & PID)
    280   1.4       cgd 				printf(" LSPID LRPID");
    281   1.4       cgd 			if (option & TIME)
    282   1.4       cgd 				printf("   STIME    RTIME    CTIME");
    283   1.4       cgd 			printf("\n");
    284   1.4       cgd 			for (i = 0; i < msginfo.msgmni; i += 1) {
    285   1.4       cgd 				if (xmsqids[i].msg_qbytes != 0) {
    286   1.4       cgd 					char    stime_buf[100], rtime_buf[100],
    287   1.4       cgd 					        ctime_buf[100];
    288   1.4       cgd 					struct msqid_ds *msqptr = &xmsqids[i];
    289   1.4       cgd 
    290  1.13       mrg 					cvt_time(msqptr->msg_stime, stime_buf,
    291  1.13       mrg 					    sizeof stime_buf);
    292  1.13       mrg 					cvt_time(msqptr->msg_rtime, rtime_buf,
    293  1.13       mrg 					    sizeof rtime_buf);
    294  1.13       mrg 					cvt_time(msqptr->msg_ctime, ctime_buf,
    295  1.13       mrg 					    sizeof ctime_buf);
    296   1.4       cgd 
    297  1.14     lukem 					printf("q %6d %10ld %s %8s %8s",
    298   1.4       cgd 					    IXSEQ_TO_IPCID(i, msqptr->msg_perm),
    299  1.14     lukem 					    (long)msqptr->msg_perm.key,
    300   1.4       cgd 					    fmt_perm(msqptr->msg_perm.mode),
    301   1.4       cgd 					    user_from_uid(msqptr->msg_perm.uid, 0),
    302   1.4       cgd 					    group_from_gid(msqptr->msg_perm.gid, 0));
    303   1.4       cgd 
    304   1.4       cgd 					if (option & CREATOR)
    305   1.4       cgd 						printf(" %8s %8s",
    306   1.4       cgd 						    user_from_uid(msqptr->msg_perm.cuid, 0),
    307   1.4       cgd 						    group_from_gid(msqptr->msg_perm.cgid, 0));
    308   1.4       cgd 
    309   1.4       cgd 					if (option & OUTSTANDING)
    310  1.14     lukem 						printf(" %6ld %6ld",
    311  1.14     lukem 						    (long)msqptr->msg_cbytes,
    312  1.14     lukem 						    (long)msqptr->msg_qnum);
    313   1.4       cgd 
    314   1.4       cgd 					if (option & BIGGEST)
    315  1.14     lukem 						printf(" %6ld",
    316  1.14     lukem 						    (long)msqptr->msg_qbytes);
    317   1.4       cgd 
    318   1.4       cgd 					if (option & PID)
    319   1.4       cgd 						printf(" %6d %6d",
    320   1.4       cgd 						    msqptr->msg_lspid,
    321   1.4       cgd 						    msqptr->msg_lrpid);
    322   1.4       cgd 
    323   1.4       cgd 					if (option & TIME)
    324   1.4       cgd 						printf("%s %s %s",
    325   1.4       cgd 						    stime_buf,
    326   1.4       cgd 						    rtime_buf,
    327   1.4       cgd 						    ctime_buf);
    328   1.1       cgd 
    329   1.4       cgd 					printf("\n");
    330   1.4       cgd 				}
    331   1.4       cgd 			}
    332   1.4       cgd 			printf("\n");
    333   1.4       cgd 		}
    334   1.4       cgd 	} else
    335   1.4       cgd 		if (display & (MSGINFO | MSGTOTAL)) {
    336   1.1       cgd 			fprintf(stderr,
    337   1.4       cgd 			    "SVID messages facility not configured in the system\n");
    338   1.4       cgd 		}
    339   1.4       cgd 	if ((display & (SHMINFO | SHMTOTAL)) &&
    340  1.11   thorpej 	    (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo,
    341  1.11   thorpej 	     sizeof(shminfo)) == sizeof(shminfo))) {
    342  1.11   thorpej 
    343   1.4       cgd 		if (display & SHMTOTAL) {
    344   1.4       cgd 			printf("shminfo:\n");
    345   1.4       cgd 			printf("\tshmmax: %7d\t(max shared memory segment size)\n",
    346   1.4       cgd 			    shminfo.shmmax);
    347   1.4       cgd 			printf("\tshmmin: %7d\t(min shared memory segment size)\n",
    348   1.4       cgd 			    shminfo.shmmin);
    349   1.4       cgd 			printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
    350   1.4       cgd 			    shminfo.shmmni);
    351   1.4       cgd 			printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
    352   1.4       cgd 			    shminfo.shmseg);
    353   1.4       cgd 			printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
    354   1.4       cgd 			    shminfo.shmall);
    355   1.1       cgd 		}
    356   1.4       cgd 		if (display & SHMINFO) {
    357   1.4       cgd 			struct shmid_ds *xshmids;
    358   1.1       cgd 
    359  1.11   thorpej 			if (kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs,
    360  1.11   thorpej 			    sizeof(shmsegs)) != sizeof(shmsegs))
    361  1.11   thorpej 				errx(1, "kvm_read (%s): %s",
    362  1.11   thorpej 				    symbols[X_SHMSEGS].n_name, kvm_geterr(kd));
    363  1.11   thorpej 
    364  1.11   thorpej 			xshmids = malloc(sizeof(struct shmid_ds) *
    365  1.12  explorer 			    shminfo.shmmni);
    366  1.11   thorpej 
    367  1.11   thorpej 			if (kvm_read(kd, (u_long)shmsegs, xshmids,
    368  1.11   thorpej 			    sizeof(struct shmid_ds) * shminfo.shmmni) !=
    369  1.11   thorpej 			    sizeof(struct shmid_ds) * shminfo.shmmni)
    370  1.11   thorpej 				errx(1, "kvm_read (shmsegs): %s",
    371  1.11   thorpej 				    kvm_geterr(kd));
    372   1.4       cgd 
    373   1.4       cgd 			printf("Shared Memory:\n");
    374   1.4       cgd 			printf("T     ID     KEY        MODE       OWNER    GROUP");
    375   1.4       cgd 			if (option & CREATOR)
    376   1.4       cgd 				printf("  CREATOR   CGROUP");
    377   1.4       cgd 			if (option & OUTSTANDING)
    378   1.4       cgd 				printf(" NATTCH");
    379   1.4       cgd 			if (option & BIGGEST)
    380   1.4       cgd 				printf("  SEGSZ");
    381   1.4       cgd 			if (option & PID)
    382   1.4       cgd 				printf("  CPID  LPID");
    383   1.4       cgd 			if (option & TIME)
    384   1.4       cgd 				printf("   ATIME    DTIME    CTIME");
    385   1.4       cgd 			printf("\n");
    386   1.4       cgd 			for (i = 0; i < shminfo.shmmni; i += 1) {
    387   1.4       cgd 				if (xshmids[i].shm_perm.mode & 0x0800) {
    388   1.4       cgd 					char    atime_buf[100], dtime_buf[100],
    389   1.4       cgd 					        ctime_buf[100];
    390   1.4       cgd 					struct shmid_ds *shmptr = &xshmids[i];
    391   1.4       cgd 
    392  1.13       mrg 					cvt_time(shmptr->shm_atime, atime_buf,
    393  1.13       mrg 					    sizeof atime_buf);
    394  1.13       mrg 					cvt_time(shmptr->shm_dtime, dtime_buf,
    395  1.13       mrg 					    sizeof dtime_buf);
    396  1.13       mrg 					cvt_time(shmptr->shm_ctime, ctime_buf,
    397  1.13       mrg 					    sizeof ctime_buf);
    398   1.4       cgd 
    399  1.14     lukem 					printf("m %6d %10ld %s %8s %8s",
    400   1.4       cgd 					    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
    401  1.14     lukem 					    (long)shmptr->shm_perm.key,
    402   1.4       cgd 					    fmt_perm(shmptr->shm_perm.mode),
    403   1.4       cgd 					    user_from_uid(shmptr->shm_perm.uid, 0),
    404   1.4       cgd 					    group_from_gid(shmptr->shm_perm.gid, 0));
    405   1.4       cgd 
    406   1.4       cgd 					if (option & CREATOR)
    407   1.4       cgd 						printf(" %8s %8s",
    408   1.4       cgd 						    user_from_uid(shmptr->shm_perm.cuid, 0),
    409   1.4       cgd 						    group_from_gid(shmptr->shm_perm.cgid, 0));
    410   1.4       cgd 
    411   1.4       cgd 					if (option & OUTSTANDING)
    412   1.4       cgd 						printf(" %6d",
    413   1.4       cgd 						    shmptr->shm_nattch);
    414   1.4       cgd 
    415   1.4       cgd 					if (option & BIGGEST)
    416   1.4       cgd 						printf(" %6d",
    417   1.4       cgd 						    shmptr->shm_segsz);
    418   1.4       cgd 
    419   1.4       cgd 					if (option & PID)
    420   1.4       cgd 						printf(" %6d %6d",
    421   1.4       cgd 						    shmptr->shm_cpid,
    422   1.4       cgd 						    shmptr->shm_lpid);
    423   1.4       cgd 
    424   1.4       cgd 					if (option & TIME)
    425   1.4       cgd 						printf("%s %s %s",
    426   1.4       cgd 						    atime_buf,
    427   1.4       cgd 						    dtime_buf,
    428   1.4       cgd 						    ctime_buf);
    429   1.1       cgd 
    430   1.4       cgd 					printf("\n");
    431   1.1       cgd 				}
    432   1.1       cgd 			}
    433   1.4       cgd 			printf("\n");
    434   1.4       cgd 		}
    435   1.4       cgd 	} else
    436   1.4       cgd 		if (display & (SHMINFO | SHMTOTAL)) {
    437   1.4       cgd 			fprintf(stderr,
    438   1.4       cgd 			    "SVID shared memory facility not configured in the system\n");
    439   1.1       cgd 		}
    440   1.4       cgd 	if ((display & (SEMINFO | SEMTOTAL)) &&
    441  1.11   thorpej 	    (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo,
    442  1.11   thorpej 	     sizeof(seminfo)) == sizeof(seminfo))) {
    443   1.4       cgd 		struct semid_ds *xsema;
    444   1.1       cgd 
    445   1.4       cgd 		if (display & SEMTOTAL) {
    446   1.4       cgd 			printf("seminfo:\n");
    447   1.4       cgd 			printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
    448   1.4       cgd 			    seminfo.semmap);
    449   1.4       cgd 			printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
    450   1.4       cgd 			    seminfo.semmni);
    451   1.4       cgd 			printf("\tsemmns: %6d\t(# of semaphores in system)\n",
    452   1.4       cgd 			    seminfo.semmns);
    453   1.4       cgd 			printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
    454   1.4       cgd 			    seminfo.semmnu);
    455   1.4       cgd 			printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
    456   1.4       cgd 			    seminfo.semmsl);
    457   1.4       cgd 			printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
    458   1.4       cgd 			    seminfo.semopm);
    459   1.4       cgd 			printf("\tsemume: %6d\t(max # of undo entries per process)\n",
    460   1.4       cgd 			    seminfo.semume);
    461   1.4       cgd 			printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
    462   1.4       cgd 			    seminfo.semusz);
    463   1.4       cgd 			printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
    464   1.4       cgd 			    seminfo.semvmx);
    465   1.4       cgd 			printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
    466   1.4       cgd 			    seminfo.semaem);
    467   1.4       cgd 		}
    468   1.4       cgd 		if (display & SEMINFO) {
    469   1.4       cgd 			if (semconfig(SEM_CONFIG_FREEZE) != 0) {
    470   1.4       cgd 				perror("semconfig");
    471   1.4       cgd 				fprintf(stderr,
    472   1.4       cgd 				    "Can't lock semaphore facility - winging it...\n");
    473   1.1       cgd 			}
    474  1.11   thorpej 			if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
    475  1.11   thorpej 			    sizeof(sema)) != sizeof(sema))
    476  1.11   thorpej 				errx(1, "kvm_read (%s): %s",
    477  1.11   thorpej 				    symbols[X_SEMA].n_name, kvm_geterr(kd));
    478  1.11   thorpej 
    479  1.11   thorpej 			xsema = malloc(sizeof(struct semid_ds) *
    480  1.11   thorpej 			    seminfo.semmni);
    481  1.11   thorpej 
    482  1.11   thorpej 			if (kvm_read(kd, (u_long)sema, xsema,
    483  1.11   thorpej 			    sizeof(struct semid_ds) * seminfo.semmni) !=
    484  1.11   thorpej 			    sizeof(struct semid_ds) * seminfo.semmni)
    485  1.11   thorpej 				errx(1, "kvm_read (sema): %s",
    486  1.11   thorpej 				    kvm_geterr(kd));
    487   1.4       cgd 
    488   1.4       cgd 			printf("Semaphores:\n");
    489   1.4       cgd 			printf("T     ID     KEY        MODE       OWNER    GROUP");
    490   1.4       cgd 			if (option & CREATOR)
    491   1.4       cgd 				printf("  CREATOR   CGROUP");
    492   1.4       cgd 			if (option & BIGGEST)
    493   1.4       cgd 				printf(" NSEMS");
    494   1.4       cgd 			if (option & TIME)
    495   1.4       cgd 				printf("   OTIME    CTIME");
    496   1.4       cgd 			printf("\n");
    497   1.4       cgd 			for (i = 0; i < seminfo.semmni; i += 1) {
    498   1.4       cgd 				if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
    499   1.4       cgd 					char    ctime_buf[100], otime_buf[100];
    500   1.4       cgd 					struct semid_ds *semaptr = &xsema[i];
    501   1.4       cgd 
    502  1.13       mrg 					cvt_time(semaptr->sem_otime, otime_buf,
    503  1.13       mrg 					    sizeof otime_buf);
    504  1.13       mrg 					cvt_time(semaptr->sem_ctime, ctime_buf,
    505  1.13       mrg 					    sizeof ctime_buf);
    506   1.1       cgd 
    507  1.14     lukem 					printf("s %6d %10ld %s %8s %8s",
    508   1.4       cgd 					    IXSEQ_TO_IPCID(i, semaptr->sem_perm),
    509  1.14     lukem 					    (long)semaptr->sem_perm.key,
    510   1.4       cgd 					    fmt_perm(semaptr->sem_perm.mode),
    511   1.4       cgd 					    user_from_uid(semaptr->sem_perm.uid, 0),
    512   1.4       cgd 					    group_from_gid(semaptr->sem_perm.gid, 0));
    513   1.4       cgd 
    514   1.4       cgd 					if (option & CREATOR)
    515   1.4       cgd 						printf(" %8s %8s",
    516   1.4       cgd 						    user_from_uid(semaptr->sem_perm.cuid, 0),
    517   1.4       cgd 						    group_from_gid(semaptr->sem_perm.cgid, 0));
    518   1.4       cgd 
    519   1.4       cgd 					if (option & BIGGEST)
    520   1.4       cgd 						printf(" %6d",
    521   1.4       cgd 						    semaptr->sem_nsems);
    522   1.4       cgd 
    523   1.4       cgd 					if (option & TIME)
    524   1.4       cgd 						printf("%s %s",
    525   1.4       cgd 						    otime_buf,
    526   1.4       cgd 						    ctime_buf);
    527   1.1       cgd 
    528   1.4       cgd 					printf("\n");
    529   1.4       cgd 				}
    530   1.4       cgd 			}
    531   1.1       cgd 
    532   1.4       cgd 			(void) semconfig(SEM_CONFIG_THAW);
    533   1.1       cgd 
    534   1.4       cgd 			printf("\n");
    535   1.4       cgd 		}
    536   1.4       cgd 	} else
    537   1.4       cgd 		if (display & (SEMINFO | SEMTOTAL)) {
    538   1.4       cgd 			fprintf(stderr, "SVID semaphores facility not configured in the system\n");
    539   1.1       cgd 		}
    540   1.4       cgd 	kvm_close(kd);
    541   1.1       cgd 
    542   1.1       cgd 	exit(0);
    543   1.5       cgd }
    544   1.5       cgd 
    545   1.5       cgd void
    546   1.5       cgd usage()
    547   1.5       cgd {
    548   1.5       cgd 
    549   1.5       cgd 	fprintf(stderr,
    550  1.11   thorpej 	    "usage: %s [-abcmopqst] [-C corefile] [-N namelist]\n",
    551  1.11   thorpej 	    __progname);
    552   1.5       cgd 	exit(1);
    553   1.1       cgd }
    554