file_cmds-202.2.tar.gz
[apple/file_cmds.git] / ipcs / ipcs.c
1 /*
2 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29
30 #include <assert.h>
31 #include <err.h>
32 #include <fcntl.h>
33 #include <grp.h>
34 #include <nlist.h>
35 #include <limits.h>
36 #include <paths.h>
37 #include <pwd.h>
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sysexits.h>
44
45 #include "sys/types.h"
46 #include <sys/ucred.h>
47 #include <sys/time.h>
48 #include <sys/proc.h>
49 #include <sys/param.h>
50 #include <sys/sysctl.h>
51 #include <errno.h>
52 #include "sys/ipcs.h"
53 #define KERNEL 1 /* To get new ipc_perm and __(sem|shm|msg)ds_new */
54 #include "sys/ipc.h"
55 #include "sys/sem_internal.h"
56 #include "sys/shm_internal.h"
57 #include "sys/msg.h"
58
59
60 /* The following is a kludge, until the problem of multiple inclusions
61 of ipc.h is taken care of. */
62 #ifndef IXSEQ_TO_IPCID
63 #define IXSEQ_TO_IPCID(ix,perm) (((perm._seq) << 16L) | (ix & 0xffff))
64 #endif
65
66 char *
67 fmt_perm(u_short mode, char write_char)
68 {
69 static char buffer[100];
70
71 buffer[0] = '-';
72 buffer[1] = '-';
73 buffer[2] = ((mode & 0400) ? 'r' : '-');
74 buffer[3] = ((mode & 0200) ? write_char : '-');
75 buffer[4] = '-';
76 buffer[5] = ((mode & 0040) ? 'r' : '-');
77 buffer[6] = ((mode & 0020) ? write_char : '-');
78 buffer[7] = '-';
79 buffer[8] = ((mode & 0004) ? 'r' : '-');
80 buffer[9] = ((mode & 0002) ? write_char : '-');
81 buffer[10] = '-';
82 buffer[11] = '\0';
83 return (&buffer[0]);
84 }
85
86 void
87 cvt_time(time_t t, char *buf)
88 {
89 struct tm *tm;
90
91 if (t == 0) {
92 strcpy(buf, "no-entry");
93 } else {
94 tm = localtime(&t);
95 sprintf(buf, "%2d:%02d:%02d",
96 tm->tm_hour, tm->tm_min, tm->tm_sec);
97 }
98 }
99 #define SHMINFO 1
100 #define SHMTOTAL 2
101 #define MSGINFO 4
102 #define MSGTOTAL 8
103 #define SEMINFO 16
104 #define SEMTOTAL 32
105
106 #define BIGGEST 1
107 #define CREATOR 2
108 #define OUTSTANDING 4
109 #define PID 8
110 #define TIME 16
111
112 void usage()
113 {
114 errx(EX_USAGE, "%s","usage: ipcs [-abcmopqstMQST]\n");
115 }
116
117 int safe_sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
118 size_t newlen)
119
120 {
121 int rv, sv_errno=0;
122
123 if (seteuid(0)) /* iterator needs root write access to sysctl */
124 err(1, "seteuid(0) failed");
125
126 rv = sysctlbyname(name, oldp, oldlenp, newp, newlen);
127 if (rv < 0)
128 sv_errno = errno;
129
130 if (seteuid(getuid()))
131 err(1, "seteuid(%d) failed", getuid());
132
133 if (rv < 0)
134 errno = sv_errno;
135 return rv;
136 }
137
138 int
139 main(argc, argv)
140 int argc;
141 char *argv[];
142 {
143 int display = 0;
144 int option = 0;
145 int exit_val = 0;
146 time_t now;
147 char datestring[100];
148 int i;
149
150 if (seteuid(getuid())) /* run as user */
151 err(1, "seteuid(%d) failed", getuid());
152
153 while ((i = getopt(argc, argv, "MmQqSsabcoptT")) != -1)
154 switch (i) {
155 case 'M':
156 display = SHMTOTAL;
157 break;
158 case 'm':
159 display |= SHMINFO;
160 break;
161 case 'Q':
162 display = MSGTOTAL;
163 break;
164 case 'q':
165 display |= MSGINFO;
166 break;
167 case 'S':
168 display = SEMTOTAL;
169 break;
170 case 's':
171 display |= SEMINFO;
172 break;
173 case 'T':
174 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
175 break;
176 case 'a':
177 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
178 break;
179 case 'b':
180 option |= BIGGEST;
181 break;
182 case 'c':
183 option |= CREATOR;
184 break;
185 case 'o':
186 option |= OUTSTANDING;
187 break;
188 case 'p':
189 option |= PID;
190 break;
191 case 't':
192 option |= TIME;
193 break;
194 default:
195 usage();
196 }
197 if (display == 0)
198 display = SHMINFO | MSGINFO | SEMINFO;
199 now = time(0);
200 if (0 == strftime(datestring, sizeof(datestring), "%a %b %e %H:%M:%S %Z %Y", localtime(&now)))
201 errx(1, "strftime failed\n");
202 printf("IPC status from <running system> as of %s\n", datestring);
203 if ((display & (MSGINFO | MSGTOTAL))) {
204 if (display & MSGTOTAL) {
205 struct IPCS_command ic;
206 struct msginfo msginfo;
207 size_t ic_size = sizeof(ic);
208
209 ic.ipcs_magic = IPCS_MAGIC;
210 ic.ipcs_op = IPCS_MSG_CONF;
211 ic.ipcs_cursor = 0; /* 0 for fw. compat. */
212 ic.ipcs_data = &msginfo;
213 ic.ipcs_datalen = sizeof(msginfo);
214
215 if (safe_sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size)) {
216 if (errno != EPERM) {
217 char buffer[1024];
218 snprintf(buffer, 1024, "sysctlbyname(IPCS_MSG_SYSCTL, op=CONF, &ic, &%ld) datalen=%d",
219 sizeof(ic), ic.ipcs_datalen);
220 perror(buffer);
221 } else
222 perror("sysctlbyname IPCS_MSG_SYSCTL");
223 }
224
225 printf("msginfo:\n");
226 printf("\tmsgmax: %6d\t(max characters in a message)\n",
227 msginfo.msgmax);
228 printf("\tmsgmni: %6d\t(# of message queues)\n",
229 msginfo.msgmni);
230 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
231 msginfo.msgmnb);
232 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
233 msginfo.msgtql);
234 printf("\tmsgssz: %6d\t(size of a message segment)\n",
235 msginfo.msgssz);
236 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
237 msginfo.msgseg);
238 }
239 if (display & MSGINFO) {
240 struct IPCS_command ic;
241 struct __msqid_ds_new ds;
242 struct __msqid_ds_new *msqptr = &ds;
243 size_t ic_size = sizeof(ic);
244
245 printf("T ID KEY MODE OWNER GROUP");
246 if (option & CREATOR)
247 printf(" CREATOR CGROUP");
248 if (option & OUTSTANDING)
249 printf(" CBYTES QNUM");
250 if (option & BIGGEST)
251 printf(" QBYTES");
252 if (option & PID)
253 printf(" LSPID LRPID");
254 if (option & TIME)
255 printf(" STIME RTIME CTIME");
256 printf("\nMessage Queues:\n");
257
258 ic.ipcs_magic = IPCS_MAGIC;
259 ic.ipcs_op = IPCS_MSG_ITER;
260 ic.ipcs_cursor = 0; /* start */
261 ic.ipcs_datalen = sizeof(*msqptr);
262 ic.ipcs_data = msqptr;
263
264 memset(msqptr, 0, sizeof(*msqptr));
265
266 while(!(safe_sysctlbyname(IPCS_MSG_SYSCTL, &ic, &ic_size, &ic, ic_size))) {
267 ic.ipcs_data = msqptr;
268
269 if (msqptr->msg_qbytes != 0) {
270 char stime_buf[100], rtime_buf[100],
271 ctime_buf[100];
272
273 cvt_time(msqptr->msg_stime, stime_buf);
274 cvt_time(msqptr->msg_rtime, rtime_buf);
275 cvt_time(msqptr->msg_ctime, ctime_buf);
276
277 printf("q %6d 0x%08x %s %8s %8s",
278 IXSEQ_TO_IPCID((ic.ipcs_cursor-1), msqptr->msg_perm),
279 (int)msqptr->msg_perm._key,
280 fmt_perm(msqptr->msg_perm.mode, 'w'),
281 user_from_uid(msqptr->msg_perm.uid, 0),
282 group_from_gid(msqptr->msg_perm.gid, 0));
283
284 if (option & CREATOR)
285 printf(" %8s %8s",
286 user_from_uid(msqptr->msg_perm.cuid, 0),
287 group_from_gid(msqptr->msg_perm.cgid, 0));
288
289 if (option & OUTSTANDING)
290 printf(" %6lu %6lu",
291 msqptr->msg_cbytes,
292 msqptr->msg_qnum);
293
294 if (option & BIGGEST)
295 printf(" %6lu",
296 msqptr->msg_qbytes);
297
298 if (option & PID)
299 printf(" %6d %6d",
300 msqptr->msg_lspid,
301 msqptr->msg_lrpid);
302
303 if (option & TIME)
304 printf(" %s %s %s",
305 stime_buf,
306 rtime_buf,
307 ctime_buf);
308
309 printf("\n");
310 }
311 memset(msqptr, 0, sizeof(*msqptr));
312 errno = 0;
313 }
314
315 if (errno != ENOENT && errno != ERANGE) {
316 if (errno != EPERM) {
317 errx(1, "sysctlbyname(IPCS_MSG_SYSCTL, op=ITER, &ic, &%ld) datalen=%d failed:%s\n",
318 sizeof(ic), ic.ipcs_datalen, strerror(errno));
319 } else
320 errx(1, "sysctlbyname IPCS_MSG_SYSCTL: %s", strerror(errno));
321 }
322 printf("\n");
323 }
324 } else
325 if (display & (MSGINFO | MSGTOTAL)) {
326 errx(1, "%s", "SVID messages facility not configured in the system\n");
327 }
328
329 if ((display & (SHMINFO | SHMTOTAL))) {
330 if (display & SHMTOTAL) {
331 struct IPCS_command ic;
332 struct shminfo shminfo;
333 size_t ic_size = sizeof(ic);
334
335 ic.ipcs_magic = IPCS_MAGIC;
336 ic.ipcs_op = IPCS_SHM_CONF;
337 ic.ipcs_cursor = 0; /* 0 for fw. compat. */
338 ic.ipcs_data = &shminfo;
339 ic.ipcs_datalen = sizeof(shminfo);
340
341 if (safe_sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size)) {
342 if (errno != EPERM) {
343 errx(1, "sysctlbyname(IPCS_SHM_SYSCTL, op=CONF, &ic, &%ld) datalen=%d failed: %s\n",
344 sizeof(ic), ic.ipcs_datalen, strerror(errno));
345 } else
346 errx(1, "sysctlbyname: %s", strerror(errno));
347 }
348 printf("shminfo:\n");
349 printf("\tshmmax: %7lld\t(max shared memory segment size)\n",
350 shminfo.shmmax);
351 printf("\tshmmin: %7lld\t(min shared memory segment size)\n",
352 shminfo.shmmin);
353 printf("\tshmmni: %7lld\t(max number of shared memory identifiers)\n",
354 shminfo.shmmni);
355 printf("\tshmseg: %7lld\t(max shared memory segments per process)\n",
356 shminfo.shmseg);
357 printf("\tshmall: %7lld\t(max amount of shared memory in pages)\n\n",
358 shminfo.shmall);
359 }
360 if (display & SHMINFO) {
361 struct IPCS_command ic;
362 struct __shmid_ds_new ds;
363 struct __shmid_ds_new *shmptr = &ds;
364 size_t ic_size = sizeof(ic);
365
366 printf("T ID KEY MODE OWNER GROUP");
367 if (option & CREATOR)
368 printf(" CREATOR CGROUP");
369 if (option & OUTSTANDING)
370 printf(" NATTCH");
371 if (option & BIGGEST)
372 printf(" SEGSZ");
373 if (option & PID)
374 printf(" CPID LPID");
375 if (option & TIME)
376 printf(" ATIME DTIME CTIME");
377 printf("\nShared Memory:\n");
378 { /* XXX */
379
380 ic.ipcs_magic = IPCS_MAGIC;
381 ic.ipcs_op = IPCS_SHM_ITER;
382 ic.ipcs_cursor = 0; /* start */
383 ic.ipcs_datalen = sizeof(*shmptr);
384 ic.ipcs_data = shmptr;
385 memset(shmptr, 0, sizeof(shmptr));
386
387 while(!(safe_sysctlbyname(IPCS_SHM_SYSCTL, &ic, &ic_size, &ic, ic_size))) {
388 ic.ipcs_data = shmptr; /* xnu workaround */
389
390 if (shmptr->shm_perm.mode & 0x0800) {
391 char atime_buf[100], dtime_buf[100],
392 ctime_buf[100];
393
394 cvt_time(shmptr->shm_atime, atime_buf);
395 cvt_time(shmptr->shm_dtime, dtime_buf);
396 cvt_time(shmptr->shm_ctime, ctime_buf);
397
398 printf("m %6d 0x%08x %s %8s %8s",
399 IXSEQ_TO_IPCID((ic.ipcs_cursor-1), shmptr->shm_perm),
400 (int)shmptr->shm_perm._key,
401 fmt_perm(shmptr->shm_perm.mode, 'w'),
402 user_from_uid(shmptr->shm_perm.uid, 0),
403 group_from_gid(shmptr->shm_perm.gid, 0));
404
405 if (option & CREATOR)
406 printf(" %8s %8s",
407 user_from_uid(shmptr->shm_perm.cuid, 0),
408 group_from_gid(shmptr->shm_perm.cgid, 0));
409
410 if (option & OUTSTANDING)
411 printf(" %6d",
412 shmptr->shm_nattch);
413
414 if (option & BIGGEST)
415 printf(" %6ld",
416 shmptr->shm_segsz);
417
418 if (option & PID)
419 printf(" %6d %6d",
420 shmptr->shm_cpid,
421 shmptr->shm_lpid);
422
423 if (option & TIME)
424 printf(" %s %s %s",
425 atime_buf,
426 dtime_buf,
427 ctime_buf);
428
429 printf("\n");
430 }
431 memset(shmptr, 0, sizeof(*shmptr));
432 errno = 0;
433 }
434
435 if (errno != ENOENT && errno != ERANGE) {
436 if (errno != EPERM) {
437 errx(1, "sysctlbyname(IPCS_SHM_SYSCTL, op=ITER, &ic, &%ld) datalen=%d failed:%s\n",
438 sizeof(ic), ic.ipcs_datalen, strerror(errno));
439 } else
440 errx(1, "sysctlbyname: %s", strerror(errno));
441 }
442 } /* XXX */
443 printf("\n");
444 }
445 }
446 else
447 if (display & (SHMINFO | SHMTOTAL)) {
448 errx(1, "%s", "SVID shared memory facility not configured in the system\n");
449 }
450
451 if ((display & (SEMINFO | SEMTOTAL))) {
452 if (display & SEMTOTAL) {
453 struct IPCS_command ic;
454 struct seminfo seminfo;
455 size_t ic_size = sizeof(ic);
456
457 ic.ipcs_magic = IPCS_MAGIC;
458 ic.ipcs_op = IPCS_SEM_CONF;
459 ic.ipcs_cursor = 0; /* 0 for fw. compat. */
460 ic.ipcs_data = &seminfo;
461 ic.ipcs_datalen = sizeof(seminfo);
462
463 if (safe_sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size)) {
464 if (errno != EPERM) {
465 char buffer[1024];
466 snprintf(buffer, 1024, "sysctlbyname(IPCS_SEM_SYSCTL, op=CONF, &ic, &%ld) datalen=%d",
467 sizeof(ic), ic.ipcs_datalen);
468 perror(buffer);
469 } else
470 perror("sysctlbyname IPCS_SEM_SYSCTL/SEM_CONF");
471 }
472
473 printf("seminfo:\n");
474 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
475 seminfo.semmap);
476 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
477 seminfo.semmni);
478 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
479 seminfo.semmns);
480 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
481 seminfo.semmnu);
482 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
483 seminfo.semmsl);
484 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
485 seminfo.semopm);
486 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
487 seminfo.semume);
488 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
489 seminfo.semusz);
490 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
491 seminfo.semvmx);
492 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
493 seminfo.semaem);
494 }
495 if (display & SEMINFO) {
496 struct IPCS_command ic;
497 struct __semid_ds_new ds;
498 struct __semid_ds_new *semaptr = &ds;
499 size_t ic_size = sizeof(ic);
500
501 printf("T ID KEY MODE OWNER GROUP");
502 if (option & CREATOR)
503 printf(" CREATOR CGROUP");
504 if (option & BIGGEST)
505 printf(" NSEMS");
506 if (option & TIME)
507 printf(" OTIME CTIME");
508 printf("\nSemaphores:\n");
509
510 ic.ipcs_magic = IPCS_MAGIC;
511 ic.ipcs_op = IPCS_SEM_ITER;
512 ic.ipcs_cursor = 0; /* start */
513 ic.ipcs_datalen = sizeof(*semaptr);
514 ic.ipcs_data = semaptr;
515
516 memset(semaptr, 0, sizeof(*semaptr));
517
518 while(!(safe_sysctlbyname(IPCS_SEM_SYSCTL, &ic, &ic_size, &ic, ic_size))) {
519 ic.ipcs_data = semaptr; /* xnu workaround */
520
521 if ((semaptr->sem_perm.mode & SEM_ALLOC) != 0) {
522 char ctime_buf[100], otime_buf[100];
523
524 cvt_time(semaptr->sem_otime, otime_buf);
525 cvt_time(semaptr->sem_ctime, ctime_buf);
526
527 printf("s %6d 0x%08x %s %8s %8s",
528 IXSEQ_TO_IPCID((ic.ipcs_cursor-1), semaptr->sem_perm),
529 (int)semaptr->sem_perm._key,
530 fmt_perm(semaptr->sem_perm.mode, 'a'),
531 user_from_uid(semaptr->sem_perm.uid, 0),
532 group_from_gid(semaptr->sem_perm.gid, 0));
533
534 if (option & CREATOR)
535 printf(" %8s %8s",
536 user_from_uid(semaptr->sem_perm.cuid, 0),
537 group_from_gid(semaptr->sem_perm.cgid, 0));
538
539 if (option & BIGGEST)
540 printf(" %6d",
541 semaptr->sem_nsems);
542
543 if (option & TIME)
544 printf(" %s %s",
545 otime_buf,
546 ctime_buf);
547
548 printf("\n");
549 }
550 memset(semaptr, 0, sizeof(*semaptr));
551 errno = 0;
552 }
553
554 if (errno != ENOENT && errno != ERANGE) {
555 if (errno != EPERM) {
556 errx(1, "sysctlbyname(IPCS_SEM_SYSCTL/ITER, op=ITER, &ic, &%ld) datalen=%d failed: %s\n",
557 sizeof(ic), ic.ipcs_datalen, strerror(errno));
558 } else
559 errx(1, "sysctlbyname: IPCS_SEM_SYSCTL %s", strerror(errno));
560 }
561 printf("\n");
562 }
563 } else
564 if (display & (SEMINFO | SEMTOTAL)) {
565 errx(1, "%s", "SVID semaphores facility not configured in the system\n");
566 }
567
568 exit(exit_val);
569 }