blob: 2668cafd4622687197728ee483633f57b51714f5 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley6eb1e412005-06-20 04:30:36 +00002/*
3 * ipcs.c -- provides information on allocated ipc resources.
4 *
5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
6 * Adapted for busybox from util-linux-2.12a.
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley6eb1e412005-06-20 04:30:36 +00009 */
10
Pere Orga5bc8c002011-04-11 03:29:49 +020011//usage:#define ipcs_trivial_usage
12//usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
13//usage:#define ipcs_full_usage "\n\n"
14//usage: " -i Show specific resource"
15//usage: "\nResource specification:"
16//usage: "\n -m Shared memory segments"
17//usage: "\n -q Message queues"
18//usage: "\n -s Semaphore arrays"
19//usage: "\n -a All (default)"
20//usage: "\nOutput format:"
21//usage: "\n -t Time"
22//usage: "\n -c Creator"
23//usage: "\n -p Pid"
24//usage: "\n -l Limits"
25//usage: "\n -u Summary"
26
Rob Landley6eb1e412005-06-20 04:30:36 +000027/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
28/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
29/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
30#include <sys/types.h>
31#include <sys/ipc.h>
32#include <sys/sem.h>
33#include <sys/msg.h>
34#include <sys/shm.h>
35
Denis Vlasenkofad2b862007-05-31 22:16:38 +000036#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000037
38/*-------------------------------------------------------------------*/
39/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
40 but inside #ifdef __KERNEL__ ... #endif */
41#ifndef SHM_DEST
42/* shm_mode upper byte flags */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000043#define SHM_DEST 01000 /* segment will be destroyed on last detach */
44#define SHM_LOCKED 02000 /* segment will not be swapped */
Rob Landley6eb1e412005-06-20 04:30:36 +000045#endif
46
47/* For older kernels the same holds for the defines below */
48#ifndef MSG_STAT
49#define MSG_STAT 11
50#define MSG_INFO 12
51#endif
52
53#ifndef SHM_STAT
54#define SHM_STAT 13
55#define SHM_INFO 14
56struct shm_info {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000057 int used_ids;
Dan Fandrichf3854f72011-06-03 20:25:17 +020058 unsigned long shm_tot; /* total allocated shm */
59 unsigned long shm_rss; /* total resident shm */
60 unsigned long shm_swp; /* total swapped shm */
61 unsigned long swap_attempts;
62 unsigned long swap_successes;
Rob Landley6eb1e412005-06-20 04:30:36 +000063};
64#endif
65
66#ifndef SEM_STAT
67#define SEM_STAT 18
68#define SEM_INFO 19
69#endif
70
71/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
72#ifndef IPC_INFO
73#define IPC_INFO 3
74#endif
75/*-------------------------------------------------------------------*/
76
77/* The last arg of semctl is a union semun, but where is it defined?
78 X/OPEN tells us to define it ourselves, but until recently
79 Linux include files would also define it. */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000080#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000081/* union semun is defined by including <sys/sem.h> */
82#else
83/* according to X/OPEN we have to define it ourselves */
84union semun {
85 int val;
86 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000087 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000088 struct seminfo *__buf;
89};
90#endif
91
92/* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
93 libc 4/5 does not mention struct ipc_term at all, but includes
94 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
95 glibc-1.09 has no support for sysv ipc.
96 glibc 2 uses __key, __seq */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000097#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
Rob Landley6eb1e412005-06-20 04:30:36 +000098#define KEY __key
99#else
100#define KEY key
101#endif
102
103#define LIMITS 1
104#define STATUS 2
105#define CREATOR 3
106#define TIME 4
107#define PID 5
108
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000109static char format;
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000110
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000111static void print_perms(int id, struct ipc_perm *ipcp)
112{
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000113 struct passwd *pw;
114 struct group *gr;
115
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000116 printf("%-10d %-10o", id, ipcp->mode & 0777);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000117
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000118 pw = getpwuid(ipcp->cuid);
119 if (pw) printf(" %-10s", pw->pw_name);
120 else printf(" %-10d", ipcp->cuid);
121 gr = getgrgid(ipcp->cgid);
122 if (gr) printf(" %-10s", gr->gr_name);
123 else printf(" %-10d", ipcp->cgid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000124
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000125 pw = getpwuid(ipcp->uid);
126 if (pw) printf(" %-10s", pw->pw_name);
127 else printf(" %-10d", ipcp->uid);
128 gr = getgrgid(ipcp->gid);
129 if (gr) printf(" %-10s\n", gr->gr_name);
130 else printf(" %-10d\n", ipcp->gid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000131}
132
133
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200134static NOINLINE void do_shm(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000135{
136 int maxid, shmid, id;
137 struct shmid_ds shmseg;
138 struct shm_info shm_info;
139 struct shminfo shminfo;
140 struct ipc_perm *ipcp = &shmseg.shm_perm;
141 struct passwd *pw;
142
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000143 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000144 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000145 printf("kernel not configured for %s\n", "shared memory");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000146 return;
147 }
148
149 switch (format) {
150 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000151 printf("------ Shared Memory %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000152 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000153 return;
154 /* glibc 2.1.3 and all earlier libc's have ints as fields
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100155 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000156 printf("max number of segments = %lu\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100157 "max seg size (kbytes) = %lu\n"
158 "max total shared memory (pages) = %lu\n"
159 "min seg size (bytes) = %lu\n",
160 (unsigned long) shminfo.shmmni,
161 (unsigned long) (shminfo.shmmax >> 10),
162 (unsigned long) shminfo.shmall,
163 (unsigned long) shminfo.shmmin);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000164 return;
165
166 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000167 printf("------ Shared Memory %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100168 printf("segments allocated %d\n"
169 "pages allocated %ld\n"
170 "pages resident %ld\n"
171 "pages swapped %ld\n"
172 "Swap performance: %ld attempts\t%ld successes\n",
173 shm_info.used_ids,
174 shm_info.shm_tot,
175 shm_info.shm_rss,
176 shm_info.shm_swp,
177 shm_info.swap_attempts, shm_info.swap_successes);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000178 return;
179
180 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000181 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100182 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
183 "shmid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000184 break;
185
186 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000187 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100188 printf("%-10s %-10s %-20s %-20s %-20s\n",
189 "shmid", "owner", "attached", "detached", "changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000190 break;
191
192 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000193 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
Denys Vlasenko69675782013-01-14 01:34:48 +0100194 printf("%-10s %-10s %-10s %-10s\n",
195 "shmid", "owner", "cpid", "lpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000196 break;
197
198 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000199 printf("------ Shared Memory %s --------\n", "Segments");
Denys Vlasenko69675782013-01-14 01:34:48 +0100200 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
201 "key", "shmid", "owner", "perms", "bytes", "nattch",
202 "status");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000203 break;
204 }
205
206 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000207 shmid = shmctl(id, SHM_STAT, &shmseg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000208 if (shmid < 0)
209 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000210 if (format == CREATOR) {
211 print_perms(shmid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000212 continue;
213 }
214 pw = getpwuid(ipcp->uid);
215 switch (format) {
216 case TIME:
217 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000218 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000219 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000220 printf("%-10d %-10d", shmid, ipcp->uid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000221 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000222 printf(" %-20.16s", shmseg.shm_atime
Denys Vlasenko69675782013-01-14 01:34:48 +0100223 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000224 printf(" %-20.16s", shmseg.shm_dtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100225 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000226 printf(" %-20.16s\n", shmseg.shm_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100227 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000228 break;
229 case PID:
230 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000231 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000232 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000233 printf("%-10d %-10d", shmid, ipcp->uid);
234 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000235 break;
236
237 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000238 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000239 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000240 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000241 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000242 printf("%-10d %-10d", shmid, ipcp->uid);
243 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100244 /*
245 * earlier: int, Austin has size_t
246 */
247 (unsigned long) shmseg.shm_segsz,
248 /*
249 * glibc-2.1.3 and earlier has unsigned short;
250 * Austin has shmatt_t
251 */
252 (long) shmseg.shm_nattch,
253 ipcp->mode & SHM_DEST ? "dest" : " ",
254 ipcp->mode & SHM_LOCKED ? "locked" : " ");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000255 break;
256 }
257 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000258}
259
260
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200261static NOINLINE void do_sem(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000262{
263 int maxid, semid, id;
264 struct semid_ds semary;
265 struct seminfo seminfo;
266 struct ipc_perm *ipcp = &semary.sem_perm;
267 struct passwd *pw;
268 union semun arg;
269
Dan Fandrichf3854f72011-06-03 20:25:17 +0200270 arg.array = (unsigned short *) (void *) &seminfo;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000271 maxid = semctl(0, 0, SEM_INFO, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000272 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000273 printf("kernel not configured for %s\n", "semaphores");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000274 return;
275 }
276
277 switch (format) {
278 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000279 printf("------ Semaphore %s --------\n", "Limits");
Dan Fandrichf3854f72011-06-03 20:25:17 +0200280 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000281 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000282 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000283 printf("max number of arrays = %d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100284 "max semaphores per array = %d\n"
285 "max semaphores system wide = %d\n"
286 "max ops per semop call = %d\n"
287 "semaphore max value = %d\n",
288 seminfo.semmni,
289 seminfo.semmsl,
290 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000291 return;
292
293 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000294 printf("------ Semaphore %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100295 printf("used arrays = %d\n"
296 "allocated semaphores = %d\n",
297 seminfo.semusz, seminfo.semaem);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000298 return;
299
300 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000301 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100302 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
303 "semid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000304 break;
305
306 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000307 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100308 printf("%-8s %-10s %-26.24s %-26.24s\n",
309 "shmid", "owner", "last-op", "last-changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000310 break;
311
312 case PID:
313 break;
314
315 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000316 printf("------ Semaphore %s --------\n", "Arrays");
Denys Vlasenko69675782013-01-14 01:34:48 +0100317 printf("%-10s %-10s %-10s %-10s %-10s\n",
318 "key", "semid", "owner", "perms", "nsems");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000319 break;
320 }
321
322 for (id = 0; id <= maxid; id++) {
323 arg.buf = (struct semid_ds *) &semary;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000324 semid = semctl(id, 0, SEM_STAT, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000325 if (semid < 0)
326 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000327 if (format == CREATOR) {
328 print_perms(semid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000329 continue;
330 }
331 pw = getpwuid(ipcp->uid);
332 switch (format) {
333 case TIME:
334 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000335 printf("%-8d %-10.10s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000336 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000337 printf("%-8d %-10d", semid, ipcp->uid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000338 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000339 printf(" %-26.24s", semary.sem_otime
Denys Vlasenko69675782013-01-14 01:34:48 +0100340 ? ctime(&semary.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000341 printf(" %-26.24s\n", semary.sem_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100342 ? ctime(&semary.sem_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000343 break;
344 case PID:
345 break;
346
347 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000348 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000349 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000350 printf("%-10d %-10.9s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000351 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000352 printf("%-10d %-9d", semid, ipcp->uid);
353 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100354 /*
355 * glibc-2.1.3 and earlier has unsigned short;
356 * glibc-2.1.91 has variation between
357 * unsigned short and unsigned long
358 * Austin prescribes unsigned short.
359 */
360 (long) semary.sem_nsems);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000361 break;
362 }
363 }
364}
365
366
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200367static NOINLINE void do_msg(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000368{
369 int maxid, msqid, id;
370 struct msqid_ds msgque;
371 struct msginfo msginfo;
372 struct ipc_perm *ipcp = &msgque.msg_perm;
373 struct passwd *pw;
374
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000375 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000376 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000377 printf("kernel not configured for %s\n", "message queues");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000378 return;
379 }
380
381 switch (format) {
382 case LIMITS:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000383 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000384 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000385 printf("------ Message%s --------\n", "s: Limits");
Denys Vlasenko69675782013-01-14 01:34:48 +0100386 printf("max queues system wide = %d\n"
387 "max size of message (bytes) = %d\n"
388 "default max size of queue (bytes) = %d\n",
389 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000390 return;
391
392 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000393 printf("------ Message%s --------\n", "s: Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100394 printf("allocated queues = %d\n"
395 "used headers = %d\n"
396 "used space = %d bytes\n",
397 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000398 return;
399
400 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000401 printf("------ Message%s --------\n", " Queues: Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100402 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
403 "msqid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000404 break;
405
406 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000407 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100408 printf("%-8s %-10s %-20s %-20s %-20s\n",
409 "msqid", "owner", "send", "recv", "change");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000410 break;
411
412 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000413 printf("------ Message%s --------\n", " Queues PIDs");
Denys Vlasenko69675782013-01-14 01:34:48 +0100414 printf("%-10s %-10s %-10s %-10s\n",
415 "msqid", "owner", "lspid", "lrpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000416 break;
417
418 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000419 printf("------ Message%s --------\n", " Queues");
Denys Vlasenko69675782013-01-14 01:34:48 +0100420 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
421 "key", "msqid", "owner", "perms", "used-bytes", "messages");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000422 break;
423 }
424
425 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000426 msqid = msgctl(id, MSG_STAT, &msgque);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000427 if (msqid < 0)
428 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000429 if (format == CREATOR) {
430 print_perms(msqid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000431 continue;
432 }
433 pw = getpwuid(ipcp->uid);
434 switch (format) {
435 case TIME:
436 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000437 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000438 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000439 printf("%-8d %-10d", msqid, ipcp->uid);
440 printf(" %-20.16s", msgque.msg_stime
Denys Vlasenko69675782013-01-14 01:34:48 +0100441 ? ctime(&msgque.msg_stime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000442 printf(" %-20.16s", msgque.msg_rtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100443 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000444 printf(" %-20.16s\n", msgque.msg_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100445 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000446 break;
447 case PID:
448 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000449 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000450 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000451 printf("%-8d %-10d", msqid, ipcp->uid);
452 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000453 break;
454
455 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000456 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000457 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000458 printf("%-10d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000459 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000460 printf("%-10d %-10d", msqid, ipcp->uid);
461 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100462 /*
463 * glibc-2.1.3 and earlier has unsigned short;
464 * glibc-2.1.91 has variation between
465 * unsigned short, unsigned long
466 * Austin has msgqnum_t
467 */
468 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000469 break;
470 }
471 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000472}
473
474
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000475static void print_shm(int shmid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000476{
477 struct shmid_ds shmds;
478 struct ipc_perm *ipcp = &shmds.shm_perm;
479
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000480 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
481 bb_perror_msg("shmctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000482 return;
483 }
484
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000485 printf("\nShared memory Segment shmid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100486 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
487 "mode=%#o\taccess_perms=%#o\n"
488 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
489 shmid,
490 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
491 ipcp->mode, ipcp->mode & 0777,
492 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
493 (long) shmds.shm_nattch);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000494 printf("att_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100495 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000496 printf("det_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100497 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000498 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000499}
500
501
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000502static void print_msg(int msqid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000503{
504 struct msqid_ds buf;
505 struct ipc_perm *ipcp = &buf.msg_perm;
506
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000507 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
508 bb_perror_msg("msgctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000509 return;
510 }
511
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000512 printf("\nMessage Queue msqid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100513 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
514 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
515 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
516 /*
517 * glibc-2.1.3 and earlier has unsigned short;
518 * glibc-2.1.91 has variation between
519 * unsigned short, unsigned long
520 * Austin has msgqnum_t (for msg_qbytes)
521 */
522 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
523 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000524
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000525 printf("send_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100526 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000527 printf("rcv_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100528 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000529 printf("change_time=%-26.24s\n\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100530 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000531}
532
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000533static void print_sem(int semid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000534{
535 struct semid_ds semds;
536 struct ipc_perm *ipcp = &semds.sem_perm;
537 union semun arg;
538 unsigned int i;
539
540 arg.buf = &semds;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000541 if (semctl(semid, 0, IPC_STAT, arg)) {
542 bb_perror_msg("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000543 return;
544 }
545
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000546 printf("\nSemaphore Array semid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100547 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
548 "mode=%#o, access_perms=%#o\n"
549 "nsems = %ld\n"
550 "otime = %-26.24s\n",
551 semid,
552 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
553 ipcp->mode, ipcp->mode & 0777,
554 (long) semds.sem_nsems,
555 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000556 printf("ctime = %-26.24s\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100557 "%-10s %-10s %-10s %-10s %-10s\n",
558 ctime(&semds.sem_ctime),
559 "semnum", "value", "ncount", "zcount", "pid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000560
561 arg.val = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000562 for (i = 0; i < semds.sem_nsems; i++) {
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000563 int val, ncnt, zcnt, pid;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000564
565 val = semctl(semid, i, GETVAL, arg);
566 ncnt = semctl(semid, i, GETNCNT, arg);
567 zcnt = semctl(semid, i, GETZCNT, arg);
568 pid = semctl(semid, i, GETPID, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000569 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000570 bb_perror_msg_and_die("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000571 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000572 printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000573 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000574 bb_putchar('\n');
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000575}
Rob Landley6eb1e412005-06-20 04:30:36 +0000576
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000577int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000578int ipcs_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000579{
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000580 int id = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000581 unsigned flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000582 unsigned opt;
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000583 char *opt_i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000584#define flag_print (1<<0)
585#define flag_msg (1<<1)
586#define flag_sem (1<<2)
587#define flag_shm (1<<3)
Rob Landley6eb1e412005-06-20 04:30:36 +0000588
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000589 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000590 if (opt & 0x1) { // -i
Denis Vlasenko13858992006-10-08 12:49:22 +0000591 id = xatoi(opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000592 flags |= flag_print;
Rob Landley6eb1e412005-06-20 04:30:36 +0000593 }
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000594 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
595 if (opt & 0x4) flags |= flag_msg; // -q
596 if (opt & 0x8) flags |= flag_sem; // -s
597 if (opt & 0x10) flags |= flag_shm; // -m
598 if (opt & 0x20) format = TIME; // -t
599 if (opt & 0x40) format = CREATOR; // -c
600 if (opt & 0x80) format = PID; // -p
601 if (opt & 0x100) format = LIMITS; // -l
602 if (opt & 0x200) format = STATUS; // -u
Rob Landley6eb1e412005-06-20 04:30:36 +0000603
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000604 if (flags & flag_print) {
605 if (flags & flag_shm) {
606 print_shm(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000607 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000608 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000609 if (flags & flag_sem) {
610 print_sem(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000611 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000612 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000613 if (flags & flag_msg) {
614 print_msg(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000615 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000616 }
617 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000618 }
619
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000620 if (!(flags & (flag_shm | flag_msg | flag_sem)))
621 flags |= flag_msg | flag_shm | flag_sem;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000622 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000623
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000624 if (flags & flag_shm) {
625 do_shm();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000626 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000627 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000628 if (flags & flag_sem) {
629 do_sem();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000630 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000631 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000632 if (flags & flag_msg) {
633 do_msg();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000634 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000635 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000636 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000637}