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