blob: 857065983da9528c4c185c424e31e5d6d0ec98c6 [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 *
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Rob Landley6eb1e412005-06-20 04:30:36 +00009 */
10
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000011#include "busybox.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000012#include <errno.h>
13#include <time.h>
14#include <pwd.h>
15#include <grp.h>
16
17/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
18/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
19/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
20#include <sys/types.h>
21#include <sys/ipc.h>
22#include <sys/sem.h>
23#include <sys/msg.h>
24#include <sys/shm.h>
25
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000026
Rob Landley6eb1e412005-06-20 04:30:36 +000027
28/*-------------------------------------------------------------------*/
29/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
30 but inside #ifdef __KERNEL__ ... #endif */
31#ifndef SHM_DEST
32/* shm_mode upper byte flags */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000033#define SHM_DEST 01000 /* segment will be destroyed on last detach */
34#define SHM_LOCKED 02000 /* segment will not be swapped */
Rob Landley6eb1e412005-06-20 04:30:36 +000035#endif
36
37/* For older kernels the same holds for the defines below */
38#ifndef MSG_STAT
39#define MSG_STAT 11
40#define MSG_INFO 12
41#endif
42
43#ifndef SHM_STAT
44#define SHM_STAT 13
45#define SHM_INFO 14
46struct shm_info {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000047 int used_ids;
48 ulong shm_tot; /* total allocated shm */
49 ulong shm_rss; /* total resident shm */
50 ulong shm_swp; /* total swapped shm */
51 ulong swap_attempts;
52 ulong swap_successes;
Rob Landley6eb1e412005-06-20 04:30:36 +000053};
54#endif
55
56#ifndef SEM_STAT
57#define SEM_STAT 18
58#define SEM_INFO 19
59#endif
60
61/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
62#ifndef IPC_INFO
63#define IPC_INFO 3
64#endif
65/*-------------------------------------------------------------------*/
66
67/* The last arg of semctl is a union semun, but where is it defined?
68 X/OPEN tells us to define it ourselves, but until recently
69 Linux include files would also define it. */
70#if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
71/* union semun is defined by including <sys/sem.h> */
72#else
73/* according to X/OPEN we have to define it ourselves */
74union semun {
75 int val;
76 struct semid_ds *buf;
77 unsigned short int *array;
78 struct seminfo *__buf;
79};
80#endif
81
82/* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
83 libc 4/5 does not mention struct ipc_term at all, but includes
84 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
85 glibc-1.09 has no support for sysv ipc.
86 glibc 2 uses __key, __seq */
87#if defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
88#define KEY __key
89#else
90#define KEY key
91#endif
92
93#define LIMITS 1
94#define STATUS 2
95#define CREATOR 3
96#define TIME 4
97#define PID 5
98
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000099static char format;
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000100
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101static void print_perms(int id, struct ipc_perm *ipcp)
102{
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000103 struct passwd *pw;
104 struct group *gr;
105
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000106 bb_printf("%-10d %-10o", id, ipcp->mode & 0777);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000107
108 if ((pw = getpwuid(ipcp->cuid)))
109 bb_printf(" %-10s", pw->pw_name);
110 else
111 bb_printf(" %-10d", ipcp->cuid);
112 if ((gr = getgrgid(ipcp->cgid)))
113 bb_printf(" %-10s", gr->gr_name);
114 else
115 bb_printf(" %-10d", ipcp->cgid);
116
117 if ((pw = getpwuid(ipcp->uid)))
118 bb_printf(" %-10s", pw->pw_name);
119 else
120 bb_printf(" %-10d", ipcp->uid);
121 if ((gr = getgrgid(ipcp->gid)))
122 bb_printf(" %-10s\n", gr->gr_name);
123 else
124 bb_printf(" %-10d\n", ipcp->gid);
125}
126
127
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000128static void do_shm(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000129{
130 int maxid, shmid, id;
131 struct shmid_ds shmseg;
132 struct shm_info shm_info;
133 struct shminfo shminfo;
134 struct ipc_perm *ipcp = &shmseg.shm_perm;
135 struct passwd *pw;
136
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000137 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000138 if (maxid < 0) {
Denis Vlasenko59839142006-09-10 18:26:51 +0000139 bb_printf("kernel not configured for %s\n", "shared memory");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000140 return;
141 }
142
143 switch (format) {
144 case LIMITS:
Denis Vlasenko59839142006-09-10 18:26:51 +0000145 bb_printf("------ Shared Memory %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000146 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000147 return;
148 /* glibc 2.1.3 and all earlier libc's have ints as fields
149 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000150 bb_printf("max number of segments = %lu\n"
151 "max seg size (kbytes) = %lu\n"
152 "max total shared memory (pages) = %lu\n"
153 "min seg size (bytes) = %lu\n",
154 (unsigned long) shminfo.shmmni,
155 (unsigned long) (shminfo.shmmax >> 10),
156 (unsigned long) shminfo.shmall,
157 (unsigned long) shminfo.shmmin);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000158 return;
159
160 case STATUS:
Denis Vlasenko59839142006-09-10 18:26:51 +0000161 bb_printf("------ Shared Memory %s --------\n", "Status");
162 bb_printf( "segments allocated %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000163 "pages allocated %ld\n"
164 "pages resident %ld\n"
165 "pages swapped %ld\n"
Denis Vlasenko59839142006-09-10 18:26:51 +0000166 "Swap performance: %ld attempts\t%ld successes\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000167 shm_info.used_ids,
168 shm_info.shm_tot,
169 shm_info.shm_rss,
170 shm_info.shm_swp,
171 shm_info.swap_attempts, shm_info.swap_successes);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000172 return;
173
174 case CREATOR:
Denis Vlasenko59839142006-09-10 18:26:51 +0000175 bb_printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
176 bb_printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000177 "shmid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000178 break;
179
180 case TIME:
Denis Vlasenko59839142006-09-10 18:26:51 +0000181 bb_printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
182 bb_printf( "%-10s %-10s %-20s %-20s %-20s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000183 "shmid", "owner", "attached", "detached", "changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000184 break;
185
186 case PID:
Denis Vlasenko59839142006-09-10 18:26:51 +0000187 bb_printf("------ Shared Memory %s --------\n", "Creator/Last-op");
188 bb_printf( "%-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000189 "shmid", "owner", "cpid", "lpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000190 break;
191
192 default:
Denis Vlasenko59839142006-09-10 18:26:51 +0000193 bb_printf("------ Shared Memory %s --------\n", "Segments");
194 bb_printf( "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000195 "key", "shmid", "owner", "perms", "bytes", "nattch",
196 "status");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000197 break;
198 }
199
200 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000201 shmid = shmctl(id, SHM_STAT, &shmseg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000202 if (shmid < 0)
203 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000204 if (format == CREATOR) {
205 print_perms(shmid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000206 continue;
207 }
208 pw = getpwuid(ipcp->uid);
209 switch (format) {
210 case TIME:
211 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000212 bb_printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000213 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000214 bb_printf("%-10d %-10d", shmid, ipcp->uid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000215 /* ctime uses static buffer: use separate calls */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000216 bb_printf(" %-20.16s", shmseg.shm_atime
217 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000218 bb_printf(" %-20.16s", shmseg.shm_dtime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000219 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000220 bb_printf(" %-20.16s\n", shmseg.shm_ctime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000221 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000222 break;
223 case PID:
224 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000225 bb_printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000226 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000227 bb_printf("%-10d %-10d", shmid, ipcp->uid);
228 bb_printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000229 break;
230
231 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000232 bb_printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000233 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000234 bb_printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000235 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000236 bb_printf("%-10d %-10d", shmid, ipcp->uid);
Denis Vlasenko3ccc1002006-09-19 17:43:56 +0000237 bb_printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000238 /*
239 * earlier: int, Austin has size_t
240 */
241 (unsigned long) shmseg.shm_segsz,
242 /*
243 * glibc-2.1.3 and earlier has unsigned short;
244 * Austin has shmatt_t
245 */
246 (long) shmseg.shm_nattch,
247 ipcp->mode & SHM_DEST ? "dest" : " ",
248 ipcp->mode & SHM_LOCKED ? "locked" : " ");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000249 break;
250 }
251 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000252}
253
254
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000255static void do_sem(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000256{
257 int maxid, semid, id;
258 struct semid_ds semary;
259 struct seminfo seminfo;
260 struct ipc_perm *ipcp = &semary.sem_perm;
261 struct passwd *pw;
262 union semun arg;
263
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000264 arg.array = (ushort *) (void *) &seminfo;
265 maxid = semctl(0, 0, SEM_INFO, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000266 if (maxid < 0) {
Denis Vlasenko59839142006-09-10 18:26:51 +0000267 bb_printf("kernel not configured for %s\n", "semaphores");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000268 return;
269 }
270
271 switch (format) {
272 case LIMITS:
Denis Vlasenko59839142006-09-10 18:26:51 +0000273 bb_printf("------ Semaphore %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000274 arg.array = (ushort *) (void *) &seminfo; /* damn union */
275 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000276 return;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000277 bb_printf("max number of arrays = %d\n"
278 "max semaphores per array = %d\n"
279 "max semaphores system wide = %d\n"
280 "max ops per semop call = %d\n"
281 "semaphore max value = %d\n",
282 seminfo.semmni,
283 seminfo.semmsl,
284 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000285 return;
286
287 case STATUS:
Denis Vlasenko59839142006-09-10 18:26:51 +0000288 bb_printf("------ Semaphore %s --------\n", "Status");
289 bb_printf( "used arrays = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000290 "allocated semaphores = %d\n",
291 seminfo.semusz, seminfo.semaem);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000292 return;
293
294 case CREATOR:
Denis Vlasenko59839142006-09-10 18:26:51 +0000295 bb_printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
296 bb_printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000297 "semid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000298 break;
299
300 case TIME:
Denis Vlasenko59839142006-09-10 18:26:51 +0000301 bb_printf("------ Shared Memory %s --------\n", "Operation/Change Times");
302 bb_printf( "%-8s %-10s %-26.24s %-26.24s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000303 "shmid", "owner", "last-op", "last-changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000304 break;
305
306 case PID:
307 break;
308
309 default:
Denis Vlasenko59839142006-09-10 18:26:51 +0000310 bb_printf("------ Semaphore %s --------\n", "Arrays");
311 bb_printf( "%-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000312 "key", "semid", "owner", "perms", "nsems");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000313 break;
314 }
315
316 for (id = 0; id <= maxid; id++) {
317 arg.buf = (struct semid_ds *) &semary;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000318 semid = semctl(id, 0, SEM_STAT, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000319 if (semid < 0)
320 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000321 if (format == CREATOR) {
322 print_perms(semid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000323 continue;
324 }
325 pw = getpwuid(ipcp->uid);
326 switch (format) {
327 case TIME:
328 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000329 bb_printf("%-8d %-10.10s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000330 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000331 bb_printf("%-8d %-10d", semid, ipcp->uid);
332 /* ctime uses static buffer: use separate calls */
333 bb_printf(" %-26.24s", semary.sem_otime
334 ? ctime(&semary.sem_otime) : "Not set");
335 bb_printf(" %-26.24s\n", semary.sem_ctime
336 ? ctime(&semary.sem_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000337 break;
338 case PID:
339 break;
340
341 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000342 bb_printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000343 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000344 bb_printf("%-10d %-10.9s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000345 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000346 bb_printf("%-10d %-9d", semid, ipcp->uid);
Denis Vlasenko3ccc1002006-09-19 17:43:56 +0000347 bb_printf(" %-10o %-10ld\n", ipcp->mode & 0777,
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000348 /*
349 * glibc-2.1.3 and earlier has unsigned short;
350 * glibc-2.1.91 has variation between
351 * unsigned short and unsigned long
352 * Austin prescribes unsigned short.
353 */
354 (long) semary.sem_nsems);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000355 break;
356 }
357 }
358}
359
360
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000361static void do_msg(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000362{
363 int maxid, msqid, id;
364 struct msqid_ds msgque;
365 struct msginfo msginfo;
366 struct ipc_perm *ipcp = &msgque.msg_perm;
367 struct passwd *pw;
368
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000369 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000370 if (maxid < 0) {
Denis Vlasenko59839142006-09-10 18:26:51 +0000371 bb_printf("kernel not configured for %s\n", "message queues");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000372 return;
373 }
374
375 switch (format) {
376 case LIMITS:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000377 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000378 return;
Denis Vlasenko59839142006-09-10 18:26:51 +0000379 bb_printf("------ Message%s --------\n", "s: Limits");
380 bb_printf( "max queues system wide = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000381 "max size of message (bytes) = %d\n"
382 "default max size of queue (bytes) = %d\n",
383 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000384 return;
385
386 case STATUS:
Denis Vlasenko59839142006-09-10 18:26:51 +0000387 bb_printf("------ Message%s --------\n", "s: Status");
388 bb_printf( "allocated queues = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000389 "used headers = %d\n"
390 "used space = %d bytes\n",
391 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000392 return;
393
394 case CREATOR:
Denis Vlasenko59839142006-09-10 18:26:51 +0000395 bb_printf("------ Message%s --------\n", " Queues: Creators/Owners");
396 bb_printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000397 "msqid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000398 break;
399
400 case TIME:
Denis Vlasenko59839142006-09-10 18:26:51 +0000401 bb_printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
402 bb_printf( "%-8s %-10s %-20s %-20s %-20s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000403 "msqid", "owner", "send", "recv", "change");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000404 break;
405
406 case PID:
Denis Vlasenko59839142006-09-10 18:26:51 +0000407 bb_printf("------ Message%s --------\n", " Queues PIDs");
408 bb_printf( "%-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000409 "msqid", "owner", "lspid", "lrpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000410 break;
411
412 default:
Denis Vlasenko59839142006-09-10 18:26:51 +0000413 bb_printf("------ Message%s --------\n", " Queues");
414 bb_printf( "%-10s %-10s %-10s %-10s %-12s %-12s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000415 "key", "msqid", "owner", "perms", "used-bytes", "messages");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000416 break;
417 }
418
419 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000420 msqid = msgctl(id, MSG_STAT, &msgque);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000421 if (msqid < 0)
422 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000423 if (format == CREATOR) {
424 print_perms(msqid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000425 continue;
426 }
427 pw = getpwuid(ipcp->uid);
428 switch (format) {
429 case TIME:
430 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000431 bb_printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000432 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000433 bb_printf("%-8d %-10d", msqid, ipcp->uid);
434 bb_printf(" %-20.16s", msgque.msg_stime
435 ? ctime(&msgque.msg_stime) + 4 : "Not set");
436 bb_printf(" %-20.16s", msgque.msg_rtime
437 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
438 bb_printf(" %-20.16s\n", msgque.msg_ctime
439 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000440 break;
441 case PID:
442 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000443 bb_printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000444 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000445 bb_printf("%-8d %-10d", msqid, ipcp->uid);
446 bb_printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000447 break;
448
449 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000450 bb_printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000451 if (pw)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000452 bb_printf("%-10d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000453 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000454 bb_printf("%-10d %-10d", msqid, ipcp->uid);
455 bb_printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
456 /*
457 * glibc-2.1.3 and earlier has unsigned short;
458 * glibc-2.1.91 has variation between
459 * unsigned short, unsigned long
460 * Austin has msgqnum_t
461 */
462 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000463 break;
464 }
465 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000466}
467
468
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000469static void print_shm(int shmid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000470{
471 struct shmid_ds shmds;
472 struct ipc_perm *ipcp = &shmds.shm_perm;
473
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000474 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
475 bb_perror_msg("shmctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000476 return;
477 }
478
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000479 bb_printf("\nShared memory Segment shmid=%d\n"
480 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
481 "mode=%#o\taccess_perms=%#o\n"
482 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
483 shmid,
484 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
485 ipcp->mode, ipcp->mode & 0777,
486 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
487 (long) shmds.shm_nattch);
488 bb_printf("att_time=%-26.24s\n",
489 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
490 bb_printf("det_time=%-26.24s\n",
491 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
492 bb_printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000493}
494
495
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000496static void print_msg(int msqid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000497{
498 struct msqid_ds buf;
499 struct ipc_perm *ipcp = &buf.msg_perm;
500
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000501 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
502 bb_perror_msg("msgctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000503 return;
504 }
505
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000506 bb_printf("\nMessage Queue msqid=%d\n"
507 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
508 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
509 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
510 /*
511 * glibc-2.1.3 and earlier has unsigned short;
512 * glibc-2.1.91 has variation between
513 * unsigned short, unsigned long
514 * Austin has msgqnum_t (for msg_qbytes)
515 */
516 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
517 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
518
519 bb_printf("send_time=%-26.24s\n",
520 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
521 bb_printf("rcv_time=%-26.24s\n",
522 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
523 bb_printf("change_time=%-26.24s\n\n",
524 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000525}
526
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000527static void print_sem(int semid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000528{
529 struct semid_ds semds;
530 struct ipc_perm *ipcp = &semds.sem_perm;
531 union semun arg;
532 unsigned int i;
533
534 arg.buf = &semds;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000535 if (semctl(semid, 0, IPC_STAT, arg)) {
536 bb_perror_msg("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000537 return;
538 }
539
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000540 bb_printf("\nSemaphore Array semid=%d\n"
541 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
542 "mode=%#o, access_perms=%#o\n"
543 "nsems = %ld\n"
544 "otime = %-26.24s\n",
545 semid,
546 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
547 ipcp->mode, ipcp->mode & 0777,
548 (long) semds.sem_nsems,
549 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
550 bb_printf("ctime = %-26.24s\n"
551 "%-10s %-10s %-10s %-10s %-10s\n",
552 ctime(&semds.sem_ctime),
553 "semnum", "value", "ncount", "zcount", "pid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000554
555 arg.val = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000556 for (i = 0; i < semds.sem_nsems; i++) {
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000557 int val, ncnt, zcnt, pid;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000558
559 val = semctl(semid, i, GETVAL, arg);
560 ncnt = semctl(semid, i, GETNCNT, arg);
561 zcnt = semctl(semid, i, GETZCNT, arg);
562 pid = semctl(semid, i, GETPID, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000563 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000564 bb_perror_msg_and_die("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000565 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000566 bb_printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000567 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000568 bb_printf("\n");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000569}
Rob Landley6eb1e412005-06-20 04:30:36 +0000570
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000571int ipcs_main(int argc, char **argv)
572{
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000573 int id = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000574 unsigned flags = 0;
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000575 unsigned long opt;
576 char *opt_i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000577#define flag_print (1<<0)
578#define flag_msg (1<<1)
579#define flag_sem (1<<2)
580#define flag_shm (1<<3)
Rob Landley6eb1e412005-06-20 04:30:36 +0000581
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000582 opt = bb_getopt_ulflags(argc, argv, "i:aqsmtcplu", &opt_i);
583 if (opt & 0x1) { // -i
Denis Vlasenko7869c012006-09-22 08:30:52 +0000584 id = atoi(opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000585 flags |= flag_print;
Rob Landley6eb1e412005-06-20 04:30:36 +0000586 }
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000587 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
588 if (opt & 0x4) flags |= flag_msg; // -q
589 if (opt & 0x8) flags |= flag_sem; // -s
590 if (opt & 0x10) flags |= flag_shm; // -m
591 if (opt & 0x20) format = TIME; // -t
592 if (opt & 0x40) format = CREATOR; // -c
593 if (opt & 0x80) format = PID; // -p
594 if (opt & 0x100) format = LIMITS; // -l
595 if (opt & 0x200) format = STATUS; // -u
Rob Landley6eb1e412005-06-20 04:30:36 +0000596
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000597 if (flags & flag_print) {
598 if (flags & flag_shm) {
599 print_shm(id);
600 bb_fflush_stdout_and_exit(0);
Rob Landley6eb1e412005-06-20 04:30:36 +0000601 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000602 if (flags & flag_sem) {
603 print_sem(id);
604 bb_fflush_stdout_and_exit(0);
Rob Landley6eb1e412005-06-20 04:30:36 +0000605 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000606 if (flags & flag_msg) {
607 print_msg(id);
608 bb_fflush_stdout_and_exit(0);
Rob Landley6eb1e412005-06-20 04:30:36 +0000609 }
610 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000611 }
612
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000613 if (!(flags & (flag_shm | flag_msg | flag_sem)))
614 flags |= flag_msg | flag_shm | flag_sem;
615 bb_printf("\n");
Rob Landley6eb1e412005-06-20 04:30:36 +0000616
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000617 if (flags & flag_shm) {
618 do_shm();
619 bb_printf("\n");
Rob Landley6eb1e412005-06-20 04:30:36 +0000620 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000621 if (flags & flag_sem) {
622 do_sem();
623 bb_printf("\n");
Rob Landley6eb1e412005-06-20 04:30:36 +0000624 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000625 if (flags & flag_msg) {
626 do_msg();
627 bb_printf("\n");
Rob Landley6eb1e412005-06-20 04:30:36 +0000628 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000629 return EXIT_SUCCESS;
Rob Landley6eb1e412005-06-20 04:30:36 +0000630}