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