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