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