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