blob: c1103b09313e2cedc874447221078e9c3b819ba6 [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
Rob Landley6eb1e412005-06-20 04:30:36 +000011/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
12/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
13/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
14#include <sys/types.h>
15#include <sys/ipc.h>
16#include <sys/sem.h>
17#include <sys/msg.h>
18#include <sys/shm.h>
19
Denis Vlasenkofad2b862007-05-31 22:16:38 +000020#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000021
22/*-------------------------------------------------------------------*/
23/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
24 but inside #ifdef __KERNEL__ ... #endif */
25#ifndef SHM_DEST
26/* shm_mode upper byte flags */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000027#define SHM_DEST 01000 /* segment will be destroyed on last detach */
28#define SHM_LOCKED 02000 /* segment will not be swapped */
Rob Landley6eb1e412005-06-20 04:30:36 +000029#endif
30
31/* For older kernels the same holds for the defines below */
32#ifndef MSG_STAT
33#define MSG_STAT 11
34#define MSG_INFO 12
35#endif
36
37#ifndef SHM_STAT
38#define SHM_STAT 13
39#define SHM_INFO 14
40struct shm_info {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000041 int used_ids;
42 ulong shm_tot; /* total allocated shm */
43 ulong shm_rss; /* total resident shm */
44 ulong shm_swp; /* total swapped shm */
45 ulong swap_attempts;
46 ulong swap_successes;
Rob Landley6eb1e412005-06-20 04:30:36 +000047};
48#endif
49
50#ifndef SEM_STAT
51#define SEM_STAT 18
52#define SEM_INFO 19
53#endif
54
55/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
56#ifndef IPC_INFO
57#define IPC_INFO 3
58#endif
59/*-------------------------------------------------------------------*/
60
61/* The last arg of semctl is a union semun, but where is it defined?
62 X/OPEN tells us to define it ourselves, but until recently
63 Linux include files would also define it. */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000064#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000065/* union semun is defined by including <sys/sem.h> */
66#else
67/* according to X/OPEN we have to define it ourselves */
68union semun {
69 int val;
70 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000071 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000072 struct seminfo *__buf;
73};
74#endif
75
76/* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
77 libc 4/5 does not mention struct ipc_term at all, but includes
78 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
79 glibc-1.09 has no support for sysv ipc.
80 glibc 2 uses __key, __seq */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000081#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
Rob Landley6eb1e412005-06-20 04:30:36 +000082#define KEY __key
83#else
84#define KEY key
85#endif
86
87#define LIMITS 1
88#define STATUS 2
89#define CREATOR 3
90#define TIME 4
91#define PID 5
92
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000093static char format;
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +000094
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000095static void print_perms(int id, struct ipc_perm *ipcp)
96{
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +000097 struct passwd *pw;
98 struct group *gr;
99
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000100 printf("%-10d %-10o", id, ipcp->mode & 0777);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000101
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000102 pw = getpwuid(ipcp->cuid);
103 if (pw) printf(" %-10s", pw->pw_name);
104 else printf(" %-10d", ipcp->cuid);
105 gr = getgrgid(ipcp->cgid);
106 if (gr) printf(" %-10s", gr->gr_name);
107 else printf(" %-10d", ipcp->cgid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000108
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000109 pw = getpwuid(ipcp->uid);
110 if (pw) printf(" %-10s", pw->pw_name);
111 else printf(" %-10d", ipcp->uid);
112 gr = getgrgid(ipcp->gid);
113 if (gr) printf(" %-10s\n", gr->gr_name);
114 else printf(" %-10d\n", ipcp->gid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000115}
116
117
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200118static NOINLINE void do_shm(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000119{
120 int maxid, shmid, id;
121 struct shmid_ds shmseg;
122 struct shm_info shm_info;
123 struct shminfo shminfo;
124 struct ipc_perm *ipcp = &shmseg.shm_perm;
125 struct passwd *pw;
126
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000127 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000128 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000129 printf("kernel not configured for %s\n", "shared memory");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000130 return;
131 }
132
133 switch (format) {
134 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000135 printf("------ Shared Memory %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000136 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000137 return;
138 /* glibc 2.1.3 and all earlier libc's have ints as fields
139 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000140 printf("max number of segments = %lu\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000141 "max seg size (kbytes) = %lu\n"
142 "max total shared memory (pages) = %lu\n"
143 "min seg size (bytes) = %lu\n",
144 (unsigned long) shminfo.shmmni,
145 (unsigned long) (shminfo.shmmax >> 10),
146 (unsigned long) shminfo.shmall,
147 (unsigned long) shminfo.shmmin);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000148 return;
149
150 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000151 printf("------ Shared Memory %s --------\n", "Status");
152 printf( "segments allocated %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000153 "pages allocated %ld\n"
154 "pages resident %ld\n"
155 "pages swapped %ld\n"
Denis Vlasenko59839142006-09-10 18:26:51 +0000156 "Swap performance: %ld attempts\t%ld successes\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000157 shm_info.used_ids,
158 shm_info.shm_tot,
159 shm_info.shm_rss,
160 shm_info.shm_swp,
161 shm_info.swap_attempts, shm_info.swap_successes);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000162 return;
163
164 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000165 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
166 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000167 "shmid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000168 break;
169
170 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000171 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
172 printf( "%-10s %-10s %-20s %-20s %-20s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000173 "shmid", "owner", "attached", "detached", "changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000174 break;
175
176 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000177 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
178 printf( "%-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000179 "shmid", "owner", "cpid", "lpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000180 break;
181
182 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000183 printf("------ Shared Memory %s --------\n", "Segments");
184 printf( "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000185 "key", "shmid", "owner", "perms", "bytes", "nattch",
186 "status");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000187 break;
188 }
189
190 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000191 shmid = shmctl(id, SHM_STAT, &shmseg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000192 if (shmid < 0)
193 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000194 if (format == CREATOR) {
195 print_perms(shmid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000196 continue;
197 }
198 pw = getpwuid(ipcp->uid);
199 switch (format) {
200 case TIME:
201 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000202 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000203 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000204 printf("%-10d %-10d", shmid, ipcp->uid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000205 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000206 printf(" %-20.16s", shmseg.shm_atime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000207 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000208 printf(" %-20.16s", shmseg.shm_dtime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000209 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000210 printf(" %-20.16s\n", shmseg.shm_ctime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000211 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000212 break;
213 case PID:
214 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000215 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000216 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000217 printf("%-10d %-10d", shmid, ipcp->uid);
218 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000219 break;
220
221 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000222 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000223 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000224 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000225 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000226 printf("%-10d %-10d", shmid, ipcp->uid);
227 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000228 /*
229 * earlier: int, Austin has size_t
230 */
231 (unsigned long) shmseg.shm_segsz,
232 /*
233 * glibc-2.1.3 and earlier has unsigned short;
234 * Austin has shmatt_t
235 */
236 (long) shmseg.shm_nattch,
237 ipcp->mode & SHM_DEST ? "dest" : " ",
238 ipcp->mode & SHM_LOCKED ? "locked" : " ");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000239 break;
240 }
241 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000242}
243
244
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200245static NOINLINE void do_sem(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000246{
247 int maxid, semid, id;
248 struct semid_ds semary;
249 struct seminfo seminfo;
250 struct ipc_perm *ipcp = &semary.sem_perm;
251 struct passwd *pw;
252 union semun arg;
253
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000254 arg.array = (ushort *) (void *) &seminfo;
255 maxid = semctl(0, 0, SEM_INFO, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000256 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000257 printf("kernel not configured for %s\n", "semaphores");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000258 return;
259 }
260
261 switch (format) {
262 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000263 printf("------ Semaphore %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000264 arg.array = (ushort *) (void *) &seminfo; /* damn union */
265 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000266 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000267 printf("max number of arrays = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000268 "max semaphores per array = %d\n"
269 "max semaphores system wide = %d\n"
270 "max ops per semop call = %d\n"
271 "semaphore max value = %d\n",
272 seminfo.semmni,
273 seminfo.semmsl,
274 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000275 return;
276
277 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000278 printf("------ Semaphore %s --------\n", "Status");
279 printf( "used arrays = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000280 "allocated semaphores = %d\n",
281 seminfo.semusz, seminfo.semaem);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000282 return;
283
284 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000285 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
286 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000287 "semid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000288 break;
289
290 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000291 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
292 printf( "%-8s %-10s %-26.24s %-26.24s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000293 "shmid", "owner", "last-op", "last-changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000294 break;
295
296 case PID:
297 break;
298
299 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000300 printf("------ Semaphore %s --------\n", "Arrays");
301 printf( "%-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000302 "key", "semid", "owner", "perms", "nsems");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000303 break;
304 }
305
306 for (id = 0; id <= maxid; id++) {
307 arg.buf = (struct semid_ds *) &semary;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000308 semid = semctl(id, 0, SEM_STAT, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000309 if (semid < 0)
310 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000311 if (format == CREATOR) {
312 print_perms(semid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000313 continue;
314 }
315 pw = getpwuid(ipcp->uid);
316 switch (format) {
317 case TIME:
318 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000319 printf("%-8d %-10.10s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000320 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000321 printf("%-8d %-10d", semid, ipcp->uid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000322 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000323 printf(" %-26.24s", semary.sem_otime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000324 ? ctime(&semary.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000325 printf(" %-26.24s\n", semary.sem_ctime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000326 ? ctime(&semary.sem_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000327 break;
328 case PID:
329 break;
330
331 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000332 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000333 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000334 printf("%-10d %-10.9s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000335 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000336 printf("%-10d %-9d", semid, ipcp->uid);
337 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000338 /*
339 * glibc-2.1.3 and earlier has unsigned short;
340 * glibc-2.1.91 has variation between
341 * unsigned short and unsigned long
342 * Austin prescribes unsigned short.
343 */
344 (long) semary.sem_nsems);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000345 break;
346 }
347 }
348}
349
350
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200351static NOINLINE void do_msg(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000352{
353 int maxid, msqid, id;
354 struct msqid_ds msgque;
355 struct msginfo msginfo;
356 struct ipc_perm *ipcp = &msgque.msg_perm;
357 struct passwd *pw;
358
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000359 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000360 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000361 printf("kernel not configured for %s\n", "message queues");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000362 return;
363 }
364
365 switch (format) {
366 case LIMITS:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000367 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000368 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000369 printf("------ Message%s --------\n", "s: Limits");
370 printf( "max queues system wide = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000371 "max size of message (bytes) = %d\n"
372 "default max size of queue (bytes) = %d\n",
373 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000374 return;
375
376 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000377 printf("------ Message%s --------\n", "s: Status");
378 printf( "allocated queues = %d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000379 "used headers = %d\n"
380 "used space = %d bytes\n",
381 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000382 return;
383
384 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000385 printf("------ Message%s --------\n", " Queues: Creators/Owners");
386 printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000387 "msqid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000388 break;
389
390 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000391 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
392 printf( "%-8s %-10s %-20s %-20s %-20s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000393 "msqid", "owner", "send", "recv", "change");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000394 break;
395
396 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000397 printf("------ Message%s --------\n", " Queues PIDs");
398 printf( "%-10s %-10s %-10s %-10s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000399 "msqid", "owner", "lspid", "lrpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000400 break;
401
402 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000403 printf("------ Message%s --------\n", " Queues");
404 printf( "%-10s %-10s %-10s %-10s %-12s %-12s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000405 "key", "msqid", "owner", "perms", "used-bytes", "messages");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000406 break;
407 }
408
409 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000410 msqid = msgctl(id, MSG_STAT, &msgque);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000411 if (msqid < 0)
412 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000413 if (format == CREATOR) {
414 print_perms(msqid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000415 continue;
416 }
417 pw = getpwuid(ipcp->uid);
418 switch (format) {
419 case TIME:
420 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000421 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000422 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000423 printf("%-8d %-10d", msqid, ipcp->uid);
424 printf(" %-20.16s", msgque.msg_stime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000425 ? ctime(&msgque.msg_stime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000426 printf(" %-20.16s", msgque.msg_rtime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000427 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000428 printf(" %-20.16s\n", msgque.msg_ctime
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000429 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000430 break;
431 case PID:
432 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000433 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000434 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000435 printf("%-8d %-10d", msqid, ipcp->uid);
436 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000437 break;
438
439 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000440 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000441 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000442 printf("%-10d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000443 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000444 printf("%-10d %-10d", msqid, ipcp->uid);
445 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000446 /*
447 * glibc-2.1.3 and earlier has unsigned short;
448 * glibc-2.1.91 has variation between
449 * unsigned short, unsigned long
450 * Austin has msgqnum_t
451 */
452 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000453 break;
454 }
455 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000456}
457
458
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000459static void print_shm(int shmid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000460{
461 struct shmid_ds shmds;
462 struct ipc_perm *ipcp = &shmds.shm_perm;
463
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000464 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
465 bb_perror_msg("shmctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000466 return;
467 }
468
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000469 printf("\nShared memory Segment shmid=%d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000470 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
471 "mode=%#o\taccess_perms=%#o\n"
472 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
473 shmid,
474 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
475 ipcp->mode, ipcp->mode & 0777,
476 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
477 (long) shmds.shm_nattch);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000478 printf("att_time=%-26.24s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000479 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000480 printf("det_time=%-26.24s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000481 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000482 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000483}
484
485
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000486static void print_msg(int msqid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000487{
488 struct msqid_ds buf;
489 struct ipc_perm *ipcp = &buf.msg_perm;
490
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000491 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
492 bb_perror_msg("msgctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000493 return;
494 }
495
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000496 printf("\nMessage Queue msqid=%d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000497 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
498 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
499 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
500 /*
501 * glibc-2.1.3 and earlier has unsigned short;
502 * glibc-2.1.91 has variation between
503 * unsigned short, unsigned long
504 * Austin has msgqnum_t (for msg_qbytes)
505 */
506 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
507 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
508
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000509 printf("send_time=%-26.24s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000510 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000511 printf("rcv_time=%-26.24s\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000512 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000513 printf("change_time=%-26.24s\n\n",
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000514 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000515}
516
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000517static void print_sem(int semid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000518{
519 struct semid_ds semds;
520 struct ipc_perm *ipcp = &semds.sem_perm;
521 union semun arg;
522 unsigned int i;
523
524 arg.buf = &semds;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000525 if (semctl(semid, 0, IPC_STAT, arg)) {
526 bb_perror_msg("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000527 return;
528 }
529
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000530 printf("\nSemaphore Array semid=%d\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000531 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
532 "mode=%#o, access_perms=%#o\n"
533 "nsems = %ld\n"
534 "otime = %-26.24s\n",
535 semid,
536 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
537 ipcp->mode, ipcp->mode & 0777,
538 (long) semds.sem_nsems,
539 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000540 printf("ctime = %-26.24s\n"
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000541 "%-10s %-10s %-10s %-10s %-10s\n",
542 ctime(&semds.sem_ctime),
543 "semnum", "value", "ncount", "zcount", "pid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000544
545 arg.val = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000546 for (i = 0; i < semds.sem_nsems; i++) {
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000547 int val, ncnt, zcnt, pid;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000548
549 val = semctl(semid, i, GETVAL, arg);
550 ncnt = semctl(semid, i, GETNCNT, arg);
551 zcnt = semctl(semid, i, GETZCNT, arg);
552 pid = semctl(semid, i, GETPID, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000553 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000554 bb_perror_msg_and_die("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000555 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000556 printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000557 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000558 bb_putchar('\n');
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000559}
Rob Landley6eb1e412005-06-20 04:30:36 +0000560
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000561int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000562int ipcs_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000563{
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000564 int id = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000565 unsigned flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000566 unsigned opt;
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000567 char *opt_i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000568#define flag_print (1<<0)
569#define flag_msg (1<<1)
570#define flag_sem (1<<2)
571#define flag_shm (1<<3)
Rob Landley6eb1e412005-06-20 04:30:36 +0000572
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000573 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000574 if (opt & 0x1) { // -i
Denis Vlasenko13858992006-10-08 12:49:22 +0000575 id = xatoi(opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000576 flags |= flag_print;
Rob Landley6eb1e412005-06-20 04:30:36 +0000577 }
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000578 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
579 if (opt & 0x4) flags |= flag_msg; // -q
580 if (opt & 0x8) flags |= flag_sem; // -s
581 if (opt & 0x10) flags |= flag_shm; // -m
582 if (opt & 0x20) format = TIME; // -t
583 if (opt & 0x40) format = CREATOR; // -c
584 if (opt & 0x80) format = PID; // -p
585 if (opt & 0x100) format = LIMITS; // -l
586 if (opt & 0x200) format = STATUS; // -u
Rob Landley6eb1e412005-06-20 04:30:36 +0000587
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000588 if (flags & flag_print) {
589 if (flags & flag_shm) {
590 print_shm(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000591 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000592 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000593 if (flags & flag_sem) {
594 print_sem(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000595 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000596 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000597 if (flags & flag_msg) {
598 print_msg(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000599 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000600 }
601 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000602 }
603
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000604 if (!(flags & (flag_shm | flag_msg | flag_sem)))
605 flags |= flag_msg | flag_shm | flag_sem;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000606 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000607
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000608 if (flags & flag_shm) {
609 do_shm();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000610 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000611 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000612 if (flags & flag_sem) {
613 do_sem();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000614 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000615 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000616 if (flags & flag_msg) {
617 do_msg();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000618 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000619 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000620 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000621}