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