K&R style function removal. Update functions to ANSI style.
[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.4 2003/10/04 20:36:46 hmp Exp $
29  */
30
31 #define _KERNEL_STRUCTURES
32
33 #include <err.h>
34 #include <fcntl.h>
35 #include <kvm.h>
36 #include <nlist.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <sys/types.h>
44 #include <sys/param.h>
45 #include <sys/time.h>
46 #include <sys/proc.h>
47 #include <sys/ipc.h>
48 #include <sys/sem.h>
49 #include <sys/shm.h>
50 #include <sys/msg.h>
51
52 struct semid_ds *sema;
53 struct seminfo  seminfo;
54 struct msginfo  msginfo;
55 struct msqid_ds *msqids;
56 struct shminfo  shminfo;
57 struct shmid_ds *shmsegs;
58
59 void    usage(void);
60
61 static struct nlist symbols[] = {
62         {"_sema"},
63 #define X_SEMA          0
64         {"_seminfo"},
65 #define X_SEMINFO       1
66         {"_semu"},
67 #define X_SEMU          2
68         {"_msginfo"},
69 #define X_MSGINFO       3
70         {"_msqids"},
71 #define X_MSQIDS        4
72         {"_shminfo"},
73 #define X_SHMINFO       5
74         {"_shmsegs"},
75 #define X_SHMSEGS       6
76         {NULL}
77 };
78
79 static kvm_t *kd;
80
81 char   *
82 fmt_perm(u_short mode)
83 {
84         static char buffer[100];
85
86         buffer[0] = '-';
87         buffer[1] = '-';
88         buffer[2] = ((mode & 0400) ? 'r' : '-');
89         buffer[3] = ((mode & 0200) ? 'w' : '-');
90         buffer[4] = ((mode & 0100) ? 'a' : '-');
91         buffer[5] = ((mode & 0040) ? 'r' : '-');
92         buffer[6] = ((mode & 0020) ? 'w' : '-');
93         buffer[7] = ((mode & 0010) ? 'a' : '-');
94         buffer[8] = ((mode & 0004) ? 'r' : '-');
95         buffer[9] = ((mode & 0002) ? 'w' : '-');
96         buffer[10] = ((mode & 0001) ? 'a' : '-');
97         buffer[11] = '\0';
98         return (&buffer[0]);
99 }
100
101 void
102 cvt_time(time_t t, char *buf)
103 {
104         struct tm *tm;
105
106         if (t == 0) {
107                 strcpy(buf, "no-entry");
108         } else {
109                 tm = localtime(&t);
110                 sprintf(buf, "%2d:%02d:%02d",
111                         tm->tm_hour, tm->tm_min, tm->tm_sec);
112         }
113 }
114 #define SHMINFO         1
115 #define SHMTOTAL        2
116 #define MSGINFO         4
117 #define MSGTOTAL        8
118 #define SEMINFO         16
119 #define SEMTOTAL        32
120
121 #define BIGGEST         1
122 #define CREATOR         2
123 #define OUTSTANDING     4
124 #define PID             8
125 #define TIME            16
126
127 int
128 main(int argc, char **argv)
129 {
130         int     display = SHMINFO | MSGINFO | SEMINFO;
131         int     option = 0;
132         char   *core = NULL, *namelist = NULL;
133         int     i;
134
135         while ((i = getopt(argc, argv, "MmQqSsabC:cN:optT")) != -1)
136                 switch (i) {
137                 case 'M':
138                         display = SHMTOTAL;
139                         break;
140                 case 'm':
141                         display = SHMINFO;
142                         break;
143                 case 'Q':
144                         display = MSGTOTAL;
145                         break;
146                 case 'q':
147                         display = MSGINFO;
148                         break;
149                 case 'S':
150                         display = SEMTOTAL;
151                         break;
152                 case 's':
153                         display = SEMINFO;
154                         break;
155                 case 'T':
156                         display = SHMTOTAL | MSGTOTAL | SEMTOTAL;
157                         break;
158                 case 'a':
159                         option |= BIGGEST | CREATOR | OUTSTANDING | PID | TIME;
160                         break;
161                 case 'b':
162                         option |= BIGGEST;
163                         break;
164                 case 'C':
165                         core = optarg;
166                         break;
167                 case 'c':
168                         option |= CREATOR;
169                         break;
170                 case 'N':
171                         namelist = optarg;
172                         break;
173                 case 'o':
174                         option |= OUTSTANDING;
175                         break;
176                 case 'p':
177                         option |= PID;
178                         break;
179                 case 't':
180                         option |= TIME;
181                         break;
182                 default:
183                         usage();
184                 }
185
186         /*
187          * Discard setgid privileges if not the running kernel so that bad
188          * guys can't print interesting stuff from kernel memory.
189          */
190         if (namelist != NULL || core != NULL)
191                 setgid(getgid());
192
193         if ((kd = kvm_open(namelist, core, NULL, O_RDONLY, "ipcs")) == NULL)
194                 exit(1);
195
196         switch (kvm_nlist(kd, symbols)) {
197         case 0:
198                 break;
199         case -1:
200                 errx(1, "unable to read kernel symbol table");
201         default:
202 #ifdef notdef           /* they'll be told more civilly later */
203                 warnx("nlist failed");
204                 for (i = 0; symbols[i].n_name != NULL; i++)
205                         if (symbols[i].n_value == 0)
206                                 warnx("symbol %s not found",
207                                     symbols[i].n_name);
208                 break;
209 #endif
210         }
211
212         if ((display & (MSGINFO | MSGTOTAL)) &&
213             kvm_read(kd, symbols[X_MSGINFO].n_value, &msginfo, sizeof(msginfo))== sizeof(msginfo)) {
214
215                 if (display & MSGTOTAL) {
216                         printf("msginfo:\n");
217                         printf("\tmsgmax: %6d\t(max characters in a message)\n",
218                             msginfo.msgmax);
219                         printf("\tmsgmni: %6d\t(# of message queues)\n",
220                             msginfo.msgmni);
221                         printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
222                             msginfo.msgmnb);
223                         printf("\tmsgtql: %6d\t(max # of messages in system)\n",
224                             msginfo.msgtql);
225                         printf("\tmsgssz: %6d\t(size of a message segment)\n",
226                             msginfo.msgssz);
227                         printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
228                             msginfo.msgseg);
229                 }
230                 if (display & MSGINFO) {
231                         struct msqid_ds *xmsqids;
232
233                         kvm_read(kd, symbols[X_MSQIDS].n_value, &msqids, sizeof(msqids));
234                         xmsqids = malloc(sizeof(struct msqid_ds) * msginfo.msgmni);
235                         kvm_read(kd, (u_long) msqids, xmsqids, sizeof(struct msqid_ds) * msginfo.msgmni);
236
237                         printf("Message Queues:\n");
238                         printf("T     ID     KEY        MODE       OWNER    GROUP");
239                         if (option & CREATOR)
240                                 printf("  CREATOR   CGROUP");
241                         if (option & OUTSTANDING)
242                                 printf(" CBYTES  QNUM");
243                         if (option & BIGGEST)
244                                 printf(" QBYTES");
245                         if (option & PID)
246                                 printf(" LSPID LRPID");
247                         if (option & TIME)
248                                 printf("   STIME    RTIME    CTIME");
249                         printf("\n");
250                         for (i = 0; i < msginfo.msgmni; i += 1) {
251                                 if (xmsqids[i].msg_qbytes != 0) {
252                                         char    stime_buf[100], rtime_buf[100],
253                                                 ctime_buf[100];
254                                         struct msqid_ds *msqptr = &xmsqids[i];
255
256                                         cvt_time(msqptr->msg_stime, stime_buf);
257                                         cvt_time(msqptr->msg_rtime, rtime_buf);
258                                         cvt_time(msqptr->msg_ctime, ctime_buf);
259
260                                         printf("q %6d %10d %s %8s %8s",
261                                             IXSEQ_TO_IPCID(i, msqptr->msg_perm),
262                                             msqptr->msg_perm.key,
263                                             fmt_perm(msqptr->msg_perm.mode),
264                                             user_from_uid(msqptr->msg_perm.uid, 0),
265                                             group_from_gid(msqptr->msg_perm.gid, 0));
266
267                                         if (option & CREATOR)
268                                                 printf(" %8s %8s",
269                                                     user_from_uid(msqptr->msg_perm.cuid, 0),
270                                                     group_from_gid(msqptr->msg_perm.cgid, 0));
271
272                                         if (option & OUTSTANDING)
273                                                 printf(" %6d %6d",
274                                                     msqptr->msg_cbytes,
275                                                     msqptr->msg_qnum);
276
277                                         if (option & BIGGEST)
278                                                 printf(" %6d",
279                                                     msqptr->msg_qbytes);
280
281                                         if (option & PID)
282                                                 printf(" %6d %6d",
283                                                     msqptr->msg_lspid,
284                                                     msqptr->msg_lrpid);
285
286                                         if (option & TIME)
287                                                 printf("%s %s %s",
288                                                     stime_buf,
289                                                     rtime_buf,
290                                                     ctime_buf);
291
292                                         printf("\n");
293                                 }
294                         }
295                         printf("\n");
296                 }
297         } else
298                 if (display & (MSGINFO | MSGTOTAL)) {
299                         fprintf(stderr,
300                             "SVID messages facility not configured in the system\n");
301                 }
302         if ((display & (SHMINFO | SHMTOTAL)) &&
303             kvm_read(kd, symbols[X_SHMINFO].n_value, &shminfo, sizeof(shminfo))) {
304                 if (display & SHMTOTAL) {
305                         printf("shminfo:\n");
306                         printf("\tshmmax: %7d\t(max shared memory segment size)\n",
307                             shminfo.shmmax);
308                         printf("\tshmmin: %7d\t(min shared memory segment size)\n",
309                             shminfo.shmmin);
310                         printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
311                             shminfo.shmmni);
312                         printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
313                             shminfo.shmseg);
314                         printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
315                             shminfo.shmall);
316                 }
317                 if (display & SHMINFO) {
318                         struct shmid_ds *xshmids;
319
320                         kvm_read(kd, symbols[X_SHMSEGS].n_value, &shmsegs, sizeof(shmsegs));
321                         xshmids = malloc(sizeof(struct shmid_ds) * shminfo.shmmni);
322                         kvm_read(kd, (u_long) shmsegs, xshmids, sizeof(struct shmid_ds) *
323                             shminfo.shmmni);
324
325                         printf("Shared Memory:\n");
326                         printf("T     ID     KEY        MODE       OWNER    GROUP");
327                         if (option & CREATOR)
328                                 printf("  CREATOR   CGROUP");
329                         if (option & OUTSTANDING)
330                                 printf(" NATTCH");
331                         if (option & BIGGEST)
332                                 printf("  SEGSZ");
333                         if (option & PID)
334                                 printf("  CPID  LPID");
335                         if (option & TIME)
336                                 printf("   ATIME    DTIME    CTIME");
337                         printf("\n");
338                         for (i = 0; i < shminfo.shmmni; i += 1) {
339                                 if (xshmids[i].shm_perm.mode & 0x0800) {
340                                         char    atime_buf[100], dtime_buf[100],
341                                                 ctime_buf[100];
342                                         struct shmid_ds *shmptr = &xshmids[i];
343
344                                         cvt_time(shmptr->shm_atime, atime_buf);
345                                         cvt_time(shmptr->shm_dtime, dtime_buf);
346                                         cvt_time(shmptr->shm_ctime, ctime_buf);
347
348                                         printf("m %6d %10d %s %8s %8s",
349                                             IXSEQ_TO_IPCID(i, shmptr->shm_perm),
350                                             shmptr->shm_perm.key,
351                                             fmt_perm(shmptr->shm_perm.mode),
352                                             user_from_uid(shmptr->shm_perm.uid, 0),
353                                             group_from_gid(shmptr->shm_perm.gid, 0));
354
355                                         if (option & CREATOR)
356                                                 printf(" %8s %8s",
357                                                     user_from_uid(shmptr->shm_perm.cuid, 0),
358                                                     group_from_gid(shmptr->shm_perm.cgid, 0));
359
360                                         if (option & OUTSTANDING)
361                                                 printf(" %6d",
362                                                     shmptr->shm_nattch);
363
364                                         if (option & BIGGEST)
365                                                 printf(" %6d",
366                                                     shmptr->shm_segsz);
367
368                                         if (option & PID)
369                                                 printf(" %6d %6d",
370                                                     shmptr->shm_cpid,
371                                                     shmptr->shm_lpid);
372
373                                         if (option & TIME)
374                                                 printf("%s %s %s",
375                                                     atime_buf,
376                                                     dtime_buf,
377                                                     ctime_buf);
378
379                                         printf("\n");
380                                 }
381                         }
382                         printf("\n");
383                 }
384         } else
385                 if (display & (SHMINFO | SHMTOTAL)) {
386                         fprintf(stderr,
387                             "SVID shared memory facility not configured in the system\n");
388                 }
389         if ((display & (SEMINFO | SEMTOTAL)) &&
390             kvm_read(kd, symbols[X_SEMINFO].n_value, &seminfo, sizeof(seminfo))) {
391                 struct semid_ds *xsema;
392
393                 if (display & SEMTOTAL) {
394                         printf("seminfo:\n");
395                         printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
396                             seminfo.semmap);
397                         printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
398                             seminfo.semmni);
399                         printf("\tsemmns: %6d\t(# of semaphores in system)\n",
400                             seminfo.semmns);
401                         printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
402                             seminfo.semmnu);
403                         printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
404                             seminfo.semmsl);
405                         printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
406                             seminfo.semopm);
407                         printf("\tsemume: %6d\t(max # of undo entries per process)\n",
408                             seminfo.semume);
409                         printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
410                             seminfo.semusz);
411                         printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
412                             seminfo.semvmx);
413                         printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
414                             seminfo.semaem);
415                 }
416                 if (display & SEMINFO) {
417                         kvm_read(kd, symbols[X_SEMA].n_value, &sema, sizeof(sema));
418                         xsema = malloc(sizeof(struct semid_ds) * seminfo.semmni);
419                         kvm_read(kd, (u_long) sema, xsema, sizeof(struct semid_ds) * seminfo.semmni);
420
421                         printf("Semaphores:\n");
422                         printf("T     ID     KEY        MODE       OWNER    GROUP");
423                         if (option & CREATOR)
424                                 printf("  CREATOR   CGROUP");
425                         if (option & BIGGEST)
426                                 printf(" NSEMS");
427                         if (option & TIME)
428                                 printf("   OTIME    CTIME");
429                         printf("\n");
430                         for (i = 0; i < seminfo.semmni; i += 1) {
431                                 if ((xsema[i].sem_perm.mode & SEM_ALLOC) != 0) {
432                                         char    ctime_buf[100], otime_buf[100];
433                                         struct semid_ds *semaptr = &xsema[i];
434
435                                         cvt_time(semaptr->sem_otime, otime_buf);
436                                         cvt_time(semaptr->sem_ctime, ctime_buf);
437
438                                         printf("s %6d %10d %s %8s %8s",
439                                             IXSEQ_TO_IPCID(i, semaptr->sem_perm),
440                                             semaptr->sem_perm.key,
441                                             fmt_perm(semaptr->sem_perm.mode),
442                                             user_from_uid(semaptr->sem_perm.uid, 0),
443                                             group_from_gid(semaptr->sem_perm.gid, 0));
444
445                                         if (option & CREATOR)
446                                                 printf(" %8s %8s",
447                                                     user_from_uid(semaptr->sem_perm.cuid, 0),
448                                                     group_from_gid(semaptr->sem_perm.cgid, 0));
449
450                                         if (option & BIGGEST)
451                                                 printf(" %6d",
452                                                     semaptr->sem_nsems);
453
454                                         if (option & TIME)
455                                                 printf("%s %s",
456                                                     otime_buf,
457                                                     ctime_buf);
458
459                                         printf("\n");
460                                 }
461                         }
462
463                         printf("\n");
464                 }
465         } else
466                 if (display & (SEMINFO | SEMTOTAL)) {
467                         fprintf(stderr, "SVID semaphores facility not configured in the system\n");
468                 }
469         kvm_close(kd);
470
471         exit(0);
472 }
473
474 void
475 usage(void)
476 {
477
478         fprintf(stderr,
479             "usage: ipcs [-abcmopqstMQST] [-C corefile] [-N namelist]\n");
480         exit(1);
481 }