ipcs.c revision 1.41 1 1.41 martin /* $NetBSD: ipcs.c,v 1.41 2008/04/28 20:24:13 martin Exp $ */
2 1.23 simonb
3 1.23 simonb /*-
4 1.23 simonb * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.23 simonb * All rights reserved.
6 1.23 simonb *
7 1.23 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.23 simonb * by Simon Burge.
9 1.23 simonb *
10 1.23 simonb * Redistribution and use in source and binary forms, with or without
11 1.23 simonb * modification, are permitted provided that the following conditions
12 1.23 simonb * are met:
13 1.23 simonb * 1. Redistributions of source code must retain the above copyright
14 1.23 simonb * notice, this list of conditions and the following disclaimer.
15 1.23 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.23 simonb * notice, this list of conditions and the following disclaimer in the
17 1.23 simonb * documentation and/or other materials provided with the distribution.
18 1.23 simonb *
19 1.23 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.23 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.23 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.23 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.23 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.23 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.23 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.23 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.23 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.23 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.23 simonb * POSSIBILITY OF SUCH DAMAGE.
30 1.23 simonb */
31 1.8 cgd
32 1.1 cgd /*
33 1.6 cgd * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo (at) sigmasoft.com>
34 1.6 cgd * All rights reserved.
35 1.1 cgd *
36 1.6 cgd * Redistribution and use in source and binary forms, with or without
37 1.6 cgd * modification, are permitted provided that the following conditions
38 1.6 cgd * are met:
39 1.6 cgd * 1. Redistributions of source code must retain the above copyright
40 1.6 cgd * notice, this list of conditions and the following disclaimer.
41 1.6 cgd * 2. Redistributions in binary form must reproduce the above copyright
42 1.6 cgd * notice, this list of conditions and the following disclaimer in the
43 1.6 cgd * documentation and/or other materials provided with the distribution.
44 1.6 cgd *
45 1.6 cgd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
46 1.6 cgd * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
47 1.6 cgd * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
48 1.6 cgd * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49 1.6 cgd * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
50 1.6 cgd * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
51 1.6 cgd * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
52 1.6 cgd * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
53 1.6 cgd * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
54 1.6 cgd * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 1.1 cgd */
56 1.1 cgd
57 1.38 perry #include <sys/cdefs.h>
58 1.1 cgd #include <sys/param.h>
59 1.23 simonb #include <sys/sysctl.h>
60 1.40 rmind #include <sys/inttypes.h>
61 1.1 cgd #include <sys/ipc.h>
62 1.1 cgd #include <sys/sem.h>
63 1.1 cgd #include <sys/shm.h>
64 1.1 cgd #include <sys/msg.h>
65 1.1 cgd
66 1.11 thorpej #include <err.h>
67 1.11 thorpej #include <fcntl.h>
68 1.14 lukem #include <grp.h>
69 1.11 thorpej #include <kvm.h>
70 1.11 thorpej #include <limits.h>
71 1.11 thorpej #include <nlist.h>
72 1.11 thorpej #include <paths.h>
73 1.14 lukem #include <pwd.h>
74 1.11 thorpej #include <stdio.h>
75 1.11 thorpej #include <stdlib.h>
76 1.11 thorpej #include <string.h>
77 1.15 kleink #include <time.h>
78 1.11 thorpej #include <unistd.h>
79 1.19 augustss
80 1.35 christos #define SHMINFO 1
81 1.35 christos #define SHMTOTAL 2
82 1.35 christos #define MSGINFO 4
83 1.35 christos #define MSGTOTAL 8
84 1.35 christos #define SEMINFO 16
85 1.35 christos #define SEMTOTAL 32
86 1.35 christos
87 1.35 christos #define BIGGEST 1
88 1.35 christos #define CREATOR 2
89 1.35 christos #define OUTSTANDING 4
90 1.35 christos #define PID 8
91 1.35 christos #define TIME 16
92 1.35 christos
93 1.35 christos static char *core = NULL, *namelist = NULL;
94 1.35 christos static int display = 0;
95 1.35 christos static int option = 0;
96 1.35 christos
97 1.35 christos static void cvt_time(time_t, char *, size_t);
98 1.35 christos static char *fmt_perm(u_short);
99 1.35 christos static void ipcs_kvm(void);
100 1.35 christos static void msg_sysctl(void);
101 1.35 christos static void sem_sysctl(void);
102 1.35 christos static void shm_sysctl(void);
103 1.35 christos static void show_msginfo(time_t, time_t, time_t, int, u_int64_t, mode_t,
104 1.35 christos uid_t, gid_t, uid_t, gid_t, u_int64_t, u_int64_t, u_int64_t, pid_t, pid_t);
105 1.35 christos static void show_msginfo_hdr(void);
106 1.35 christos static void show_msgtotal(struct msginfo *);
107 1.35 christos static void show_seminfo_hdr(void);
108 1.35 christos static void show_seminfo(time_t, time_t, int, u_int64_t, mode_t, uid_t,
109 1.35 christos gid_t, uid_t, gid_t, int16_t);
110 1.35 christos static void show_semtotal(struct seminfo *);
111 1.35 christos static void show_shminfo(time_t, time_t, time_t, int, u_int64_t, mode_t,
112 1.35 christos uid_t, gid_t, uid_t, gid_t, u_int32_t, u_int64_t, pid_t, pid_t);
113 1.35 christos static void show_shminfo_hdr(void);
114 1.35 christos static void show_shmtotal(struct shminfo *);
115 1.39 perry static void usage(void) __dead;
116 1.35 christos static void unconfsem(void);
117 1.35 christos static void unconfmsg(void);
118 1.35 christos static void unconfshm(void);
119 1.35 christos
120 1.35 christos static void
121 1.35 christos unconfsem(void)
122 1.35 christos {
123 1.35 christos warnx("SVID semaphores facility not configured in the system");
124 1.35 christos }
125 1.35 christos
126 1.35 christos static void
127 1.35 christos unconfmsg(void)
128 1.35 christos {
129 1.35 christos warnx("SVID messages facility not configured in the system");
130 1.35 christos }
131 1.35 christos
132 1.35 christos static void
133 1.35 christos unconfshm(void)
134 1.35 christos {
135 1.35 christos warnx("SVID shared memory facility not configured in the system");
136 1.35 christos }
137 1.4 cgd
138 1.35 christos static char *
139 1.23 simonb fmt_perm(u_short mode)
140 1.1 cgd {
141 1.17 mrg static char buffer[12];
142 1.1 cgd
143 1.1 cgd buffer[0] = '-';
144 1.1 cgd buffer[1] = '-';
145 1.1 cgd buffer[2] = ((mode & 0400) ? 'r' : '-');
146 1.1 cgd buffer[3] = ((mode & 0200) ? 'w' : '-');
147 1.1 cgd buffer[4] = ((mode & 0100) ? 'a' : '-');
148 1.1 cgd buffer[5] = ((mode & 0040) ? 'r' : '-');
149 1.1 cgd buffer[6] = ((mode & 0020) ? 'w' : '-');
150 1.1 cgd buffer[7] = ((mode & 0010) ? 'a' : '-');
151 1.1 cgd buffer[8] = ((mode & 0004) ? 'r' : '-');
152 1.1 cgd buffer[9] = ((mode & 0002) ? 'w' : '-');
153 1.1 cgd buffer[10] = ((mode & 0001) ? 'a' : '-');
154 1.1 cgd buffer[11] = '\0';
155 1.1 cgd return (&buffer[0]);
156 1.1 cgd }
157 1.1 cgd
158 1.35 christos static void
159 1.23 simonb cvt_time(time_t t, char *buf, size_t buflen)
160 1.1 cgd {
161 1.4 cgd struct tm *tm;
162 1.1 cgd
163 1.30 itojun if (t == 0)
164 1.30 itojun (void)strlcpy(buf, "no-entry", buflen);
165 1.30 itojun else {
166 1.4 cgd tm = localtime(&t);
167 1.13 mrg (void)snprintf(buf, buflen, "%2d:%02d:%02d",
168 1.4 cgd tm->tm_hour, tm->tm_min, tm->tm_sec);
169 1.1 cgd }
170 1.1 cgd }
171 1.4 cgd int
172 1.23 simonb main(int argc, char *argv[])
173 1.23 simonb {
174 1.23 simonb int i;
175 1.34 simonb time_t now;
176 1.4 cgd
177 1.14 lukem while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
178 1.4 cgd switch (i) {
179 1.4 cgd case 'M':
180 1.32 simonb display |= SHMTOTAL;
181 1.4 cgd break;
182 1.4 cgd case 'm':
183 1.32 simonb display |= SHMINFO;
184 1.4 cgd break;
185 1.4 cgd case 'Q':
186 1.32 simonb display |= MSGTOTAL;
187 1.4 cgd break;
188 1.4 cgd case 'q':
189 1.32 simonb display |= MSGINFO;
190 1.4 cgd break;
191 1.4 cgd case 'S':
192 1.32 simonb display |= SEMTOTAL;
193 1.4 cgd break;
194 1.4 cgd case 's':
195 1.32 simonb display |= SEMINFO;
196 1.4 cgd break;
197 1.4 cgd case 'T':
198 1.32 simonb display |= SHMTOTAL | MSGTOTAL | SEMTOTAL;
199 1.4 cgd break;
200 1.4 cgd case 'a':
201 1.4 cgd option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
202 1.4 cgd break;
203 1.4 cgd case 'b':
204 1.4 cgd option |= BIGGEST;
205 1.4 cgd break;
206 1.4 cgd case 'C':
207 1.4 cgd core = optarg;
208 1.4 cgd break;
209 1.4 cgd case 'c':
210 1.4 cgd option |= CREATOR;
211 1.4 cgd break;
212 1.4 cgd case 'N':
213 1.4 cgd namelist = optarg;
214 1.4 cgd break;
215 1.4 cgd case 'o':
216 1.4 cgd option |= OUTSTANDING;
217 1.4 cgd break;
218 1.4 cgd case 'p':
219 1.4 cgd option |= PID;
220 1.4 cgd break;
221 1.4 cgd case 't':
222 1.4 cgd option |= TIME;
223 1.4 cgd break;
224 1.4 cgd default:
225 1.5 cgd usage();
226 1.4 cgd }
227 1.11 thorpej
228 1.24 simonb if (argc - optind > 0)
229 1.24 simonb usage();
230 1.24 simonb
231 1.35 christos (void)time(&now);
232 1.35 christos (void)printf("IPC status from %s as of %s\n",
233 1.35 christos /* and extra \n from ctime(3) */
234 1.34 simonb core == NULL ? "<running system>" : core, ctime(&now));
235 1.34 simonb
236 1.32 simonb if (display == 0)
237 1.32 simonb display = SHMINFO | MSGINFO | SEMINFO;
238 1.32 simonb
239 1.25 simonb if (core == NULL) {
240 1.23 simonb if (display & (MSGINFO | MSGTOTAL))
241 1.23 simonb msg_sysctl();
242 1.23 simonb if (display & (SHMINFO | SHMTOTAL))
243 1.23 simonb shm_sysctl();
244 1.23 simonb if (display & (SEMINFO | SEMTOTAL))
245 1.23 simonb sem_sysctl();
246 1.23 simonb } else
247 1.23 simonb ipcs_kvm();
248 1.35 christos return 0;
249 1.23 simonb }
250 1.23 simonb
251 1.35 christos static void
252 1.23 simonb show_msgtotal(struct msginfo *msginfo)
253 1.23 simonb {
254 1.35 christos (void)printf("msginfo:\n");
255 1.35 christos (void)printf("\tmsgmax: %6d\t(max characters in a message)\n",
256 1.23 simonb msginfo->msgmax);
257 1.35 christos (void)printf("\tmsgmni: %6d\t(# of message queues)\n",
258 1.23 simonb msginfo->msgmni);
259 1.35 christos (void)printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
260 1.23 simonb msginfo->msgmnb);
261 1.35 christos (void)printf("\tmsgtql: %6d\t(max # of messages in system)\n",
262 1.23 simonb msginfo->msgtql);
263 1.35 christos (void)printf("\tmsgssz: %6d\t(size of a message segment)\n",
264 1.23 simonb msginfo->msgssz);
265 1.35 christos (void)printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
266 1.23 simonb msginfo->msgseg);
267 1.23 simonb }
268 1.23 simonb
269 1.35 christos static void
270 1.23 simonb show_shmtotal(struct shminfo *shminfo)
271 1.23 simonb {
272 1.35 christos (void)printf("shminfo:\n");
273 1.40 rmind (void)printf("\tshmmax: %" PRIu64 "\t(max shared memory segment size)\n",
274 1.23 simonb shminfo->shmmax);
275 1.35 christos (void)printf("\tshmmin: %7d\t(min shared memory segment size)\n",
276 1.23 simonb shminfo->shmmin);
277 1.35 christos (void)printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
278 1.23 simonb shminfo->shmmni);
279 1.35 christos (void)printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
280 1.23 simonb shminfo->shmseg);
281 1.35 christos (void)printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
282 1.23 simonb shminfo->shmall);
283 1.23 simonb }
284 1.23 simonb
285 1.35 christos static void
286 1.23 simonb show_semtotal(struct seminfo *seminfo)
287 1.23 simonb {
288 1.35 christos (void)printf("seminfo:\n");
289 1.35 christos (void)printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
290 1.23 simonb seminfo->semmap);
291 1.35 christos (void)printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
292 1.23 simonb seminfo->semmni);
293 1.35 christos (void)printf("\tsemmns: %6d\t(# of semaphores in system)\n",
294 1.23 simonb seminfo->semmns);
295 1.35 christos (void)printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
296 1.23 simonb seminfo->semmnu);
297 1.35 christos (void)printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
298 1.23 simonb seminfo->semmsl);
299 1.35 christos (void)printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
300 1.23 simonb seminfo->semopm);
301 1.35 christos (void)printf("\tsemume: %6d\t(max # of undo entries per process)\n",
302 1.23 simonb seminfo->semume);
303 1.35 christos (void)printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
304 1.23 simonb seminfo->semusz);
305 1.35 christos (void)printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
306 1.23 simonb seminfo->semvmx);
307 1.35 christos (void)printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
308 1.23 simonb seminfo->semaem);
309 1.23 simonb }
310 1.23 simonb
311 1.35 christos static void
312 1.23 simonb show_msginfo_hdr(void)
313 1.23 simonb {
314 1.35 christos (void)printf("Message Queues:\n");
315 1.35 christos (void)printf("T ID KEY MODE OWNER GROUP");
316 1.23 simonb if (option & CREATOR)
317 1.35 christos (void)printf(" CREATOR CGROUP");
318 1.23 simonb if (option & OUTSTANDING)
319 1.35 christos (void)printf(" CBYTES QNUM");
320 1.23 simonb if (option & BIGGEST)
321 1.35 christos (void)printf(" QBYTES");
322 1.23 simonb if (option & PID)
323 1.35 christos (void)printf(" LSPID LRPID");
324 1.23 simonb if (option & TIME)
325 1.35 christos (void)printf(" STIME RTIME CTIME");
326 1.35 christos (void)printf("\n");
327 1.23 simonb }
328 1.23 simonb
329 1.35 christos static void
330 1.35 christos show_msginfo(time_t s_time, time_t r_time, time_t c_time, int ipcid,
331 1.35 christos u_int64_t key,
332 1.23 simonb mode_t mode, uid_t uid, gid_t gid, uid_t cuid, gid_t cgid,
333 1.23 simonb u_int64_t cbytes, u_int64_t qnum, u_int64_t qbytes, pid_t lspid,
334 1.23 simonb pid_t lrpid)
335 1.23 simonb {
336 1.35 christos char s_time_buf[100], r_time_buf[100], c_time_buf[100];
337 1.23 simonb
338 1.23 simonb if (option & TIME) {
339 1.35 christos cvt_time(s_time, s_time_buf, sizeof(s_time_buf));
340 1.35 christos cvt_time(r_time, r_time_buf, sizeof(r_time_buf));
341 1.35 christos cvt_time(c_time, c_time_buf, sizeof(c_time_buf));
342 1.23 simonb }
343 1.23 simonb
344 1.35 christos (void)printf("q %9d %10lld %s %8s %8s", ipcid, (long long)key, fmt_perm(mode),
345 1.23 simonb user_from_uid(uid, 0), group_from_gid(gid, 0));
346 1.23 simonb
347 1.23 simonb if (option & CREATOR)
348 1.35 christos (void)printf(" %8s %8s", user_from_uid(cuid, 0),
349 1.23 simonb group_from_gid(cgid, 0));
350 1.23 simonb
351 1.23 simonb if (option & OUTSTANDING)
352 1.35 christos (void)printf(" %6lld %5lld", (long long)cbytes, (long long)qnum);
353 1.23 simonb
354 1.23 simonb if (option & BIGGEST)
355 1.35 christos (void)printf(" %6lld", (long long)qbytes);
356 1.23 simonb
357 1.23 simonb if (option & PID)
358 1.35 christos (void)printf(" %5d %5d", lspid, lrpid);
359 1.23 simonb
360 1.23 simonb if (option & TIME)
361 1.35 christos (void)printf(" %s %s %s", s_time_buf, r_time_buf, c_time_buf);
362 1.23 simonb
363 1.35 christos (void)printf("\n");
364 1.23 simonb }
365 1.23 simonb
366 1.35 christos static void
367 1.23 simonb show_shminfo_hdr(void)
368 1.23 simonb {
369 1.35 christos (void)printf("Shared Memory:\n");
370 1.35 christos (void)printf("T ID KEY MODE OWNER GROUP");
371 1.23 simonb if (option & CREATOR)
372 1.35 christos (void)printf(" CREATOR CGROUP");
373 1.23 simonb if (option & OUTSTANDING)
374 1.35 christos (void)printf(" NATTCH");
375 1.23 simonb if (option & BIGGEST)
376 1.35 christos (void)printf(" SEGSZ");
377 1.23 simonb if (option & PID)
378 1.35 christos (void)printf(" CPID LPID");
379 1.23 simonb if (option & TIME)
380 1.35 christos (void)printf(" ATIME DTIME CTIME");
381 1.35 christos (void)printf("\n");
382 1.23 simonb }
383 1.23 simonb
384 1.35 christos static void
385 1.35 christos show_shminfo(time_t atime, time_t dtime, time_t c_time, int ipcid, u_int64_t key,
386 1.23 simonb mode_t mode, uid_t uid, gid_t gid, uid_t cuid, gid_t cgid,
387 1.23 simonb u_int32_t nattch, u_int64_t segsz, pid_t cpid, pid_t lpid)
388 1.23 simonb {
389 1.35 christos char atime_buf[100], dtime_buf[100], c_time_buf[100];
390 1.23 simonb
391 1.23 simonb if (option & TIME) {
392 1.23 simonb cvt_time(atime, atime_buf, sizeof(atime_buf));
393 1.23 simonb cvt_time(dtime, dtime_buf, sizeof(dtime_buf));
394 1.35 christos cvt_time(c_time, c_time_buf, sizeof(c_time_buf));
395 1.23 simonb }
396 1.23 simonb
397 1.35 christos (void)printf("m %9d %10lld %s %8s %8s", ipcid, (long long)key, fmt_perm(mode),
398 1.23 simonb user_from_uid(uid, 0), group_from_gid(gid, 0));
399 1.23 simonb
400 1.23 simonb if (option & CREATOR)
401 1.35 christos (void)printf(" %8s %8s", user_from_uid(cuid, 0),
402 1.23 simonb group_from_gid(cgid, 0));
403 1.23 simonb
404 1.23 simonb if (option & OUTSTANDING)
405 1.35 christos (void)printf(" %6d", nattch);
406 1.23 simonb
407 1.23 simonb if (option & BIGGEST)
408 1.35 christos (void)printf(" %7llu", (long long)segsz);
409 1.23 simonb
410 1.23 simonb if (option & PID)
411 1.35 christos (void)printf(" %5d %5d", cpid, lpid);
412 1.23 simonb
413 1.23 simonb if (option & TIME)
414 1.35 christos (void)printf(" %s %s %s",
415 1.23 simonb atime_buf,
416 1.23 simonb dtime_buf,
417 1.35 christos c_time_buf);
418 1.23 simonb
419 1.35 christos (void)printf("\n");
420 1.23 simonb }
421 1.23 simonb
422 1.35 christos static void
423 1.23 simonb show_seminfo_hdr(void)
424 1.23 simonb {
425 1.35 christos (void)printf("Semaphores:\n");
426 1.35 christos (void)printf("T ID KEY MODE OWNER GROUP");
427 1.23 simonb if (option & CREATOR)
428 1.35 christos (void)printf(" CREATOR CGROUP");
429 1.23 simonb if (option & BIGGEST)
430 1.35 christos (void)printf(" NSEMS");
431 1.23 simonb if (option & TIME)
432 1.35 christos (void)printf(" OTIME CTIME");
433 1.35 christos (void)printf("\n");
434 1.23 simonb }
435 1.23 simonb
436 1.35 christos static void
437 1.35 christos show_seminfo(time_t otime, time_t c_time, int ipcid, u_int64_t key, mode_t mode,
438 1.23 simonb uid_t uid, gid_t gid, uid_t cuid, gid_t cgid, int16_t nsems)
439 1.23 simonb {
440 1.35 christos char c_time_buf[100], otime_buf[100];
441 1.23 simonb
442 1.23 simonb if (option & TIME) {
443 1.23 simonb cvt_time(otime, otime_buf, sizeof(otime_buf));
444 1.35 christos cvt_time(c_time, c_time_buf, sizeof(c_time_buf));
445 1.23 simonb }
446 1.23 simonb
447 1.35 christos (void)printf("s %9d %10lld %s %8s %8s", ipcid, (long long)key, fmt_perm(mode),
448 1.23 simonb user_from_uid(uid, 0), group_from_gid(gid, 0));
449 1.23 simonb
450 1.23 simonb if (option & CREATOR)
451 1.35 christos (void)printf(" %8s %8s", user_from_uid(cuid, 0),
452 1.23 simonb group_from_gid(cgid, 0));
453 1.23 simonb
454 1.23 simonb if (option & BIGGEST)
455 1.35 christos (void)printf(" %5d", nsems);
456 1.23 simonb
457 1.23 simonb if (option & TIME)
458 1.35 christos (void)printf(" %s %s", otime_buf, c_time_buf);
459 1.23 simonb
460 1.35 christos (void)printf("\n");
461 1.23 simonb }
462 1.23 simonb
463 1.35 christos static void
464 1.23 simonb msg_sysctl(void)
465 1.23 simonb {
466 1.23 simonb struct msg_sysctl_info *msgsi;
467 1.35 christos void *buf;
468 1.37 christos int mib[4];
469 1.23 simonb size_t len;
470 1.23 simonb int i, valid;
471 1.23 simonb
472 1.23 simonb mib[0] = CTL_KERN;
473 1.37 christos mib[1] = KERN_SYSVIPC;
474 1.37 christos mib[2] = KERN_SYSVIPC_MSG;
475 1.23 simonb len = sizeof(valid);
476 1.37 christos if (sysctl(mib, 3, &valid, &len, NULL, 0) < 0) {
477 1.37 christos warn("sysctl(KERN_SYSVIPC_MSG)");
478 1.23 simonb return;
479 1.23 simonb }
480 1.23 simonb if (!valid) {
481 1.35 christos unconfmsg();
482 1.23 simonb return;
483 1.23 simonb }
484 1.23 simonb
485 1.23 simonb mib[0] = CTL_KERN;
486 1.37 christos mib[1] = KERN_SYSVIPC;
487 1.37 christos mib[2] = KERN_SYSVIPC_INFO;
488 1.37 christos mib[3] = KERN_SYSVIPC_MSG_INFO;
489 1.23 simonb
490 1.23 simonb if (!(display & MSGINFO)) {
491 1.23 simonb /* totals only */
492 1.23 simonb len = sizeof(struct msginfo);
493 1.23 simonb } else {
494 1.37 christos if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0) {
495 1.35 christos warn("sysctl(KERN_SYSVIPC_MSG_INFO)");
496 1.23 simonb return;
497 1.23 simonb }
498 1.23 simonb }
499 1.23 simonb
500 1.23 simonb if ((buf = malloc(len)) == NULL)
501 1.23 simonb err(1, "malloc");
502 1.23 simonb msgsi = (struct msg_sysctl_info *)buf;
503 1.37 christos if (sysctl(mib, 4, msgsi, &len, NULL, 0) < 0) {
504 1.35 christos warn("sysctl(KERN_SYSVIPC_MSG_INFO)");
505 1.35 christos goto done;
506 1.23 simonb }
507 1.23 simonb
508 1.23 simonb if (display & MSGTOTAL)
509 1.23 simonb show_msgtotal(&msgsi->msginfo);
510 1.23 simonb
511 1.23 simonb if (display & MSGINFO) {
512 1.23 simonb show_msginfo_hdr();
513 1.23 simonb for (i = 0; i < msgsi->msginfo.msgmni; i++) {
514 1.23 simonb struct msgid_ds_sysctl *msqptr = &msgsi->msgids[i];
515 1.23 simonb if (msqptr->msg_qbytes != 0)
516 1.23 simonb show_msginfo(msqptr->msg_stime,
517 1.23 simonb msqptr->msg_rtime,
518 1.23 simonb msqptr->msg_ctime,
519 1.23 simonb IXSEQ_TO_IPCID(i, msqptr->msg_perm),
520 1.23 simonb msqptr->msg_perm._key,
521 1.23 simonb msqptr->msg_perm.mode,
522 1.23 simonb msqptr->msg_perm.uid,
523 1.23 simonb msqptr->msg_perm.gid,
524 1.23 simonb msqptr->msg_perm.cuid,
525 1.23 simonb msqptr->msg_perm.cgid,
526 1.23 simonb msqptr->_msg_cbytes,
527 1.23 simonb msqptr->msg_qnum,
528 1.23 simonb msqptr->msg_qbytes,
529 1.23 simonb msqptr->msg_lspid,
530 1.23 simonb msqptr->msg_lrpid);
531 1.23 simonb }
532 1.35 christos (void)printf("\n");
533 1.23 simonb }
534 1.35 christos done:
535 1.35 christos free(buf);
536 1.23 simonb }
537 1.23 simonb
538 1.35 christos static void
539 1.23 simonb shm_sysctl(void)
540 1.23 simonb {
541 1.23 simonb struct shm_sysctl_info *shmsi;
542 1.35 christos void *buf;
543 1.37 christos int mib[4];
544 1.23 simonb size_t len;
545 1.23 simonb int i /*, valid */;
546 1.23 simonb long valid;
547 1.23 simonb
548 1.23 simonb mib[0] = CTL_KERN;
549 1.37 christos mib[1] = KERN_SYSVIPC;
550 1.37 christos mib[2] = KERN_SYSVIPC_SHM;
551 1.23 simonb len = sizeof(valid);
552 1.37 christos if (sysctl(mib, 3, &valid, &len, NULL, 0) < 0) {
553 1.37 christos warn("sysctl(KERN_SYSVIPC_SHM)");
554 1.23 simonb return;
555 1.23 simonb }
556 1.23 simonb if (!valid) {
557 1.35 christos unconfshm();
558 1.23 simonb return;
559 1.23 simonb }
560 1.23 simonb
561 1.23 simonb mib[0] = CTL_KERN;
562 1.37 christos mib[1] = KERN_SYSVIPC;
563 1.37 christos mib[2] = KERN_SYSVIPC_INFO;
564 1.37 christos mib[3] = KERN_SYSVIPC_SHM_INFO;
565 1.23 simonb
566 1.23 simonb if (!(display & SHMINFO)) {
567 1.23 simonb /* totals only */
568 1.23 simonb len = sizeof(struct shminfo);
569 1.23 simonb } else {
570 1.37 christos if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0) {
571 1.35 christos warn("sysctl(KERN_SYSVIPC_SHM_INFO)");
572 1.23 simonb return;
573 1.23 simonb }
574 1.23 simonb }
575 1.23 simonb
576 1.23 simonb if ((buf = malloc(len)) == NULL)
577 1.23 simonb err(1, "malloc");
578 1.23 simonb shmsi = (struct shm_sysctl_info *)buf;
579 1.37 christos if (sysctl(mib, 4, shmsi, &len, NULL, 0) < 0) {
580 1.35 christos warn("sysctl(KERN_SYSVIPC_SHM_INFO)");
581 1.35 christos goto done;
582 1.23 simonb }
583 1.23 simonb
584 1.23 simonb if (display & SHMTOTAL)
585 1.23 simonb show_shmtotal(&shmsi->shminfo);
586 1.23 simonb
587 1.23 simonb if (display & SHMINFO) {
588 1.23 simonb show_shminfo_hdr();
589 1.23 simonb for (i = 0; i < shmsi->shminfo.shmmni; i++) {
590 1.23 simonb struct shmid_ds_sysctl *shmptr = &shmsi->shmids[i];
591 1.23 simonb if (shmptr->shm_perm.mode & 0x0800)
592 1.23 simonb show_shminfo(shmptr->shm_atime,
593 1.23 simonb shmptr->shm_dtime,
594 1.23 simonb shmptr->shm_ctime,
595 1.23 simonb IXSEQ_TO_IPCID(i, shmptr->shm_perm),
596 1.23 simonb shmptr->shm_perm._key,
597 1.23 simonb shmptr->shm_perm.mode,
598 1.23 simonb shmptr->shm_perm.uid,
599 1.23 simonb shmptr->shm_perm.gid,
600 1.23 simonb shmptr->shm_perm.cuid,
601 1.23 simonb shmptr->shm_perm.cgid,
602 1.23 simonb shmptr->shm_nattch,
603 1.23 simonb shmptr->shm_segsz,
604 1.23 simonb shmptr->shm_cpid,
605 1.23 simonb shmptr->shm_lpid);
606 1.23 simonb }
607 1.35 christos (void)printf("\n");
608 1.23 simonb }
609 1.35 christos done:
610 1.35 christos free(buf);
611 1.23 simonb }
612 1.23 simonb
613 1.35 christos static void
614 1.23 simonb sem_sysctl(void)
615 1.23 simonb {
616 1.23 simonb struct sem_sysctl_info *semsi;
617 1.35 christos void *buf;
618 1.37 christos int mib[4];
619 1.23 simonb size_t len;
620 1.23 simonb int i, valid;
621 1.23 simonb
622 1.23 simonb mib[0] = CTL_KERN;
623 1.37 christos mib[1] = KERN_SYSVIPC;
624 1.37 christos mib[2] = KERN_SYSVIPC_SEM;
625 1.23 simonb len = sizeof(valid);
626 1.37 christos if (sysctl(mib, 3, &valid, &len, NULL, 0) < 0) {
627 1.37 christos warn("sysctl(KERN_SYSVIPC_SEM)");
628 1.23 simonb return;
629 1.23 simonb }
630 1.23 simonb if (!valid) {
631 1.35 christos unconfsem();
632 1.23 simonb return;
633 1.23 simonb }
634 1.23 simonb
635 1.23 simonb mib[0] = CTL_KERN;
636 1.37 christos mib[1] = KERN_SYSVIPC;
637 1.37 christos mib[2] = KERN_SYSVIPC_INFO;
638 1.37 christos mib[3] = KERN_SYSVIPC_SEM_INFO;
639 1.23 simonb
640 1.23 simonb if (!(display & SEMINFO)) {
641 1.23 simonb /* totals only */
642 1.23 simonb len = sizeof(struct seminfo);
643 1.23 simonb } else {
644 1.37 christos if (sysctl(mib, 4, NULL, &len, NULL, 0) < 0) {
645 1.35 christos warn("sysctl(KERN_SYSVIPC_SEM_INFO)");
646 1.23 simonb return;
647 1.23 simonb }
648 1.23 simonb }
649 1.23 simonb
650 1.23 simonb if ((buf = malloc(len)) == NULL)
651 1.23 simonb err(1, "malloc");
652 1.23 simonb semsi = (struct sem_sysctl_info *)buf;
653 1.37 christos if (sysctl(mib, 4, semsi, &len, NULL, 0) < 0) {
654 1.35 christos warn("sysctl(KERN_SYSVIPC_SEM_INFO)");
655 1.35 christos goto done;
656 1.23 simonb }
657 1.23 simonb
658 1.23 simonb if (display & SEMTOTAL)
659 1.23 simonb show_semtotal(&semsi->seminfo);
660 1.23 simonb
661 1.23 simonb if (display & SEMINFO) {
662 1.23 simonb show_seminfo_hdr();
663 1.23 simonb for (i = 0; i < semsi->seminfo.semmni; i++) {
664 1.23 simonb struct semid_ds_sysctl *semaptr = &semsi->semids[i];
665 1.23 simonb if ((semaptr->sem_perm.mode & SEM_ALLOC) != 0)
666 1.23 simonb show_seminfo(semaptr->sem_otime,
667 1.23 simonb semaptr->sem_ctime,
668 1.23 simonb IXSEQ_TO_IPCID(i, semaptr->sem_perm),
669 1.23 simonb semaptr->sem_perm._key,
670 1.23 simonb semaptr->sem_perm.mode,
671 1.23 simonb semaptr->sem_perm.uid,
672 1.23 simonb semaptr->sem_perm.gid,
673 1.23 simonb semaptr->sem_perm.cuid,
674 1.23 simonb semaptr->sem_perm.cgid,
675 1.23 simonb semaptr->sem_nsems);
676 1.23 simonb }
677 1.35 christos (void)printf("\n");
678 1.23 simonb }
679 1.35 christos done:
680 1.35 christos free(buf);
681 1.23 simonb }
682 1.23 simonb
683 1.36 christos static struct nlist symbols[] = {
684 1.36 christos { .n_name = "_sema" },
685 1.36 christos #define X_SEMA 0
686 1.36 christos { .n_name = "_seminfo" },
687 1.36 christos #define X_SEMINFO 1
688 1.36 christos { .n_name = "_semu" },
689 1.36 christos #define X_SEMU 2
690 1.36 christos { .n_name = "_msginfo" },
691 1.36 christos #define X_MSGINFO 3
692 1.36 christos { .n_name = "_msqids" },
693 1.36 christos #define X_MSQIDS 4
694 1.36 christos { .n_name = "_shminfo" },
695 1.36 christos #define X_SHMINFO 5
696 1.36 christos { .n_name = "_shmsegs" },
697 1.36 christos #define X_SHMSEGS 6
698 1.36 christos { .n_name = NULL }
699 1.36 christos };
700 1.36 christos
701 1.35 christos static void
702 1.23 simonb ipcs_kvm(void)
703 1.23 simonb {
704 1.23 simonb struct msginfo msginfo;
705 1.23 simonb struct msqid_ds *msqids;
706 1.23 simonb struct seminfo seminfo;
707 1.23 simonb struct semid_ds *sema;
708 1.23 simonb struct shminfo shminfo;
709 1.23 simonb struct shmid_ds *shmsegs;
710 1.23 simonb kvm_t *kd;
711 1.23 simonb char errbuf[_POSIX2_LINE_MAX];
712 1.23 simonb int i;
713 1.11 thorpej
714 1.11 thorpej if ((kd = kvm_openfiles(namelist, core, NULL, O_RDONLY,
715 1.11 thorpej errbuf)) == NULL)
716 1.11 thorpej errx(1, "can't open kvm: %s", errbuf);
717 1.16 mrg
718 1.1 cgd
719 1.4 cgd switch (kvm_nlist(kd, symbols)) {
720 1.1 cgd case 0:
721 1.1 cgd break;
722 1.1 cgd case -1:
723 1.11 thorpej errx(1, "%s: unable to read symbol table.",
724 1.11 thorpej namelist == NULL ? _PATH_UNIX : namelist);
725 1.23 simonb /* NOTREACHED */
726 1.1 cgd default:
727 1.5 cgd #ifdef notdef /* they'll be told more civilly later */
728 1.5 cgd warnx("nlist failed");
729 1.5 cgd for (i = 0; symbols[i].n_name != NULL; i++)
730 1.5 cgd if (symbols[i].n_value == 0)
731 1.5 cgd warnx("symbol %s not found",
732 1.5 cgd symbols[i].n_name);
733 1.23 simonb #endif
734 1.1 cgd break;
735 1.1 cgd }
736 1.1 cgd
737 1.4 cgd if ((display & (MSGINFO | MSGTOTAL)) &&
738 1.11 thorpej (kvm_read(kd, symbols[X_MSGINFO].n_value,
739 1.11 thorpej &msginfo, sizeof(msginfo)) == sizeof(msginfo))) {
740 1.1 cgd
741 1.23 simonb if (display & MSGTOTAL)
742 1.23 simonb show_msgtotal(&msginfo);
743 1.23 simonb
744 1.4 cgd if (display & MSGINFO) {
745 1.4 cgd struct msqid_ds *xmsqids;
746 1.1 cgd
747 1.11 thorpej if (kvm_read(kd, symbols[X_MSQIDS].n_value,
748 1.11 thorpej &msqids, sizeof(msqids)) != sizeof(msqids))
749 1.11 thorpej errx(1, "kvm_read (%s): %s",
750 1.11 thorpej symbols[X_MSQIDS].n_name, kvm_geterr(kd));
751 1.11 thorpej
752 1.11 thorpej xmsqids = malloc(sizeof(struct msqid_ds) *
753 1.11 thorpej msginfo.msgmni);
754 1.11 thorpej
755 1.11 thorpej if (kvm_read(kd, (u_long)msqids, xmsqids,
756 1.11 thorpej sizeof(struct msqid_ds) * msginfo.msgmni) !=
757 1.11 thorpej sizeof(struct msqid_ds) * msginfo.msgmni)
758 1.11 thorpej errx(1, "kvm_read (msqids): %s",
759 1.11 thorpej kvm_geterr(kd));
760 1.4 cgd
761 1.23 simonb show_msginfo_hdr();
762 1.23 simonb for (i = 0; i < msginfo.msgmni; i++) {
763 1.23 simonb struct msqid_ds *msqptr = &xmsqids[i];
764 1.23 simonb if (msqptr->msg_qbytes != 0)
765 1.23 simonb show_msginfo(msqptr->msg_stime,
766 1.23 simonb msqptr->msg_rtime,
767 1.23 simonb msqptr->msg_ctime,
768 1.4 cgd IXSEQ_TO_IPCID(i, msqptr->msg_perm),
769 1.35 christos (u_int64_t)msqptr->msg_perm._key,
770 1.23 simonb msqptr->msg_perm.mode,
771 1.23 simonb msqptr->msg_perm.uid,
772 1.23 simonb msqptr->msg_perm.gid,
773 1.23 simonb msqptr->msg_perm.cuid,
774 1.23 simonb msqptr->msg_perm.cgid,
775 1.35 christos (u_int64_t)msqptr->_msg_cbytes,
776 1.35 christos (u_int64_t)msqptr->msg_qnum,
777 1.35 christos (u_int64_t)msqptr->msg_qbytes,
778 1.23 simonb msqptr->msg_lspid,
779 1.23 simonb msqptr->msg_lrpid);
780 1.4 cgd }
781 1.35 christos (void)printf("\n");
782 1.35 christos free(xmsqids);
783 1.4 cgd }
784 1.4 cgd } else
785 1.35 christos if (display & (MSGINFO | MSGTOTAL))
786 1.35 christos unconfmsg();
787 1.4 cgd if ((display & (SHMINFO | SHMTOTAL)) &&
788 1.11 thorpej (kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo,
789 1.11 thorpej sizeof(shminfo)) == sizeof(shminfo))) {
790 1.11 thorpej
791 1.23 simonb if (display & SHMTOTAL)
792 1.23 simonb show_shmtotal(&shminfo);
793 1.23 simonb
794 1.4 cgd if (display & SHMINFO) {
795 1.4 cgd struct shmid_ds *xshmids;
796 1.1 cgd
797 1.11 thorpej if (kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs,
798 1.11 thorpej sizeof(shmsegs)) != sizeof(shmsegs))
799 1.11 thorpej errx(1, "kvm_read (%s): %s",
800 1.11 thorpej symbols[X_SHMSEGS].n_name, kvm_geterr(kd));
801 1.11 thorpej
802 1.11 thorpej xshmids = malloc(sizeof(struct shmid_ds) *
803 1.12 explorer shminfo.shmmni);
804 1.11 thorpej
805 1.11 thorpej if (kvm_read(kd, (u_long)shmsegs, xshmids,
806 1.11 thorpej sizeof(struct shmid_ds) * shminfo.shmmni) !=
807 1.11 thorpej sizeof(struct shmid_ds) * shminfo.shmmni)
808 1.11 thorpej errx(1, "kvm_read (shmsegs): %s",
809 1.11 thorpej kvm_geterr(kd));
810 1.4 cgd
811 1.23 simonb show_shminfo_hdr();
812 1.23 simonb for (i = 0; i < shminfo.shmmni; i++) {
813 1.23 simonb struct shmid_ds *shmptr = &xshmids[i];
814 1.23 simonb if (shmptr->shm_perm.mode & 0x0800)
815 1.23 simonb show_shminfo(shmptr->shm_atime,
816 1.23 simonb shmptr->shm_dtime,
817 1.23 simonb shmptr->shm_ctime,
818 1.4 cgd IXSEQ_TO_IPCID(i, shmptr->shm_perm),
819 1.35 christos (u_int64_t)shmptr->shm_perm._key,
820 1.23 simonb shmptr->shm_perm.mode,
821 1.23 simonb shmptr->shm_perm.uid,
822 1.23 simonb shmptr->shm_perm.gid,
823 1.23 simonb shmptr->shm_perm.cuid,
824 1.23 simonb shmptr->shm_perm.cgid,
825 1.23 simonb shmptr->shm_nattch,
826 1.35 christos (u_int64_t)shmptr->shm_segsz,
827 1.23 simonb shmptr->shm_cpid,
828 1.23 simonb shmptr->shm_lpid);
829 1.1 cgd }
830 1.35 christos (void)printf("\n");
831 1.35 christos free(xshmids);
832 1.4 cgd }
833 1.4 cgd } else
834 1.35 christos if (display & (SHMINFO | SHMTOTAL))
835 1.35 christos unconfshm();
836 1.4 cgd if ((display & (SEMINFO | SEMTOTAL)) &&
837 1.11 thorpej (kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo,
838 1.11 thorpej sizeof(seminfo)) == sizeof(seminfo))) {
839 1.4 cgd struct semid_ds *xsema;
840 1.1 cgd
841 1.23 simonb if (display & SEMTOTAL)
842 1.23 simonb show_semtotal(&seminfo);
843 1.23 simonb
844 1.4 cgd if (display & SEMINFO) {
845 1.11 thorpej if (kvm_read(kd, symbols[X_SEMA].n_value, &sema,
846 1.11 thorpej sizeof(sema)) != sizeof(sema))
847 1.11 thorpej errx(1, "kvm_read (%s): %s",
848 1.11 thorpej symbols[X_SEMA].n_name, kvm_geterr(kd));
849 1.11 thorpej
850 1.11 thorpej xsema = malloc(sizeof(struct semid_ds) *
851 1.11 thorpej seminfo.semmni);
852 1.11 thorpej
853 1.11 thorpej if (kvm_read(kd, (u_long)sema, xsema,
854 1.11 thorpej sizeof(struct semid_ds) * seminfo.semmni) !=
855 1.11 thorpej sizeof(struct semid_ds) * seminfo.semmni)
856 1.11 thorpej errx(1, "kvm_read (sema): %s",
857 1.11 thorpej kvm_geterr(kd));
858 1.4 cgd
859 1.23 simonb show_seminfo_hdr();
860 1.23 simonb for (i = 0; i < seminfo.semmni; i++) {
861 1.23 simonb struct semid_ds *semaptr = &xsema[i];
862 1.23 simonb if ((semaptr->sem_perm.mode & SEM_ALLOC) != 0)
863 1.23 simonb show_seminfo(semaptr->sem_otime,
864 1.23 simonb semaptr->sem_ctime,
865 1.4 cgd IXSEQ_TO_IPCID(i, semaptr->sem_perm),
866 1.35 christos (u_int64_t)semaptr->sem_perm._key,
867 1.23 simonb semaptr->sem_perm.mode,
868 1.23 simonb semaptr->sem_perm.uid,
869 1.23 simonb semaptr->sem_perm.gid,
870 1.23 simonb semaptr->sem_perm.cuid,
871 1.23 simonb semaptr->sem_perm.cgid,
872 1.23 simonb semaptr->sem_nsems);
873 1.4 cgd }
874 1.1 cgd
875 1.35 christos (void)printf("\n");
876 1.35 christos free(xsema);
877 1.4 cgd }
878 1.4 cgd } else
879 1.35 christos if (display & (SEMINFO | SEMTOTAL))
880 1.35 christos unconfsem();
881 1.35 christos (void)kvm_close(kd);
882 1.5 cgd }
883 1.5 cgd
884 1.35 christos static void
885 1.23 simonb usage(void)
886 1.5 cgd {
887 1.5 cgd
888 1.35 christos (void)fprintf(stderr,
889 1.35 christos "Usage: %s [-abcmopqstMQST] [-C corefile] [-N namelist]\n",
890 1.26 cgd getprogname());
891 1.5 cgd exit(1);
892 1.1 cgd }
893