]>
git.saurik.com Git - apple/file_cmds.git/blob - ipcs/ipcs.c
2 * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
28 #include <sys/cdefs.h>
45 #include <sys/types.h>
46 #include <sys/ucred.h>
48 #include <sys/param.h>
50 #include <sys/sysctl.h>
55 #include <sys/sem_internal.h>
56 #include <sys/shm_internal.h>
61 /* The following is a kludge, until the problem of multiple inclusions
62 of ipc.h is taken care of. */
63 #ifndef IXSEQ_TO_IPCID
64 #define IXSEQ_TO_IPCID(ix,perm) (((perm.seq) << 16) | (ix & 0xffff))
71 static char buffer
[100];
75 buffer
[2] = ((mode
& 0400) ? 'r' : '-');
76 buffer
[3] = ((mode
& 0200) ? 'w' : '-');
77 buffer
[4] = ((mode
& 0100) ? 'a' : '-');
78 buffer
[5] = ((mode
& 0040) ? 'r' : '-');
79 buffer
[6] = ((mode
& 0020) ? 'w' : '-');
80 buffer
[7] = ((mode
& 0010) ? 'a' : '-');
81 buffer
[8] = ((mode
& 0004) ? 'r' : '-');
82 buffer
[9] = ((mode
& 0002) ? 'w' : '-');
83 buffer
[10] = ((mode
& 0001) ? 'a' : '-');
96 strcpy(buf
, "no-entry");
99 sprintf(buf
, "%2d:%02d:%02d",
100 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
112 #define OUTSTANDING 4
121 int display
= SHMINFO
| MSGINFO
| SEMINFO
;
123 char kvmoferr
[_POSIX2_LINE_MAX
]; /* Error buf for kvm_openfiles. */
126 while ((i
= getopt(argc
, argv
, "MmQqSsabcoptT")) != -1)
147 display
= SHMTOTAL
| MSGTOTAL
| SEMTOTAL
;
150 option
|= BIGGEST
| CREATOR
| OUTSTANDING
| PID
| TIME
;
159 option
|= OUTSTANDING
;
171 if ((display
& (MSGINFO
| MSGTOTAL
))) {
172 if (display
& MSGTOTAL
) {
173 struct IPCS_command ic
;
174 struct msginfo msginfo
;
175 size_t ic_size
= sizeof(ic
);
177 ic
.ipcs_magic
= IPCS_MAGIC
;
178 ic
.ipcs_op
= IPCS_MSG_CONF
;
179 ic
.ipcs_cursor
= 0; /* 0 for fw. compat. */
180 ic
.ipcs_data
= &msginfo
;
181 ic
.ipcs_datalen
= sizeof(msginfo
);
183 sysctlbyname(IPCS_MSG_SYSCTL
, &ic
, &ic_size
, &ic
, ic_size
);
184 printf("msginfo:\n");
185 printf("\tmsgmax: %6d\t(max characters in a message)\n",
187 printf("\tmsgmni: %6d\t(# of message queues)\n",
189 printf("\tmsgmnb: %6d\t(max characters in a message queue)\n",
191 printf("\tmsgtql: %6d\t(max # of messages in system)\n",
193 printf("\tmsgssz: %6d\t(size of a message segment)\n",
195 printf("\tmsgseg: %6d\t(# of message segments in system)\n\n",
198 if (display
& MSGINFO
) {
199 struct IPCS_command ic
;
201 struct msqid_ds
*msqptr
= &ds
;
202 size_t ic_size
= sizeof(ic
);
204 printf("Message Queues:\n");
205 printf("T ID KEY MODE OWNER GROUP");
206 if (option
& CREATOR
)
207 printf(" CREATOR CGROUP");
208 if (option
& OUTSTANDING
)
209 printf(" CBYTES QNUM");
210 if (option
& BIGGEST
)
213 printf(" LSPID LRPID");
215 printf(" STIME RTIME CTIME");
218 ic
.ipcs_magic
= IPCS_MAGIC
;
219 ic
.ipcs_op
= IPCS_MSG_ITER
;
220 ic
.ipcs_cursor
= 0; /* start */
221 ic
.ipcs_data
= msqptr
;
222 ic
.ipcs_datalen
= sizeof(*msqptr
);
224 while(!(sysctlbyname(IPCS_MSG_SYSCTL
, &ic
, &ic_size
, &ic
, ic_size
))) {
225 if (msqptr
->msg_qbytes
!= 0) {
226 char stime_buf
[100], rtime_buf
[100],
229 cvt_time(msqptr
->msg_stime
, stime_buf
);
230 cvt_time(msqptr
->msg_rtime
, rtime_buf
);
231 cvt_time(msqptr
->msg_ctime
, ctime_buf
);
233 printf("q %6d %10d %s %8s %8s",
234 IXSEQ_TO_IPCID(i
, msqptr
->msg_perm
),
235 (int)msqptr
->msg_perm
.key
,
236 fmt_perm(msqptr
->msg_perm
.mode
),
237 user_from_uid(msqptr
->msg_perm
.uid
, 0),
238 group_from_gid(msqptr
->msg_perm
.gid
, 0));
240 if (option
& CREATOR
)
242 user_from_uid(msqptr
->msg_perm
.cuid
, 0),
243 group_from_gid(msqptr
->msg_perm
.cgid
, 0));
245 if (option
& OUTSTANDING
)
250 if (option
& BIGGEST
)
271 if (display
& (MSGINFO
| MSGTOTAL
)) {
273 "SVID messages facility not configured in the system\n");
276 if ((display
& (SHMINFO
| SHMTOTAL
))) {
277 if (display
& SHMTOTAL
) {
278 struct IPCS_command ic
;
279 struct shminfo shminfo
;
280 size_t ic_size
= sizeof(ic
);
282 ic
.ipcs_magic
= IPCS_MAGIC
;
283 ic
.ipcs_op
= IPCS_SHM_CONF
;
284 ic
.ipcs_cursor
= 0; /* 0 for fw. compat. */
285 ic
.ipcs_data
= &shminfo
;
286 ic
.ipcs_datalen
= sizeof(shminfo
);
288 sysctlbyname(IPCS_SHM_SYSCTL
, &ic
, &ic_size
, &ic
, ic_size
);
289 printf("shminfo:\n");
290 printf("\tshmmax: %7d\t(max shared memory segment size)\n",
292 printf("\tshmmin: %7d\t(min shared memory segment size)\n",
294 printf("\tshmmni: %7d\t(max number of shared memory identifiers)\n",
296 printf("\tshmseg: %7d\t(max shared memory segments per process)\n",
298 printf("\tshmall: %7d\t(max amount of shared memory in pages)\n\n",
301 if (display
& SHMINFO
) {
302 struct IPCS_command ic
;
304 struct shmid_ds
*shmptr
= &ds
;
305 size_t ic_size
= sizeof(ic
);
307 printf("Shared Memory:\n");
308 printf("T ID KEY MODE OWNER GROUP");
309 if (option
& CREATOR
)
310 printf(" CREATOR CGROUP");
311 if (option
& OUTSTANDING
)
313 if (option
& BIGGEST
)
316 printf(" CPID LPID");
318 printf(" ATIME DTIME CTIME");
322 ic
.ipcs_magic
= IPCS_MAGIC
;
323 ic
.ipcs_op
= IPCS_SHM_ITER
;
324 ic
.ipcs_cursor
= 0; /* start */
325 ic
.ipcs_data
= shmptr
;
326 ic
.ipcs_datalen
= sizeof(*shmptr
);
328 while(!(sysctlbyname(IPCS_SHM_SYSCTL
, &ic
, &ic_size
, &ic
, ic_size
))) {
329 if (shmptr
->shm_perm
.mode
& 0x0800) {
330 char atime_buf
[100], dtime_buf
[100],
333 cvt_time(shmptr
->shm_atime
, atime_buf
);
334 cvt_time(shmptr
->shm_dtime
, dtime_buf
);
335 cvt_time(shmptr
->shm_ctime
, ctime_buf
);
337 printf("m %6d %10d %s %8s %8s",
338 IXSEQ_TO_IPCID(i
, shmptr
->shm_perm
),
339 (int)shmptr
->shm_perm
.key
,
340 fmt_perm(shmptr
->shm_perm
.mode
),
341 user_from_uid(shmptr
->shm_perm
.uid
, 0),
342 group_from_gid(shmptr
->shm_perm
.gid
, 0));
344 if (option
& CREATOR
)
346 user_from_uid(shmptr
->shm_perm
.cuid
, 0),
347 group_from_gid(shmptr
->shm_perm
.cgid
, 0));
349 if (option
& OUTSTANDING
)
353 if (option
& BIGGEST
)
376 if (display
& (SHMINFO
| SHMTOTAL
)) {
378 "SVID shared memory facility not configured in the system\n");
381 if ((display
& (SEMINFO
| SEMTOTAL
))) {
382 if (display
& SEMTOTAL
) {
383 struct IPCS_command ic
;
384 struct seminfo seminfo
;
385 size_t ic_size
= sizeof(ic
);
387 ic
.ipcs_magic
= IPCS_MAGIC
;
388 ic
.ipcs_op
= IPCS_SEM_CONF
;
389 ic
.ipcs_cursor
= 0; /* 0 for fw. compat. */
390 ic
.ipcs_data
= &seminfo
;
391 ic
.ipcs_datalen
= sizeof(seminfo
);
393 sysctlbyname(IPCS_SEM_SYSCTL
, &ic
, &ic_size
, &ic
, ic_size
);
394 printf("seminfo:\n");
395 printf("\tsemmap: %6d\t(# of entries in semaphore map)\n",
397 printf("\tsemmni: %6d\t(# of semaphore identifiers)\n",
399 printf("\tsemmns: %6d\t(# of semaphores in system)\n",
401 printf("\tsemmnu: %6d\t(# of undo structures in system)\n",
403 printf("\tsemmsl: %6d\t(max # of semaphores per id)\n",
405 printf("\tsemopm: %6d\t(max # of operations per semop call)\n",
407 printf("\tsemume: %6d\t(max # of undo entries per process)\n",
409 printf("\tsemusz: %6d\t(size in bytes of undo structure)\n",
411 printf("\tsemvmx: %6d\t(semaphore maximum value)\n",
413 printf("\tsemaem: %6d\t(adjust on exit max value)\n\n",
416 if (display
& SEMINFO
) {
417 struct IPCS_command ic
;
419 struct semid_ds
*semaptr
= &ds
;
420 size_t ic_size
= sizeof(ic
);
422 printf("Semaphores:\n");
423 printf("T ID KEY MODE OWNER GROUP");
424 if (option
& CREATOR
)
425 printf(" CREATOR CGROUP");
426 if (option
& BIGGEST
)
429 printf(" OTIME CTIME");
432 ic
.ipcs_magic
= IPCS_MAGIC
;
433 ic
.ipcs_op
= IPCS_SEM_ITER
;
434 ic
.ipcs_cursor
= 0; /* start */
435 ic
.ipcs_data
= semaptr
;
436 ic
.ipcs_datalen
= sizeof(*semaptr
);
438 while(!(sysctlbyname(IPCS_SEM_SYSCTL
, &ic
, &ic_size
, &ic
, ic_size
))) {
439 if ((semaptr
->sem_perm
.mode
& SEM_ALLOC
) != 0) {
440 char ctime_buf
[100], otime_buf
[100];
442 cvt_time(semaptr
->sem_otime
, otime_buf
);
443 cvt_time(semaptr
->sem_ctime
, ctime_buf
);
445 printf("s %6d %10d %s %8s %8s",
446 IXSEQ_TO_IPCID(i
, semaptr
->sem_perm
),
447 (int)semaptr
->sem_perm
.key
,
448 fmt_perm(semaptr
->sem_perm
.mode
),
449 user_from_uid(semaptr
->sem_perm
.uid
, 0),
450 group_from_gid(semaptr
->sem_perm
.gid
, 0));
452 if (option
& CREATOR
)
454 user_from_uid(semaptr
->sem_perm
.cuid
, 0),
455 group_from_gid(semaptr
->sem_perm
.cgid
, 0));
457 if (option
& BIGGEST
)
473 if (display
& (SEMINFO
| SEMTOTAL
)) {
474 fprintf(stderr
, "SVID semaphores facility not configured in the system\n");
484 "usage: ipcs [-abcmopqstMQST]\n");