blob: 88ae9225d708a9bbdd418871872b478d4c705ad5 [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010010//config:config IPCS
11//config: bool "ipcs"
12//config: default y
13//config: select PLATFORM_LINUX
14//config: help
15//config: The ipcs utility is used to provide information on the currently
16//config: allocated System V interprocess (IPC) objects in the system.
17
18//applet:IF_IPCS(APPLET(ipcs, BB_DIR_USR_BIN, BB_SUID_DROP))
19
20//kbuild:lib-$(CONFIG_IPCS) += ipcs.o
Rob Landley6eb1e412005-06-20 04:30:36 +000021
Pere Orga5bc8c002011-04-11 03:29:49 +020022//usage:#define ipcs_trivial_usage
23//usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]"
24//usage:#define ipcs_full_usage "\n\n"
25//usage: " -i Show specific resource"
26//usage: "\nResource specification:"
27//usage: "\n -m Shared memory segments"
28//usage: "\n -q Message queues"
29//usage: "\n -s Semaphore arrays"
30//usage: "\n -a All (default)"
31//usage: "\nOutput format:"
32//usage: "\n -t Time"
33//usage: "\n -c Creator"
34//usage: "\n -p Pid"
35//usage: "\n -l Limits"
36//usage: "\n -u Summary"
37
Rob Landley6eb1e412005-06-20 04:30:36 +000038/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
39/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
40/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
41#include <sys/types.h>
42#include <sys/ipc.h>
43#include <sys/sem.h>
44#include <sys/msg.h>
45#include <sys/shm.h>
46
Denis Vlasenkofad2b862007-05-31 22:16:38 +000047#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000048
49/*-------------------------------------------------------------------*/
50/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
51 but inside #ifdef __KERNEL__ ... #endif */
52#ifndef SHM_DEST
53/* shm_mode upper byte flags */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000054#define SHM_DEST 01000 /* segment will be destroyed on last detach */
55#define SHM_LOCKED 02000 /* segment will not be swapped */
Rob Landley6eb1e412005-06-20 04:30:36 +000056#endif
57
58/* For older kernels the same holds for the defines below */
59#ifndef MSG_STAT
60#define MSG_STAT 11
61#define MSG_INFO 12
62#endif
63
64#ifndef SHM_STAT
65#define SHM_STAT 13
66#define SHM_INFO 14
67struct shm_info {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000068 int used_ids;
Dan Fandrichf3854f72011-06-03 20:25:17 +020069 unsigned long shm_tot; /* total allocated shm */
70 unsigned long shm_rss; /* total resident shm */
71 unsigned long shm_swp; /* total swapped shm */
72 unsigned long swap_attempts;
73 unsigned long swap_successes;
Rob Landley6eb1e412005-06-20 04:30:36 +000074};
75#endif
76
77#ifndef SEM_STAT
78#define SEM_STAT 18
79#define SEM_INFO 19
80#endif
81
82/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
83#ifndef IPC_INFO
84#define IPC_INFO 3
85#endif
86/*-------------------------------------------------------------------*/
87
88/* The last arg of semctl is a union semun, but where is it defined?
89 X/OPEN tells us to define it ourselves, but until recently
90 Linux include files would also define it. */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000091#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000092/* union semun is defined by including <sys/sem.h> */
93#else
94/* according to X/OPEN we have to define it ourselves */
95union semun {
96 int val;
97 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000098 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000099 struct seminfo *__buf;
100};
101#endif
102
103/* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
104 libc 4/5 does not mention struct ipc_term at all, but includes
105 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
106 glibc-1.09 has no support for sysv ipc.
107 glibc 2 uses __key, __seq */
Denis Vlasenkoff131b92007-04-10 15:42:06 +0000108#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
Rob Landley6eb1e412005-06-20 04:30:36 +0000109#define KEY __key
110#else
111#define KEY key
112#endif
113
114#define LIMITS 1
115#define STATUS 2
116#define CREATOR 3
117#define TIME 4
118#define PID 5
119
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000120static char format;
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000121
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000122static void print_perms(int id, struct ipc_perm *ipcp)
123{
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000124 struct passwd *pw;
125 struct group *gr;
126
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000127 printf("%-10d %-10o", id, ipcp->mode & 0777);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000128
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000129 pw = getpwuid(ipcp->cuid);
130 if (pw) printf(" %-10s", pw->pw_name);
131 else printf(" %-10d", ipcp->cuid);
132 gr = getgrgid(ipcp->cgid);
133 if (gr) printf(" %-10s", gr->gr_name);
134 else printf(" %-10d", ipcp->cgid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000135
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000136 pw = getpwuid(ipcp->uid);
137 if (pw) printf(" %-10s", pw->pw_name);
138 else printf(" %-10d", ipcp->uid);
139 gr = getgrgid(ipcp->gid);
140 if (gr) printf(" %-10s\n", gr->gr_name);
141 else printf(" %-10d\n", ipcp->gid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000142}
143
144
Denys Vlasenkoa7bb3c12009-10-08 12:28:08 +0200145static NOINLINE void do_shm(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000146{
147 int maxid, shmid, id;
148 struct shmid_ds shmseg;
149 struct shm_info shm_info;
150 struct shminfo shminfo;
151 struct ipc_perm *ipcp = &shmseg.shm_perm;
152 struct passwd *pw;
153
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000154 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000155 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000156 printf("kernel not configured for %s\n", "shared memory");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000157 return;
158 }
159
160 switch (format) {
161 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000162 printf("------ Shared Memory %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000163 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000164 return;
165 /* glibc 2.1.3 and all earlier libc's have ints as fields
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100166 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000167 printf("max number of segments = %lu\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100168 "max seg size (kbytes) = %lu\n"
169 "max total shared memory (pages) = %lu\n"
170 "min seg size (bytes) = %lu\n",
171 (unsigned long) shminfo.shmmni,
172 (unsigned long) (shminfo.shmmax >> 10),
173 (unsigned long) shminfo.shmall,
174 (unsigned long) shminfo.shmmin);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000175 return;
176
177 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000178 printf("------ Shared Memory %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100179 printf("segments allocated %d\n"
Denys Vlasenko327f5502013-11-29 16:45:45 +0100180 "pages allocated %lu\n"
181 "pages resident %lu\n"
182 "pages swapped %lu\n"
183 "Swap performance: %lu attempts\t%lu successes\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100184 shm_info.used_ids,
185 shm_info.shm_tot,
186 shm_info.shm_rss,
187 shm_info.shm_swp,
188 shm_info.swap_attempts, shm_info.swap_successes);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000189 return;
190
191 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000192 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100193 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
194 "shmid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000195 break;
196
197 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000198 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100199 printf("%-10s %-10s %-20s %-20s %-20s\n",
200 "shmid", "owner", "attached", "detached", "changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000201 break;
202
203 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000204 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
Denys Vlasenko69675782013-01-14 01:34:48 +0100205 printf("%-10s %-10s %-10s %-10s\n",
206 "shmid", "owner", "cpid", "lpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000207 break;
208
209 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000210 printf("------ Shared Memory %s --------\n", "Segments");
Denys Vlasenko69675782013-01-14 01:34:48 +0100211 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
212 "key", "shmid", "owner", "perms", "bytes", "nattch",
213 "status");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000214 break;
215 }
216
217 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000218 shmid = shmctl(id, SHM_STAT, &shmseg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000219 if (shmid < 0)
220 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000221 if (format == CREATOR) {
222 print_perms(shmid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000223 continue;
224 }
225 pw = getpwuid(ipcp->uid);
226 switch (format) {
227 case TIME:
228 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000229 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000230 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000231 printf("%-10d %-10d", shmid, ipcp->uid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000232 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000233 printf(" %-20.16s", shmseg.shm_atime
Denys Vlasenko69675782013-01-14 01:34:48 +0100234 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000235 printf(" %-20.16s", shmseg.shm_dtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100236 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000237 printf(" %-20.16s\n", shmseg.shm_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100238 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000239 break;
240 case PID:
241 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000242 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000243 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000244 printf("%-10d %-10d", shmid, ipcp->uid);
245 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000246 break;
247
248 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000249 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000250 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000251 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000252 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000253 printf("%-10d %-10d", shmid, ipcp->uid);
254 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100255 /*
256 * earlier: int, Austin has size_t
257 */
258 (unsigned long) shmseg.shm_segsz,
259 /*
260 * glibc-2.1.3 and earlier has unsigned short;
261 * Austin has shmatt_t
262 */
263 (long) shmseg.shm_nattch,
264 ipcp->mode & SHM_DEST ? "dest" : " ",
265 ipcp->mode & SHM_LOCKED ? "locked" : " ");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000266 break;
267 }
268 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000269}
270
271
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200272static NOINLINE void do_sem(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000273{
274 int maxid, semid, id;
275 struct semid_ds semary;
276 struct seminfo seminfo;
277 struct ipc_perm *ipcp = &semary.sem_perm;
278 struct passwd *pw;
279 union semun arg;
280
Dan Fandrichf3854f72011-06-03 20:25:17 +0200281 arg.array = (unsigned short *) (void *) &seminfo;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000282 maxid = semctl(0, 0, SEM_INFO, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000283 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000284 printf("kernel not configured for %s\n", "semaphores");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000285 return;
286 }
287
288 switch (format) {
289 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000290 printf("------ Semaphore %s --------\n", "Limits");
Dan Fandrichf3854f72011-06-03 20:25:17 +0200291 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000292 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000293 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000294 printf("max number of arrays = %d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100295 "max semaphores per array = %d\n"
296 "max semaphores system wide = %d\n"
297 "max ops per semop call = %d\n"
298 "semaphore max value = %d\n",
299 seminfo.semmni,
300 seminfo.semmsl,
301 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000302 return;
303
304 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000305 printf("------ Semaphore %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100306 printf("used arrays = %d\n"
307 "allocated semaphores = %d\n",
308 seminfo.semusz, seminfo.semaem);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000309 return;
310
311 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000312 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100313 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
314 "semid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000315 break;
316
317 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000318 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100319 printf("%-8s %-10s %-26.24s %-26.24s\n",
320 "shmid", "owner", "last-op", "last-changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000321 break;
322
323 case PID:
324 break;
325
326 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000327 printf("------ Semaphore %s --------\n", "Arrays");
Denys Vlasenko69675782013-01-14 01:34:48 +0100328 printf("%-10s %-10s %-10s %-10s %-10s\n",
329 "key", "semid", "owner", "perms", "nsems");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000330 break;
331 }
332
333 for (id = 0; id <= maxid; id++) {
334 arg.buf = (struct semid_ds *) &semary;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000335 semid = semctl(id, 0, SEM_STAT, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000336 if (semid < 0)
337 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000338 if (format == CREATOR) {
339 print_perms(semid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000340 continue;
341 }
342 pw = getpwuid(ipcp->uid);
343 switch (format) {
344 case TIME:
345 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000346 printf("%-8d %-10.10s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000347 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000348 printf("%-8d %-10d", semid, ipcp->uid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000349 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000350 printf(" %-26.24s", semary.sem_otime
Denys Vlasenko69675782013-01-14 01:34:48 +0100351 ? ctime(&semary.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000352 printf(" %-26.24s\n", semary.sem_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100353 ? ctime(&semary.sem_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000354 break;
355 case PID:
356 break;
357
358 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000359 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000360 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000361 printf("%-10d %-10.9s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000362 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000363 printf("%-10d %-9d", semid, ipcp->uid);
364 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100365 /*
366 * glibc-2.1.3 and earlier has unsigned short;
367 * glibc-2.1.91 has variation between
368 * unsigned short and unsigned long
369 * Austin prescribes unsigned short.
370 */
371 (long) semary.sem_nsems);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000372 break;
373 }
374 }
375}
376
377
Denys Vlasenkoadf922e2009-10-08 14:35:37 +0200378static NOINLINE void do_msg(void)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000379{
380 int maxid, msqid, id;
381 struct msqid_ds msgque;
382 struct msginfo msginfo;
383 struct ipc_perm *ipcp = &msgque.msg_perm;
384 struct passwd *pw;
385
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000386 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000387 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000388 printf("kernel not configured for %s\n", "message queues");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000389 return;
390 }
391
392 switch (format) {
393 case LIMITS:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000394 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000395 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000396 printf("------ Message%s --------\n", "s: Limits");
Denys Vlasenko69675782013-01-14 01:34:48 +0100397 printf("max queues system wide = %d\n"
398 "max size of message (bytes) = %d\n"
399 "default max size of queue (bytes) = %d\n",
400 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000401 return;
402
403 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000404 printf("------ Message%s --------\n", "s: Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100405 printf("allocated queues = %d\n"
406 "used headers = %d\n"
407 "used space = %d bytes\n",
408 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000409 return;
410
411 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000412 printf("------ Message%s --------\n", " Queues: Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100413 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
414 "msqid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000415 break;
416
417 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000418 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100419 printf("%-8s %-10s %-20s %-20s %-20s\n",
420 "msqid", "owner", "send", "recv", "change");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000421 break;
422
423 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000424 printf("------ Message%s --------\n", " Queues PIDs");
Denys Vlasenko69675782013-01-14 01:34:48 +0100425 printf("%-10s %-10s %-10s %-10s\n",
426 "msqid", "owner", "lspid", "lrpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000427 break;
428
429 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000430 printf("------ Message%s --------\n", " Queues");
Denys Vlasenko69675782013-01-14 01:34:48 +0100431 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
432 "key", "msqid", "owner", "perms", "used-bytes", "messages");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000433 break;
434 }
435
436 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000437 msqid = msgctl(id, MSG_STAT, &msgque);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000438 if (msqid < 0)
439 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000440 if (format == CREATOR) {
441 print_perms(msqid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000442 continue;
443 }
444 pw = getpwuid(ipcp->uid);
445 switch (format) {
446 case TIME:
447 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000448 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000449 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000450 printf("%-8d %-10d", msqid, ipcp->uid);
451 printf(" %-20.16s", msgque.msg_stime
Denys Vlasenko69675782013-01-14 01:34:48 +0100452 ? ctime(&msgque.msg_stime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000453 printf(" %-20.16s", msgque.msg_rtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100454 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000455 printf(" %-20.16s\n", msgque.msg_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100456 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000457 break;
458 case PID:
459 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000460 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000461 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000462 printf("%-8d %-10d", msqid, ipcp->uid);
463 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000464 break;
465
466 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000467 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000468 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000469 printf("%-10d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000470 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000471 printf("%-10d %-10d", msqid, ipcp->uid);
472 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100473 /*
474 * glibc-2.1.3 and earlier has unsigned short;
475 * glibc-2.1.91 has variation between
476 * unsigned short, unsigned long
477 * Austin has msgqnum_t
478 */
479 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000480 break;
481 }
482 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000483}
484
485
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000486static void print_shm(int shmid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000487{
488 struct shmid_ds shmds;
489 struct ipc_perm *ipcp = &shmds.shm_perm;
490
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000491 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
492 bb_perror_msg("shmctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000493 return;
494 }
495
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000496 printf("\nShared memory Segment shmid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100497 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
498 "mode=%#o\taccess_perms=%#o\n"
499 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
500 shmid,
501 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
502 ipcp->mode, ipcp->mode & 0777,
503 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
504 (long) shmds.shm_nattch);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000505 printf("att_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100506 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000507 printf("det_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100508 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000509 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000510}
511
512
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000513static void print_msg(int msqid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000514{
515 struct msqid_ds buf;
516 struct ipc_perm *ipcp = &buf.msg_perm;
517
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000518 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
519 bb_perror_msg("msgctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000520 return;
521 }
522
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000523 printf("\nMessage Queue msqid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100524 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
525 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
526 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
527 /*
528 * glibc-2.1.3 and earlier has unsigned short;
529 * glibc-2.1.91 has variation between
530 * unsigned short, unsigned long
531 * Austin has msgqnum_t (for msg_qbytes)
532 */
533 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
534 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000535
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000536 printf("send_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100537 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000538 printf("rcv_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100539 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000540 printf("change_time=%-26.24s\n\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100541 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000542}
543
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000544static void print_sem(int semid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000545{
546 struct semid_ds semds;
547 struct ipc_perm *ipcp = &semds.sem_perm;
548 union semun arg;
549 unsigned int i;
550
551 arg.buf = &semds;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000552 if (semctl(semid, 0, IPC_STAT, arg)) {
553 bb_perror_msg("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000554 return;
555 }
556
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000557 printf("\nSemaphore Array semid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100558 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
559 "mode=%#o, access_perms=%#o\n"
560 "nsems = %ld\n"
561 "otime = %-26.24s\n",
562 semid,
563 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
564 ipcp->mode, ipcp->mode & 0777,
565 (long) semds.sem_nsems,
566 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000567 printf("ctime = %-26.24s\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100568 "%-10s %-10s %-10s %-10s %-10s\n",
569 ctime(&semds.sem_ctime),
570 "semnum", "value", "ncount", "zcount", "pid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000571
572 arg.val = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000573 for (i = 0; i < semds.sem_nsems; i++) {
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000574 int val, ncnt, zcnt, pid;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000575
576 val = semctl(semid, i, GETVAL, arg);
577 ncnt = semctl(semid, i, GETNCNT, arg);
578 zcnt = semctl(semid, i, GETZCNT, arg);
579 pid = semctl(semid, i, GETPID, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000580 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000581 bb_perror_msg_and_die("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000582 }
Denys Vlasenko327f5502013-11-29 16:45:45 +0100583 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000584 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000585 bb_putchar('\n');
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000586}
Rob Landley6eb1e412005-06-20 04:30:36 +0000587
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000588int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000589int ipcs_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000590{
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000591 int id = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000592 unsigned flags = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000593 unsigned opt;
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000594 char *opt_i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000595#define flag_print (1<<0)
596#define flag_msg (1<<1)
597#define flag_sem (1<<2)
598#define flag_shm (1<<3)
Rob Landley6eb1e412005-06-20 04:30:36 +0000599
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000600 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000601 if (opt & 0x1) { // -i
Denis Vlasenko13858992006-10-08 12:49:22 +0000602 id = xatoi(opt_i);
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000603 flags |= flag_print;
Rob Landley6eb1e412005-06-20 04:30:36 +0000604 }
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000605 if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a
606 if (opt & 0x4) flags |= flag_msg; // -q
607 if (opt & 0x8) flags |= flag_sem; // -s
608 if (opt & 0x10) flags |= flag_shm; // -m
609 if (opt & 0x20) format = TIME; // -t
610 if (opt & 0x40) format = CREATOR; // -c
611 if (opt & 0x80) format = PID; // -p
612 if (opt & 0x100) format = LIMITS; // -l
613 if (opt & 0x200) format = STATUS; // -u
Rob Landley6eb1e412005-06-20 04:30:36 +0000614
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000615 if (flags & flag_print) {
616 if (flags & flag_shm) {
617 print_shm(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000618 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000619 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000620 if (flags & flag_sem) {
621 print_sem(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000622 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000623 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000624 if (flags & flag_msg) {
625 print_msg(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000626 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000627 }
628 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000629 }
630
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000631 if (!(flags & (flag_shm | flag_msg | flag_sem)))
632 flags |= flag_msg | flag_shm | flag_sem;
Denis Vlasenko4daad902007-09-27 10:20:47 +0000633 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000634
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000635 if (flags & flag_shm) {
636 do_shm();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000637 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000638 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000639 if (flags & flag_sem) {
640 do_sem();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000641 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000642 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000643 if (flags & flag_msg) {
644 do_msg();
Denis Vlasenko4daad902007-09-27 10:20:47 +0000645 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000646 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000647 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000648}