blob: 4863a5c29ce6e7fecec1ebb8b55d5ec97955e8a6 [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
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "ipcs (11 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: default y
13//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: The ipcs utility is used to provide information on the currently
16//config: allocated System V interprocess (IPC) objects in the system.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010017
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +020018//applet:IF_IPCS(APPLET_NOEXEC(ipcs, ipcs, BB_DIR_USR_BIN, BB_SUID_DROP, ipcs))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010019
20//kbuild:lib-$(CONFIG_IPCS) += ipcs.o
Rob Landley6eb1e412005-06-20 04:30:36 +000021
Rob Landley6eb1e412005-06-20 04:30:36 +000022/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
23/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
24/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
25#include <sys/types.h>
26#include <sys/ipc.h>
27#include <sys/sem.h>
28#include <sys/msg.h>
29#include <sys/shm.h>
30
Denis Vlasenkofad2b862007-05-31 22:16:38 +000031#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000032
33/*-------------------------------------------------------------------*/
34/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
35 but inside #ifdef __KERNEL__ ... #endif */
36#ifndef SHM_DEST
37/* shm_mode upper byte flags */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000038#define SHM_DEST 01000 /* segment will be destroyed on last detach */
39#define SHM_LOCKED 02000 /* segment will not be swapped */
Rob Landley6eb1e412005-06-20 04:30:36 +000040#endif
41
42/* For older kernels the same holds for the defines below */
43#ifndef MSG_STAT
44#define MSG_STAT 11
45#define MSG_INFO 12
46#endif
47
48#ifndef SHM_STAT
49#define SHM_STAT 13
50#define SHM_INFO 14
51struct shm_info {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000052 int used_ids;
Dan Fandrichf3854f72011-06-03 20:25:17 +020053 unsigned long shm_tot; /* total allocated shm */
54 unsigned long shm_rss; /* total resident shm */
55 unsigned long shm_swp; /* total swapped shm */
56 unsigned long swap_attempts;
57 unsigned long swap_successes;
Rob Landley6eb1e412005-06-20 04:30:36 +000058};
59#endif
60
61#ifndef SEM_STAT
62#define SEM_STAT 18
63#define SEM_INFO 19
64#endif
65
66/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
67#ifndef IPC_INFO
68#define IPC_INFO 3
69#endif
70/*-------------------------------------------------------------------*/
71
72/* The last arg of semctl is a union semun, but where is it defined?
73 X/OPEN tells us to define it ourselves, but until recently
74 Linux include files would also define it. */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000075#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000076/* union semun is defined by including <sys/sem.h> */
77#else
78/* according to X/OPEN we have to define it ourselves */
79union semun {
80 int val;
81 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000082 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000083 struct seminfo *__buf;
84};
85#endif
86
87/* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
88 libc 4/5 does not mention struct ipc_term at all, but includes
89 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
90 glibc-1.09 has no support for sysv ipc.
91 glibc 2 uses __key, __seq */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000092#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
Rob Landley6eb1e412005-06-20 04:30:36 +000093#define KEY __key
94#else
95#define KEY key
96#endif
97
98#define LIMITS 1
99#define STATUS 2
100#define CREATOR 3
101#define TIME 4
102#define PID 5
103
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000104static void print_perms(int id, struct ipc_perm *ipcp)
105{
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000106 struct passwd *pw;
107 struct group *gr;
108
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000109 printf("%-10d %-10o", id, ipcp->mode & 0777);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000110
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000111 pw = getpwuid(ipcp->cuid);
112 if (pw) printf(" %-10s", pw->pw_name);
113 else printf(" %-10d", ipcp->cuid);
114 gr = getgrgid(ipcp->cgid);
115 if (gr) printf(" %-10s", gr->gr_name);
116 else printf(" %-10d", ipcp->cgid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000117
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000118 pw = getpwuid(ipcp->uid);
119 if (pw) printf(" %-10s", pw->pw_name);
120 else printf(" %-10d", ipcp->uid);
121 gr = getgrgid(ipcp->gid);
122 if (gr) printf(" %-10s\n", gr->gr_name);
123 else printf(" %-10d\n", ipcp->gid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000124}
125
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100126static NOINLINE void do_shm(int format)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000127{
128 int maxid, shmid, id;
129 struct shmid_ds shmseg;
130 struct shm_info shm_info;
131 struct shminfo shminfo;
132 struct ipc_perm *ipcp = &shmseg.shm_perm;
133 struct passwd *pw;
134
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000135 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000136 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000137 printf("kernel not configured for %s\n", "shared memory");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000138 return;
139 }
140
141 switch (format) {
142 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000143 printf("------ Shared Memory %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000144 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000145 return;
146 /* glibc 2.1.3 and all earlier libc's have ints as fields
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100147 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000148 printf("max number of segments = %lu\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100149 "max seg size (kbytes) = %lu\n"
150 "max total shared memory (pages) = %lu\n"
151 "min seg size (bytes) = %lu\n",
152 (unsigned long) shminfo.shmmni,
153 (unsigned long) (shminfo.shmmax >> 10),
154 (unsigned long) shminfo.shmall,
155 (unsigned long) shminfo.shmmin);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000156 return;
157
158 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000159 printf("------ Shared Memory %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100160 printf("segments allocated %d\n"
Denys Vlasenko327f5502013-11-29 16:45:45 +0100161 "pages allocated %lu\n"
162 "pages resident %lu\n"
163 "pages swapped %lu\n"
164 "Swap performance: %lu attempts\t%lu successes\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100165 shm_info.used_ids,
166 shm_info.shm_tot,
167 shm_info.shm_rss,
168 shm_info.shm_swp,
169 shm_info.swap_attempts, shm_info.swap_successes);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000170 return;
171
172 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000173 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100174 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
175 "shmid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000176 break;
177
178 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000179 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100180 printf("%-10s %-10s %-20s %-20s %-20s\n",
181 "shmid", "owner", "attached", "detached", "changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000182 break;
183
184 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000185 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
Denys Vlasenko69675782013-01-14 01:34:48 +0100186 printf("%-10s %-10s %-10s %-10s\n",
187 "shmid", "owner", "cpid", "lpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000188 break;
189
190 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000191 printf("------ Shared Memory %s --------\n", "Segments");
Denys Vlasenko69675782013-01-14 01:34:48 +0100192 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
193 "key", "shmid", "owner", "perms", "bytes", "nattch",
194 "status");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000195 break;
196 }
197
198 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000199 shmid = shmctl(id, SHM_STAT, &shmseg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000200 if (shmid < 0)
201 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000202 if (format == CREATOR) {
203 print_perms(shmid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000204 continue;
205 }
206 pw = getpwuid(ipcp->uid);
207 switch (format) {
208 case TIME:
209 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000210 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000211 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000212 printf("%-10d %-10d", shmid, ipcp->uid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000213 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000214 printf(" %-20.16s", shmseg.shm_atime
Denys Vlasenko69675782013-01-14 01:34:48 +0100215 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000216 printf(" %-20.16s", shmseg.shm_dtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100217 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000218 printf(" %-20.16s\n", shmseg.shm_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100219 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000220 break;
221 case PID:
222 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000223 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000224 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000225 printf("%-10d %-10d", shmid, ipcp->uid);
226 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000227 break;
228
229 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000230 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000231 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000232 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000233 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000234 printf("%-10d %-10d", shmid, ipcp->uid);
235 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100236 /*
237 * earlier: int, Austin has size_t
238 */
239 (unsigned long) shmseg.shm_segsz,
240 /*
241 * glibc-2.1.3 and earlier has unsigned short;
242 * Austin has shmatt_t
243 */
244 (long) shmseg.shm_nattch,
245 ipcp->mode & SHM_DEST ? "dest" : " ",
246 ipcp->mode & SHM_LOCKED ? "locked" : " ");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000247 break;
248 }
249 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000250}
251
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100252static NOINLINE void do_sem(int format)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000253{
254 int maxid, semid, id;
255 struct semid_ds semary;
256 struct seminfo seminfo;
257 struct ipc_perm *ipcp = &semary.sem_perm;
258 struct passwd *pw;
259 union semun arg;
260
Dan Fandrichf3854f72011-06-03 20:25:17 +0200261 arg.array = (unsigned short *) (void *) &seminfo;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000262 maxid = semctl(0, 0, SEM_INFO, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000263 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000264 printf("kernel not configured for %s\n", "semaphores");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000265 return;
266 }
267
268 switch (format) {
269 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000270 printf("------ Semaphore %s --------\n", "Limits");
Dan Fandrichf3854f72011-06-03 20:25:17 +0200271 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000272 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000273 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000274 printf("max number of arrays = %d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100275 "max semaphores per array = %d\n"
276 "max semaphores system wide = %d\n"
277 "max ops per semop call = %d\n"
278 "semaphore max value = %d\n",
279 seminfo.semmni,
280 seminfo.semmsl,
281 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000282 return;
283
284 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000285 printf("------ Semaphore %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100286 printf("used arrays = %d\n"
287 "allocated semaphores = %d\n",
288 seminfo.semusz, seminfo.semaem);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000289 return;
290
291 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000292 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100293 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
294 "semid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000295 break;
296
297 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000298 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100299 printf("%-8s %-10s %-26.24s %-26.24s\n",
300 "shmid", "owner", "last-op", "last-changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000301 break;
302
303 case PID:
304 break;
305
306 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000307 printf("------ Semaphore %s --------\n", "Arrays");
Denys Vlasenko69675782013-01-14 01:34:48 +0100308 printf("%-10s %-10s %-10s %-10s %-10s\n",
309 "key", "semid", "owner", "perms", "nsems");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000310 break;
311 }
312
313 for (id = 0; id <= maxid; id++) {
314 arg.buf = (struct semid_ds *) &semary;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000315 semid = semctl(id, 0, SEM_STAT, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000316 if (semid < 0)
317 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000318 if (format == CREATOR) {
319 print_perms(semid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000320 continue;
321 }
322 pw = getpwuid(ipcp->uid);
323 switch (format) {
324 case TIME:
325 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000326 printf("%-8d %-10.10s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000327 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000328 printf("%-8d %-10d", semid, ipcp->uid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000329 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000330 printf(" %-26.24s", semary.sem_otime
Denys Vlasenko69675782013-01-14 01:34:48 +0100331 ? ctime(&semary.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000332 printf(" %-26.24s\n", semary.sem_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100333 ? ctime(&semary.sem_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000334 break;
335 case PID:
336 break;
337
338 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000339 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000340 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000341 printf("%-10d %-10.9s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000342 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000343 printf("%-10d %-9d", semid, ipcp->uid);
344 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100345 /*
346 * glibc-2.1.3 and earlier has unsigned short;
347 * glibc-2.1.91 has variation between
348 * unsigned short and unsigned long
349 * Austin prescribes unsigned short.
350 */
351 (long) semary.sem_nsems);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000352 break;
353 }
354 }
355}
356
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100357static NOINLINE void do_msg(int format)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000358{
359 int maxid, msqid, id;
360 struct msqid_ds msgque;
361 struct msginfo msginfo;
362 struct ipc_perm *ipcp = &msgque.msg_perm;
363 struct passwd *pw;
364
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000365 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000366 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000367 printf("kernel not configured for %s\n", "message queues");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000368 return;
369 }
370
371 switch (format) {
372 case LIMITS:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000373 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000374 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000375 printf("------ Message%s --------\n", "s: Limits");
Denys Vlasenko69675782013-01-14 01:34:48 +0100376 printf("max queues system wide = %d\n"
377 "max size of message (bytes) = %d\n"
378 "default max size of queue (bytes) = %d\n",
379 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000380 return;
381
382 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000383 printf("------ Message%s --------\n", "s: Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100384 printf("allocated queues = %d\n"
385 "used headers = %d\n"
386 "used space = %d bytes\n",
387 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000388 return;
389
390 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000391 printf("------ Message%s --------\n", " Queues: Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100392 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
393 "msqid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000394 break;
395
396 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000397 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100398 printf("%-8s %-10s %-20s %-20s %-20s\n",
399 "msqid", "owner", "send", "recv", "change");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000400 break;
401
402 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000403 printf("------ Message%s --------\n", " Queues PIDs");
Denys Vlasenko69675782013-01-14 01:34:48 +0100404 printf("%-10s %-10s %-10s %-10s\n",
405 "msqid", "owner", "lspid", "lrpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000406 break;
407
408 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000409 printf("------ Message%s --------\n", " Queues");
Denys Vlasenko69675782013-01-14 01:34:48 +0100410 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
411 "key", "msqid", "owner", "perms", "used-bytes", "messages");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000412 break;
413 }
414
415 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000416 msqid = msgctl(id, MSG_STAT, &msgque);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000417 if (msqid < 0)
418 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000419 if (format == CREATOR) {
420 print_perms(msqid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000421 continue;
422 }
423 pw = getpwuid(ipcp->uid);
424 switch (format) {
425 case TIME:
426 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000427 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000428 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000429 printf("%-8d %-10d", msqid, ipcp->uid);
430 printf(" %-20.16s", msgque.msg_stime
Denys Vlasenko69675782013-01-14 01:34:48 +0100431 ? ctime(&msgque.msg_stime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000432 printf(" %-20.16s", msgque.msg_rtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100433 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000434 printf(" %-20.16s\n", msgque.msg_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100435 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000436 break;
437 case PID:
438 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000439 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000440 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000441 printf("%-8d %-10d", msqid, ipcp->uid);
442 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000443 break;
444
445 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000446 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000447 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000448 printf("%-10d %-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("%-10d %-10d", msqid, ipcp->uid);
451 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100452 /*
453 * glibc-2.1.3 and earlier has unsigned short;
454 * glibc-2.1.91 has variation between
455 * unsigned short, unsigned long
456 * Austin has msgqnum_t
457 */
458 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000459 break;
460 }
461 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000462}
463
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000464static void print_shm(int shmid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000465{
466 struct shmid_ds shmds;
467 struct ipc_perm *ipcp = &shmds.shm_perm;
468
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000469 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
470 bb_perror_msg("shmctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000471 return;
472 }
473
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000474 printf("\nShared memory Segment shmid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100475 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
476 "mode=%#o\taccess_perms=%#o\n"
477 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
478 shmid,
479 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
480 ipcp->mode, ipcp->mode & 0777,
481 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
482 (long) shmds.shm_nattch);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000483 printf("att_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100484 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000485 printf("det_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100486 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000487 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000488}
489
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000490static void print_msg(int msqid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000491{
492 struct msqid_ds buf;
493 struct ipc_perm *ipcp = &buf.msg_perm;
494
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000495 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
496 bb_perror_msg("msgctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000497 return;
498 }
499
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000500 printf("\nMessage Queue msqid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100501 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
502 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
503 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
504 /*
505 * glibc-2.1.3 and earlier has unsigned short;
506 * glibc-2.1.91 has variation between
507 * unsigned short, unsigned long
508 * Austin has msgqnum_t (for msg_qbytes)
509 */
510 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
511 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000512
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000513 printf("send_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100514 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000515 printf("rcv_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100516 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000517 printf("change_time=%-26.24s\n\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100518 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000519}
520
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000521static void print_sem(int semid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000522{
523 struct semid_ds semds;
524 struct ipc_perm *ipcp = &semds.sem_perm;
525 union semun arg;
526 unsigned int i;
527
528 arg.buf = &semds;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000529 if (semctl(semid, 0, IPC_STAT, arg)) {
530 bb_perror_msg("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000531 return;
532 }
533
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000534 printf("\nSemaphore Array semid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100535 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
536 "mode=%#o, access_perms=%#o\n"
537 "nsems = %ld\n"
538 "otime = %-26.24s\n",
539 semid,
540 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
541 ipcp->mode, ipcp->mode & 0777,
542 (long) semds.sem_nsems,
543 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000544 printf("ctime = %-26.24s\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100545 "%-10s %-10s %-10s %-10s %-10s\n",
546 ctime(&semds.sem_ctime),
547 "semnum", "value", "ncount", "zcount", "pid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000548
549 arg.val = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000550 for (i = 0; i < semds.sem_nsems; i++) {
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000551 int val, ncnt, zcnt, pid;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000552
553 val = semctl(semid, i, GETVAL, arg);
554 ncnt = semctl(semid, i, GETNCNT, arg);
555 zcnt = semctl(semid, i, GETZCNT, arg);
556 pid = semctl(semid, i, GETPID, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000557 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000558 bb_perror_msg_and_die("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000559 }
Denys Vlasenko327f5502013-11-29 16:45:45 +0100560 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000561 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000562 bb_putchar('\n');
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000563}
Rob Landley6eb1e412005-06-20 04:30:36 +0000564
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +0200565//usage:#define ipcs_trivial_usage
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100566//usage: "[[-smq] -i SHMID] | [[-asmq] [-tcplu]]"
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +0200567//usage:#define ipcs_full_usage "\n\n"
Denys Vlasenko77982822018-03-08 13:23:06 +0100568//usage: " -i ID Show specific resource"
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +0200569//usage: "\nResource specification:"
570//usage: "\n -m Shared memory segments"
571//usage: "\n -q Message queues"
572//usage: "\n -s Semaphore arrays"
573//usage: "\n -a All (default)"
574//usage: "\nOutput format:"
575//usage: "\n -t Time"
576//usage: "\n -c Creator"
577//usage: "\n -p Pid"
578//usage: "\n -l Limits"
579//usage: "\n -u Summary"
580
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000581int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000582int ipcs_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000583{
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100584 int format = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000585 unsigned opt;
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000586 char *opt_i;
Rob Landley6eb1e412005-06-20 04:30:36 +0000587
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000588 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
Denys Vlasenko77982822018-03-08 13:23:06 +0100589#define flag_msg (1<<2)
590#define flag_sem (1<<3)
591#define flag_shm (1<<4)
592 if (opt & (1<<5)) format = TIME; // -t
593 if (opt & (1<<6)) format = CREATOR; // -c
594 if (opt & (1<<7)) format = PID; // -p
595 if (opt & (1<<8)) format = LIMITS; // -l
596 if (opt & (1<<9)) format = STATUS; // -u
Rob Landley6eb1e412005-06-20 04:30:36 +0000597
Denys Vlasenko77982822018-03-08 13:23:06 +0100598 if (opt & (1<<0)) { // -i
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100599 int id;
600
601 id = xatoi(opt_i);
Denys Vlasenko77982822018-03-08 13:23:06 +0100602 if (opt & flag_shm) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000603 print_shm(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000604 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000605 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100606 if (opt & flag_sem) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000607 print_sem(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000608 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000609 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100610 if (opt & flag_msg) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000611 print_msg(id);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000612 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000613 }
614 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000615 }
616
Denys Vlasenko77982822018-03-08 13:23:06 +0100617 if ((opt & (1<<1)) // -a
618 || !(opt & (flag_msg | flag_sem | flag_shm)) // none of -q,-s,-m == all
619 ) {
620 opt |= flag_msg | flag_sem | flag_shm;
621 }
622
Denis Vlasenko4daad902007-09-27 10:20:47 +0000623 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000624
Denys Vlasenko77982822018-03-08 13:23:06 +0100625 if (opt & flag_msg) {
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100626 do_msg(format);
Denys Vlasenko890bd5d2017-08-09 19:27:01 +0200627 bb_putchar('\n');
628 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100629 if (opt & flag_shm) {
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100630 do_shm(format);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000631 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000632 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100633 if (opt & flag_sem) {
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100634 do_sem(format);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000635 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000636 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000637 fflush_stdout_and_exit(EXIT_SUCCESS);
Rob Landley6eb1e412005-06-20 04:30:36 +0000638}