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