xref: /netbsd-src/usr.bin/ipcs/ipcs.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
19  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *	$Id: ipcs.c,v 1.7 1994/09/05 00:28:59 mycroft Exp $
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <paths.h>
35 #include <nlist.h>
36 #include <kvm.h>
37 #include <err.h>
38 
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/proc.h>
42 #define KERNEL
43 #include <sys/ipc.h>
44 #include <sys/sem.h>
45 #include <sys/shm.h>
46 #include <sys/msg.h>
47 
48 int	semconfig __P((int,...));
49 void	usage __P((void));
50 
51 static struct nlist symbols[] = {
52 	{"_sema"},
53 #define X_SEMA		0
54 	{"_seminfo"},
55 #define X_SEMINFO	1
56 	{"_semu"},
57 #define X_SEMU		2
58 	{"_msginfo"},
59 #define X_MSGINFO	3
60 	{"_msqids"},
61 #define X_MSQIDS	4
62 	{"_shminfo"},
63 #define X_SHMINFO	5
64 	{"_shmsegs"},
65 #define X_SHMSEGS	6
66 	{NULL}
67 };
68 
69 static kvm_t *kd;
70 
71 char   *
72 fmt_perm(mode)
73 	u_short mode;
74 {
75 	static char buffer[100];
76 
77 	buffer[0] = '-';
78 	buffer[1] = '-';
79 	buffer[2] = ((mode & 0400) ? 'r' : '-');
80 	buffer[3] = ((mode & 0200) ? 'w' : '-');
81 	buffer[4] = ((mode & 0100) ? 'a' : '-');
82 	buffer[5] = ((mode & 0040) ? 'r' : '-');
83 	buffer[6] = ((mode & 0020) ? 'w' : '-');
84 	buffer[7] = ((mode & 0010) ? 'a' : '-');
85 	buffer[8] = ((mode & 0004) ? 'r' : '-');
86 	buffer[9] = ((mode & 0002) ? 'w' : '-');
87 	buffer[10] = ((mode & 0001) ? 'a' : '-');
88 	buffer[11] = '\0';
89 	return (&buffer[0]);
90 }
91 
92 void
93 cvt_time(t, buf)
94 	time_t  t;
95 	char   *buf;
96 {
97 	struct tm *tm;
98 
99 	if (t == 0) {
100 		strcpy(buf, "no-entry");
101 	} else {
102 		tm = localtime(&t);
103 		sprintf(buf, "%2d:%02d:%02d",
104 			tm->tm_hour, tm->tm_min, tm->tm_sec);
105 	}
106 }
107 #define	SHMINFO		1
108 #define	SHMTOTAL	2
109 #define	MSGINFO		4
110 #define	MSGTOTAL	8
111 #define	SEMINFO		16
112 #define	SEMTOTAL	32
113 
114 #define BIGGEST		1
115 #define CREATOR		2
116 #define OUTSTANDING	4
117 #define PID		8
118 #define TIME		16
119 
120 int
121 main(argc, argv)
122 	int     argc;
123 	char   *argv[];
124 {
125 	int     display = SHMINFO | MSGINFO | SEMINFO;
126 	int     option = 0;
127 	char   *core = NULL, *namelist = NULL;
128 	int     i;
129 
130 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != EOF)
131 		switch (i) {
132 		case 'M':
133 			display = SHMTOTAL;
134 			break;
135 		case 'm':
136 			display = SHMINFO;
137 			break;
138 		case 'Q':
139 			display = MSGTOTAL;
140 			break;
141 		case 'q':
142 			display = MSGINFO;
143 			break;
144 		case 'S':
145 			display = SEMTOTAL;
146 			break;
147 		case 's':
148 			display = SEMINFO;
149 			break;
150 		case 'T':
151 			display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
152 			break;
153 		case 'a':
154 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
155 			break;
156 		case 'b':
157 			option |= BIGGEST;
158 			break;
159 		case 'C':
160 			core = optarg;
161 			break;
162 		case 'c':
163 			option |= CREATOR;
164 			break;
165 		case 'N':
166 			namelist = optarg;
167 			break;
168 		case 'o':
169 			option |= OUTSTANDING;
170 			break;
171 		case 'p':
172 			option |= PID;
173 			break;
174 		case 't':
175 			option |= TIME;
176 			break;
177 		default:
178 			usage();
179 		}
180 	if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
181 		exit(1);
182 
183 	switch (kvm_nlist(kd, symbols)) {
184 	case 0:
185 		break;
186 	case -1:
187 		errx(1, "unable to read kernel symbol table.");
188 	default:
189 #ifdef notdef		/* they'll be told more civilly later */
190 		warnx("nlist failed");
191 		for (i = 0; symbols[i].n_name != NULL; i++)
192 			if (symbols[i].n_value == 0)
193 				warnx("symbol %s not found",
194 				    symbols[i].n_name);
195 		break;
196 #endif
197 	}
198 
199 	if ((display & (MSGINFO | MSGTOTAL)) &&
200 	    kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))) {
201 
202 		if (display & MSGTOTAL) {
203 			printf("msginfo:\n");
204 			printf("\tmsgmax: %6d\t(max characters in a message)\n",
205 			    msginfo.msgmax);
206 			printf("\tmsgmni: %6d\t(# of message queues)\n",
207 			    msginfo.msgmni);
208 			printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
209 			    msginfo.msgmnb);
210 			printf("\tmsgtql: %6d\t(max # of messages in system)\n",
211 			    msginfo.msgtql);
212 			printf("\tmsgssz: %6d\t(size of a message segment)\n",
213 			    msginfo.msgssz);
214 			printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
215 			    msginfo.msgseg);
216 		}
217 		if (display & MSGINFO) {
218 			struct msqid_ds *xmsqids;
219 
220 			kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
221 			xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
222 			kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
223 
224 			printf("Message Queues:\n");
225 			printf("T     ID     KEY        MODE       OWNER    GROUP");
226 			if (option & CREATOR)
227 				printf("  CREATOR   CGROUP");
228 			if (option & OUTSTANDING)
229 				printf(" CBYTES  QNUM");
230 			if (option & BIGGEST)
231 				printf(" QBYTES");
232 			if (option & PID)
233 				printf(" LSPID LRPID");
234 			if (option & TIME)
235 				printf("   STIME    RTIME    CTIME");
236 			printf("\n");
237 			for (i = 0; i < msginfo.msgmni; i += 1) {
238 				if (xmsqids[i].msg_qbytes != 0) {
239 					char    stime_buf[100], rtime_buf[100],
240 					        ctime_buf[100];
241 					struct msqid_ds *msqptr = &xmsqids[i];
242 
243 					cvt_time(msqptr->msg_stime, stime_buf);
244 					cvt_time(msqptr->msg_rtime, rtime_buf);
245 					cvt_time(msqptr->msg_ctime, ctime_buf);
246 
247 					printf("q %6d %10d %s %8s %8s",
248 					    IXSEQ_TO_IPCID(i, msqptr->msg_perm),
249 					    msqptr->msg_perm.key,
250 					    fmt_perm(msqptr->msg_perm.mode),
251 					    user_from_uid(msqptr->msg_perm.uid, 0),
252 					    group_from_gid(msqptr->msg_perm.gid, 0));
253 
254 					if (option & CREATOR)
255 						printf(" %8s %8s",
256 						    user_from_uid(msqptr->msg_perm.cuid, 0),
257 						    group_from_gid(msqptr->msg_perm.cgid, 0));
258 
259 					if (option & OUTSTANDING)
260 						printf(" %6d %6d",
261 						    msqptr->msg_cbytes,
262 						    msqptr->msg_qnum);
263 
264 					if (option & BIGGEST)
265 						printf(" %6d",
266 						    msqptr->msg_qbytes);
267 
268 					if (option & PID)
269 						printf(" %6d %6d",
270 						    msqptr->msg_lspid,
271 						    msqptr->msg_lrpid);
272 
273 					if (option & TIME)
274 						printf("%s %s %s",
275 						    stime_buf,
276 						    rtime_buf,
277 						    ctime_buf);
278 
279 					printf("\n");
280 				}
281 			}
282 			printf("\n");
283 		}
284 	} else
285 		if (display & (MSGINFO | MSGTOTAL)) {
286 			fprintf(stderr,
287 			    "SVID messages facility not configured in the system\n");
288 		}
289 	if ((display & (SHMINFO | SHMTOTAL)) &&
290 	    kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
291 		if (display & SHMTOTAL) {
292 			printf("shminfo:\n");
293 			printf("\tshmmax: %7d\t(max shared memory segment size)\n",
294 			    shminfo.shmmax);
295 			printf("\tshmmin: %7d\t(min shared memory segment size)\n",
296 			    shminfo.shmmin);
297 			printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
298 			    shminfo.shmmni);
299 			printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
300 			    shminfo.shmseg);
301 			printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
302 			    shminfo.shmall);
303 		}
304 		if (display & SHMINFO) {
305 			struct shmid_ds *xshmids;
306 
307 			kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
308 			xshmids = malloc(sizeof(struct shmid_ds) * msginfo.msgmni);
309 			kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
310 			    shminfo.shmmni);
311 
312 			printf("Shared Memory:\n");
313 			printf("T     ID     KEY        MODE       OWNER    GROUP");
314 			if (option & CREATOR)
315 				printf("  CREATOR   CGROUP");
316 			if (option & OUTSTANDING)
317 				printf(" NATTCH");
318 			if (option & BIGGEST)
319 				printf("  SEGSZ");
320 			if (option & PID)
321 				printf("  CPID  LPID");
322 			if (option & TIME)
323 				printf("   ATIME    DTIME    CTIME");
324 			printf("\n");
325 			for (i = 0; i < shminfo.shmmni; i += 1) {
326 				if (xshmids[i].shm_perm.mode & 0x0800) {
327 					char    atime_buf[100], dtime_buf[100],
328 					        ctime_buf[100];
329 					struct shmid_ds *shmptr = &xshmids[i];
330 
331 					cvt_time(shmptr->shm_atime, atime_buf);
332 					cvt_time(shmptr->shm_dtime, dtime_buf);
333 					cvt_time(shmptr->shm_ctime, ctime_buf);
334 
335 					printf("m %6d %10d %s %8s %8s",
336 					    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
337 					    shmptr->shm_perm.key,
338 					    fmt_perm(shmptr->shm_perm.mode),
339 					    user_from_uid(shmptr->shm_perm.uid, 0),
340 					    group_from_gid(shmptr->shm_perm.gid, 0));
341 
342 					if (option & CREATOR)
343 						printf(" %8s %8s",
344 						    user_from_uid(shmptr->shm_perm.cuid, 0),
345 						    group_from_gid(shmptr->shm_perm.cgid, 0));
346 
347 					if (option & OUTSTANDING)
348 						printf(" %6d",
349 						    shmptr->shm_nattch);
350 
351 					if (option & BIGGEST)
352 						printf(" %6d",
353 						    shmptr->shm_segsz);
354 
355 					if (option & PID)
356 						printf(" %6d %6d",
357 						    shmptr->shm_cpid,
358 						    shmptr->shm_lpid);
359 
360 					if (option & TIME)
361 						printf("%s %s %s",
362 						    atime_buf,
363 						    dtime_buf,
364 						    ctime_buf);
365 
366 					printf("\n");
367 				}
368 			}
369 			printf("\n");
370 		}
371 	} else
372 		if (display & (SHMINFO | SHMTOTAL)) {
373 			fprintf(stderr,
374 			    "SVID shared memory facility not configured in the system\n");
375 		}
376 	if ((display & (SEMINFO | SEMTOTAL)) &&
377 	    kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
378 		struct semid_ds *xsema;
379 
380 		if (display & SEMTOTAL) {
381 			printf("seminfo:\n");
382 			printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
383 			    seminfo.semmap);
384 			printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
385 			    seminfo.semmni);
386 			printf("\tsemmns: %6d\t(# of semaphores in system)\n",
387 			    seminfo.semmns);
388 			printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
389 			    seminfo.semmnu);
390 			printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
391 			    seminfo.semmsl);
392 			printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
393 			    seminfo.semopm);
394 			printf("\tsemume: %6d\t(max # of undo entries per process)\n",
395 			    seminfo.semume);
396 			printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
397 			    seminfo.semusz);
398 			printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
399 			    seminfo.semvmx);
400 			printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
401 			    seminfo.semaem);
402 		}
403 		if (display & SEMINFO) {
404 			if (semconfig(SEM_CONFIG_FREEZE) != 0) {
405 				perror("semconfig");
406 				fprintf(stderr,
407 				    "Can't lock semaphore facility - winging it...\n");
408 			}
409 			kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
410 			xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
411 			kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
412 
413 			printf("Semaphores:\n");
414 			printf("T     ID     KEY        MODE       OWNER    GROUP");
415 			if (option & CREATOR)
416 				printf("  CREATOR   CGROUP");
417 			if (option & BIGGEST)
418 				printf(" NSEMS");
419 			if (option & TIME)
420 				printf("   OTIME    CTIME");
421 			printf("\n");
422 			for (i = 0; i < seminfo.semmni; i += 1) {
423 				if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
424 					char    ctime_buf[100], otime_buf[100];
425 					struct semid_ds *semaptr = &xsema[i];
426 					int     j, value;
427 					union semun junk;
428 
429 					cvt_time(semaptr->sem_otime, otime_buf);
430 					cvt_time(semaptr->sem_ctime, ctime_buf);
431 
432 					printf("s %6d %10d %s %8s %8s",
433 					    IXSEQ_TO_IPCID(i, semaptr->sem_perm),
434 					    semaptr->sem_perm.key,
435 					    fmt_perm(semaptr->sem_perm.mode),
436 					    user_from_uid(semaptr->sem_perm.uid, 0),
437 					    group_from_gid(semaptr->sem_perm.gid, 0));
438 
439 					if (option & CREATOR)
440 						printf(" %8s %8s",
441 						    user_from_uid(semaptr->sem_perm.cuid, 0),
442 						    group_from_gid(semaptr->sem_perm.cgid, 0));
443 
444 					if (option & BIGGEST)
445 						printf(" %6d",
446 						    semaptr->sem_nsems);
447 
448 					if (option & TIME)
449 						printf("%s %s",
450 						    otime_buf,
451 						    ctime_buf);
452 
453 					printf("\n");
454 				}
455 			}
456 
457 			(void) semconfig(SEM_CONFIG_THAW);
458 
459 			printf("\n");
460 		}
461 	} else
462 		if (display & (SEMINFO | SEMTOTAL)) {
463 			fprintf(stderr, "SVID semaphores facility not configured in the system\n");
464 		}
465 	kvm_close(kd);
466 
467 	exit(0);
468 }
469 
470 void
471 usage()
472 {
473 
474 	fprintf(stderr,
475 	    "usage: ipcs [-abcmopqst] [-C corefile] [-N namelist]\n");
476 	exit(1);
477 }
478