xref: /freebsd/usr.bin/ipcs/ipcs.c (revision 55e2cb41f216b713fb865aaf392ddb17ebbd57d1)
14816f94eSDoug Rabson /*
249fbc2acSDoug Rabson  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
349fbc2acSDoug Rabson  * All rights reserved.
449fbc2acSDoug Rabson  *
549fbc2acSDoug Rabson  * Redistribution and use in source and binary forms, with or without
649fbc2acSDoug Rabson  * modification, are permitted provided that the following conditions
749fbc2acSDoug Rabson  * are met:
849fbc2acSDoug Rabson  * 1. Redistributions of source code must retain the above copyright
949fbc2acSDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1049fbc2acSDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1149fbc2acSDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1249fbc2acSDoug Rabson  *    documentation and/or other materials provided with the distribution.
1349fbc2acSDoug Rabson  * 3. The name of the author may not be used to endorse or promote products
1449fbc2acSDoug Rabson  *    derived from this software without specific prior written permission.
1549fbc2acSDoug Rabson  *
1649fbc2acSDoug Rabson  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1749fbc2acSDoug Rabson  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
1849fbc2acSDoug Rabson  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
1949fbc2acSDoug Rabson  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2049fbc2acSDoug Rabson  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2149fbc2acSDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2249fbc2acSDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2349fbc2acSDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2449fbc2acSDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2549fbc2acSDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264816f94eSDoug Rabson  */
274816f94eSDoug Rabson 
28e026a48cSDavid E. O'Brien #include <sys/cdefs.h>
29e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$");
3000bbaadcSPhilippe Charnier 
314816f94eSDoug Rabson #include <sys/param.h>
324816f94eSDoug Rabson #include <sys/proc.h>
33c4473420SPeter Wemm #define _KERNEL
344816f94eSDoug Rabson #include <sys/sem.h>
354816f94eSDoug Rabson #include <sys/shm.h>
364816f94eSDoug Rabson #include <sys/msg.h>
37fa44a292SEdwin Groothuis #undef _KERNEL
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   *
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
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
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;
115fa44a292SEdwin Groothuis 	uid_t   uid = 0;
1164816f94eSDoug Rabson 
1174d2e8e5fSBosko Milekic 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
11849fbc2acSDoug Rabson 		switch (i) {
11949fbc2acSDoug Rabson 		case 'a':
12049fbc2acSDoug Rabson 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
12149fbc2acSDoug Rabson 			break;
12249fbc2acSDoug Rabson 		case 'b':
12349fbc2acSDoug Rabson 			option |= BIGGEST;
12449fbc2acSDoug Rabson 			break;
12549fbc2acSDoug Rabson 		case 'C':
12649fbc2acSDoug Rabson 			core = optarg;
12749fbc2acSDoug Rabson 			break;
12849fbc2acSDoug Rabson 		case 'c':
12949fbc2acSDoug Rabson 			option |= CREATOR;
13049fbc2acSDoug Rabson 			break;
131fa44a292SEdwin Groothuis 		case 'M':
132fa44a292SEdwin Groothuis 			display = SHMTOTAL;
133fa44a292SEdwin Groothuis 			break;
134fa44a292SEdwin Groothuis 		case 'm':
135fa44a292SEdwin Groothuis 			display = SHMINFO;
136fa44a292SEdwin Groothuis 			break;
13749fbc2acSDoug Rabson 		case 'N':
13849fbc2acSDoug Rabson 			namelist = optarg;
13949fbc2acSDoug Rabson 			break;
14049fbc2acSDoug Rabson 		case 'o':
14149fbc2acSDoug Rabson 			option |= OUTSTANDING;
14249fbc2acSDoug Rabson 			break;
14349fbc2acSDoug Rabson 		case 'p':
14449fbc2acSDoug Rabson 			option |= PID;
14549fbc2acSDoug Rabson 			break;
146fa44a292SEdwin Groothuis 		case 'Q':
147fa44a292SEdwin Groothuis 			display = MSGTOTAL;
148fa44a292SEdwin Groothuis 			break;
149fa44a292SEdwin Groothuis 		case 'q':
150fa44a292SEdwin Groothuis 			display = MSGINFO;
151fa44a292SEdwin Groothuis 			break;
152fa44a292SEdwin Groothuis 		case 'S':
153fa44a292SEdwin Groothuis 			display = SEMTOTAL;
154fa44a292SEdwin Groothuis 			break;
155fa44a292SEdwin Groothuis 		case 's':
156fa44a292SEdwin Groothuis 			display = SEMINFO;
157fa44a292SEdwin Groothuis 			break;
15855e2cb41SEdwin Groothuis 		case 'T':
15955e2cb41SEdwin Groothuis 			display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
16055e2cb41SEdwin Groothuis 			break;
16149fbc2acSDoug Rabson 		case 't':
16249fbc2acSDoug Rabson 			option |= TIME;
16349fbc2acSDoug Rabson 			break;
1644d2e8e5fSBosko Milekic 		case 'u':
1654d2e8e5fSBosko Milekic 			user = optarg;
1664d2e8e5fSBosko Milekic 			uid = user2uid(user);
1674d2e8e5fSBosko Milekic 			break;
168fa44a292SEdwin Groothuis 		case 'y':
169fa44a292SEdwin Groothuis 			use_sysctl = 0;
170fa44a292SEdwin Groothuis 			break;
1714816f94eSDoug Rabson 		default:
17249fbc2acSDoug Rabson 			usage();
1734816f94eSDoug Rabson 		}
174661a5e43SPaul Traina 
175661a5e43SPaul Traina 	/*
176b15abeffSDima Dorfman 	 * If paths to the exec file or core file were specified, we
177b15abeffSDima Dorfman 	 * aren't operating on the running kernel, so we can't use
178b15abeffSDima Dorfman 	 * sysctl.
179661a5e43SPaul Traina 	 */
180661a5e43SPaul Traina 	if (namelist != NULL || core != NULL)
181b15abeffSDima Dorfman 		use_sysctl = 0;
182661a5e43SPaul Traina 
183b15abeffSDima Dorfman 	if (!use_sysctl) {
184b15abeffSDima Dorfman 		kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
185b15abeffSDima Dorfman 		if (kd == NULL)
186b15abeffSDima Dorfman 			errx(1, "kvm_openfiles: %s", kvmoferr);
18749fbc2acSDoug Rabson 		switch (kvm_nlist(kd, symbols)) {
18849fbc2acSDoug Rabson 		case 0:
1894816f94eSDoug Rabson 			break;
19049fbc2acSDoug Rabson 		case -1:
19100bbaadcSPhilippe Charnier 			errx(1, "unable to read kernel symbol table");
19249fbc2acSDoug Rabson 		default:
19376e46948SDavid Malone 			break;
1944816f94eSDoug Rabson 		}
195b15abeffSDima Dorfman 	}
1964816f94eSDoug Rabson 
197b15abeffSDima Dorfman 	kget(X_MSGINFO, &msginfo, sizeof(msginfo));
198b15abeffSDima Dorfman 	if ((display & (MSGINFO | MSGTOTAL))) {
199fa44a292SEdwin Groothuis 		if (display & MSGTOTAL)
200fa44a292SEdwin Groothuis 			print_kmsqtotal(msginfo);
201fa44a292SEdwin Groothuis 
202fa44a292SEdwin Groothuis 		if (display & MSGINFO) {
203fa44a292SEdwin Groothuis 			struct msqid_kernel *kxmsqids;
204fa44a292SEdwin Groothuis 			size_t kxmsqids_len;
205fa44a292SEdwin Groothuis 
206fa44a292SEdwin Groothuis 			kxmsqids_len =
207fa44a292SEdwin Groothuis 			    sizeof(struct msqid_kernel) * msginfo.msgmni;
208fa44a292SEdwin Groothuis 			kxmsqids = malloc(kxmsqids_len);
209fa44a292SEdwin Groothuis 			kget(X_MSQIDS, kxmsqids, kxmsqids_len);
210fa44a292SEdwin Groothuis 
211fa44a292SEdwin Groothuis 			print_kmsqheader(option);
212fa44a292SEdwin Groothuis 
213fa44a292SEdwin Groothuis 			for (i = 0; i < msginfo.msgmni; i += 1) {
214fa44a292SEdwin Groothuis 				if (kxmsqids[i].u.msg_qbytes != 0) {
215fa44a292SEdwin Groothuis 					if (user &&
216fa44a292SEdwin Groothuis 					    uid != kxmsqids[i].u.msg_perm.uid)
217fa44a292SEdwin Groothuis 						continue;
218fa44a292SEdwin Groothuis 
219fa44a292SEdwin Groothuis 					print_kmsqptr(i, option, &kxmsqids[i]);
220fa44a292SEdwin Groothuis 				}
221fa44a292SEdwin Groothuis 
222fa44a292SEdwin Groothuis 			}
223fa44a292SEdwin Groothuis 
224fa44a292SEdwin Groothuis 			printf("\n");
225fa44a292SEdwin Groothuis 		}
226fa44a292SEdwin Groothuis 	} else
227fa44a292SEdwin Groothuis 		if (display & (MSGINFO | MSGTOTAL)) {
228fa44a292SEdwin Groothuis 			fprintf(stderr,
229fa44a292SEdwin Groothuis 			    "SVID messages facility "
230fa44a292SEdwin Groothuis 			    "not configured in the system\n");
231fa44a292SEdwin Groothuis 		}
232fa44a292SEdwin Groothuis 
233fa44a292SEdwin Groothuis 	kget(X_SHMINFO, &shminfo, sizeof(shminfo));
234fa44a292SEdwin Groothuis 	if ((display & (SHMINFO | SHMTOTAL))) {
235fa44a292SEdwin Groothuis 
236fa44a292SEdwin Groothuis 		if (display & SHMTOTAL)
237fa44a292SEdwin Groothuis 			print_kshmtotal(shminfo);
238fa44a292SEdwin Groothuis 
239fa44a292SEdwin Groothuis 		if (display & SHMINFO) {
240fa44a292SEdwin Groothuis 			struct shmid_kernel *kxshmids;
241fa44a292SEdwin Groothuis 			size_t kxshmids_len;
242fa44a292SEdwin Groothuis 
243fa44a292SEdwin Groothuis 			kxshmids_len =
244fa44a292SEdwin Groothuis 			    sizeof(struct shmid_kernel) * shminfo.shmmni;
245fa44a292SEdwin Groothuis 			kxshmids = malloc(kxshmids_len);
246fa44a292SEdwin Groothuis 			kget(X_SHMSEGS, kxshmids, kxshmids_len);
247fa44a292SEdwin Groothuis 
248fa44a292SEdwin Groothuis 			print_kshmheader(option);
249fa44a292SEdwin Groothuis 
250fa44a292SEdwin Groothuis 			for (i = 0; i < shminfo.shmmni; i += 1) {
251fa44a292SEdwin Groothuis 				if (kxshmids[i].u.shm_perm.mode & 0x0800) {
252fa44a292SEdwin Groothuis 					if (user &&
253fa44a292SEdwin Groothuis 					    uid != kxshmids[i].u.shm_perm.uid)
254fa44a292SEdwin Groothuis 						continue;
255fa44a292SEdwin Groothuis 
256fa44a292SEdwin Groothuis 					print_kshmptr(i, option, &kxshmids[i]);
257fa44a292SEdwin Groothuis 				}
258fa44a292SEdwin Groothuis 			}
259fa44a292SEdwin Groothuis 			printf("\n");
260fa44a292SEdwin Groothuis 		}
261fa44a292SEdwin Groothuis 	} else
262fa44a292SEdwin Groothuis 		if (display & (SHMINFO | SHMTOTAL)) {
263fa44a292SEdwin Groothuis 			fprintf(stderr,
264fa44a292SEdwin Groothuis 			    "SVID shared memory facility "
265fa44a292SEdwin Groothuis 			    "not configured in the system\n");
266fa44a292SEdwin Groothuis 		}
267fa44a292SEdwin Groothuis 
268fa44a292SEdwin Groothuis 	kget(X_SEMINFO, &seminfo, sizeof(seminfo));
269fa44a292SEdwin Groothuis 	if ((display & (SEMINFO | SEMTOTAL))) {
270fa44a292SEdwin Groothuis 		struct semid_kernel *kxsema;
271fa44a292SEdwin Groothuis 		size_t kxsema_len;
272fa44a292SEdwin Groothuis 
273fa44a292SEdwin Groothuis 		if (display & SEMTOTAL)
274fa44a292SEdwin Groothuis 			print_ksemtotal(seminfo);
275fa44a292SEdwin Groothuis 
276fa44a292SEdwin Groothuis 		if (display & SEMINFO) {
277fa44a292SEdwin Groothuis 			kxsema_len =
278fa44a292SEdwin Groothuis 			    sizeof(struct semid_kernel) * seminfo.semmni;
279fa44a292SEdwin Groothuis 			kxsema = malloc(kxsema_len);
280fa44a292SEdwin Groothuis 			kget(X_SEMA, kxsema, kxsema_len);
281fa44a292SEdwin Groothuis 
282fa44a292SEdwin Groothuis 			print_ksemheader(option);
283fa44a292SEdwin Groothuis 
284fa44a292SEdwin Groothuis 			for (i = 0; i < seminfo.semmni; i += 1) {
28555e2cb41SEdwin Groothuis 				if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC)
28655e2cb41SEdwin Groothuis 				    != 0) {
287fa44a292SEdwin Groothuis 					if (user &&
288fa44a292SEdwin Groothuis 					    uid != kxsema[i].u.sem_perm.uid)
289fa44a292SEdwin Groothuis 						continue;
290fa44a292SEdwin Groothuis 
291fa44a292SEdwin Groothuis 					print_ksemptr(i, option, &kxsema[i]);
292fa44a292SEdwin Groothuis 
293fa44a292SEdwin Groothuis 				}
294fa44a292SEdwin Groothuis 			}
295fa44a292SEdwin Groothuis 
296fa44a292SEdwin Groothuis 			printf("\n");
297fa44a292SEdwin Groothuis 		}
298fa44a292SEdwin Groothuis 	} else
299fa44a292SEdwin Groothuis 		if (display & (SEMINFO | SEMTOTAL)) {
300fa44a292SEdwin Groothuis 			fprintf(stderr,
301fa44a292SEdwin Groothuis 			    "SVID semaphores facility "
302fa44a292SEdwin Groothuis 			    "not configured in the system\n");
303fa44a292SEdwin Groothuis 		}
304fa44a292SEdwin Groothuis 
305fa44a292SEdwin Groothuis 	if (!use_sysctl)
306fa44a292SEdwin Groothuis 		kvm_close(kd);
307fa44a292SEdwin Groothuis 
308fa44a292SEdwin Groothuis 	exit(0);
309fa44a292SEdwin Groothuis }
310fa44a292SEdwin Groothuis 
311fa44a292SEdwin Groothuis void
312fa44a292SEdwin Groothuis print_kmsqtotal(struct msginfo msginfo)
313fa44a292SEdwin Groothuis {
314fa44a292SEdwin Groothuis 
31549fbc2acSDoug Rabson 	printf("msginfo:\n");
316336c393fSGiorgos Keramidas 	printf("\tmsgmax: %12d\t(max characters in a message)\n",
31749fbc2acSDoug Rabson 	    msginfo.msgmax);
318336c393fSGiorgos Keramidas 	printf("\tmsgmni: %12d\t(# of message queues)\n",
31949fbc2acSDoug Rabson 	    msginfo.msgmni);
320336c393fSGiorgos Keramidas 	printf("\tmsgmnb: %12d\t(max characters in a message queue)\n",
32149fbc2acSDoug Rabson 	    msginfo.msgmnb);
322336c393fSGiorgos Keramidas 	printf("\tmsgtql: %12d\t(max # of messages in system)\n",
32349fbc2acSDoug Rabson 	    msginfo.msgtql);
324336c393fSGiorgos Keramidas 	printf("\tmsgssz: %12d\t(size of a message segment)\n",
32549fbc2acSDoug Rabson 	    msginfo.msgssz);
326336c393fSGiorgos Keramidas 	printf("\tmsgseg: %12d\t(# of message segments in system)\n\n",
32749fbc2acSDoug Rabson 	    msginfo.msgseg);
3284816f94eSDoug Rabson }
3294816f94eSDoug Rabson 
33055e2cb41SEdwin Groothuis void print_kmsqheader(int option)
33155e2cb41SEdwin Groothuis {
3324816f94eSDoug Rabson 
33349fbc2acSDoug Rabson 	printf("Message Queues:\n");
334fa44a292SEdwin Groothuis 	printf("T %12s %12s %-11s %-8s %-8s",
335fa44a292SEdwin Groothuis 	    "ID", "KEY", "MODE", "OWNER", "GROUP");
33649fbc2acSDoug Rabson 	if (option & CREATOR)
337336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "CREATOR", "CGROUP");
33849fbc2acSDoug Rabson 	if (option & OUTSTANDING)
339336c393fSGiorgos Keramidas 		printf(" %20s %20s", "CBYTES", "QNUM");
34049fbc2acSDoug Rabson 	if (option & BIGGEST)
341336c393fSGiorgos Keramidas 		printf(" %20s", "QBYTES");
34249fbc2acSDoug Rabson 	if (option & PID)
343336c393fSGiorgos Keramidas 		printf(" %12s %12s", "LSPID", "LRPID");
34449fbc2acSDoug Rabson 	if (option & TIME)
345336c393fSGiorgos Keramidas 		printf(" %-8s %-8s %-8s", "STIME", "RTIME", "CTIME");
34649fbc2acSDoug Rabson 	printf("\n");
347fa44a292SEdwin Groothuis }
3484816f94eSDoug Rabson 
349fa44a292SEdwin Groothuis void
350fa44a292SEdwin Groothuis print_kmsqptr(int i, int option, struct msqid_kernel *kmsqptr)
351fa44a292SEdwin Groothuis {
352fa44a292SEdwin Groothuis 	char    stime_buf[100], rtime_buf[100], ctime_buf[100];
353fa44a292SEdwin Groothuis 
35475d6abdbSRobert Watson 	cvt_time(kmsqptr->u.msg_stime, stime_buf);
35575d6abdbSRobert Watson 	cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
35675d6abdbSRobert Watson 	cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
3574816f94eSDoug Rabson 
35855e2cb41SEdwin Groothuis 	printf("q %12d %12d %s %-8s %-8s",
35975d6abdbSRobert Watson 	    IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
36075d6abdbSRobert Watson 	    (int)kmsqptr->u.msg_perm.key,
36175d6abdbSRobert Watson 	    fmt_perm(kmsqptr->u.msg_perm.mode),
36275d6abdbSRobert Watson 	    user_from_uid(kmsqptr->u.msg_perm.uid, 0),
36375d6abdbSRobert Watson 	    group_from_gid(kmsqptr->u.msg_perm.gid, 0));
3644816f94eSDoug Rabson 
36549fbc2acSDoug Rabson 	if (option & CREATOR)
36655e2cb41SEdwin Groothuis 		printf(" %-8s %-8s",
36775d6abdbSRobert Watson 		    user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
36875d6abdbSRobert Watson 		    group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
3694816f94eSDoug Rabson 
37049fbc2acSDoug Rabson 	if (option & OUTSTANDING)
371336c393fSGiorgos Keramidas 		printf(" %12lu %12lu",
37275d6abdbSRobert Watson 		    kmsqptr->u.msg_cbytes,
37375d6abdbSRobert Watson 		    kmsqptr->u.msg_qnum);
3744816f94eSDoug Rabson 
37549fbc2acSDoug Rabson 	if (option & BIGGEST)
376fa44a292SEdwin Groothuis 		printf(" %20lu", kmsqptr->u.msg_qbytes);
3774816f94eSDoug Rabson 
37849fbc2acSDoug Rabson 	if (option & PID)
379336c393fSGiorgos Keramidas 		printf(" %12d %12d",
38075d6abdbSRobert Watson 		    kmsqptr->u.msg_lspid,
38175d6abdbSRobert Watson 		    kmsqptr->u.msg_lrpid);
3824816f94eSDoug Rabson 
38349fbc2acSDoug Rabson 	if (option & TIME)
38449fbc2acSDoug Rabson 		printf(" %s %s %s",
38549fbc2acSDoug Rabson 		    stime_buf,
38649fbc2acSDoug Rabson 		    rtime_buf,
38749fbc2acSDoug Rabson 		    ctime_buf);
3884816f94eSDoug Rabson 
38949fbc2acSDoug Rabson 	printf("\n");
39049fbc2acSDoug Rabson }
391b15abeffSDima Dorfman 
392fa44a292SEdwin Groothuis void
393fa44a292SEdwin Groothuis print_kshmtotal(struct shminfo shminfo)
394fa44a292SEdwin Groothuis {
395fa44a292SEdwin Groothuis 
39649fbc2acSDoug Rabson 	printf("shminfo:\n");
397336c393fSGiorgos Keramidas 	printf("\tshmmax: %12d\t(max shared memory segment size)\n",
39849fbc2acSDoug Rabson 	    shminfo.shmmax);
399336c393fSGiorgos Keramidas 	printf("\tshmmin: %12d\t(min shared memory segment size)\n",
40049fbc2acSDoug Rabson 	    shminfo.shmmin);
401336c393fSGiorgos Keramidas 	printf("\tshmmni: %12d\t(max number of shared memory identifiers)\n",
40249fbc2acSDoug Rabson 	    shminfo.shmmni);
403336c393fSGiorgos Keramidas 	printf("\tshmseg: %12d\t(max shared memory segments per process)\n",
40449fbc2acSDoug Rabson 	    shminfo.shmseg);
405336c393fSGiorgos Keramidas 	printf("\tshmall: %12d\t(max amount of shared memory in pages)\n\n",
40649fbc2acSDoug Rabson 	    shminfo.shmall);
40749fbc2acSDoug Rabson }
40849fbc2acSDoug Rabson 
409fa44a292SEdwin Groothuis void
410fa44a292SEdwin Groothuis print_kshmheader(int option)
411fa44a292SEdwin Groothuis {
41249fbc2acSDoug Rabson 
41349fbc2acSDoug Rabson 	printf("Shared Memory:\n");
414fa44a292SEdwin Groothuis 	printf("T %12s %12s %-11s %-8s %-8s",
415fa44a292SEdwin Groothuis 	    "ID", "KEY", "MODE", "OWNER", "GROUP");
41649fbc2acSDoug Rabson 	if (option & CREATOR)
417336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "CREATOR", "CGROUP");
41849fbc2acSDoug Rabson 	if (option & OUTSTANDING)
419336c393fSGiorgos Keramidas 		printf(" %12s", "NATTCH");
42049fbc2acSDoug Rabson 	if (option & BIGGEST)
421336c393fSGiorgos Keramidas 		printf(" %12s", "SEGSZ");
42249fbc2acSDoug Rabson 	if (option & PID)
423336c393fSGiorgos Keramidas 		printf(" %12s %12s", "CPID", "LPID");
42449fbc2acSDoug Rabson 	if (option & TIME)
425336c393fSGiorgos Keramidas 		printf(" %-8s %-8s %-8s", "ATIME", "DTIME", "CTIME");
42649fbc2acSDoug Rabson 	printf("\n");
427fa44a292SEdwin Groothuis }
42849fbc2acSDoug Rabson 
429fa44a292SEdwin Groothuis void
430fa44a292SEdwin Groothuis print_kshmptr(int i, int option, struct shmid_kernel *kshmptr)
431fa44a292SEdwin Groothuis {
432fa44a292SEdwin Groothuis 	char    atime_buf[100], dtime_buf[100], ctime_buf[100];
433fa44a292SEdwin Groothuis 
43475d6abdbSRobert Watson 	cvt_time(kshmptr->u.shm_atime, atime_buf);
43575d6abdbSRobert Watson 	cvt_time(kshmptr->u.shm_dtime, dtime_buf);
43675d6abdbSRobert Watson 	cvt_time(kshmptr->u.shm_ctime, ctime_buf);
43749fbc2acSDoug Rabson 
43855e2cb41SEdwin Groothuis 	printf("m %12d %12d %s %-8s %-8s",
43975d6abdbSRobert Watson 	    IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
44075d6abdbSRobert Watson 	    (int)kshmptr->u.shm_perm.key,
44175d6abdbSRobert Watson 	    fmt_perm(kshmptr->u.shm_perm.mode),
44275d6abdbSRobert Watson 	    user_from_uid(kshmptr->u.shm_perm.uid, 0),
44375d6abdbSRobert Watson 	    group_from_gid(kshmptr->u.shm_perm.gid, 0));
44449fbc2acSDoug Rabson 
44549fbc2acSDoug Rabson 	if (option & CREATOR)
44655e2cb41SEdwin Groothuis 		printf(" %-8s %-8s",
44775d6abdbSRobert Watson 		    user_from_uid(kshmptr->u.shm_perm.cuid, 0),
44875d6abdbSRobert Watson 		    group_from_gid(kshmptr->u.shm_perm.cgid, 0));
44949fbc2acSDoug Rabson 
45049fbc2acSDoug Rabson 	if (option & OUTSTANDING)
451336c393fSGiorgos Keramidas 		printf(" %12d",
45275d6abdbSRobert Watson 		    kshmptr->u.shm_nattch);
45349fbc2acSDoug Rabson 
45449fbc2acSDoug Rabson 	if (option & BIGGEST)
455336c393fSGiorgos Keramidas 		printf(" %12d",
45675d6abdbSRobert Watson 		    kshmptr->u.shm_segsz);
45749fbc2acSDoug Rabson 
45849fbc2acSDoug Rabson 	if (option & PID)
459336c393fSGiorgos Keramidas 		printf(" %12d %12d",
46075d6abdbSRobert Watson 		    kshmptr->u.shm_cpid,
46175d6abdbSRobert Watson 		    kshmptr->u.shm_lpid);
46249fbc2acSDoug Rabson 
46349fbc2acSDoug Rabson 	if (option & TIME)
46449fbc2acSDoug Rabson 		printf(" %s %s %s",
46549fbc2acSDoug Rabson 		    atime_buf,
46649fbc2acSDoug Rabson 		    dtime_buf,
46749fbc2acSDoug Rabson 		    ctime_buf);
46849fbc2acSDoug Rabson 
46949fbc2acSDoug Rabson 	printf("\n");
47049fbc2acSDoug Rabson }
471b15abeffSDima Dorfman 
472fa44a292SEdwin Groothuis void
473fa44a292SEdwin Groothuis print_ksemtotal(struct seminfo seminfo)
474fa44a292SEdwin Groothuis {
47549fbc2acSDoug Rabson 
47649fbc2acSDoug Rabson 	printf("seminfo:\n");
477336c393fSGiorgos Keramidas 	printf("\tsemmap: %12d\t(# of entries in semaphore map)\n",
47849fbc2acSDoug Rabson 	    seminfo.semmap);
479336c393fSGiorgos Keramidas 	printf("\tsemmni: %12d\t(# of semaphore identifiers)\n",
48049fbc2acSDoug Rabson 	    seminfo.semmni);
481336c393fSGiorgos Keramidas 	printf("\tsemmns: %12d\t(# of semaphores in system)\n",
48249fbc2acSDoug Rabson 	    seminfo.semmns);
483336c393fSGiorgos Keramidas 	printf("\tsemmnu: %12d\t(# of undo structures in system)\n",
48449fbc2acSDoug Rabson 	    seminfo.semmnu);
485336c393fSGiorgos Keramidas 	printf("\tsemmsl: %12d\t(max # of semaphores per id)\n",
48649fbc2acSDoug Rabson 	    seminfo.semmsl);
487336c393fSGiorgos Keramidas 	printf("\tsemopm: %12d\t(max # of operations per semop call)\n",
48849fbc2acSDoug Rabson 	    seminfo.semopm);
489336c393fSGiorgos Keramidas 	printf("\tsemume: %12d\t(max # of undo entries per process)\n",
49049fbc2acSDoug Rabson 	    seminfo.semume);
491336c393fSGiorgos Keramidas 	printf("\tsemusz: %12d\t(size in bytes of undo structure)\n",
49249fbc2acSDoug Rabson 	    seminfo.semusz);
493336c393fSGiorgos Keramidas 	printf("\tsemvmx: %12d\t(semaphore maximum value)\n",
49449fbc2acSDoug Rabson 	    seminfo.semvmx);
495336c393fSGiorgos Keramidas 	printf("\tsemaem: %12d\t(adjust on exit max value)\n\n",
49649fbc2acSDoug Rabson 	    seminfo.semaem);
49749fbc2acSDoug Rabson }
498fa44a292SEdwin Groothuis 
499fa44a292SEdwin Groothuis void
50055e2cb41SEdwin Groothuis print_ksemheader(int option)
50155e2cb41SEdwin Groothuis {
50249fbc2acSDoug Rabson 
50349fbc2acSDoug Rabson 	printf("Semaphores:\n");
504fa44a292SEdwin Groothuis 	printf("T %12s %12s %-11s %-8s %-8s",
505fa44a292SEdwin Groothuis 	    "ID", "KEY", "MODE", "OWNER", "GROUP");
50649fbc2acSDoug Rabson 	if (option & CREATOR)
507336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "CREATOR", "CGROUP");
50849fbc2acSDoug Rabson 	if (option & BIGGEST)
509336c393fSGiorgos Keramidas 		printf(" %12s", "NSEMS");
51049fbc2acSDoug Rabson 	if (option & TIME)
511336c393fSGiorgos Keramidas 		printf(" %-8s %-8s", "OTIME", "CTIME");
51249fbc2acSDoug Rabson 	printf("\n");
513fa44a292SEdwin Groothuis }
51449fbc2acSDoug Rabson 
515fa44a292SEdwin Groothuis void
516fa44a292SEdwin Groothuis print_ksemptr(int i, int option, struct semid_kernel *ksemaptr)
517fa44a292SEdwin Groothuis {
518fa44a292SEdwin Groothuis 	char    ctime_buf[100], otime_buf[100];
519fa44a292SEdwin Groothuis 
52075d6abdbSRobert Watson 	cvt_time(ksemaptr->u.sem_otime, otime_buf);
52175d6abdbSRobert Watson 	cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
52249fbc2acSDoug Rabson 
52355e2cb41SEdwin Groothuis 	printf("s %12d %12d %s %-8s %-8s",
52475d6abdbSRobert Watson 	    IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
52575d6abdbSRobert Watson 	    (int)ksemaptr->u.sem_perm.key,
52675d6abdbSRobert Watson 	    fmt_perm(ksemaptr->u.sem_perm.mode),
52775d6abdbSRobert Watson 	    user_from_uid(ksemaptr->u.sem_perm.uid, 0),
52875d6abdbSRobert Watson 	    group_from_gid(ksemaptr->u.sem_perm.gid, 0));
52949fbc2acSDoug Rabson 
53049fbc2acSDoug Rabson 	if (option & CREATOR)
53155e2cb41SEdwin Groothuis 		printf(" %-8s %-8s",
53275d6abdbSRobert Watson 		    user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
53375d6abdbSRobert Watson 		    group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
53449fbc2acSDoug Rabson 
53549fbc2acSDoug Rabson 	if (option & BIGGEST)
536336c393fSGiorgos Keramidas 		printf(" %12d",
53775d6abdbSRobert Watson 		    ksemaptr->u.sem_nsems);
53849fbc2acSDoug Rabson 
53949fbc2acSDoug Rabson 	if (option & TIME)
54049fbc2acSDoug Rabson 		printf(" %s %s",
54149fbc2acSDoug Rabson 		    otime_buf,
54249fbc2acSDoug Rabson 		    ctime_buf);
54349fbc2acSDoug Rabson 
54449fbc2acSDoug Rabson 	printf("\n");
5454816f94eSDoug Rabson }
54649fbc2acSDoug Rabson 
5474d2e8e5fSBosko Milekic uid_t
5484d2e8e5fSBosko Milekic user2uid(char *username)
5494d2e8e5fSBosko Milekic {
5504d2e8e5fSBosko Milekic 	struct passwd *pwd;
5514d2e8e5fSBosko Milekic 	uid_t uid;
5524d2e8e5fSBosko Milekic 	char *r;
5534d2e8e5fSBosko Milekic 
5544d2e8e5fSBosko Milekic 	uid = strtoul(username, &r, 0);
5554d2e8e5fSBosko Milekic 	if (!*r && r != username)
5564d2e8e5fSBosko Milekic 		return (uid);
5574d2e8e5fSBosko Milekic 	if ((pwd = getpwnam(username)) == NULL)
5584d2e8e5fSBosko Milekic 		errx(1, "getpwnam failed: No such user");
5594d2e8e5fSBosko Milekic 	endpwent();
5604d2e8e5fSBosko Milekic 	return (pwd->pw_uid);
5614d2e8e5fSBosko Milekic }
5624d2e8e5fSBosko Milekic 
563b15abeffSDima Dorfman void
5645bcb8532SStefan Farfeleder usage(void)
56549fbc2acSDoug Rabson {
56649fbc2acSDoug Rabson 
56749fbc2acSDoug Rabson 	fprintf(stderr,
568fa44a292SEdwin Groothuis 	    "usage: "
569fa44a292SEdwin Groothuis 	    "ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");
57049fbc2acSDoug Rabson 	exit(1);
57149fbc2acSDoug Rabson }
572