xref: /freebsd/usr.bin/ipcs/ipcs.c (revision 5e3934b1)
11de7b4b8SPedro F. Giffuni /*-
21de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
31de7b4b8SPedro F. Giffuni  *
449fbc2acSDoug Rabson  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
549fbc2acSDoug Rabson  * All rights reserved.
649fbc2acSDoug Rabson  *
749fbc2acSDoug Rabson  * Redistribution and use in source and binary forms, with or without
849fbc2acSDoug Rabson  * modification, are permitted provided that the following conditions
949fbc2acSDoug Rabson  * are met:
1049fbc2acSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1149fbc2acSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1249fbc2acSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1349fbc2acSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1449fbc2acSDoug Rabson  *    documentation and/or other materials provided with the distribution.
1549fbc2acSDoug Rabson  * 3. The name of the author may not be used to endorse or promote products
1649fbc2acSDoug Rabson  *    derived from this software without specific prior written permission.
1749fbc2acSDoug Rabson  *
1849fbc2acSDoug Rabson  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1949fbc2acSDoug Rabson  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2049fbc2acSDoug Rabson  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2149fbc2acSDoug Rabson  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2249fbc2acSDoug Rabson  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2349fbc2acSDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2449fbc2acSDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2549fbc2acSDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2649fbc2acSDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2749fbc2acSDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284816f94eSDoug Rabson  */
294816f94eSDoug Rabson 
304816f94eSDoug Rabson #include <sys/param.h>
314816f94eSDoug Rabson #include <sys/proc.h>
32ab4a4d40SLi-Wen Hsu #define	_WANT_SYSVMSG_INTERNALS
334816f94eSDoug Rabson #include <sys/msg.h>
34ab4a4d40SLi-Wen Hsu #define	_WANT_SYSVSEM_INTERNALS
35ab4a4d40SLi-Wen Hsu #include <sys/sem.h>
36ab4a4d40SLi-Wen Hsu #define	_WANT_SYSVSHM_INTERNALS
37ab4a4d40SLi-Wen Hsu #include <sys/shm.h>
38fa44a292SEdwin Groothuis 
39fa44a292SEdwin Groothuis #include <err.h>
40fa44a292SEdwin Groothuis #include <fcntl.h>
41fa44a292SEdwin Groothuis #include <grp.h>
42fa44a292SEdwin Groothuis #include <kvm.h>
43fa44a292SEdwin Groothuis #include <limits.h>
44fa44a292SEdwin Groothuis #include <pwd.h>
45fa44a292SEdwin Groothuis #include <stdio.h>
46fa44a292SEdwin Groothuis #include <stdlib.h>
47fa44a292SEdwin Groothuis #include <string.h>
48fa44a292SEdwin Groothuis #include <unistd.h>
494816f94eSDoug Rabson 
5055e2cb41SEdwin Groothuis #include "ipc.h"
513012a727SDavid Greenman 
5276e46948SDavid Malone char   *fmt_perm(u_short);
5376e46948SDavid Malone void	cvt_time(time_t, char *);
54f1bb2cd2SWarner Losh void	usage(void);
554d2e8e5fSBosko Milekic uid_t	user2uid(char *username);
5655e2cb41SEdwin Groothuis 
57fa44a292SEdwin Groothuis void	print_kmsqtotal(struct msginfo msginfo);
58fa44a292SEdwin Groothuis void	print_kmsqheader(int option);
59fa44a292SEdwin Groothuis void	print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr);
60fa44a292SEdwin Groothuis void	print_kshmtotal(struct shminfo shminfo);
61fa44a292SEdwin Groothuis void	print_kshmheader(int option);
62fa44a292SEdwin Groothuis void	print_kshmptr(int i, int option, struct shmid_kernel *kshmptr);
63fa44a292SEdwin Groothuis void	print_ksemtotal(struct seminfo seminfo);
64fa44a292SEdwin Groothuis void	print_ksemheader(int option);
65fa44a292SEdwin Groothuis void	print_ksemptr(int i, int option, struct semid_kernel *ksemaptr);
664816f94eSDoug Rabson 
674816f94eSDoug Rabson char   *
fmt_perm(u_short mode)685bcb8532SStefan Farfeleder fmt_perm(u_short mode)
694816f94eSDoug Rabson {
704816f94eSDoug Rabson 	static char buffer[100];
714816f94eSDoug Rabson 
724816f94eSDoug Rabson 	buffer[0] = '-';
734816f94eSDoug Rabson 	buffer[1] = '-';
744816f94eSDoug Rabson 	buffer[2] = ((mode & 0400) ? 'r' : '-');
754816f94eSDoug Rabson 	buffer[3] = ((mode & 0200) ? 'w' : '-');
764816f94eSDoug Rabson 	buffer[4] = ((mode & 0100) ? 'a' : '-');
774816f94eSDoug Rabson 	buffer[5] = ((mode & 0040) ? 'r' : '-');
784816f94eSDoug Rabson 	buffer[6] = ((mode & 0020) ? 'w' : '-');
794816f94eSDoug Rabson 	buffer[7] = ((mode & 0010) ? 'a' : '-');
804816f94eSDoug Rabson 	buffer[8] = ((mode & 0004) ? 'r' : '-');
814816f94eSDoug Rabson 	buffer[9] = ((mode & 0002) ? 'w' : '-');
824816f94eSDoug Rabson 	buffer[10] = ((mode & 0001) ? 'a' : '-');
834816f94eSDoug Rabson 	buffer[11] = '\0';
844816f94eSDoug Rabson 	return (&buffer[0]);
854816f94eSDoug Rabson }
864816f94eSDoug Rabson 
874816f94eSDoug Rabson void
cvt_time(time_t t,char * buf)885bcb8532SStefan Farfeleder cvt_time(time_t t, char *buf)
894816f94eSDoug Rabson {
9049fbc2acSDoug Rabson 	struct tm *tm;
9149fbc2acSDoug Rabson 
924816f94eSDoug Rabson 	if (t == 0) {
9349fbc2acSDoug Rabson 		strcpy(buf, "no-entry");
944816f94eSDoug Rabson 	} else {
9549fbc2acSDoug Rabson 		tm = localtime(&t);
9649fbc2acSDoug Rabson 		sprintf(buf, "%2d:%02d:%02d",
9749fbc2acSDoug Rabson 			tm->tm_hour, tm->tm_min, tm->tm_sec);
984816f94eSDoug Rabson 	}
994816f94eSDoug Rabson }
1004816f94eSDoug Rabson 
10149fbc2acSDoug Rabson #define BIGGEST		1
10249fbc2acSDoug Rabson #define CREATOR		2
10349fbc2acSDoug Rabson #define OUTSTANDING	4
10449fbc2acSDoug Rabson #define PID		8
10549fbc2acSDoug Rabson #define TIME		16
10649fbc2acSDoug Rabson 
10749fbc2acSDoug Rabson int
main(int argc,char * argv[])1085bcb8532SStefan Farfeleder main(int argc, char *argv[])
1094816f94eSDoug Rabson {
11049fbc2acSDoug Rabson 	int     display = SHMINFO | MSGINFO | SEMINFO;
11149fbc2acSDoug Rabson 	int     option = 0;
1124d2e8e5fSBosko Milekic 	char   *core = NULL, *user = NULL, *namelist = NULL;
113b15abeffSDima Dorfman 	char	kvmoferr[_POSIX2_LINE_MAX];  /* Error buf for kvm_openfiles. */
1144816f94eSDoug Rabson 	int     i;
11511f4012fSEitan Adler 	u_long  shmidx;
116fa44a292SEdwin Groothuis 	uid_t   uid = 0;
1174816f94eSDoug Rabson 
1184d2e8e5fSBosko Milekic 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
11949fbc2acSDoug Rabson 		switch (i) {
12049fbc2acSDoug Rabson 		case 'a':
12149fbc2acSDoug Rabson 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
12249fbc2acSDoug Rabson 			break;
12349fbc2acSDoug Rabson 		case 'b':
12449fbc2acSDoug Rabson 			option |= BIGGEST;
12549fbc2acSDoug Rabson 			break;
12649fbc2acSDoug Rabson 		case 'C':
12749fbc2acSDoug Rabson 			core = optarg;
12849fbc2acSDoug Rabson 			break;
12949fbc2acSDoug Rabson 		case 'c':
13049fbc2acSDoug Rabson 			option |= CREATOR;
13149fbc2acSDoug Rabson 			break;
132fa44a292SEdwin Groothuis 		case 'M':
133fa44a292SEdwin Groothuis 			display = SHMTOTAL;
134fa44a292SEdwin Groothuis 			break;
135fa44a292SEdwin Groothuis 		case 'm':
136fa44a292SEdwin Groothuis 			display = SHMINFO;
137fa44a292SEdwin Groothuis 			break;
13849fbc2acSDoug Rabson 		case 'N':
13949fbc2acSDoug Rabson 			namelist = optarg;
14049fbc2acSDoug Rabson 			break;
14149fbc2acSDoug Rabson 		case 'o':
14249fbc2acSDoug Rabson 			option |= OUTSTANDING;
14349fbc2acSDoug Rabson 			break;
14449fbc2acSDoug Rabson 		case 'p':
14549fbc2acSDoug Rabson 			option |= PID;
14649fbc2acSDoug Rabson 			break;
147fa44a292SEdwin Groothuis 		case 'Q':
148fa44a292SEdwin Groothuis 			display = MSGTOTAL;
149fa44a292SEdwin Groothuis 			break;
150fa44a292SEdwin Groothuis 		case 'q':
151fa44a292SEdwin Groothuis 			display = MSGINFO;
152fa44a292SEdwin Groothuis 			break;
153fa44a292SEdwin Groothuis 		case 'S':
154fa44a292SEdwin Groothuis 			display = SEMTOTAL;
155fa44a292SEdwin Groothuis 			break;
156fa44a292SEdwin Groothuis 		case 's':
157fa44a292SEdwin Groothuis 			display = SEMINFO;
158fa44a292SEdwin Groothuis 			break;
15955e2cb41SEdwin Groothuis 		case 'T':
16055e2cb41SEdwin Groothuis 			display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
16155e2cb41SEdwin Groothuis 			break;
16249fbc2acSDoug Rabson 		case 't':
16349fbc2acSDoug Rabson 			option |= TIME;
16449fbc2acSDoug Rabson 			break;
1654d2e8e5fSBosko Milekic 		case 'u':
1664d2e8e5fSBosko Milekic 			user = optarg;
1674d2e8e5fSBosko Milekic 			uid = user2uid(user);
1684d2e8e5fSBosko Milekic 			break;
169fa44a292SEdwin Groothuis 		case 'y':
170fa44a292SEdwin Groothuis 			use_sysctl = 0;
171fa44a292SEdwin Groothuis 			break;
1724816f94eSDoug Rabson 		default:
17349fbc2acSDoug Rabson 			usage();
1744816f94eSDoug Rabson 		}
175661a5e43SPaul Traina 
176661a5e43SPaul Traina 	/*
177b15abeffSDima Dorfman 	 * If paths to the exec file or core file were specified, we
178b15abeffSDima Dorfman 	 * aren't operating on the running kernel, so we can't use
179b15abeffSDima Dorfman 	 * sysctl.
180661a5e43SPaul Traina 	 */
181661a5e43SPaul Traina 	if (namelist != NULL || core != NULL)
182b15abeffSDima Dorfman 		use_sysctl = 0;
183661a5e43SPaul Traina 
184b15abeffSDima Dorfman 	if (!use_sysctl) {
185b15abeffSDima Dorfman 		kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
186b15abeffSDima Dorfman 		if (kd == NULL)
187b15abeffSDima Dorfman 			errx(1, "kvm_openfiles: %s", kvmoferr);
18849fbc2acSDoug Rabson 		switch (kvm_nlist(kd, symbols)) {
18949fbc2acSDoug Rabson 		case 0:
1904816f94eSDoug Rabson 			break;
19149fbc2acSDoug Rabson 		case -1:
19200bbaadcSPhilippe Charnier 			errx(1, "unable to read kernel symbol table");
19349fbc2acSDoug Rabson 		default:
19476e46948SDavid Malone 			break;
1954816f94eSDoug Rabson 		}
196b15abeffSDima Dorfman 	}
1974816f94eSDoug Rabson 
198b15abeffSDima Dorfman 	kget(X_MSGINFO, &msginfo, sizeof(msginfo));
199558fe071SAndriy Voskoboinyk 	if (display & (MSGINFO | MSGTOTAL)) {
200fa44a292SEdwin Groothuis 		if (display & MSGTOTAL)
201fa44a292SEdwin Groothuis 			print_kmsqtotal(msginfo);
202fa44a292SEdwin Groothuis 
203fa44a292SEdwin Groothuis 		if (display & MSGINFO) {
204fa44a292SEdwin Groothuis 			struct msqid_kernel *kxmsqids;
205fa44a292SEdwin Groothuis 			size_t kxmsqids_len;
206fa44a292SEdwin Groothuis 
207fa44a292SEdwin Groothuis 			kxmsqids_len =
208fa44a292SEdwin Groothuis 			    sizeof(struct msqid_kernel) * msginfo.msgmni;
209fa44a292SEdwin Groothuis 			kxmsqids = malloc(kxmsqids_len);
210fa44a292SEdwin Groothuis 			kget(X_MSQIDS, kxmsqids, kxmsqids_len);
211fa44a292SEdwin Groothuis 
212fa44a292SEdwin Groothuis 			print_kmsqheader(option);
213fa44a292SEdwin Groothuis 
214fa44a292SEdwin Groothuis 			for (i = 0; i < msginfo.msgmni; i += 1) {
215fa44a292SEdwin Groothuis 				if (kxmsqids[i].u.msg_qbytes != 0) {
216fa44a292SEdwin Groothuis 					if (user &&
217fa44a292SEdwin Groothuis 					    uid != kxmsqids[i].u.msg_perm.uid)
218fa44a292SEdwin Groothuis 						continue;
219fa44a292SEdwin Groothuis 
220fa44a292SEdwin Groothuis 					print_kmsqptr(i, option, &kxmsqids[i]);
221fa44a292SEdwin Groothuis 				}
222fa44a292SEdwin Groothuis 
223fa44a292SEdwin Groothuis 			}
224fa44a292SEdwin Groothuis 
225fa44a292SEdwin Groothuis 			printf("\n");
226fa44a292SEdwin Groothuis 		}
227fa44a292SEdwin Groothuis 	}
228fa44a292SEdwin Groothuis 
229fa44a292SEdwin Groothuis 	kget(X_SHMINFO, &shminfo, sizeof(shminfo));
230558fe071SAndriy Voskoboinyk 	if (display & (SHMINFO | SHMTOTAL)) {
231fa44a292SEdwin Groothuis 
232fa44a292SEdwin Groothuis 		if (display & SHMTOTAL)
233fa44a292SEdwin Groothuis 			print_kshmtotal(shminfo);
234fa44a292SEdwin Groothuis 
235fa44a292SEdwin Groothuis 		if (display & SHMINFO) {
236fa44a292SEdwin Groothuis 			struct shmid_kernel *kxshmids;
237fa44a292SEdwin Groothuis 			size_t kxshmids_len;
238fa44a292SEdwin Groothuis 
239fa44a292SEdwin Groothuis 			kxshmids_len =
240fa44a292SEdwin Groothuis 			    sizeof(struct shmid_kernel) * shminfo.shmmni;
241fa44a292SEdwin Groothuis 			kxshmids = malloc(kxshmids_len);
242fa44a292SEdwin Groothuis 			kget(X_SHMSEGS, kxshmids, kxshmids_len);
243fa44a292SEdwin Groothuis 
244fa44a292SEdwin Groothuis 			print_kshmheader(option);
245fa44a292SEdwin Groothuis 
24611f4012fSEitan Adler 			for (shmidx = 0; shmidx < shminfo.shmmni; shmidx += 1) {
24711f4012fSEitan Adler 				if (kxshmids[shmidx].u.shm_perm.mode & 0x0800) {
248fa44a292SEdwin Groothuis 					if (user &&
24911f4012fSEitan Adler 					    uid != kxshmids[shmidx].u.shm_perm.uid)
250fa44a292SEdwin Groothuis 						continue;
251fa44a292SEdwin Groothuis 
25211f4012fSEitan Adler 					print_kshmptr(shmidx, option, &kxshmids[shmidx]);
253fa44a292SEdwin Groothuis 				}
254fa44a292SEdwin Groothuis 			}
255fa44a292SEdwin Groothuis 			printf("\n");
256fa44a292SEdwin Groothuis 		}
257fa44a292SEdwin Groothuis 	}
258fa44a292SEdwin Groothuis 
259fa44a292SEdwin Groothuis 	kget(X_SEMINFO, &seminfo, sizeof(seminfo));
260558fe071SAndriy Voskoboinyk 	if (display & (SEMINFO | SEMTOTAL)) {
261fa44a292SEdwin Groothuis 		struct semid_kernel *kxsema;
262fa44a292SEdwin Groothuis 		size_t kxsema_len;
263fa44a292SEdwin Groothuis 
264fa44a292SEdwin Groothuis 		if (display & SEMTOTAL)
265fa44a292SEdwin Groothuis 			print_ksemtotal(seminfo);
266fa44a292SEdwin Groothuis 
267fa44a292SEdwin Groothuis 		if (display & SEMINFO) {
268fa44a292SEdwin Groothuis 			kxsema_len =
269fa44a292SEdwin Groothuis 			    sizeof(struct semid_kernel) * seminfo.semmni;
270fa44a292SEdwin Groothuis 			kxsema = malloc(kxsema_len);
271fa44a292SEdwin Groothuis 			kget(X_SEMA, kxsema, kxsema_len);
272fa44a292SEdwin Groothuis 
273fa44a292SEdwin Groothuis 			print_ksemheader(option);
274fa44a292SEdwin Groothuis 
275fa44a292SEdwin Groothuis 			for (i = 0; i < seminfo.semmni; i += 1) {
27655e2cb41SEdwin Groothuis 				if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
27755e2cb41SEdwin Groothuis 				    != 0) {
278fa44a292SEdwin Groothuis 					if (user &&
279fa44a292SEdwin Groothuis 					    uid != kxsema[i].u.sem_perm.uid)
280fa44a292SEdwin Groothuis 						continue;
281fa44a292SEdwin Groothuis 
282fa44a292SEdwin Groothuis 					print_ksemptr(i, option, &kxsema[i]);
283fa44a292SEdwin Groothuis 
284fa44a292SEdwin Groothuis 				}
285fa44a292SEdwin Groothuis 			}
286fa44a292SEdwin Groothuis 
287fa44a292SEdwin Groothuis 			printf("\n");
288fa44a292SEdwin Groothuis 		}
289fa44a292SEdwin Groothuis 	}
290fa44a292SEdwin Groothuis 
291fa44a292SEdwin Groothuis 	if (!use_sysctl)
292fa44a292SEdwin Groothuis 		kvm_close(kd);
293fa44a292SEdwin Groothuis 
294fa44a292SEdwin Groothuis 	exit(0);
295fa44a292SEdwin Groothuis }
296fa44a292SEdwin Groothuis 
297fa44a292SEdwin Groothuis void
print_kmsqtotal(struct msginfo local_msginfo)29811f4012fSEitan Adler print_kmsqtotal(struct msginfo local_msginfo)
299fa44a292SEdwin Groothuis {
300fa44a292SEdwin Groothuis 
30149fbc2acSDoug Rabson 	printf("msginfo:\n");
302336c393fSGiorgos Keramidas 	printf("\tmsgmax: %12d\t(max characters in a message)\n",
30311f4012fSEitan Adler 	    local_msginfo.msgmax);
304336c393fSGiorgos Keramidas 	printf("\tmsgmni: %12d\t(# of message queues)\n",
30511f4012fSEitan Adler 	    local_msginfo.msgmni);
306336c393fSGiorgos Keramidas 	printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
30711f4012fSEitan Adler 	    local_msginfo.msgmnb);
308336c393fSGiorgos Keramidas 	printf("\tmsgtql: %12d\t(max # of messages in system)\n",
30911f4012fSEitan Adler 	    local_msginfo.msgtql);
310336c393fSGiorgos Keramidas 	printf("\tmsgssz: %12d\t(size of a message segment)\n",
31111f4012fSEitan Adler 	    local_msginfo.msgssz);
312336c393fSGiorgos Keramidas 	printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
31311f4012fSEitan Adler 	    local_msginfo.msgseg);
3144816f94eSDoug Rabson }
3154816f94eSDoug Rabson 
print_kmsqheader(int option)31655e2cb41SEdwin Groothuis void print_kmsqheader(int option)
31755e2cb41SEdwin Groothuis {
3184816f94eSDoug Rabson 
31949fbc2acSDoug Rabson 	printf("Message Queues:\n");
320fa44a292SEdwin Groothuis 	printf("T %12s %12s %-11s %-8s %-8s",
321fa44a292SEdwin Groothuis 	    "ID", "KEY", "MODE", "OWNER", "GROUP");
32249fbc2acSDoug Rabson 	if (option & CREATOR)
323336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "CREATOR", "CGROUP");
32449fbc2acSDoug Rabson 	if (option & OUTSTANDING)
325336c393fSGiorgos Keramidas 		printf(" %20s %20s", "CBYTES", "QNUM");
32649fbc2acSDoug Rabson 	if (option & BIGGEST)
327336c393fSGiorgos Keramidas 		printf(" %20s", "QBYTES");
32849fbc2acSDoug Rabson 	if (option & PID)
329336c393fSGiorgos Keramidas 		printf(" %12s %12s", "LSPID", "LRPID");
33049fbc2acSDoug Rabson 	if (option & TIME)
331336c393fSGiorgos Keramidas 		printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
33249fbc2acSDoug Rabson 	printf("\n");
333fa44a292SEdwin Groothuis }
3344816f94eSDoug Rabson 
335fa44a292SEdwin Groothuis void
print_kmsqptr(int i,int option,struct msqid_kernel * kmsqptr)336fa44a292SEdwin Groothuis print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
337fa44a292SEdwin Groothuis {
338fa44a292SEdwin Groothuis 	char    stime_buf[100], rtime_buf[100], ctime_buf[100];
339fa44a292SEdwin Groothuis 
34075d6abdbSRobert Watson 	cvt_time(kmsqptr->u.msg_stime, stime_buf);
34175d6abdbSRobert Watson 	cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
34275d6abdbSRobert Watson 	cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
3434816f94eSDoug Rabson 
34455e2cb41SEdwin Groothuis 	printf("q %12d %12d %s %-8s %-8s",
34575d6abdbSRobert Watson 	    IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
34675d6abdbSRobert Watson 	    (int)kmsqptr->u.msg_perm.key,
34775d6abdbSRobert Watson 	    fmt_perm(kmsqptr->u.msg_perm.mode),
34875d6abdbSRobert Watson 	    user_from_uid(kmsqptr->u.msg_perm.uid, 0),
34975d6abdbSRobert Watson 	    group_from_gid(kmsqptr->u.msg_perm.gid, 0));
3504816f94eSDoug Rabson 
35149fbc2acSDoug Rabson 	if (option & CREATOR)
35255e2cb41SEdwin Groothuis 		printf(" %-8s %-8s",
35375d6abdbSRobert Watson 		    user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
35475d6abdbSRobert Watson 		    group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
3554816f94eSDoug Rabson 
35649fbc2acSDoug Rabson 	if (option & OUTSTANDING)
357336c393fSGiorgos Keramidas 		printf(" %12lu %12lu",
35875d6abdbSRobert Watson 		    kmsqptr->u.msg_cbytes,
35975d6abdbSRobert Watson 		    kmsqptr->u.msg_qnum);
3604816f94eSDoug Rabson 
36149fbc2acSDoug Rabson 	if (option & BIGGEST)
362fa44a292SEdwin Groothuis 		printf(" %20lu", kmsqptr->u.msg_qbytes);
3634816f94eSDoug Rabson 
36449fbc2acSDoug Rabson 	if (option & PID)
365336c393fSGiorgos Keramidas 		printf(" %12d %12d",
36675d6abdbSRobert Watson 		    kmsqptr->u.msg_lspid,
36775d6abdbSRobert Watson 		    kmsqptr->u.msg_lrpid);
3684816f94eSDoug Rabson 
36949fbc2acSDoug Rabson 	if (option & TIME)
37049fbc2acSDoug Rabson 		printf(" %s %s %s",
37149fbc2acSDoug Rabson 		    stime_buf,
37249fbc2acSDoug Rabson 		    rtime_buf,
37349fbc2acSDoug Rabson 		    ctime_buf);
3744816f94eSDoug Rabson 
37549fbc2acSDoug Rabson 	printf("\n");
37649fbc2acSDoug Rabson }
377b15abeffSDima Dorfman 
378fa44a292SEdwin Groothuis void
print_kshmtotal(struct shminfo local_shminfo)37911f4012fSEitan Adler print_kshmtotal(struct shminfo local_shminfo)
380fa44a292SEdwin Groothuis {
381fa44a292SEdwin Groothuis 
38249fbc2acSDoug Rabson 	printf("shminfo:\n");
38349f8bb9aSRuslan Ermilov 	printf("\tshmmax: %12lu\t(max shared memory segment size)\n",
38411f4012fSEitan Adler 	    local_shminfo.shmmax);
38549f8bb9aSRuslan Ermilov 	printf("\tshmmin: %12lu\t(min shared memory segment size)\n",
38611f4012fSEitan Adler 	    local_shminfo.shmmin);
38749f8bb9aSRuslan Ermilov 	printf("\tshmmni: %12lu\t(max number of shared memory identifiers)\n",
38811f4012fSEitan Adler 	    local_shminfo.shmmni);
38949f8bb9aSRuslan Ermilov 	printf("\tshmseg: %12lu\t(max shared memory segments per process)\n",
39011f4012fSEitan Adler 	    local_shminfo.shmseg);
39149f8bb9aSRuslan Ermilov 	printf("\tshmall: %12lu\t(max amount of shared memory in pages)\n\n",
39211f4012fSEitan Adler 	    local_shminfo.shmall);
39349fbc2acSDoug Rabson }
39449fbc2acSDoug Rabson 
395fa44a292SEdwin Groothuis void
print_kshmheader(int option)396fa44a292SEdwin Groothuis print_kshmheader(int option)
397fa44a292SEdwin Groothuis {
39849fbc2acSDoug Rabson 
39949fbc2acSDoug Rabson 	printf("Shared Memory:\n");
400fa44a292SEdwin Groothuis 	printf("T %12s %12s %-11s %-8s %-8s",
401fa44a292SEdwin Groothuis 	    "ID", "KEY", "MODE", "OWNER", "GROUP");
40249fbc2acSDoug Rabson 	if (option & CREATOR)
403336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "CREATOR", "CGROUP");
40449fbc2acSDoug Rabson 	if (option & OUTSTANDING)
405336c393fSGiorgos Keramidas 		printf(" %12s", "NATTCH");
40649fbc2acSDoug Rabson 	if (option & BIGGEST)
407336c393fSGiorgos Keramidas 		printf(" %12s", "SEGSZ");
40849fbc2acSDoug Rabson 	if (option & PID)
409336c393fSGiorgos Keramidas 		printf(" %12s %12s", "CPID", "LPID");
41049fbc2acSDoug Rabson 	if (option & TIME)
411336c393fSGiorgos Keramidas 		printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
41249fbc2acSDoug Rabson 	printf("\n");
413fa44a292SEdwin Groothuis }
41449fbc2acSDoug Rabson 
415fa44a292SEdwin Groothuis void
print_kshmptr(int i,int option,struct shmid_kernel * kshmptr)416fa44a292SEdwin Groothuis print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
417fa44a292SEdwin Groothuis {
418fa44a292SEdwin Groothuis 	char    atime_buf[100], dtime_buf[100], ctime_buf[100];
419fa44a292SEdwin Groothuis 
42075d6abdbSRobert Watson 	cvt_time(kshmptr->u.shm_atime, atime_buf);
42175d6abdbSRobert Watson 	cvt_time(kshmptr->u.shm_dtime, dtime_buf);
42275d6abdbSRobert Watson 	cvt_time(kshmptr->u.shm_ctime, ctime_buf);
42349fbc2acSDoug Rabson 
42455e2cb41SEdwin Groothuis 	printf("m %12d %12d %s %-8s %-8s",
42575d6abdbSRobert Watson 	    IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
42675d6abdbSRobert Watson 	    (int)kshmptr->u.shm_perm.key,
42775d6abdbSRobert Watson 	    fmt_perm(kshmptr->u.shm_perm.mode),
42875d6abdbSRobert Watson 	    user_from_uid(kshmptr->u.shm_perm.uid, 0),
42975d6abdbSRobert Watson 	    group_from_gid(kshmptr->u.shm_perm.gid, 0));
43049fbc2acSDoug Rabson 
43149fbc2acSDoug Rabson 	if (option & CREATOR)
43255e2cb41SEdwin Groothuis 		printf(" %-8s %-8s",
43375d6abdbSRobert Watson 		    user_from_uid(kshmptr->u.shm_perm.cuid, 0),
43475d6abdbSRobert Watson 		    group_from_gid(kshmptr->u.shm_perm.cgid, 0));
43549fbc2acSDoug Rabson 
43649fbc2acSDoug Rabson 	if (option & OUTSTANDING)
437336c393fSGiorgos Keramidas 		printf(" %12d",
43875d6abdbSRobert Watson 		    kshmptr->u.shm_nattch);
43949fbc2acSDoug Rabson 
44049fbc2acSDoug Rabson 	if (option & BIGGEST)
44165067cc8SKonstantin Belousov 		printf(" %12zu",
442b648d480SJohn Baldwin 		    kshmptr->u.shm_segsz);
44349fbc2acSDoug Rabson 
44449fbc2acSDoug Rabson 	if (option & PID)
445336c393fSGiorgos Keramidas 		printf(" %12d %12d",
44675d6abdbSRobert Watson 		    kshmptr->u.shm_cpid,
44775d6abdbSRobert Watson 		    kshmptr->u.shm_lpid);
44849fbc2acSDoug Rabson 
44949fbc2acSDoug Rabson 	if (option & TIME)
45049fbc2acSDoug Rabson 		printf(" %s %s %s",
45149fbc2acSDoug Rabson 		    atime_buf,
45249fbc2acSDoug Rabson 		    dtime_buf,
45349fbc2acSDoug Rabson 		    ctime_buf);
45449fbc2acSDoug Rabson 
45549fbc2acSDoug Rabson 	printf("\n");
45649fbc2acSDoug Rabson }
457b15abeffSDima Dorfman 
458fa44a292SEdwin Groothuis void
print_ksemtotal(struct seminfo local_seminfo)45911f4012fSEitan Adler print_ksemtotal(struct seminfo local_seminfo)
460fa44a292SEdwin Groothuis {
46149fbc2acSDoug Rabson 
46249fbc2acSDoug Rabson 	printf("seminfo:\n");
463336c393fSGiorgos Keramidas 	printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
46411f4012fSEitan Adler 	    local_seminfo.semmni);
465336c393fSGiorgos Keramidas 	printf("\tsemmns: %12d\t(# of semaphores in system)\n",
46611f4012fSEitan Adler 	    local_seminfo.semmns);
467336c393fSGiorgos Keramidas 	printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
46811f4012fSEitan Adler 	    local_seminfo.semmnu);
469336c393fSGiorgos Keramidas 	printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
47011f4012fSEitan Adler 	    local_seminfo.semmsl);
471336c393fSGiorgos Keramidas 	printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
47211f4012fSEitan Adler 	    local_seminfo.semopm);
473336c393fSGiorgos Keramidas 	printf("\tsemume: %12d\t(max # of undo entries per process)\n",
47411f4012fSEitan Adler 	    local_seminfo.semume);
475336c393fSGiorgos Keramidas 	printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
47611f4012fSEitan Adler 	    local_seminfo.semusz);
477336c393fSGiorgos Keramidas 	printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
47811f4012fSEitan Adler 	    local_seminfo.semvmx);
479336c393fSGiorgos Keramidas 	printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
48011f4012fSEitan Adler 	    local_seminfo.semaem);
48149fbc2acSDoug Rabson }
482fa44a292SEdwin Groothuis 
483fa44a292SEdwin Groothuis void
print_ksemheader(int option)48455e2cb41SEdwin Groothuis print_ksemheader(int option)
48555e2cb41SEdwin Groothuis {
48649fbc2acSDoug Rabson 
48749fbc2acSDoug Rabson 	printf("Semaphores:\n");
488fa44a292SEdwin Groothuis 	printf("T %12s %12s %-11s %-8s %-8s",
489fa44a292SEdwin Groothuis 	    "ID", "KEY", "MODE", "OWNER", "GROUP");
49049fbc2acSDoug Rabson 	if (option & CREATOR)
491336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "CREATOR", "CGROUP");
49249fbc2acSDoug Rabson 	if (option & BIGGEST)
493336c393fSGiorgos Keramidas 		printf(" %12s", "NSEMS");
49449fbc2acSDoug Rabson 	if (option & TIME)
495336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "OTIME", "CTIME");
49649fbc2acSDoug Rabson 	printf("\n");
497fa44a292SEdwin Groothuis }
49849fbc2acSDoug Rabson 
499fa44a292SEdwin Groothuis void
print_ksemptr(int i,int option,struct semid_kernel * ksemaptr)500fa44a292SEdwin Groothuis print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
501fa44a292SEdwin Groothuis {
502fa44a292SEdwin Groothuis 	char    ctime_buf[100], otime_buf[100];
503fa44a292SEdwin Groothuis 
50475d6abdbSRobert Watson 	cvt_time(ksemaptr->u.sem_otime, otime_buf);
50575d6abdbSRobert Watson 	cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
50649fbc2acSDoug Rabson 
50755e2cb41SEdwin Groothuis 	printf("s %12d %12d %s %-8s %-8s",
50875d6abdbSRobert Watson 	    IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
50975d6abdbSRobert Watson 	    (int)ksemaptr->u.sem_perm.key,
51075d6abdbSRobert Watson 	    fmt_perm(ksemaptr->u.sem_perm.mode),
51175d6abdbSRobert Watson 	    user_from_uid(ksemaptr->u.sem_perm.uid, 0),
51275d6abdbSRobert Watson 	    group_from_gid(ksemaptr->u.sem_perm.gid, 0));
51349fbc2acSDoug Rabson 
51449fbc2acSDoug Rabson 	if (option & CREATOR)
51555e2cb41SEdwin Groothuis 		printf(" %-8s %-8s",
51675d6abdbSRobert Watson 		    user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
51775d6abdbSRobert Watson 		    group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
51849fbc2acSDoug Rabson 
51949fbc2acSDoug Rabson 	if (option & BIGGEST)
520336c393fSGiorgos Keramidas 		printf(" %12d",
52175d6abdbSRobert Watson 		    ksemaptr->u.sem_nsems);
52249fbc2acSDoug Rabson 
52349fbc2acSDoug Rabson 	if (option & TIME)
52449fbc2acSDoug Rabson 		printf(" %s %s",
52549fbc2acSDoug Rabson 		    otime_buf,
52649fbc2acSDoug Rabson 		    ctime_buf);
52749fbc2acSDoug Rabson 
52849fbc2acSDoug Rabson 	printf("\n");
5294816f94eSDoug Rabson }
53049fbc2acSDoug Rabson 
5314d2e8e5fSBosko Milekic uid_t
user2uid(char * username)5324d2e8e5fSBosko Milekic user2uid(char *username)
5334d2e8e5fSBosko Milekic {
5344d2e8e5fSBosko Milekic 	struct passwd *pwd;
5354d2e8e5fSBosko Milekic 	uid_t uid;
5364d2e8e5fSBosko Milekic 	char *r;
5374d2e8e5fSBosko Milekic 
5384d2e8e5fSBosko Milekic 	uid = strtoul(username, &r, 0);
5394d2e8e5fSBosko Milekic 	if (!*r && r != username)
5404d2e8e5fSBosko Milekic 		return (uid);
5414d2e8e5fSBosko Milekic 	if ((pwd = getpwnam(username)) == NULL)
5424d2e8e5fSBosko Milekic 		errx(1, "getpwnam failed: No such user");
5434d2e8e5fSBosko Milekic 	endpwent();
5444d2e8e5fSBosko Milekic 	return (pwd->pw_uid);
5454d2e8e5fSBosko Milekic }
5464d2e8e5fSBosko Milekic 
547b15abeffSDima Dorfman void
usage(void)5485bcb8532SStefan Farfeleder usage(void)
54949fbc2acSDoug Rabson {
55049fbc2acSDoug Rabson 
55149fbc2acSDoug Rabson 	fprintf(stderr,
552fa44a292SEdwin Groothuis 	    "usage: "
553fa44a292SEdwin Groothuis 	    "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");
55449fbc2acSDoug Rabson 	exit(1);
55549fbc2acSDoug Rabson }
556