MFC r1.12 (HEAD):
[dragonfly.git] / usr.bin / ipcs / ipcs.c
blob66ea535dd21ee6f6d2d16d3f24104d3841650920
1 /*
2 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
3 * All rights reserved.
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.
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.
27 * $FreeBSD: src/usr.bin/ipcs/ipcs.c,v 1.12.2.4 2003/04/08 11:01:34 tjr Exp $
28 * $DragonFly: src/usr.bin/ipcs/ipcs.c,v 1.8 2005/04/19 20:38:57 dillon Exp $
31 #define _KERNEL_STRUCTURES
33 #include <err.h>
34 #include <fcntl.h>
35 #include <grp.h>
36 #include <kvm.h>
37 #include <nlist.h>
38 #include <paths.h>
39 #include <pwd.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/time.h>
48 #include <sys/ipc.h>
49 #include <sys/sem.h>
50 #include <sys/shm.h>
51 #include <sys/msg.h>
53 struct semid_ds *sema;
54 struct seminfo seminfo;
55 struct msginfo msginfo;
56 struct msqid_ds *msqids;
57 struct shminfo shminfo;
58 struct shmid_ds *shmsegs;
60 static void usage(void);
61 static uid_t user2uid(const char *);
62 static gid_t grp2gid(const char *);
64 static struct nlist symbols[] = {
65 {"_sema"},
66 #define X_SEMA 0
67 {"_seminfo"},
68 #define X_SEMINFO 1
69 {"_semu"},
70 #define X_SEMU 2
71 {"_msginfo"},
72 #define X_MSGINFO 3
73 {"_msqids"},
74 #define X_MSQIDS 4
75 {"_shminfo"},
76 #define X_SHMINFO 5
77 {"_shmsegs"},
78 #define X_SHMSEGS 6
79 {NULL}
82 static kvm_t *kd;
84 char *
85 fmt_perm(u_short mode)
87 static char buffer[100];
89 buffer[0] = '-';
90 buffer[1] = '-';
91 buffer[2] = ((mode & 0400) ? 'r' : '-');
92 buffer[3] = ((mode & 0200) ? 'w' : '-');
93 buffer[4] = ((mode & 0100) ? 'a' : '-');
94 buffer[5] = ((mode & 0040) ? 'r' : '-');
95 buffer[6] = ((mode & 0020) ? 'w' : '-');
96 buffer[7] = ((mode & 0010) ? 'a' : '-');
97 buffer[8] = ((mode & 0004) ? 'r' : '-');
98 buffer[9] = ((mode & 0002) ? 'w' : '-');
99 buffer[10] = ((mode & 0001) ? 'a' : '-');
100 buffer[11] = '\0';
101 return (&buffer[0]);
104 void
105 cvt_time(time_t t, char *buf)
107 struct tm *tm;
109 if (t == 0) {
110 strcpy(buf, "no-entry");
111 } else {
112 tm = localtime(&t);
113 sprintf(buf, "%2d:%02d:%02d",
114 tm->tm_hour, tm->tm_min, tm->tm_sec);
117 #define SHMINFO 1
118 #define SHMTOTAL 2
119 #define MSGINFO 4
120 #define MSGTOTAL 8
121 #define SEMINFO 16
122 #define SEMTOTAL 32
124 #define BIGGEST 1
125 #define CREATOR 2
126 #define OUTSTANDING 4
127 #define PID 8
128 #define TIME 16
131 main(int argc, char **argv)
133 int display = SHMINFO | MSGINFO | SEMINFO;
134 int option = 0;
135 char *core, *namelist;
136 char *user, *grp;
137 int i;
138 uid_t useruid;
139 gid_t grpgid;
141 core = namelist = NULL;
142 user = grp = NULL;
144 while ((i = getopt(argc, argv, "MmQqSsabC:cN:ou:g:ptT")) != -1)
145 switch (i) {
146 case 'M':
147 display = SHMTOTAL;
148 break;
149 case 'm':
150 display = SHMINFO;
151 break;
152 case 'Q':
153 display = MSGTOTAL;
154 break;
155 case 'q':
156 display = MSGINFO;
157 break;
158 case 'S':
159 display = SEMTOTAL;
160 break;
161 case 's':
162 display = SEMINFO;
163 break;
164 case 'T':
165 display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
166 break;
167 case 'a':
168 option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
169 break;
170 case 'b':
171 option |= BIGGEST;
172 break;
173 case 'C':
174 core = optarg;
175 break;
176 case 'c':
177 option |= CREATOR;
178 break;
179 case 'N':
180 namelist = optarg;
181 break;
182 case 'o':
183 option |= OUTSTANDING;
184 break;
185 case 'p':
186 option |= PID;
187 break;
188 case 't':
189 option |= TIME;
190 break;
191 case 'u':
192 user = optarg;
193 grp = NULL;
194 useruid = user2uid(optarg);
195 break;
196 case 'g':
197 grp = optarg;
198 user = NULL;
199 grpgid = grp2gid(optarg);
200 break;
201 default:
202 usage();
206 * Discard setgid privileges if not the running kernel so that bad
207 * guys can't print interesting stuff from kernel memory.
209 if (namelist != NULL || core != NULL)
210 setgid(getgid());
212 if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
213 exit(1);
215 switch (kvm_nlist(kd, symbols)) {
216 case 0:
217 break;
218 case -1:
219 errx(1, "unable to read kernel symbol table");
220 default:
221 #ifdef notdef /* they'll be told more civilly later */
222 warnx("nlist failed");
223 for (i = 0; symbols[i].n_name != NULL; i++)
224 if (symbols[i].n_value == 0)
225 warnx("symbol %s not found",
226 symbols[i].n_name);
227 #endif
228 break;
231 if ((display & (MSGINFO | MSGTOTAL)) &&
232 kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
234 if (display & MSGTOTAL) {
235 printf("msginfo:\n");
236 printf("\tmsgmax: %6d\t(max characters in a message)\n",
237 msginfo.msgmax);
238 printf("\tmsgmni: %6d\t(# of message queues)\n",
239 msginfo.msgmni);
240 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
241 msginfo.msgmnb);
242 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
243 msginfo.msgtql);
244 printf("\tmsgssz: %6d\t(size of a message segment)\n",
245 msginfo.msgssz);
246 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
247 msginfo.msgseg);
249 if (display & MSGINFO) {
250 struct msqid_ds *xmsqids;
252 kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
253 xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
254 kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
256 printf("Message Queues:\n");
257 printf("T ID KEY MODE OWNER GROUP");
258 if (option & CREATOR)
259 printf(" CREATOR CGROUP");
260 if (option & OUTSTANDING)
261 printf(" CBYTES QNUM");
262 if (option & BIGGEST)
263 printf(" QBYTES");
264 if (option & PID)
265 printf(" LSPID LRPID");
266 if (option & TIME)
267 printf(" STIME RTIME CTIME");
268 printf("\n");
269 for (i = 0; i < msginfo.msgmni; i += 1) {
270 if (xmsqids[i].msg_qbytes != 0) {
271 char stime_buf[100], rtime_buf[100],
272 ctime_buf[100];
273 struct msqid_ds *msqptr = &xmsqids[i];
275 if (user && useruid != msqptr->msg_perm.uid)
276 continue;
277 if (grp && grpgid != msqptr->msg_perm.gid)
278 continue;
280 cvt_time(msqptr->msg_stime, stime_buf);
281 cvt_time(msqptr->msg_rtime, rtime_buf);
282 cvt_time(msqptr->msg_ctime, ctime_buf);
284 printf("q %6d %10d %s %8s %8s",
285 IXSEQ_TO_IPCID(i, msqptr->msg_perm),
286 msqptr->msg_perm.key,
287 fmt_perm(msqptr->msg_perm.mode),
288 user_from_uid(msqptr->msg_perm.uid, 0),
289 group_from_gid(msqptr->msg_perm.gid, 0));
291 if (option & CREATOR)
292 printf(" %8s %8s",
293 user_from_uid(msqptr->msg_perm.cuid, 0),
294 group_from_gid(msqptr->msg_perm.cgid, 0));
296 if (option & OUTSTANDING)
297 printf(" %6d %6d",
298 msqptr->msg_cbytes,
299 msqptr->msg_qnum);
301 if (option & BIGGEST)
302 printf(" %6d",
303 msqptr->msg_qbytes);
305 if (option & PID)
306 printf(" %6d %6d",
307 msqptr->msg_lspid,
308 msqptr->msg_lrpid);
310 if (option & TIME)
311 printf("%s %s %s",
312 stime_buf,
313 rtime_buf,
314 ctime_buf);
316 printf("\n");
319 printf("\n");
321 } else
322 if (display & (MSGINFO | MSGTOTAL)) {
323 fprintf(stderr,
324 "SVID messages facility not configured in the system\n");
326 if ((display & (SHMINFO | SHMTOTAL)) &&
327 kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
328 if (display & SHMTOTAL) {
329 printf("shminfo:\n");
330 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
331 shminfo.shmmax);
332 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
333 shminfo.shmmin);
334 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
335 shminfo.shmmni);
336 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
337 shminfo.shmseg);
338 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
339 shminfo.shmall);
341 if (display & SHMINFO) {
342 struct shmid_ds *xshmids;
344 kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
345 xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
346 kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
347 shminfo.shmmni);
349 printf("Shared Memory:\n");
350 printf("T ID KEY MODE OWNER GROUP");
351 if (option & CREATOR)
352 printf(" CREATOR CGROUP");
353 if (option & OUTSTANDING)
354 printf(" NATTCH");
355 if (option & BIGGEST)
356 printf(" SEGSZ");
357 if (option & PID)
358 printf(" CPID LPID");
359 if (option & TIME)
360 printf(" ATIME DTIME CTIME");
361 printf("\n");
362 for (i = 0; i < shminfo.shmmni; i += 1) {
363 if (xshmids[i].shm_perm.mode & 0x0800) {
364 char atime_buf[100], dtime_buf[100],
365 ctime_buf[100];
366 struct shmid_ds *shmptr = &xshmids[i];
368 if (user && useruid != shmptr->shm_perm.uid)
369 continue;
371 if (grp && grpgid != shmptr->shm_perm.gid)
372 continue;
374 cvt_time(shmptr->shm_atime, atime_buf);
375 cvt_time(shmptr->shm_dtime, dtime_buf);
376 cvt_time(shmptr->shm_ctime, ctime_buf);
378 printf("m %6d %10d %s %8s %8s",
379 IXSEQ_TO_IPCID(i, shmptr->shm_perm),
380 shmptr->shm_perm.key,
381 fmt_perm(shmptr->shm_perm.mode),
382 user_from_uid(shmptr->shm_perm.uid, 0),
383 group_from_gid(shmptr->shm_perm.gid, 0));
385 if (option & CREATOR)
386 printf(" %8s %8s",
387 user_from_uid(shmptr->shm_perm.cuid, 0),
388 group_from_gid(shmptr->shm_perm.cgid, 0));
390 if (option & OUTSTANDING)
391 printf(" %6d",
392 shmptr->shm_nattch);
394 if (option & BIGGEST)
395 printf(" %6d",
396 shmptr->shm_segsz);
398 if (option & PID)
399 printf(" %6d %6d",
400 shmptr->shm_cpid,
401 shmptr->shm_lpid);
403 if (option & TIME)
404 printf("%s %s %s",
405 atime_buf,
406 dtime_buf,
407 ctime_buf);
409 printf("\n");
412 printf("\n");
414 } else
415 if (display & (SHMINFO | SHMTOTAL)) {
416 fprintf(stderr,
417 "SVID shared memory facility not configured in the system\n");
419 if ((display & (SEMINFO | SEMTOTAL)) &&
420 kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
421 struct semid_ds *xsema;
423 if (display & SEMTOTAL) {
424 printf("seminfo:\n");
425 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
426 seminfo.semmap);
427 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
428 seminfo.semmni);
429 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
430 seminfo.semmns);
431 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
432 seminfo.semmnu);
433 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
434 seminfo.semmsl);
435 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
436 seminfo.semopm);
437 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
438 seminfo.semume);
439 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
440 seminfo.semusz);
441 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
442 seminfo.semvmx);
443 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
444 seminfo.semaem);
446 if (display & SEMINFO) {
447 kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
448 xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
449 kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
451 printf("Semaphores:\n");
452 printf("T ID KEY MODE OWNER GROUP");
453 if (option & CREATOR)
454 printf(" CREATOR CGROUP");
455 if (option & BIGGEST)
456 printf(" NSEMS");
457 if (option & TIME)
458 printf(" OTIME CTIME");
459 printf("\n");
460 for (i = 0; i < seminfo.semmni; i += 1) {
461 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
462 char ctime_buf[100], otime_buf[100];
463 struct semid_ds *semaptr = &xsema[i];
465 if (user && useruid != semaptr->sem_perm.uid)
466 continue;
468 if (grp && grpgid != semaptr->sem_perm.gid)
469 continue;
471 cvt_time(semaptr->sem_otime, otime_buf);
472 cvt_time(semaptr->sem_ctime, ctime_buf);
474 printf("s %6d %10d %s %8s %8s",
475 IXSEQ_TO_IPCID(i, semaptr->sem_perm),
476 semaptr->sem_perm.key,
477 fmt_perm(semaptr->sem_perm.mode),
478 user_from_uid(semaptr->sem_perm.uid, 0),
479 group_from_gid(semaptr->sem_perm.gid, 0));
481 if (option & CREATOR)
482 printf(" %8s %8s",
483 user_from_uid(semaptr->sem_perm.cuid, 0),
484 group_from_gid(semaptr->sem_perm.cgid, 0));
486 if (option & BIGGEST)
487 printf(" %6d",
488 semaptr->sem_nsems);
490 if (option & TIME)
491 printf("%s %s",
492 otime_buf,
493 ctime_buf);
495 printf("\n");
499 printf("\n");
501 } else
502 if (display & (SEMINFO | SEMTOTAL)) {
503 fprintf(stderr, "SVID semaphores facility not configured in the system\n");
505 kvm_close(kd);
507 exit(0);
510 static uid_t
511 user2uid(const char *username)
513 struct passwd *pwd;
514 uid_t uid;
515 char *r;
517 uid = strtoul(username, &r, 0);
518 if (!*r && r != username)
519 return (uid);
520 if ((pwd = getpwnam(username)) == NULL)
521 errx(1, "No such user");
522 endpwent();
523 return (pwd->pw_uid);
526 static gid_t
527 grp2gid(const char *groupname)
529 struct group *grp;
530 gid_t gid;
531 char *r;
533 gid = strtol(groupname, &r, 0);
534 if (!*r && r != groupname)
535 return (gid);
536 if ((grp = getgrnam(groupname)) == NULL)
537 errx(1, "No such group");
538 endgrent();
539 return (grp->gr_gid);
542 static void
543 usage(void)
546 fprintf(stderr,
547 "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist] [-u user] [-g group]\n");
548 exit(1);