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