xref: /freebsd/usr.bin/ipcs/ipcs.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <assert.h>
32 #include <err.h>
33 #include <fcntl.h>
34 #include <grp.h>
35 #include <kvm.h>
36 #include <nlist.h>
37 #include <limits.h>
38 #include <paths.h>
39 #include <pwd.h>
40 #include <stddef.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51 #define _KERNEL
52 #include <sys/ipc.h>
53 #include <sys/sem.h>
54 #include <sys/shm.h>
55 #include <sys/msg.h>
56 
57 /* SysCtlGatherStruct structure. */
58 struct scgs_vector {
59 	const char *sysctl;
60 	off_t offset;
61 	size_t size;
62 };
63 
64 int	use_sysctl = 1;
65 struct semid_kernel	*sema;
66 struct seminfo	seminfo;
67 struct msginfo	msginfo;
68 struct msqid_kernel	*msqids;
69 struct shminfo	shminfo;
70 struct shmid_kernel	*shmsegs;
71 
72 char   *fmt_perm(u_short);
73 void	cvt_time(time_t, char *);
74 void	sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vec);
75 void	kget(int idx, void *addr, size_t size);
76 void	usage(void);
77 uid_t	user2uid(char *username);
78 
79 static struct nlist symbols[] = {
80 	{"sema"},
81 #define X_SEMA		0
82 	{"seminfo"},
83 #define X_SEMINFO	1
84 	{"msginfo"},
85 #define X_MSGINFO	2
86 	{"msqids"},
87 #define X_MSQIDS	3
88 	{"shminfo"},
89 #define X_SHMINFO	4
90 	{"shmsegs"},
91 #define X_SHMSEGS	5
92 	{NULL}
93 };
94 
95 #define	SHMINFO_XVEC				\
96 X(shmmax, sizeof(int))				\
97 X(shmmin, sizeof(int))				\
98 X(shmmni, sizeof(int))				\
99 X(shmseg, sizeof(int))				\
100 X(shmall, sizeof(int))
101 
102 #define	SEMINFO_XVEC				\
103 X(semmap, sizeof(int))				\
104 X(semmni, sizeof(int))				\
105 X(semmns, sizeof(int))				\
106 X(semmnu, sizeof(int))				\
107 X(semmsl, sizeof(int))				\
108 X(semopm, sizeof(int))				\
109 X(semume, sizeof(int))				\
110 X(semusz, sizeof(int))				\
111 X(semvmx, sizeof(int))				\
112 X(semaem, sizeof(int))
113 
114 #define	MSGINFO_XVEC				\
115 X(msgmax, sizeof(int))				\
116 X(msgmni, sizeof(int))				\
117 X(msgmnb, sizeof(int))				\
118 X(msgtql, sizeof(int))				\
119 X(msgssz, sizeof(int))				\
120 X(msgseg, sizeof(int))
121 
122 #define	X(a, b)	{ "kern.ipc." #a, offsetof(TYPEC, a), (b) },
123 #define	TYPEC	struct shminfo
124 struct scgs_vector shminfo_scgsv[] = { SHMINFO_XVEC { NULL } };
125 #undef	TYPEC
126 #define	TYPEC	struct seminfo
127 struct scgs_vector seminfo_scgsv[] = { SEMINFO_XVEC { NULL } };
128 #undef	TYPEC
129 #define	TYPEC	struct msginfo
130 struct scgs_vector msginfo_scgsv[] = { MSGINFO_XVEC { NULL } };
131 #undef	TYPEC
132 #undef	X
133 
134 static kvm_t *kd;
135 
136 char   *
137 fmt_perm(u_short mode)
138 {
139 	static char buffer[100];
140 
141 	buffer[0] = '-';
142 	buffer[1] = '-';
143 	buffer[2] = ((mode & 0400) ? 'r' : '-');
144 	buffer[3] = ((mode & 0200) ? 'w' : '-');
145 	buffer[4] = ((mode & 0100) ? 'a' : '-');
146 	buffer[5] = ((mode & 0040) ? 'r' : '-');
147 	buffer[6] = ((mode & 0020) ? 'w' : '-');
148 	buffer[7] = ((mode & 0010) ? 'a' : '-');
149 	buffer[8] = ((mode & 0004) ? 'r' : '-');
150 	buffer[9] = ((mode & 0002) ? 'w' : '-');
151 	buffer[10] = ((mode & 0001) ? 'a' : '-');
152 	buffer[11] = '\0';
153 	return (&buffer[0]);
154 }
155 
156 void
157 cvt_time(time_t t, char *buf)
158 {
159 	struct tm *tm;
160 
161 	if (t == 0) {
162 		strcpy(buf, "no-entry");
163 	} else {
164 		tm = localtime(&t);
165 		sprintf(buf, "%2d:%02d:%02d",
166 			tm->tm_hour, tm->tm_min, tm->tm_sec);
167 	}
168 }
169 #define	SHMINFO		1
170 #define	SHMTOTAL	2
171 #define	MSGINFO		4
172 #define	MSGTOTAL	8
173 #define	SEMINFO		16
174 #define	SEMTOTAL	32
175 
176 #define BIGGEST		1
177 #define CREATOR		2
178 #define OUTSTANDING	4
179 #define PID		8
180 #define TIME		16
181 
182 int
183 main(int argc, char *argv[])
184 {
185 	int     display = SHMINFO | MSGINFO | SEMINFO;
186 	int     option = 0;
187 	char   *core = NULL, *user = NULL, *namelist = NULL;
188 	char	kvmoferr[_POSIX2_LINE_MAX];  /* Error buf for kvm_openfiles. */
189 	int     i;
190 	uid_t   uid;
191 
192 	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
193 		switch (i) {
194 		case 'M':
195 			display = SHMTOTAL;
196 			break;
197 		case 'm':
198 			display = SHMINFO;
199 			break;
200 		case 'Q':
201 			display = MSGTOTAL;
202 			break;
203 		case 'q':
204 			display = MSGINFO;
205 			break;
206 		case 'S':
207 			display = SEMTOTAL;
208 			break;
209 		case 's':
210 			display = SEMINFO;
211 			break;
212 		case 'T':
213 			display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
214 			break;
215 		case 'a':
216 			option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
217 			break;
218 		case 'b':
219 			option |= BIGGEST;
220 			break;
221 		case 'C':
222 			core = optarg;
223 			break;
224 		case 'c':
225 			option |= CREATOR;
226 			break;
227 		case 'N':
228 			namelist = optarg;
229 			break;
230 		case 'o':
231 			option |= OUTSTANDING;
232 			break;
233 		case 'p':
234 			option |= PID;
235 			break;
236 		case 't':
237 			option |= TIME;
238 			break;
239 		case 'y':
240 			use_sysctl = 0;
241 			break;
242 		case 'u':
243 			user = optarg;
244 			uid = user2uid(user);
245 			break;
246 		default:
247 			usage();
248 		}
249 
250 	/*
251 	 * If paths to the exec file or core file were specified, we
252 	 * aren't operating on the running kernel, so we can't use
253 	 * sysctl.
254 	 */
255 	if (namelist != NULL || core != NULL)
256 		use_sysctl = 0;
257 
258 	if (!use_sysctl) {
259 		kd = kvm_openfiles(namelist, core, NULL, O_RDONLY, kvmoferr);
260 		if (kd == NULL)
261 			errx(1, "kvm_openfiles: %s", kvmoferr);
262 		switch (kvm_nlist(kd, symbols)) {
263 		case 0:
264 			break;
265 		case -1:
266 			errx(1, "unable to read kernel symbol table");
267 		default:
268 #ifdef notdef		/* they'll be told more civilly later */
269 			warnx("nlist failed");
270 			for (i = 0; symbols[i].n_name != NULL; i++)
271 				if (symbols[i].n_value == 0)
272 					warnx("symbol %s not found",
273 					    symbols[i].n_name);
274 #endif
275 			break;
276 		}
277 	}
278 
279 	kget(X_MSGINFO, &msginfo, sizeof(msginfo));
280 	if ((display & (MSGINFO | MSGTOTAL))) {
281 		if (display & MSGTOTAL) {
282 			printf("msginfo:\n");
283 			printf("\tmsgmax: %6d\t(max characters in a message)\n",
284 			    msginfo.msgmax);
285 			printf("\tmsgmni: %6d\t(# of message queues)\n",
286 			    msginfo.msgmni);
287 			printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
288 			    msginfo.msgmnb);
289 			printf("\tmsgtql: %6d\t(max # of messages in system)\n",
290 			    msginfo.msgtql);
291 			printf("\tmsgssz: %6d\t(size of a message segment)\n",
292 			    msginfo.msgssz);
293 			printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
294 			    msginfo.msgseg);
295 		}
296 		if (display & MSGINFO) {
297 			struct msqid_kernel *kxmsqids;
298 			size_t kxmsqids_len;
299 
300 
301 			kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
302 			kxmsqids = malloc(kxmsqids_len);
303 			kget(X_MSQIDS, kxmsqids, kxmsqids_len);
304 
305 			printf("Message Queues:\n");
306 			printf("T     ID     KEY        MODE       OWNER    GROUP");
307 			if (option & CREATOR)
308 				printf("  CREATOR   CGROUP");
309 			if (option & OUTSTANDING)
310 				printf(" CBYTES  QNUM");
311 			if (option & BIGGEST)
312 				printf(" QBYTES");
313 			if (option & PID)
314 				printf(" LSPID LRPID");
315 			if (option & TIME)
316 				printf("    STIME    RTIME    CTIME");
317 			printf("\n");
318 			for (i = 0; i < msginfo.msgmni; i += 1) {
319 				if (kxmsqids[i].u.msg_qbytes != 0) {
320 					char    stime_buf[100], rtime_buf[100],
321 					        ctime_buf[100];
322 					struct msqid_kernel *kmsqptr = &kxmsqids[i];
323 
324 					if (user)
325 						if (uid != kmsqptr->u.msg_perm.uid)
326 							continue;
327 					cvt_time(kmsqptr->u.msg_stime, stime_buf);
328 					cvt_time(kmsqptr->u.msg_rtime, rtime_buf);
329 					cvt_time(kmsqptr->u.msg_ctime, ctime_buf);
330 
331 					printf("q %6d %10d %s %8s %8s",
332 					    IXSEQ_TO_IPCID(i, kmsqptr->u.msg_perm),
333 					    (int)kmsqptr->u.msg_perm.key,
334 					    fmt_perm(kmsqptr->u.msg_perm.mode),
335 					    user_from_uid(kmsqptr->u.msg_perm.uid, 0),
336 					    group_from_gid(kmsqptr->u.msg_perm.gid, 0));
337 
338 					if (option & CREATOR)
339 						printf(" %8s %8s",
340 						    user_from_uid(kmsqptr->u.msg_perm.cuid, 0),
341 						    group_from_gid(kmsqptr->u.msg_perm.cgid, 0));
342 
343 					if (option & OUTSTANDING)
344 						printf(" %6lu %6lu",
345 						    kmsqptr->u.msg_cbytes,
346 						    kmsqptr->u.msg_qnum);
347 
348 					if (option & BIGGEST)
349 						printf(" %6lu",
350 						    kmsqptr->u.msg_qbytes);
351 
352 					if (option & PID)
353 						printf(" %6d %6d",
354 						    kmsqptr->u.msg_lspid,
355 						    kmsqptr->u.msg_lrpid);
356 
357 					if (option & TIME)
358 						printf(" %s %s %s",
359 						    stime_buf,
360 						    rtime_buf,
361 						    ctime_buf);
362 
363 					printf("\n");
364 				}
365 			}
366 			printf("\n");
367 		}
368 	} else
369 		if (display & (MSGINFO | MSGTOTAL)) {
370 			fprintf(stderr,
371 			    "SVID messages facility not configured in the system\n");
372 		}
373 
374 	kget(X_SHMINFO, &shminfo, sizeof(shminfo));
375 	if ((display & (SHMINFO | SHMTOTAL))) {
376 		if (display & SHMTOTAL) {
377 			printf("shminfo:\n");
378 			printf("\tshmmax: %7d\t(max shared memory segment size)\n",
379 			    shminfo.shmmax);
380 			printf("\tshmmin: %7d\t(min shared memory segment size)\n",
381 			    shminfo.shmmin);
382 			printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
383 			    shminfo.shmmni);
384 			printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
385 			    shminfo.shmseg);
386 			printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
387 			    shminfo.shmall);
388 		}
389 		if (display & SHMINFO) {
390 			struct shmid_kernel *kxshmids;
391 			size_t kxshmids_len;
392 
393 			kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
394 			kxshmids = malloc(kxshmids_len);
395 			kget(X_SHMSEGS, kxshmids, kxshmids_len);
396 
397 			printf("Shared Memory:\n");
398 			printf("T     ID     KEY        MODE       OWNER    GROUP");
399 			if (option & CREATOR)
400 				printf("  CREATOR   CGROUP");
401 			if (option & OUTSTANDING)
402 				printf(" NATTCH");
403 			if (option & BIGGEST)
404 				printf("  SEGSZ");
405 			if (option & PID)
406 				printf("  CPID  LPID");
407 			if (option & TIME)
408 				printf("    ATIME    DTIME    CTIME");
409 			printf("\n");
410 			for (i = 0; i < shminfo.shmmni; i += 1) {
411 				if (kxshmids[i].u.shm_perm.mode & 0x0800) {
412 					char    atime_buf[100], dtime_buf[100],
413 					        ctime_buf[100];
414 					struct shmid_kernel *kshmptr = &kxshmids[i];
415 
416 					if (user)
417 						if (uid != kshmptr->u.shm_perm.uid)
418 							continue;
419 					cvt_time(kshmptr->u.shm_atime, atime_buf);
420 					cvt_time(kshmptr->u.shm_dtime, dtime_buf);
421 					cvt_time(kshmptr->u.shm_ctime, ctime_buf);
422 
423 					printf("m %6d %10d %s %8s %8s",
424 					    IXSEQ_TO_IPCID(i, kshmptr->u.shm_perm),
425 					    (int)kshmptr->u.shm_perm.key,
426 					    fmt_perm(kshmptr->u.shm_perm.mode),
427 					    user_from_uid(kshmptr->u.shm_perm.uid, 0),
428 					    group_from_gid(kshmptr->u.shm_perm.gid, 0));
429 
430 					if (option & CREATOR)
431 						printf(" %8s %8s",
432 						    user_from_uid(kshmptr->u.shm_perm.cuid, 0),
433 						    group_from_gid(kshmptr->u.shm_perm.cgid, 0));
434 
435 					if (option & OUTSTANDING)
436 						printf(" %6d",
437 						    kshmptr->u.shm_nattch);
438 
439 					if (option & BIGGEST)
440 						printf(" %6d",
441 						    kshmptr->u.shm_segsz);
442 
443 					if (option & PID)
444 						printf(" %6d %6d",
445 						    kshmptr->u.shm_cpid,
446 						    kshmptr->u.shm_lpid);
447 
448 					if (option & TIME)
449 						printf(" %s %s %s",
450 						    atime_buf,
451 						    dtime_buf,
452 						    ctime_buf);
453 
454 					printf("\n");
455 				}
456 			}
457 			printf("\n");
458 		}
459 	} else
460 		if (display & (SHMINFO | SHMTOTAL)) {
461 			fprintf(stderr,
462 			    "SVID shared memory facility not configured in the system\n");
463 		}
464 
465 	kget(X_SEMINFO, &seminfo, sizeof(seminfo));
466 	if ((display & (SEMINFO | SEMTOTAL))) {
467 		struct semid_kernel *kxsema;
468 		size_t kxsema_len;
469 
470 		if (display & SEMTOTAL) {
471 			printf("seminfo:\n");
472 			printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
473 			    seminfo.semmap);
474 			printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
475 			    seminfo.semmni);
476 			printf("\tsemmns: %6d\t(# of semaphores in system)\n",
477 			    seminfo.semmns);
478 			printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
479 			    seminfo.semmnu);
480 			printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
481 			    seminfo.semmsl);
482 			printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
483 			    seminfo.semopm);
484 			printf("\tsemume: %6d\t(max # of undo entries per process)\n",
485 			    seminfo.semume);
486 			printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
487 			    seminfo.semusz);
488 			printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
489 			    seminfo.semvmx);
490 			printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
491 			    seminfo.semaem);
492 		}
493 		if (display & SEMINFO) {
494 			kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
495 			kxsema = malloc(kxsema_len);
496 			kget(X_SEMA, kxsema, kxsema_len);
497 
498 			printf("Semaphores:\n");
499 			printf("T     ID     KEY        MODE       OWNER    GROUP");
500 			if (option & CREATOR)
501 				printf("  CREATOR   CGROUP");
502 			if (option & BIGGEST)
503 				printf(" NSEMS");
504 			if (option & TIME)
505 				printf("    OTIME    CTIME");
506 			printf("\n");
507 			for (i = 0; i < seminfo.semmni; i += 1) {
508 				if ((kxsema[i].u.sem_perm.mode & SEM_ALLOC) != 0) {
509 					char    ctime_buf[100], otime_buf[100];
510 					struct semid_kernel *ksemaptr = &kxsema[i];
511 
512 					if (user)
513 						if (uid != ksemaptr->u.sem_perm.uid)
514 							continue;
515 					cvt_time(ksemaptr->u.sem_otime, otime_buf);
516 					cvt_time(ksemaptr->u.sem_ctime, ctime_buf);
517 
518 					printf("s %6d %10d %s %8s %8s",
519 					    IXSEQ_TO_IPCID(i, ksemaptr->u.sem_perm),
520 					    (int)ksemaptr->u.sem_perm.key,
521 					    fmt_perm(ksemaptr->u.sem_perm.mode),
522 					    user_from_uid(ksemaptr->u.sem_perm.uid, 0),
523 					    group_from_gid(ksemaptr->u.sem_perm.gid, 0));
524 
525 					if (option & CREATOR)
526 						printf(" %8s %8s",
527 						    user_from_uid(ksemaptr->u.sem_perm.cuid, 0),
528 						    group_from_gid(ksemaptr->u.sem_perm.cgid, 0));
529 
530 					if (option & BIGGEST)
531 						printf(" %6d",
532 						    ksemaptr->u.sem_nsems);
533 
534 					if (option & TIME)
535 						printf(" %s %s",
536 						    otime_buf,
537 						    ctime_buf);
538 
539 					printf("\n");
540 				}
541 			}
542 
543 			printf("\n");
544 		}
545 	} else
546 		if (display & (SEMINFO | SEMTOTAL)) {
547 			fprintf(stderr, "SVID semaphores facility not configured in the system\n");
548 		}
549 	if (!use_sysctl)
550 		kvm_close(kd);
551 
552 	exit(0);
553 }
554 
555 void
556 sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vecarr)
557 {
558 	struct scgs_vector *xp;
559 	size_t tsiz;
560 	int rv;
561 
562 	for (xp = vecarr; xp->sysctl != NULL; xp++) {
563 		assert(xp->offset <= size);
564 		tsiz = xp->size;
565 		rv = sysctlbyname(xp->sysctl, (char *)addr + xp->offset,
566 		    &tsiz, NULL, 0);
567 		if (rv == -1)
568 			errx(1, "sysctlbyname: %s", xp->sysctl);
569 		if (tsiz != xp->size)
570 			errx(1, "%s size mismatch (expected %d, got %d)",
571 			    xp->sysctl, xp->size, tsiz);
572 	}
573 }
574 
575 void
576 kget(int idx, void *addr, size_t size)
577 {
578 	char *symn;			/* symbol name */
579 	size_t tsiz;
580 	int rv;
581 	unsigned long kaddr;
582 	const char *sym2sysctl[] = {	/* symbol to sysctl name table */
583 		"kern.ipc.sema",
584 		"kern.ipc.seminfo",
585 		"kern.ipc.msginfo",
586 		"kern.ipc.msqids",
587 		"kern.ipc.shminfo",
588 		"kern.ipc.shmsegs" };
589 
590 	assert((unsigned)idx <= sizeof(sym2sysctl) / sizeof(*sym2sysctl));
591 	if (!use_sysctl) {
592 		symn = symbols[idx].n_name;
593 		if (*symn == '_')
594 			symn++;
595 		if (symbols[idx].n_type == 0 || symbols[idx].n_value == 0)
596 			errx(1, "symbol %s undefined", symn);
597 		/*
598 		 * For some symbols, the value we retreieve is
599 		 * actually a pointer; since we want the actual value,
600 		 * we have to manually dereference it.
601 		 */
602 		switch (idx) {
603 		case X_MSQIDS:
604 			tsiz = sizeof(msqids);
605 			rv = kvm_read(kd, symbols[idx].n_value,
606 			    &msqids, tsiz);
607 			kaddr = (u_long)msqids;
608 			break;
609 		case X_SHMSEGS:
610 			tsiz = sizeof(shmsegs);
611 			rv = kvm_read(kd, symbols[idx].n_value,
612 			    &shmsegs, tsiz);
613 			kaddr = (u_long)shmsegs;
614 			break;
615 		case X_SEMA:
616 			tsiz = sizeof(sema);
617 			rv = kvm_read(kd, symbols[idx].n_value,
618 			    &sema, tsiz);
619 			kaddr = (u_long)sema;
620 			break;
621 		default:
622 			rv = tsiz = 0;
623 			kaddr = symbols[idx].n_value;
624 			break;
625 		}
626 		if ((unsigned)rv != tsiz)
627 			errx(1, "%s: %s", symn, kvm_geterr(kd));
628 		if ((unsigned)kvm_read(kd, kaddr, addr, size) != size)
629 			errx(1, "%s: %s", symn, kvm_geterr(kd));
630 	} else {
631 		switch (idx) {
632 		case X_SHMINFO:
633 			sysctlgatherstruct(addr, size, shminfo_scgsv);
634 			break;
635 		case X_SEMINFO:
636 			sysctlgatherstruct(addr, size, seminfo_scgsv);
637 			break;
638 		case X_MSGINFO:
639 			sysctlgatherstruct(addr, size, msginfo_scgsv);
640 			break;
641 		default:
642 			tsiz = size;
643 			rv = sysctlbyname(sym2sysctl[idx], addr, &tsiz,
644 			    NULL, 0);
645 			if (rv == -1)
646 				err(1, "sysctlbyname: %s", sym2sysctl[idx]);
647 			if (tsiz != size)
648 				errx(1, "%s size mismatch "
649 				    "(expected %d, got %d)",
650 				    sym2sysctl[idx], size, tsiz);
651 			break;
652 		}
653 	}
654 }
655 
656 uid_t
657 user2uid(char *username)
658 {
659 	struct passwd *pwd;
660 	uid_t uid;
661 	char *r;
662 
663 	uid = strtoul(username, &r, 0);
664 	if (!*r && r != username)
665 		return (uid);
666 	if ((pwd = getpwnam(username)) == NULL)
667 		errx(1, "getpwnam failed: No such user");
668 	endpwent();
669 	return (pwd->pw_uid);
670 }
671 
672 void
673 usage(void)
674 {
675 
676 	fprintf(stderr,
677 	    "usage: ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");
678 	exit(1);
679 }
680