xref: /freebsd/usr.bin/ipcs/ipcs.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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.5 1996/05/30 02:18:44 pst 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/time.h>
42 #include <sys/proc.h>
43 #define KERNEL
44 #include <sys/ipc.h>
45 #include <sys/sem.h>
46 #include <sys/shm.h>
47 #include <sys/msg.h>
48 
49 struct semid_ds	*sema;
50 struct seminfo	seminfo;
51 struct msginfo	msginfo;
52 struct msqid_ds	*msqids;
53 struct shminfo	shminfo;
54 struct shmid_ds	*shmsegs;
55 
56 int	semconfig __P((int,...));
57 void	usage __P((void));
58 
59 static struct nlist symbols[] = {
60 	{"_sema"},
61 #define X_SEMA		0
62 	{"_seminfo"},
63 #define X_SEMINFO	1
64 	{"_semu"},
65 #define X_SEMU		2
66 	{"_msginfo"},
67 #define X_MSGINFO	3
68 	{"_msqids"},
69 #define X_MSQIDS	4
70 	{"_shminfo"},
71 #define X_SHMINFO	5
72 	{"_shmsegs"},
73 #define X_SHMSEGS	6
74 	{NULL}
75 };
76 
77 static kvm_t *kd;
78 
79 char   *
80 fmt_perm(mode)
81 	u_short mode;
82 {
83 	static char buffer[100];
84 
85 	buffer[0] = '-';
86 	buffer[1] = '-';
87 	buffer[2] = ((mode & 0400) ? 'r' : '-');
88 	buffer[3] = ((mode & 0200) ? 'w' : '-');
89 	buffer[4] = ((mode & 0100) ? 'a' : '-');
90 	buffer[5] = ((mode & 0040) ? 'r' : '-');
91 	buffer[6] = ((mode & 0020) ? 'w' : '-');
92 	buffer[7] = ((mode & 0010) ? 'a' : '-');
93 	buffer[8] = ((mode & 0004) ? 'r' : '-');
94 	buffer[9] = ((mode & 0002) ? 'w' : '-');
95 	buffer[10] = ((mode & 0001) ? 'a' : '-');
96 	buffer[11] = '\0';
97 	return (&buffer[0]);
98 }
99 
100 void
101 cvt_time(t, buf)
102 	time_t  t;
103 	char   *buf;
104 {
105 	struct tm *tm;
106 
107 	if (t == 0) {
108 		strcpy(buf, "no-entry");
109 	} else {
110 		tm = localtime(&t);
111 		sprintf(buf, "%2d:%02d:%02d",
112 			tm->tm_hour, tm->tm_min, tm->tm_sec);
113 	}
114 }
115 #define	SHMINFO		1
116 #define	SHMTOTAL	2
117 #define	MSGINFO		4
118 #define	MSGTOTAL	8
119 #define	SEMINFO		16
120 #define	SEMTOTAL	32
121 
122 #define BIGGEST		1
123 #define CREATOR		2
124 #define OUTSTANDING	4
125 #define PID		8
126 #define TIME		16
127 
128 int
129 main(argc, argv)
130 	int     argc;
131 	char   *argv[];
132 {
133 	int     display = SHMINFO | MSGINFO | SEMINFO;
134 	int     option = 0;
135 	char   *core = NULL, *namelist = NULL;
136 	int     i;
137 
138 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != EOF)
139 		switch (i) {
140 		case 'M':
141 			display = SHMTOTAL;
142 			break;
143 		case 'm':
144 			display = SHMINFO;
145 			break;
146 		case 'Q':
147 			display = MSGTOTAL;
148 			break;
149 		case 'q':
150 			display = MSGINFO;
151 			break;
152 		case 'S':
153 			display = SEMTOTAL;
154 			break;
155 		case 's':
156 			display = SEMINFO;
157 			break;
158 		case 'T':
159 			display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
160 			break;
161 		case 'a':
162 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
163 			break;
164 		case 'b':
165 			option |= BIGGEST;
166 			break;
167 		case 'C':
168 			core = optarg;
169 			break;
170 		case 'c':
171 			option |= CREATOR;
172 			break;
173 		case 'N':
174 			namelist = optarg;
175 			break;
176 		case 'o':
177 			option |= OUTSTANDING;
178 			break;
179 		case 'p':
180 			option |= PID;
181 			break;
182 		case 't':
183 			option |= TIME;
184 			break;
185 		default:
186 			usage();
187 		}
188 
189 	/*
190 	 * Discard setgid privileges if not the running kernel so that bad
191 	 * guys can't print interesting stuff from kernel memory.
192 	 */
193 	if (namelist != NULL || core != NULL)
194 		setgid(getgid());
195 
196 	if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
197 		exit(1);
198 
199 	switch (kvm_nlist(kd, symbols)) {
200 	case 0:
201 		break;
202 	case -1:
203 		errx(1, "unable to read kernel symbol table.");
204 	default:
205 #ifdef notdef		/* they'll be told more civilly later */
206 		warnx("nlist failed");
207 		for (i = 0; symbols[i].n_name != NULL; i++)
208 			if (symbols[i].n_value == 0)
209 				warnx("symbol %s not found",
210 				    symbols[i].n_name);
211 		break;
212 #endif
213 	}
214 
215 	if ((display & (MSGINFO | MSGTOTAL)) &&
216 	    kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))) {
217 
218 		if (display & MSGTOTAL) {
219 			printf("msginfo:\n");
220 			printf("\tmsgmax: %6d\t(max characters in a message)\n",
221 			    msginfo.msgmax);
222 			printf("\tmsgmni: %6d\t(# of message queues)\n",
223 			    msginfo.msgmni);
224 			printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
225 			    msginfo.msgmnb);
226 			printf("\tmsgtql: %6d\t(max # of messages in system)\n",
227 			    msginfo.msgtql);
228 			printf("\tmsgssz: %6d\t(size of a message segment)\n",
229 			    msginfo.msgssz);
230 			printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
231 			    msginfo.msgseg);
232 		}
233 		if (display & MSGINFO) {
234 			struct msqid_ds *xmsqids;
235 
236 			kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
237 			xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
238 			kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
239 
240 			printf("Message Queues:\n");
241 			printf("T     ID     KEY        MODE       OWNER    GROUP");
242 			if (option & CREATOR)
243 				printf("  CREATOR   CGROUP");
244 			if (option & OUTSTANDING)
245 				printf(" CBYTES  QNUM");
246 			if (option & BIGGEST)
247 				printf(" QBYTES");
248 			if (option & PID)
249 				printf(" LSPID LRPID");
250 			if (option & TIME)
251 				printf("   STIME    RTIME    CTIME");
252 			printf("\n");
253 			for (i = 0; i < msginfo.msgmni; i += 1) {
254 				if (xmsqids[i].msg_qbytes != 0) {
255 					char    stime_buf[100], rtime_buf[100],
256 					        ctime_buf[100];
257 					struct msqid_ds *msqptr = &xmsqids[i];
258 
259 					cvt_time(msqptr->msg_stime, stime_buf);
260 					cvt_time(msqptr->msg_rtime, rtime_buf);
261 					cvt_time(msqptr->msg_ctime, ctime_buf);
262 
263 					printf("q %6d %10d %s %8s %8s",
264 					    IXSEQ_TO_IPCID(i, msqptr->msg_perm),
265 					    msqptr->msg_perm.key,
266 					    fmt_perm(msqptr->msg_perm.mode),
267 					    user_from_uid(msqptr->msg_perm.uid, 0),
268 					    group_from_gid(msqptr->msg_perm.gid, 0));
269 
270 					if (option & CREATOR)
271 						printf(" %8s %8s",
272 						    user_from_uid(msqptr->msg_perm.cuid, 0),
273 						    group_from_gid(msqptr->msg_perm.cgid, 0));
274 
275 					if (option & OUTSTANDING)
276 						printf(" %6d %6d",
277 						    msqptr->msg_cbytes,
278 						    msqptr->msg_qnum);
279 
280 					if (option & BIGGEST)
281 						printf(" %6d",
282 						    msqptr->msg_qbytes);
283 
284 					if (option & PID)
285 						printf(" %6d %6d",
286 						    msqptr->msg_lspid,
287 						    msqptr->msg_lrpid);
288 
289 					if (option & TIME)
290 						printf("%s %s %s",
291 						    stime_buf,
292 						    rtime_buf,
293 						    ctime_buf);
294 
295 					printf("\n");
296 				}
297 			}
298 			printf("\n");
299 		}
300 	} else
301 		if (display & (MSGINFO | MSGTOTAL)) {
302 			fprintf(stderr,
303 			    "SVID messages facility not configured in the system\n");
304 		}
305 	if ((display & (SHMINFO | SHMTOTAL)) &&
306 	    kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
307 		if (display & SHMTOTAL) {
308 			printf("shminfo:\n");
309 			printf("\tshmmax: %7d\t(max shared memory segment size)\n",
310 			    shminfo.shmmax);
311 			printf("\tshmmin: %7d\t(min shared memory segment size)\n",
312 			    shminfo.shmmin);
313 			printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
314 			    shminfo.shmmni);
315 			printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
316 			    shminfo.shmseg);
317 			printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
318 			    shminfo.shmall);
319 		}
320 		if (display & SHMINFO) {
321 			struct shmid_ds *xshmids;
322 
323 			kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
324 			xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
325 			kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
326 			    shminfo.shmmni);
327 
328 			printf("Shared Memory:\n");
329 			printf("T     ID     KEY        MODE       OWNER    GROUP");
330 			if (option & CREATOR)
331 				printf("  CREATOR   CGROUP");
332 			if (option & OUTSTANDING)
333 				printf(" NATTCH");
334 			if (option & BIGGEST)
335 				printf("  SEGSZ");
336 			if (option & PID)
337 				printf("  CPID  LPID");
338 			if (option & TIME)
339 				printf("   ATIME    DTIME    CTIME");
340 			printf("\n");
341 			for (i = 0; i < shminfo.shmmni; i += 1) {
342 				if (xshmids[i].shm_perm.mode & 0x0800) {
343 					char    atime_buf[100], dtime_buf[100],
344 					        ctime_buf[100];
345 					struct shmid_ds *shmptr = &xshmids[i];
346 
347 					cvt_time(shmptr->shm_atime, atime_buf);
348 					cvt_time(shmptr->shm_dtime, dtime_buf);
349 					cvt_time(shmptr->shm_ctime, ctime_buf);
350 
351 					printf("m %6d %10d %s %8s %8s",
352 					    IXSEQ_TO_IPCID(i, shmptr->shm_perm),
353 					    shmptr->shm_perm.key,
354 					    fmt_perm(shmptr->shm_perm.mode),
355 					    user_from_uid(shmptr->shm_perm.uid, 0),
356 					    group_from_gid(shmptr->shm_perm.gid, 0));
357 
358 					if (option & CREATOR)
359 						printf(" %8s %8s",
360 						    user_from_uid(shmptr->shm_perm.cuid, 0),
361 						    group_from_gid(shmptr->shm_perm.cgid, 0));
362 
363 					if (option & OUTSTANDING)
364 						printf(" %6d",
365 						    shmptr->shm_nattch);
366 
367 					if (option & BIGGEST)
368 						printf(" %6d",
369 						    shmptr->shm_segsz);
370 
371 					if (option & PID)
372 						printf(" %6d %6d",
373 						    shmptr->shm_cpid,
374 						    shmptr->shm_lpid);
375 
376 					if (option & TIME)
377 						printf("%s %s %s",
378 						    atime_buf,
379 						    dtime_buf,
380 						    ctime_buf);
381 
382 					printf("\n");
383 				}
384 			}
385 			printf("\n");
386 		}
387 	} else
388 		if (display & (SHMINFO | SHMTOTAL)) {
389 			fprintf(stderr,
390 			    "SVID shared memory facility not configured in the system\n");
391 		}
392 	if ((display & (SEMINFO | SEMTOTAL)) &&
393 	    kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
394 		struct semid_ds *xsema;
395 
396 		if (display & SEMTOTAL) {
397 			printf("seminfo:\n");
398 			printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
399 			    seminfo.semmap);
400 			printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
401 			    seminfo.semmni);
402 			printf("\tsemmns: %6d\t(# of semaphores in system)\n",
403 			    seminfo.semmns);
404 			printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
405 			    seminfo.semmnu);
406 			printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
407 			    seminfo.semmsl);
408 			printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
409 			    seminfo.semopm);
410 			printf("\tsemume: %6d\t(max # of undo entries per process)\n",
411 			    seminfo.semume);
412 			printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
413 			    seminfo.semusz);
414 			printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
415 			    seminfo.semvmx);
416 			printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
417 			    seminfo.semaem);
418 		}
419 		if (display & SEMINFO) {
420 			if (semconfig(SEM_CONFIG_FREEZE) != 0) {
421 				perror("semconfig");
422 				fprintf(stderr,
423 				    "Can't lock semaphore facility - winging it...\n");
424 			}
425 			kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
426 			xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
427 			kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
428 
429 			printf("Semaphores:\n");
430 			printf("T     ID     KEY        MODE       OWNER    GROUP");
431 			if (option & CREATOR)
432 				printf("  CREATOR   CGROUP");
433 			if (option & BIGGEST)
434 				printf(" NSEMS");
435 			if (option & TIME)
436 				printf("   OTIME    CTIME");
437 			printf("\n");
438 			for (i = 0; i < seminfo.semmni; i += 1) {
439 				if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
440 					char    ctime_buf[100], otime_buf[100];
441 					struct semid_ds *semaptr = &xsema[i];
442 					int     j, value;
443 					union semun junk;
444 
445 					cvt_time(semaptr->sem_otime, otime_buf);
446 					cvt_time(semaptr->sem_ctime, ctime_buf);
447 
448 					printf("s %6d %10d %s %8s %8s",
449 					    IXSEQ_TO_IPCID(i, semaptr->sem_perm),
450 					    semaptr->sem_perm.key,
451 					    fmt_perm(semaptr->sem_perm.mode),
452 					    user_from_uid(semaptr->sem_perm.uid, 0),
453 					    group_from_gid(semaptr->sem_perm.gid, 0));
454 
455 					if (option & CREATOR)
456 						printf(" %8s %8s",
457 						    user_from_uid(semaptr->sem_perm.cuid, 0),
458 						    group_from_gid(semaptr->sem_perm.cgid, 0));
459 
460 					if (option & BIGGEST)
461 						printf(" %6d",
462 						    semaptr->sem_nsems);
463 
464 					if (option & TIME)
465 						printf("%s %s",
466 						    otime_buf,
467 						    ctime_buf);
468 
469 					printf("\n");
470 				}
471 			}
472 
473 			(void) semconfig(SEM_CONFIG_THAW);
474 
475 			printf("\n");
476 		}
477 	} else
478 		if (display & (SEMINFO | SEMTOTAL)) {
479 			fprintf(stderr, "SVID semaphores facility not configured in the system\n");
480 		}
481 	kvm_close(kd);
482 
483 	exit(0);
484 }
485 
486 void
487 usage()
488 {
489 
490 	fprintf(stderr,
491 	    "usage: ipcs [-abcmopqst] [-C corefile] [-N namelist]\n");
492 	exit(1);
493 }
494