b9bf9574775a5a134c0bd6cac0cb90249e4d57b6
[dragonfly.git] / usr.bin / 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  * $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 $
29  */
30
31 #define _KERNEL_STRUCTURES
32
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>
44
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>
52
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;
59
60 static void     usage(void);
61 static uid_t    user2uid(const char *);
62 static gid_t    grp2gid(const char *);
63
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}
80 };
81
82 static kvm_t *kd;
83
84 char   *
85 fmt_perm(u_short mode)
86 {
87         static char buffer[100];
88
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]);
102 }
103
104 void
105 cvt_time(time_t t, char *buf)
106 {
107         struct tm *tm;
108
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);
115         }
116 }
117 #define SHMINFO         1
118 #define SHMTOTAL        2
119 #define MSGINFO         4
120 #define MSGTOTAL        8
121 #define SEMINFO         16
122 #define SEMTOTAL        32
123
124 #define BIGGEST         1
125 #define CREATOR         2
126 #define OUTSTANDING     4
127 #define PID             8
128 #define TIME            16
129
130 int
131 main(int argc, char **argv)
132 {
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;
140
141         core = namelist = NULL;
142         user = grp = NULL;
143
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();
203                 }
204
205         /*
206          * Discard setgid privileges if not the running kernel so that bad
207          * guys can't print interesting stuff from kernel memory.
208          */
209         if (namelist != NULL || core != NULL)
210                 setgid(getgid());
211
212         if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
213                 exit(1);
214
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;
229         }
230
231         if ((display & (MSGINFO | MSGTOTAL)) &&
232             kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
233
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);
248                 }
249                 if (display & MSGINFO) {
250                         struct msqid_ds *xmsqids;
251
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);
255
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];
274
275                                         if (user && useruid != msqptr->msg_perm.uid)
276                                                         continue;
277                                         if (grp && grpgid != msqptr->msg_perm.gid)
278                                                         continue;
279         
280                                         cvt_time(msqptr->msg_stime, stime_buf);
281                                         cvt_time(msqptr->msg_rtime, rtime_buf);
282                                         cvt_time(msqptr->msg_ctime, ctime_buf);
283
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));
290
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));
295
296                                         if (option & OUTSTANDING)
297                                                 printf(" %6d %6d",
298                                                     msqptr->msg_cbytes,
299                                                     msqptr->msg_qnum);
300
301                                         if (option & BIGGEST)
302                                                 printf(" %6d",
303                                                     msqptr->msg_qbytes);
304
305                                         if (option & PID)
306                                                 printf(" %6d %6d",
307                                                     msqptr->msg_lspid,
308                                                     msqptr->msg_lrpid);
309
310                                         if (option & TIME)
311                                                 printf("%s %s %s",
312                                                     stime_buf,
313                                                     rtime_buf,
314                                                     ctime_buf);
315
316                                         printf("\n");
317                                 }
318                         }
319                         printf("\n");
320                 }
321         } else
322                 if (display & (MSGINFO | MSGTOTAL)) {
323                         fprintf(stderr,
324                             "SVID messages facility not configured in the system\n");
325                 }
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: %12lu\t(max shared memory segment size)\n",
331                             shminfo.shmmax);
332                         printf("\tshmmin: %12d\t(min shared memory segment size)\n",
333                             shminfo.shmmin);
334                         printf("\tshmmni: %12d\t(max number of shared memory identifiers)\n",
335                             shminfo.shmmni);
336                         printf("\tshmseg: %12d\t(max shared memory segments per process)\n",
337                             shminfo.shmseg);
338                         printf("\tshmall: %12d\t(max amount of shared memory in pages)\n\n",
339                             shminfo.shmall);
340                 }
341                 if (display & SHMINFO) {
342                         struct shmid_ds *xshmids;
343
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);
348
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];
367
368                                         if (user && useruid != shmptr->shm_perm.uid)
369                                                 continue;
370
371                                         if (grp && grpgid != shmptr->shm_perm.gid)
372                                                 continue;
373
374                                         cvt_time(shmptr->shm_atime, atime_buf);
375                                         cvt_time(shmptr->shm_dtime, dtime_buf);
376                                         cvt_time(shmptr->shm_ctime, ctime_buf);
377
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));
384
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));
389
390                                         if (option & OUTSTANDING)
391                                                 printf(" %6d",
392                                                     shmptr->shm_nattch);
393
394                                         if (option & BIGGEST)
395                                                 printf(" %12ul",
396                                                     shmptr->shm_segsz);
397
398                                         if (option & PID)
399                                                 printf(" %6d %6d",
400                                                     shmptr->shm_cpid,
401                                                     shmptr->shm_lpid);
402
403                                         if (option & TIME)
404                                                 printf(" %s %s %s",
405                                                     atime_buf,
406                                                     dtime_buf,
407                                                     ctime_buf);
408
409                                         printf("\n");
410                                 }
411                         }
412                         printf("\n");
413                 }
414         } else
415                 if (display & (SHMINFO | SHMTOTAL)) {
416                         fprintf(stderr,
417                             "SVID shared memory facility not configured in the system\n");
418                 }
419         if ((display & (SEMINFO | SEMTOTAL)) &&
420             kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
421                 struct semid_ds *xsema;
422
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);
445                 }
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);
450
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];
464
465                                         if (user && useruid != semaptr->sem_perm.uid)
466                                                 continue;
467
468                                         if (grp && grpgid != semaptr->sem_perm.gid)
469                                                 continue;
470
471                                         cvt_time(semaptr->sem_otime, otime_buf);
472                                         cvt_time(semaptr->sem_ctime, ctime_buf);
473
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));
480
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));
485
486                                         if (option & BIGGEST)
487                                                 printf(" %6d",
488                                                     semaptr->sem_nsems);
489
490                                         if (option & TIME)
491                                                 printf("%s %s",
492                                                     otime_buf,
493                                                     ctime_buf);
494
495                                         printf("\n");
496                                 }
497                         }
498
499                         printf("\n");
500                 }
501         } else
502                 if (display & (SEMINFO | SEMTOTAL)) {
503                         fprintf(stderr, "SVID semaphores facility not configured in the system\n");
504                 }
505         kvm_close(kd);
506
507         exit(0);
508 }
509
510 static uid_t
511 user2uid(const char *username)
512 {
513         struct passwd *pwd;
514         uid_t uid;
515         char *r;
516
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);
524 }
525
526 static gid_t
527 grp2gid(const char *groupname)
528 {
529         struct group *grp;
530         gid_t gid;
531         char *r;
532
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);
540 }       
541
542 static void
543 usage(void)
544 {
545
546         fprintf(stderr,
547             "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist] [-u user] [-g group]\n");
548         exit(1);
549 }