Home | History | Annotate | Line # | Download | only in ipcs
ipcs.c revision 1.33
      1  1.33   hubertf /*	$NetBSD: ipcs.c,v 1.33 2004/09/15 19:45:17 hubertf Exp $	*/
      2  1.23    simonb 
      3  1.23    simonb /*-
      4  1.23    simonb  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.23    simonb  * All rights reserved.
      6  1.23    simonb  *
      7  1.23    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.23    simonb  * by Simon Burge.
      9  1.23    simonb  *
     10  1.23    simonb  * Redistribution and use in source and binary forms, with or without
     11  1.23    simonb  * modification, are permitted provided that the following conditions
     12  1.23    simonb  * are met:
     13  1.23    simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.23    simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.23    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.23    simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.23    simonb  *    documentation and/or other materials provided with the distribution.
     18  1.23    simonb  * 3. All advertising materials mentioning features or use of this software
     19  1.23    simonb  *    must display the following acknowledgement:
     20  1.23    simonb  *        This product includes software developed by the NetBSD
     21  1.23    simonb  *        Foundation, Inc. and its contributors.
     22  1.23    simonb  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.23    simonb  *    contributors may be used to endorse or promote products derived
     24  1.23    simonb  *    from this software without specific prior written permission.
     25  1.23    simonb  *
     26  1.23    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.23    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.23    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.23    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.23    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.23    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.23    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.23    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.23    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.23    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.23    simonb  * POSSIBILITY OF SUCH DAMAGE.
     37  1.23    simonb  */
     38   1.8       cgd 
     39   1.1       cgd /*
     40   1.6       cgd  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo (at) sigmasoft.com>
     41   1.6       cgd  * All rights reserved.
     42   1.1       cgd  *
     43   1.6       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.6       cgd  * modification, are permitted provided that the following conditions
     45   1.6       cgd  * are met:
     46   1.6       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.6       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.6       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.6       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.6       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.6       cgd  *
     52   1.6       cgd  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53   1.6       cgd  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54   1.6       cgd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55   1.6       cgd  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56   1.6       cgd  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57   1.6       cgd  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58   1.6       cgd  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59   1.6       cgd  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60   1.6       cgd  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61   1.6       cgd  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.1       cgd  */
     63   1.1       cgd 
     64   1.1       cgd #include <sys/param.h>
     65  1.23    simonb #include <sys/sysctl.h>
     66   1.1       cgd #include <sys/ipc.h>
     67   1.1       cgd #include <sys/sem.h>
     68   1.1       cgd #include <sys/shm.h>
     69   1.1       cgd #include <sys/msg.h>
     70   1.1       cgd 
     71  1.11   thorpej #include <err.h>
     72  1.11   thorpej #include <fcntl.h>
     73  1.14     lukem #include <grp.h>
     74  1.11   thorpej #include <kvm.h>
     75  1.11   thorpej #include <limits.h>
     76  1.11   thorpej #include <nlist.h>
     77  1.11   thorpej #include <paths.h>
     78  1.14     lukem #include <pwd.h>
     79  1.11   thorpej #include <stdio.h>
     80  1.11   thorpej #include <stdlib.h>
     81  1.11   thorpej #include <string.h>
     82  1.15    kleink #include <time.h>
     83  1.11   thorpej #include <unistd.h>
     84  1.19  augustss 
     85  1.23    simonb void	cvt_time(time_t, char *, size_t);
     86  1.23    simonb char   *fmt_perm(u_short);
     87  1.23    simonb void	ipcs_kvm(void);
     88  1.23    simonb int	main(int, char **);
     89  1.23    simonb void	msg_sysctl(void);
     90  1.23    simonb void	sem_sysctl(void);
     91  1.23    simonb void	shm_sysctl(void);
     92  1.23    simonb void	show_msginfo(time_t, time_t, time_t, int, u_int64_t, mode_t, uid_t,
     93  1.23    simonb 	    gid_t, uid_t, gid_t, u_int64_t, u_int64_t, u_int64_t, pid_t, pid_t);
     94  1.23    simonb void	show_msginfo_hdr(void);
     95  1.23    simonb void	show_msgtotal(struct msginfo *);
     96  1.23    simonb void	show_seminfo_hdr(void);
     97  1.23    simonb void	show_seminfo(time_t, time_t, int, u_int64_t, mode_t, uid_t, gid_t,
     98  1.23    simonb 	    uid_t, gid_t, int16_t);
     99  1.23    simonb void	show_semtotal(struct seminfo *);
    100  1.23    simonb void	show_shminfo(time_t, time_t, time_t, int, u_int64_t, mode_t, uid_t,
    101  1.23    simonb 	    gid_t, uid_t, gid_t, u_int32_t, u_int64_t, pid_t, pid_t);
    102  1.23    simonb void	show_shminfo_hdr(void);
    103  1.23    simonb void	show_shmtotal(struct shminfo *);
    104  1.23    simonb void	usage(void);
    105   1.4       cgd 
    106  1.23    simonb char *
    107  1.23    simonb fmt_perm(u_short mode)
    108   1.1       cgd {
    109  1.17       mrg 	static char buffer[12];
    110   1.1       cgd 
    111   1.1       cgd 	buffer[0] = '-';
    112   1.1       cgd 	buffer[1] = '-';
    113   1.1       cgd 	buffer[2] = ((mode & 0400) ? 'r' : '-');
    114   1.1       cgd 	buffer[3] = ((mode & 0200) ? 'w' : '-');
    115   1.1       cgd 	buffer[4] = ((mode & 0100) ? 'a' : '-');
    116   1.1       cgd 	buffer[5] = ((mode & 0040) ? 'r' : '-');
    117   1.1       cgd 	buffer[6] = ((mode & 0020) ? 'w' : '-');
    118   1.1       cgd 	buffer[7] = ((mode & 0010) ? 'a' : '-');
    119   1.1       cgd 	buffer[8] = ((mode & 0004) ? 'r' : '-');
    120   1.1       cgd 	buffer[9] = ((mode & 0002) ? 'w' : '-');
    121   1.1       cgd 	buffer[10] = ((mode & 0001) ? 'a' : '-');
    122   1.1       cgd 	buffer[11] = '\0';
    123   1.1       cgd 	return (&buffer[0]);
    124   1.1       cgd }
    125   1.1       cgd 
    126   1.1       cgd void
    127  1.23    simonb cvt_time(time_t t, char *buf, size_t buflen)
    128   1.1       cgd {
    129   1.4       cgd 	struct tm *tm;
    130   1.1       cgd 
    131  1.30    itojun 	if (t == 0)
    132  1.30    itojun 		(void)strlcpy(buf, "no-entry", buflen);
    133  1.30    itojun 	else {
    134   1.4       cgd 		tm = localtime(&t);
    135  1.13       mrg 		(void)snprintf(buf, buflen, "%2d:%02d:%02d",
    136   1.4       cgd 			tm->tm_hour, tm->tm_min, tm->tm_sec);
    137   1.1       cgd 	}
    138   1.1       cgd }
    139   1.4       cgd #define	SHMINFO		1
    140   1.4       cgd #define	SHMTOTAL	2
    141   1.4       cgd #define	MSGINFO		4
    142   1.4       cgd #define	MSGTOTAL	8
    143   1.4       cgd #define	SEMINFO		16
    144   1.4       cgd #define	SEMTOTAL	32
    145   1.4       cgd 
    146   1.4       cgd #define BIGGEST		1
    147   1.4       cgd #define CREATOR		2
    148   1.4       cgd #define OUTSTANDING	4
    149   1.4       cgd #define PID		8
    150   1.4       cgd #define TIME		16
    151   1.4       cgd 
    152  1.23    simonb char	*core = NULL, *namelist = NULL;
    153  1.32    simonb int	display = 0;
    154  1.23    simonb int	option = 0;
    155  1.23    simonb 
    156   1.4       cgd int
    157  1.23    simonb main(int argc, char *argv[])
    158  1.23    simonb {
    159  1.23    simonb 	int i;
    160   1.4       cgd 
    161  1.14     lukem 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
    162   1.4       cgd 		switch (i) {
    163   1.4       cgd 		case 'M':
    164  1.32    simonb 			display |= SHMTOTAL;
    165   1.4       cgd 			break;
    166   1.4       cgd 		case 'm':
    167  1.32    simonb 			display |= SHMINFO;
    168   1.4       cgd 			break;
    169   1.4       cgd 		case 'Q':
    170  1.32    simonb 			display |= MSGTOTAL;
    171   1.4       cgd 			break;
    172   1.4       cgd 		case 'q':
    173  1.32    simonb 			display |= MSGINFO;
    174   1.4       cgd 			break;
    175   1.4       cgd 		case 'S':
    176  1.32    simonb 			display |= SEMTOTAL;
    177   1.4       cgd 			break;
    178   1.4       cgd 		case 's':
    179  1.32    simonb 			display |= SEMINFO;
    180   1.4       cgd 			break;
    181   1.4       cgd 		case 'T':
    182  1.32    simonb 			display |= SHMTOTAL | MSGTOTAL | SEMTOTAL;
    183   1.4       cgd 			break;
    184   1.4       cgd 		case 'a':
    185   1.4       cgd 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
    186   1.4       cgd 			break;
    187   1.4       cgd 		case 'b':
    188   1.4       cgd 			option |= BIGGEST;
    189   1.4       cgd 			break;
    190   1.4       cgd 		case 'C':
    191   1.4       cgd 			core = optarg;
    192   1.4       cgd 			break;
    193   1.4       cgd 		case 'c':
    194   1.4       cgd 			option |= CREATOR;
    195   1.4       cgd 			break;
    196   1.4       cgd 		case 'N':
    197   1.4       cgd 			namelist = optarg;
    198   1.4       cgd 			break;
    199   1.4       cgd 		case 'o':
    200   1.4       cgd 			option |= OUTSTANDING;
    201   1.4       cgd 			break;
    202   1.4       cgd 		case 'p':
    203   1.4       cgd 			option |= PID;
    204   1.4       cgd 			break;
    205   1.4       cgd 		case 't':
    206   1.4       cgd 			option |= TIME;
    207   1.4       cgd 			break;
    208   1.4       cgd 		default:
    209   1.5       cgd 			usage();
    210   1.4       cgd 		}
    211  1.11   thorpej 
    212  1.24    simonb 	if (argc - optind > 0)
    213  1.24    simonb 		usage();
    214  1.24    simonb 
    215  1.32    simonb         if (display == 0)
    216  1.32    simonb 		display = SHMINFO | MSGINFO | SEMINFO;
    217  1.32    simonb 
    218  1.25    simonb 	if (core == NULL) {
    219  1.23    simonb 		if (display & (MSGINFO | MSGTOTAL))
    220  1.23    simonb 			msg_sysctl();
    221  1.23    simonb 		if (display & (SHMINFO | SHMTOTAL))
    222  1.23    simonb 			shm_sysctl();
    223  1.23    simonb 		if (display & (SEMINFO | SEMTOTAL))
    224  1.23    simonb 			sem_sysctl();
    225  1.23    simonb 	} else
    226  1.23    simonb 		ipcs_kvm();
    227  1.23    simonb 	exit(0);
    228  1.23    simonb }
    229  1.23    simonb 
    230  1.23    simonb void
    231  1.23    simonb show_msgtotal(struct msginfo *msginfo)
    232  1.23    simonb {
    233  1.23    simonb 	printf("msginfo:\n");
    234  1.23    simonb 	printf("\tmsgmax: %6d\t(max characters in a message)\n",
    235  1.23    simonb 	    msginfo->msgmax);
    236  1.23    simonb 	printf("\tmsgmni: %6d\t(# of message queues)\n",
    237  1.23    simonb 	    msginfo->msgmni);
    238  1.23    simonb 	printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
    239  1.23    simonb 	    msginfo->msgmnb);
    240  1.23    simonb 	printf("\tmsgtql: %6d\t(max # of messages in system)\n",
    241  1.23    simonb 	    msginfo->msgtql);
    242  1.23    simonb 	printf("\tmsgssz: %6d\t(size of a message segment)\n",
    243  1.23    simonb 	    msginfo->msgssz);
    244  1.23    simonb 	printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
    245  1.23    simonb 	    msginfo->msgseg);
    246  1.23    simonb }
    247  1.23    simonb 
    248  1.23    simonb void
    249  1.23    simonb show_shmtotal(struct shminfo *shminfo)
    250  1.23    simonb {
    251  1.23    simonb 	printf("shminfo:\n");
    252  1.23    simonb 	printf("\tshmmax: %7d\t(max shared memory segment size)\n",
    253  1.23    simonb 	    shminfo->shmmax);
    254  1.23    simonb 	printf("\tshmmin: %7d\t(min shared memory segment size)\n",
    255  1.23    simonb 	    shminfo->shmmin);
    256  1.23    simonb 	printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
    257  1.23    simonb 	    shminfo->shmmni);
    258  1.23    simonb 	printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
    259  1.23    simonb 	    shminfo->shmseg);
    260  1.23    simonb 	printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
    261  1.23    simonb 	    shminfo->shmall);
    262  1.23    simonb }
    263  1.23    simonb 
    264  1.23    simonb void
    265  1.23    simonb show_semtotal(struct seminfo *seminfo)
    266  1.23    simonb {
    267  1.23    simonb 	printf("seminfo:\n");
    268  1.23    simonb 	printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
    269  1.23    simonb 	    seminfo->semmap);
    270  1.23    simonb 	printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
    271  1.23    simonb 	    seminfo->semmni);
    272  1.23    simonb 	printf("\tsemmns: %6d\t(# of semaphores in system)\n",
    273  1.23    simonb 	    seminfo->semmns);
    274  1.23    simonb 	printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
    275  1.23    simonb 	    seminfo->semmnu);
    276  1.23    simonb 	printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
    277  1.23    simonb 	    seminfo->semmsl);
    278  1.23    simonb 	printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
    279  1.23    simonb 	    seminfo->semopm);
    280  1.23    simonb 	printf("\tsemume: %6d\t(max # of undo entries per process)\n",
    281  1.23    simonb 	    seminfo->semume);
    282  1.23    simonb 	printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
    283  1.23    simonb 	    seminfo->semusz);
    284  1.23    simonb 	printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
    285  1.23    simonb 	    seminfo->semvmx);
    286  1.23    simonb 	printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
    287  1.23    simonb 	    seminfo->semaem);
    288  1.23    simonb }
    289  1.23    simonb 
    290  1.23    simonb void
    291  1.23    simonb show_msginfo_hdr(void)
    292  1.23    simonb {
    293  1.23    simonb 	printf("Message Queues:\n");
    294  1.29    simonb 	printf("T        ID     KEY        MODE       OWNER    GROUP");
    295  1.23    simonb 	if (option & CREATOR)
    296  1.23    simonb 		printf("  CREATOR   CGROUP");
    297  1.23    simonb 	if (option & OUTSTANDING)
    298  1.23    simonb 		printf(" CBYTES  QNUM");
    299  1.23    simonb 	if (option & BIGGEST)
    300  1.23    simonb 		printf(" QBYTES");
    301  1.23    simonb 	if (option & PID)
    302  1.23    simonb 		printf(" LSPID LRPID");
    303  1.23    simonb 	if (option & TIME)
    304  1.23    simonb 		printf("    STIME    RTIME    CTIME");
    305  1.23    simonb 	printf("\n");
    306  1.23    simonb }
    307  1.23    simonb 
    308  1.23    simonb void
    309  1.23    simonb show_msginfo(time_t stime, time_t rtime, time_t ctime, int ipcid, u_int64_t key,
    310  1.23    simonb     mode_t mode, uid_t uid, gid_t gid, uid_t cuid, gid_t cgid,
    311  1.23    simonb     u_int64_t cbytes, u_int64_t qnum, u_int64_t qbytes, pid_t lspid,
    312  1.23    simonb     pid_t lrpid)
    313  1.23    simonb {
    314  1.23    simonb 	char stime_buf[100], rtime_buf[100], ctime_buf[100];
    315  1.23    simonb 
    316  1.23    simonb 	if (option & TIME) {
    317  1.23    simonb 		cvt_time(stime, stime_buf, sizeof(stime_buf));
    318  1.23    simonb 		cvt_time(rtime, rtime_buf, sizeof(rtime_buf));
    319  1.23    simonb 		cvt_time(ctime, ctime_buf, sizeof(ctime_buf));
    320  1.23    simonb 	}
    321  1.23    simonb 
    322  1.29    simonb 	printf("q %9d %10lld %s %8s %8s", ipcid, (long long)key, fmt_perm(mode),
    323  1.23    simonb 	    user_from_uid(uid, 0), group_from_gid(gid, 0));
    324  1.23    simonb 
    325  1.23    simonb 	if (option & CREATOR)
    326  1.23    simonb 		printf(" %8s %8s", user_from_uid(cuid, 0),
    327  1.23    simonb 		    group_from_gid(cgid, 0));
    328  1.23    simonb 
    329  1.23    simonb 	if (option & OUTSTANDING)
    330  1.23    simonb 		printf(" %6lld %5lld", (long long)cbytes, (long long)qnum);
    331  1.23    simonb 
    332  1.23    simonb 	if (option & BIGGEST)
    333  1.23    simonb 		printf(" %6lld", (long long)qbytes);
    334  1.23    simonb 
    335  1.23    simonb 	if (option & PID)
    336  1.23    simonb 		printf(" %5d %5d", lspid, lrpid);
    337  1.23    simonb 
    338  1.23    simonb 	if (option & TIME)
    339  1.23    simonb 		printf(" %s %s %s", stime_buf, rtime_buf, ctime_buf);
    340  1.23    simonb 
    341  1.23    simonb 	printf("\n");
    342  1.23    simonb }
    343  1.23    simonb 
    344  1.23    simonb void
    345  1.23    simonb show_shminfo_hdr(void)
    346  1.23    simonb {
    347  1.23    simonb 	printf("Shared Memory:\n");
    348  1.29    simonb 	printf("T        ID     KEY        MODE       OWNER    GROUP");
    349  1.23    simonb 	if (option & CREATOR)
    350  1.23    simonb 		printf("  CREATOR   CGROUP");
    351  1.23    simonb 	if (option & OUTSTANDING)
    352  1.23    simonb 		printf(" NATTCH");
    353  1.23    simonb 	if (option & BIGGEST)
    354  1.23    simonb 		printf("   SEGSZ");
    355  1.23    simonb 	if (option & PID)
    356  1.23    simonb 		printf("  CPID  LPID");
    357  1.23    simonb 	if (option & TIME)
    358  1.23    simonb 		printf("    ATIME    DTIME    CTIME");
    359  1.23    simonb 	printf("\n");
    360  1.23    simonb }
    361  1.23    simonb 
    362  1.23    simonb void
    363  1.23    simonb show_shminfo(time_t atime, time_t dtime, time_t ctime, int ipcid, u_int64_t key,
    364  1.23    simonb     mode_t mode, uid_t uid, gid_t gid, uid_t cuid, gid_t cgid,
    365  1.23    simonb     u_int32_t nattch, u_int64_t segsz, pid_t cpid, pid_t lpid)
    366  1.23    simonb {
    367  1.23    simonb 	char atime_buf[100], dtime_buf[100], ctime_buf[100];
    368  1.23    simonb 
    369  1.23    simonb 	if (option & TIME) {
    370  1.23    simonb 		cvt_time(atime, atime_buf, sizeof(atime_buf));
    371  1.23    simonb 		cvt_time(dtime, dtime_buf, sizeof(dtime_buf));
    372  1.23    simonb 		cvt_time(ctime, ctime_buf, sizeof(ctime_buf));
    373  1.23    simonb 	}
    374  1.23    simonb 
    375  1.29    simonb 	printf("m %9d %10lld %s %8s %8s", ipcid, (long long)key, fmt_perm(mode),
    376  1.23    simonb 	    user_from_uid(uid, 0), group_from_gid(gid, 0));
    377  1.23    simonb 
    378  1.23    simonb 	if (option & CREATOR)
    379  1.23    simonb 		printf(" %8s %8s", user_from_uid(cuid, 0),
    380  1.23    simonb 		    group_from_gid(cgid, 0));
    381  1.23    simonb 
    382  1.23    simonb 	if (option & OUTSTANDING)
    383  1.23    simonb 		printf(" %6d", nattch);
    384  1.23    simonb 
    385  1.23    simonb 	if (option & BIGGEST)
    386  1.23    simonb 		printf(" %7llu", (long long)segsz);
    387  1.23    simonb 
    388  1.23    simonb 	if (option & PID)
    389  1.23    simonb 		printf(" %5d %5d", cpid, lpid);
    390  1.23    simonb 
    391  1.23    simonb 	if (option & TIME)
    392  1.23    simonb 		printf(" %s %s %s",
    393  1.23    simonb 		    atime_buf,
    394  1.23    simonb 		    dtime_buf,
    395  1.23    simonb 		    ctime_buf);
    396  1.23    simonb 
    397  1.23    simonb 	printf("\n");
    398  1.23    simonb }
    399  1.23    simonb 
    400  1.23    simonb void
    401  1.23    simonb show_seminfo_hdr(void)
    402  1.23    simonb {
    403  1.23    simonb 	printf("Semaphores:\n");
    404  1.29    simonb 	printf("T        ID     KEY        MODE       OWNER    GROUP");
    405  1.23    simonb 	if (option & CREATOR)
    406  1.23    simonb 		printf("  CREATOR   CGROUP");
    407  1.23    simonb 	if (option & BIGGEST)
    408  1.23    simonb 		printf(" NSEMS");
    409  1.23    simonb 	if (option & TIME)
    410  1.23    simonb 		printf("    OTIME    CTIME");
    411  1.23    simonb 	printf("\n");
    412  1.23    simonb }
    413  1.23    simonb 
    414  1.23    simonb void
    415  1.23    simonb show_seminfo(time_t otime, time_t ctime, int ipcid, u_int64_t key, mode_t mode,
    416  1.23    simonb     uid_t uid, gid_t gid, uid_t cuid, gid_t cgid, int16_t nsems)
    417  1.23    simonb {
    418  1.23    simonb 	char ctime_buf[100], otime_buf[100];
    419  1.23    simonb 
    420  1.23    simonb 	if (option & TIME) {
    421  1.23    simonb 		cvt_time(otime, otime_buf, sizeof(otime_buf));
    422  1.23    simonb 		cvt_time(ctime, ctime_buf, sizeof(ctime_buf));
    423  1.23    simonb 	}
    424  1.23    simonb 
    425  1.29    simonb 	printf("s %9d %10lld %s %8s %8s", ipcid, (long long)key, fmt_perm(mode),
    426  1.23    simonb 	    user_from_uid(uid, 0), group_from_gid(gid, 0));
    427  1.23    simonb 
    428  1.23    simonb 	if (option & CREATOR)
    429  1.23    simonb 		printf(" %8s %8s", user_from_uid(cuid, 0),
    430  1.23    simonb 		    group_from_gid(cgid, 0));
    431  1.23    simonb 
    432  1.23    simonb 	if (option & BIGGEST)
    433  1.23    simonb 		printf(" %5d", nsems);
    434  1.23    simonb 
    435  1.23    simonb 	if (option & TIME)
    436  1.23    simonb 		printf(" %s %s", otime_buf, ctime_buf);
    437  1.23    simonb 
    438  1.23    simonb 	printf("\n");
    439  1.23    simonb }
    440  1.23    simonb 
    441  1.23    simonb void
    442  1.23    simonb msg_sysctl(void)
    443  1.23    simonb {
    444  1.23    simonb 	struct msg_sysctl_info *msgsi;
    445  1.23    simonb 	char *buf;
    446  1.23    simonb 	int mib[3];
    447  1.23    simonb 	size_t len;
    448  1.23    simonb 	int i, valid;
    449  1.23    simonb 
    450  1.23    simonb 	mib[0] = CTL_KERN;
    451  1.23    simonb 	mib[1] = KERN_SYSVMSG;
    452  1.23    simonb 	len = sizeof(valid);
    453  1.31      fvdl 	if (sysctl(mib, 2, &valid, &len, NULL, 0) < 0) {
    454  1.23    simonb 		perror("sysctl(KERN_SYSVMSG)");
    455  1.23    simonb 		return;
    456  1.23    simonb 	}
    457  1.23    simonb 	if (!valid) {
    458  1.23    simonb 		fprintf(stderr,
    459  1.23    simonb 		    "SVID messages facility not configured in the system\n");
    460  1.23    simonb 		return;
    461  1.23    simonb 	}
    462  1.23    simonb 
    463  1.23    simonb 	mib[0] = CTL_KERN;
    464  1.23    simonb 	mib[1] = KERN_SYSVIPC_INFO;
    465  1.23    simonb 	mib[2] = KERN_SYSVIPC_MSG_INFO;
    466  1.23    simonb 
    467  1.23    simonb 	if (!(display & MSGINFO)) {
    468  1.23    simonb 		/* totals only */
    469  1.23    simonb 		len = sizeof(struct msginfo);
    470  1.23    simonb 	} else {
    471  1.31      fvdl 		if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) {
    472  1.23    simonb 			perror("sysctl(KERN_SYSVIPC_MSG_INFO)");
    473  1.23    simonb 			return;
    474  1.23    simonb 		}
    475  1.23    simonb 	}
    476  1.23    simonb 
    477  1.23    simonb 	if ((buf = malloc(len)) == NULL)
    478  1.23    simonb 		err(1, "malloc");
    479  1.23    simonb 	msgsi = (struct msg_sysctl_info *)buf;
    480  1.31      fvdl 	if (sysctl(mib, 3, msgsi, &len, NULL, 0) < 0) {
    481  1.23    simonb 		perror("sysctl(KERN_SYSVIPC_MSG_INFO)");
    482  1.23    simonb 		return;
    483  1.23    simonb 	}
    484  1.23    simonb 
    485  1.23    simonb 	if (display & MSGTOTAL)
    486  1.23    simonb 		show_msgtotal(&msgsi->msginfo);
    487  1.23    simonb 
    488  1.23    simonb 	if (display & MSGINFO) {
    489  1.23    simonb 		show_msginfo_hdr();
    490  1.23    simonb 		for (i = 0; i < msgsi->msginfo.msgmni; i++) {
    491  1.23    simonb 			struct msgid_ds_sysctl *msqptr = &msgsi->msgids[i];
    492  1.23    simonb 			if (msqptr->msg_qbytes != 0)
    493  1.23    simonb 				show_msginfo(msqptr->msg_stime,
    494  1.23    simonb 				    msqptr->msg_rtime,
    495  1.23    simonb 				    msqptr->msg_ctime,
    496  1.23    simonb 				    IXSEQ_TO_IPCID(i, msqptr->msg_perm),
    497  1.23    simonb 				    msqptr->msg_perm._key,
    498  1.23    simonb 				    msqptr->msg_perm.mode,
    499  1.23    simonb 				    msqptr->msg_perm.uid,
    500  1.23    simonb 				    msqptr->msg_perm.gid,
    501  1.23    simonb 				    msqptr->msg_perm.cuid,
    502  1.23    simonb 				    msqptr->msg_perm.cgid,
    503  1.23    simonb 				    msqptr->_msg_cbytes,
    504  1.23    simonb 				    msqptr->msg_qnum,
    505  1.23    simonb 				    msqptr->msg_qbytes,
    506  1.23    simonb 				    msqptr->msg_lspid,
    507  1.23    simonb 				    msqptr->msg_lrpid);
    508  1.23    simonb 		}
    509  1.23    simonb 		printf("\n");
    510  1.23    simonb 	}
    511  1.23    simonb }
    512  1.23    simonb 
    513  1.23    simonb void
    514  1.23    simonb shm_sysctl(void)
    515  1.23    simonb {
    516  1.23    simonb 	struct shm_sysctl_info *shmsi;
    517  1.23    simonb 	char *buf;
    518  1.23    simonb 	int mib[3];
    519  1.23    simonb 	size_t len;
    520  1.23    simonb 	int i /*, valid */;
    521  1.23    simonb 	long valid;
    522  1.23    simonb 
    523  1.23    simonb 	mib[0] = CTL_KERN;
    524  1.23    simonb 	mib[1] = KERN_SYSVSHM;
    525  1.23    simonb 	len = sizeof(valid);
    526  1.31      fvdl 	if (sysctl(mib, 2, &valid, &len, NULL, 0) < 0) {
    527  1.23    simonb 		perror("sysctl(KERN_SYSVSHM)");
    528  1.23    simonb 		return;
    529  1.23    simonb 	}
    530  1.23    simonb 	if (!valid) {
    531  1.23    simonb 		fprintf(stderr,
    532  1.23    simonb 		    "SVID shared memory facility not configured in the system\n");
    533  1.23    simonb 		return;
    534  1.23    simonb 	}
    535  1.23    simonb 
    536  1.23    simonb 	mib[0] = CTL_KERN;
    537  1.23    simonb 	mib[1] = KERN_SYSVIPC_INFO;
    538  1.23    simonb 	mib[2] = KERN_SYSVIPC_SHM_INFO;
    539  1.23    simonb 
    540  1.23    simonb 	if (!(display & SHMINFO)) {
    541  1.23    simonb 		/* totals only */
    542  1.23    simonb 		len = sizeof(struct shminfo);
    543  1.23    simonb 	} else {
    544  1.31      fvdl 		if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) {
    545  1.23    simonb 			perror("sysctl(KERN_SYSVIPC_SHM_INFO)");
    546  1.23    simonb 			return;
    547  1.23    simonb 		}
    548  1.23    simonb 	}
    549  1.23    simonb 
    550  1.23    simonb 	if ((buf = malloc(len)) == NULL)
    551  1.23    simonb 		err(1, "malloc");
    552  1.23    simonb 	shmsi = (struct shm_sysctl_info *)buf;
    553  1.31      fvdl 	if (sysctl(mib, 3, shmsi, &len, NULL, 0) < 0) {
    554  1.23    simonb 		perror("sysctl(KERN_SYSVIPC_SHM_INFO)");
    555  1.23    simonb 		return;
    556  1.23    simonb 	}
    557  1.23    simonb 
    558  1.23    simonb 	if (display & SHMTOTAL)
    559  1.23    simonb 		show_shmtotal(&shmsi->shminfo);
    560  1.23    simonb 
    561  1.23    simonb 	if (display & SHMINFO) {
    562  1.23    simonb 		show_shminfo_hdr();
    563  1.23    simonb 		for (i = 0; i < shmsi->shminfo.shmmni; i++) {
    564  1.23    simonb 			struct shmid_ds_sysctl *shmptr = &shmsi->shmids[i];
    565  1.23    simonb 			if (shmptr->shm_perm.mode & 0x0800)
    566  1.23    simonb 				show_shminfo(shmptr->shm_atime,
    567  1.23    simonb 				    shmptr->shm_dtime,
    568  1.23    simonb 				    shmptr->shm_ctime,
    569  1.23    simonb 				    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
    570  1.23    simonb 				    shmptr->shm_perm._key,
    571  1.23    simonb 				    shmptr->shm_perm.mode,
    572  1.23    simonb 				    shmptr->shm_perm.uid,
    573  1.23    simonb 				    shmptr->shm_perm.gid,
    574  1.23    simonb 				    shmptr->shm_perm.cuid,
    575  1.23    simonb 				    shmptr->shm_perm.cgid,
    576  1.23    simonb 				    shmptr->shm_nattch,
    577  1.23    simonb 				    shmptr->shm_segsz,
    578  1.23    simonb 				    shmptr->shm_cpid,
    579  1.23    simonb 				    shmptr->shm_lpid);
    580  1.23    simonb 		}
    581  1.23    simonb 		printf("\n");
    582  1.23    simonb 	}
    583  1.23    simonb }
    584  1.23    simonb 
    585  1.23    simonb void
    586  1.23    simonb sem_sysctl(void)
    587  1.23    simonb {
    588  1.23    simonb 	struct sem_sysctl_info *semsi;
    589  1.23    simonb 	char *buf;
    590  1.23    simonb 	int mib[3];
    591  1.23    simonb 	size_t len;
    592  1.23    simonb 	int i, valid;
    593  1.23    simonb 
    594  1.23    simonb 	mib[0] = CTL_KERN;
    595  1.23    simonb 	mib[1] = KERN_SYSVSEM;
    596  1.23    simonb 	len = sizeof(valid);
    597  1.31      fvdl 	if (sysctl(mib, 2, &valid, &len, NULL, 0) < 0) {
    598  1.23    simonb 		perror("sysctl(KERN_SYSVSEM)");
    599  1.23    simonb 		return;
    600  1.23    simonb 	}
    601  1.23    simonb 	if (!valid) {
    602  1.23    simonb 		fprintf(stderr,
    603  1.28       chs 		    "SVID semaphores facility not configured in the system\n");
    604  1.23    simonb 		return;
    605  1.23    simonb 	}
    606  1.23    simonb 
    607  1.23    simonb 	mib[0] = CTL_KERN;
    608  1.23    simonb 	mib[1] = KERN_SYSVIPC_INFO;
    609  1.23    simonb 	mib[2] = KERN_SYSVIPC_SEM_INFO;
    610  1.23    simonb 
    611  1.23    simonb 	if (!(display & SEMINFO)) {
    612  1.23    simonb 		/* totals only */
    613  1.23    simonb 		len = sizeof(struct seminfo);
    614  1.23    simonb 	} else {
    615  1.31      fvdl 		if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0) {
    616  1.23    simonb 			perror("sysctl(KERN_SYSVIPC_SEM_INFO)");
    617  1.23    simonb 			return;
    618  1.23    simonb 		}
    619  1.23    simonb 	}
    620  1.23    simonb 
    621  1.23    simonb 	if ((buf = malloc(len)) == NULL)
    622  1.23    simonb 		err(1, "malloc");
    623  1.23    simonb 	semsi = (struct sem_sysctl_info *)buf;
    624  1.31      fvdl 	if (sysctl(mib, 3, semsi, &len, NULL, 0) < 0) {
    625  1.23    simonb 		perror("sysctl(KERN_SYSVIPC_SEM_INFO)");
    626  1.23    simonb 		return;
    627  1.23    simonb 	}
    628  1.23    simonb 
    629  1.23    simonb 	if (display & SEMTOTAL)
    630  1.23    simonb 		show_semtotal(&semsi->seminfo);
    631  1.23    simonb 
    632  1.23    simonb 	if (display & SEMINFO) {
    633  1.23    simonb 		show_seminfo_hdr();
    634  1.23    simonb 		for (i = 0; i < semsi->seminfo.semmni; i++) {
    635  1.23    simonb 			struct semid_ds_sysctl *semaptr = &semsi->semids[i];
    636  1.23    simonb 			if ((semaptr->sem_perm.mode & SEM_ALLOC) != 0)
    637  1.23    simonb 				show_seminfo(semaptr->sem_otime,
    638  1.23    simonb 				    semaptr->sem_ctime,
    639  1.23    simonb 				    IXSEQ_TO_IPCID(i, semaptr->sem_perm),
    640  1.23    simonb 				    semaptr->sem_perm._key,
    641  1.23    simonb 				    semaptr->sem_perm.mode,
    642  1.23    simonb 				    semaptr->sem_perm.uid,
    643  1.23    simonb 				    semaptr->sem_perm.gid,
    644  1.23    simonb 				    semaptr->sem_perm.cuid,
    645  1.23    simonb 				    semaptr->sem_perm.cgid,
    646  1.23    simonb 				    semaptr->sem_nsems);
    647  1.23    simonb 		}
    648  1.23    simonb 		printf("\n");
    649  1.23    simonb 	}
    650  1.23    simonb }
    651  1.23    simonb 
    652  1.23    simonb void
    653  1.23    simonb ipcs_kvm(void)
    654  1.23    simonb {
    655  1.23    simonb 	struct msginfo msginfo;
    656  1.23    simonb 	struct msqid_ds *msqids;
    657  1.23    simonb 	struct seminfo seminfo;
    658  1.23    simonb 	struct semid_ds *sema;
    659  1.23    simonb 	struct shminfo shminfo;
    660  1.23    simonb 	struct shmid_ds *shmsegs;
    661  1.23    simonb 	kvm_t *kd;
    662  1.23    simonb 	char errbuf[_POSIX2_LINE_MAX];
    663  1.23    simonb 	int i;
    664  1.23    simonb 	struct nlist symbols[] = {
    665  1.23    simonb 		{"_sema"},
    666  1.23    simonb 	#define X_SEMA		0
    667  1.23    simonb 		{"_seminfo"},
    668  1.23    simonb 	#define X_SEMINFO	1
    669  1.23    simonb 		{"_semu"},
    670  1.23    simonb 	#define X_SEMU		2
    671  1.23    simonb 		{"_msginfo"},
    672  1.23    simonb 	#define X_MSGINFO	3
    673  1.23    simonb 		{"_msqids"},
    674  1.23    simonb 	#define X_MSQIDS	4
    675  1.23    simonb 		{"_shminfo"},
    676  1.23    simonb 	#define X_SHMINFO	5
    677  1.23    simonb 		{"_shmsegs"},
    678  1.23    simonb 	#define X_SHMSEGS	6
    679  1.23    simonb 		{NULL}
    680  1.23    simonb 	};
    681  1.11   thorpej 
    682  1.11   thorpej 	if ((kd = kvm_openfiles(namelist, core, NULL, O_RDONLY,
    683  1.11   thorpej 	    errbuf)) == NULL)
    684  1.11   thorpej 		errx(1, "can't open kvm: %s", errbuf);
    685  1.16       mrg 
    686   1.1       cgd 
    687   1.4       cgd 	switch (kvm_nlist(kd, symbols)) {
    688   1.1       cgd 	case 0:
    689   1.1       cgd 		break;
    690   1.1       cgd 	case -1:
    691  1.11   thorpej 		errx(1, "%s: unable to read symbol table.",
    692  1.11   thorpej 		    namelist == NULL ? _PATH_UNIX : namelist);
    693  1.23    simonb 		/* NOTREACHED */
    694   1.1       cgd 	default:
    695   1.5       cgd #ifdef notdef		/* they'll be told more civilly later */
    696   1.5       cgd 		warnx("nlist failed");
    697   1.5       cgd 		for (i = 0; symbols[i].n_name != NULL; i++)
    698   1.5       cgd 			if (symbols[i].n_value == 0)
    699   1.5       cgd 				warnx("symbol %s not found",
    700   1.5       cgd 				    symbols[i].n_name);
    701  1.23    simonb #endif
    702   1.1       cgd 		break;
    703   1.1       cgd 	}
    704   1.1       cgd 
    705   1.4       cgd 	if ((display & (MSGINFO | MSGTOTAL)) &&
    706  1.11   thorpej 	    (kvm_read(kd, symbols[X_MSGINFO].n_value,
    707  1.11   thorpej 	     &msginfo, sizeof(msginfo)) == sizeof(msginfo))) {
    708   1.1       cgd 
    709  1.23    simonb 		if (display & MSGTOTAL)
    710  1.23    simonb 			show_msgtotal(&msginfo);
    711  1.23    simonb 
    712   1.4       cgd 		if (display & MSGINFO) {
    713   1.4       cgd 			struct msqid_ds *xmsqids;
    714   1.1       cgd 
    715  1.11   thorpej 			if (kvm_read(kd, symbols[X_MSQIDS].n_value,
    716  1.11   thorpej 			    &msqids, sizeof(msqids)) != sizeof(msqids))
    717  1.11   thorpej 				errx(1, "kvm_read (%s): %s",
    718  1.11   thorpej 				    symbols[X_MSQIDS].n_name, kvm_geterr(kd));
    719  1.11   thorpej 
    720  1.11   thorpej 			xmsqids = malloc(sizeof(struct msqid_ds) *
    721  1.11   thorpej 			    msginfo.msgmni);
    722  1.11   thorpej 
    723  1.11   thorpej 			if (kvm_read(kd, (u_long)msqids, xmsqids,
    724  1.11   thorpej 			    sizeof(struct msqid_ds) * msginfo.msgmni) !=
    725  1.11   thorpej 			    sizeof(struct msqid_ds) * msginfo.msgmni)
    726  1.11   thorpej 				errx(1, "kvm_read (msqids): %s",
    727  1.11   thorpej 				    kvm_geterr(kd));
    728   1.4       cgd 
    729  1.23    simonb 			show_msginfo_hdr();
    730  1.23    simonb 			for (i = 0; i < msginfo.msgmni; i++) {
    731  1.23    simonb 				struct msqid_ds *msqptr = &xmsqids[i];
    732  1.23    simonb 				if (msqptr->msg_qbytes != 0)
    733  1.23    simonb 					show_msginfo(msqptr->msg_stime,
    734  1.23    simonb 					    msqptr->msg_rtime,
    735  1.23    simonb 					    msqptr->msg_ctime,
    736   1.4       cgd 					    IXSEQ_TO_IPCID(i, msqptr->msg_perm),
    737  1.23    simonb 					    msqptr->msg_perm._key,
    738  1.23    simonb 					    msqptr->msg_perm.mode,
    739  1.23    simonb 					    msqptr->msg_perm.uid,
    740  1.23    simonb 					    msqptr->msg_perm.gid,
    741  1.23    simonb 					    msqptr->msg_perm.cuid,
    742  1.23    simonb 					    msqptr->msg_perm.cgid,
    743  1.23    simonb 					    msqptr->_msg_cbytes,
    744  1.23    simonb 					    msqptr->msg_qnum,
    745  1.23    simonb 					    msqptr->msg_qbytes,
    746  1.23    simonb 					    msqptr->msg_lspid,
    747  1.23    simonb 					    msqptr->msg_lrpid);
    748   1.4       cgd 			}
    749   1.4       cgd 			printf("\n");
    750   1.4       cgd 		}
    751   1.4       cgd 	} else
    752   1.4       cgd 		if (display & (MSGINFO | MSGTOTAL)) {
    753   1.1       cgd 			fprintf(stderr,
    754   1.4       cgd 			    "SVID messages facility not configured in the system\n");
    755   1.4       cgd 		}
    756   1.4       cgd 	if ((display & (SHMINFO | SHMTOTAL)) &&
    757  1.11   thorpej 	    (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo,
    758  1.11   thorpej 	     sizeof(shminfo)) == sizeof(shminfo))) {
    759  1.11   thorpej 
    760  1.23    simonb 		if (display & SHMTOTAL)
    761  1.23    simonb 			show_shmtotal(&shminfo);
    762  1.23    simonb 
    763   1.4       cgd 		if (display & SHMINFO) {
    764   1.4       cgd 			struct shmid_ds *xshmids;
    765   1.1       cgd 
    766  1.11   thorpej 			if (kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs,
    767  1.11   thorpej 			    sizeof(shmsegs)) != sizeof(shmsegs))
    768  1.11   thorpej 				errx(1, "kvm_read (%s): %s",
    769  1.11   thorpej 				    symbols[X_SHMSEGS].n_name, kvm_geterr(kd));
    770  1.11   thorpej 
    771  1.11   thorpej 			xshmids = malloc(sizeof(struct shmid_ds) *
    772  1.12  explorer 			    shminfo.shmmni);
    773  1.11   thorpej 
    774  1.11   thorpej 			if (kvm_read(kd, (u_long)shmsegs, xshmids,
    775  1.11   thorpej 			    sizeof(struct shmid_ds) * shminfo.shmmni) !=
    776  1.11   thorpej 			    sizeof(struct shmid_ds) * shminfo.shmmni)
    777  1.11   thorpej 				errx(1, "kvm_read (shmsegs): %s",
    778  1.11   thorpej 				    kvm_geterr(kd));
    779   1.4       cgd 
    780  1.23    simonb 			show_shminfo_hdr();
    781  1.23    simonb 			for (i = 0; i < shminfo.shmmni; i++) {
    782  1.23    simonb 				struct shmid_ds *shmptr = &xshmids[i];
    783  1.23    simonb 				if (shmptr->shm_perm.mode & 0x0800)
    784  1.23    simonb 					show_shminfo(shmptr->shm_atime,
    785  1.23    simonb 					    shmptr->shm_dtime,
    786  1.23    simonb 					    shmptr->shm_ctime,
    787   1.4       cgd 					    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
    788  1.23    simonb 					    shmptr->shm_perm._key,
    789  1.23    simonb 					    shmptr->shm_perm.mode,
    790  1.23    simonb 					    shmptr->shm_perm.uid,
    791  1.23    simonb 					    shmptr->shm_perm.gid,
    792  1.23    simonb 					    shmptr->shm_perm.cuid,
    793  1.23    simonb 					    shmptr->shm_perm.cgid,
    794  1.23    simonb 					    shmptr->shm_nattch,
    795  1.23    simonb 					    shmptr->shm_segsz,
    796  1.23    simonb 					    shmptr->shm_cpid,
    797  1.23    simonb 					    shmptr->shm_lpid);
    798   1.1       cgd 			}
    799   1.4       cgd 			printf("\n");
    800   1.4       cgd 		}
    801   1.4       cgd 	} else
    802   1.4       cgd 		if (display & (SHMINFO | SHMTOTAL)) {
    803   1.4       cgd 			fprintf(stderr,
    804   1.4       cgd 			    "SVID shared memory facility not configured in the system\n");
    805   1.1       cgd 		}
    806   1.4       cgd 	if ((display & (SEMINFO | SEMTOTAL)) &&
    807  1.11   thorpej 	    (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo,
    808  1.11   thorpej 	     sizeof(seminfo)) == sizeof(seminfo))) {
    809   1.4       cgd 		struct semid_ds *xsema;
    810   1.1       cgd 
    811  1.23    simonb 		if (display & SEMTOTAL)
    812  1.23    simonb 			show_semtotal(&seminfo);
    813  1.23    simonb 
    814   1.4       cgd 		if (display & SEMINFO) {
    815  1.11   thorpej 			if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
    816  1.11   thorpej 			    sizeof(sema)) != sizeof(sema))
    817  1.11   thorpej 				errx(1, "kvm_read (%s): %s",
    818  1.11   thorpej 				    symbols[X_SEMA].n_name, kvm_geterr(kd));
    819  1.11   thorpej 
    820  1.11   thorpej 			xsema = malloc(sizeof(struct semid_ds) *
    821  1.11   thorpej 			    seminfo.semmni);
    822  1.11   thorpej 
    823  1.11   thorpej 			if (kvm_read(kd, (u_long)sema, xsema,
    824  1.11   thorpej 			    sizeof(struct semid_ds) * seminfo.semmni) !=
    825  1.11   thorpej 			    sizeof(struct semid_ds) * seminfo.semmni)
    826  1.11   thorpej 				errx(1, "kvm_read (sema): %s",
    827  1.11   thorpej 				    kvm_geterr(kd));
    828   1.4       cgd 
    829  1.23    simonb 			show_seminfo_hdr();
    830  1.23    simonb 			for (i = 0; i < seminfo.semmni; i++) {
    831  1.23    simonb 				struct semid_ds *semaptr = &xsema[i];
    832  1.23    simonb 				if ((semaptr->sem_perm.mode & SEM_ALLOC) != 0)
    833  1.23    simonb 					show_seminfo(semaptr->sem_otime,
    834  1.23    simonb 					    semaptr->sem_ctime,
    835   1.4       cgd 					    IXSEQ_TO_IPCID(i, semaptr->sem_perm),
    836  1.23    simonb 					    semaptr->sem_perm._key,
    837  1.23    simonb 					    semaptr->sem_perm.mode,
    838  1.23    simonb 					    semaptr->sem_perm.uid,
    839  1.23    simonb 					    semaptr->sem_perm.gid,
    840  1.23    simonb 					    semaptr->sem_perm.cuid,
    841  1.23    simonb 					    semaptr->sem_perm.cgid,
    842  1.23    simonb 					    semaptr->sem_nsems);
    843   1.4       cgd 			}
    844   1.1       cgd 
    845   1.4       cgd 			printf("\n");
    846   1.4       cgd 		}
    847   1.4       cgd 	} else
    848   1.4       cgd 		if (display & (SEMINFO | SEMTOTAL)) {
    849   1.4       cgd 			fprintf(stderr, "SVID semaphores facility not configured in the system\n");
    850   1.1       cgd 		}
    851   1.4       cgd 	kvm_close(kd);
    852   1.5       cgd }
    853   1.5       cgd 
    854   1.5       cgd void
    855  1.23    simonb usage(void)
    856   1.5       cgd {
    857   1.5       cgd 
    858   1.5       cgd 	fprintf(stderr,
    859  1.24    simonb 	    "usage: %s [-abcmopqstMQST] [-C corefile] [-N namelist]\n",
    860  1.26       cgd 	    getprogname());
    861   1.5       cgd 	exit(1);
    862   1.1       cgd }
    863