blob: 5973cbf571a8396589cf286c881c9a9a5cf50795 [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
Denys Vlasenkodd898c92016-11-23 11:46:32 +010013//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: The ipcs utility is used to provide information on the currently
15//config: allocated System V interprocess (IPC) objects in the system.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010016
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +020017//applet:IF_IPCS(APPLET_NOEXEC(ipcs, ipcs, BB_DIR_USR_BIN, BB_SUID_DROP, ipcs))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010018
19//kbuild:lib-$(CONFIG_IPCS) += ipcs.o
Rob Landley6eb1e412005-06-20 04:30:36 +000020
Rob Landley6eb1e412005-06-20 04:30:36 +000021/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
22/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
23/* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */
24#include <sys/types.h>
25#include <sys/ipc.h>
26#include <sys/sem.h>
27#include <sys/msg.h>
28#include <sys/shm.h>
29
Denis Vlasenkofad2b862007-05-31 22:16:38 +000030#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000031
32/*-------------------------------------------------------------------*/
33/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
34 but inside #ifdef __KERNEL__ ... #endif */
35#ifndef SHM_DEST
36/* shm_mode upper byte flags */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000037#define SHM_DEST 01000 /* segment will be destroyed on last detach */
38#define SHM_LOCKED 02000 /* segment will not be swapped */
Rob Landley6eb1e412005-06-20 04:30:36 +000039#endif
40
41/* For older kernels the same holds for the defines below */
42#ifndef MSG_STAT
43#define MSG_STAT 11
44#define MSG_INFO 12
45#endif
46
47#ifndef SHM_STAT
48#define SHM_STAT 13
49#define SHM_INFO 14
50struct shm_info {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000051 int used_ids;
Dan Fandrichf3854f72011-06-03 20:25:17 +020052 unsigned long shm_tot; /* total allocated shm */
53 unsigned long shm_rss; /* total resident shm */
54 unsigned long shm_swp; /* total swapped shm */
55 unsigned long swap_attempts;
56 unsigned long swap_successes;
Rob Landley6eb1e412005-06-20 04:30:36 +000057};
58#endif
59
60#ifndef SEM_STAT
61#define SEM_STAT 18
62#define SEM_INFO 19
63#endif
64
65/* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */
66#ifndef IPC_INFO
67#define IPC_INFO 3
68#endif
69/*-------------------------------------------------------------------*/
70
71/* The last arg of semctl is a union semun, but where is it defined?
72 X/OPEN tells us to define it ourselves, but until recently
73 Linux include files would also define it. */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000074#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000075/* union semun is defined by including <sys/sem.h> */
76#else
77/* according to X/OPEN we have to define it ourselves */
78union semun {
79 int val;
80 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000081 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000082 struct seminfo *__buf;
83};
84#endif
85
86/* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm;
87 libc 4/5 does not mention struct ipc_term at all, but includes
88 <linux/ipc.h>, which defines a struct ipc_perm with such fields.
89 glibc-1.09 has no support for sysv ipc.
90 glibc 2 uses __key, __seq */
Denis Vlasenkoff131b92007-04-10 15:42:06 +000091#if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
Rob Landley6eb1e412005-06-20 04:30:36 +000092#define KEY __key
93#else
94#define KEY key
95#endif
96
97#define LIMITS 1
98#define STATUS 2
99#define CREATOR 3
100#define TIME 4
101#define PID 5
102
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000103static void print_perms(int id, struct ipc_perm *ipcp)
104{
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000105 struct passwd *pw;
106 struct group *gr;
107
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000108 printf("%-10d %-10o", id, ipcp->mode & 0777);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000109
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000110 pw = getpwuid(ipcp->cuid);
111 if (pw) printf(" %-10s", pw->pw_name);
112 else printf(" %-10d", ipcp->cuid);
113 gr = getgrgid(ipcp->cgid);
114 if (gr) printf(" %-10s", gr->gr_name);
115 else printf(" %-10d", ipcp->cgid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000116
Denis Vlasenko1acdc892007-09-27 10:17:53 +0000117 pw = getpwuid(ipcp->uid);
118 if (pw) printf(" %-10s", pw->pw_name);
119 else printf(" %-10d", ipcp->uid);
120 gr = getgrgid(ipcp->gid);
121 if (gr) printf(" %-10s\n", gr->gr_name);
122 else printf(" %-10d\n", ipcp->gid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000123}
124
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100125static NOINLINE void do_shm(int format)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000126{
127 int maxid, shmid, id;
128 struct shmid_ds shmseg;
129 struct shm_info shm_info;
130 struct shminfo shminfo;
131 struct ipc_perm *ipcp = &shmseg.shm_perm;
132 struct passwd *pw;
133
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000134 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000135 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000136 printf("kernel not configured for %s\n", "shared memory");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000137 return;
138 }
139
140 switch (format) {
141 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000142 printf("------ Shared Memory %s --------\n", "Limits");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000143 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000144 return;
145 /* glibc 2.1.3 and all earlier libc's have ints as fields
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100146 * of struct shminfo; glibc 2.1.91 has unsigned long; ach */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000147 printf("max number of segments = %lu\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100148 "max seg size (kbytes) = %lu\n"
149 "max total shared memory (pages) = %lu\n"
150 "min seg size (bytes) = %lu\n",
151 (unsigned long) shminfo.shmmni,
152 (unsigned long) (shminfo.shmmax >> 10),
153 (unsigned long) shminfo.shmall,
154 (unsigned long) shminfo.shmmin);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000155 return;
156
157 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000158 printf("------ Shared Memory %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100159 printf("segments allocated %d\n"
Denys Vlasenko327f5502013-11-29 16:45:45 +0100160 "pages allocated %lu\n"
161 "pages resident %lu\n"
162 "pages swapped %lu\n"
163 "Swap performance: %lu attempts\t%lu successes\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100164 shm_info.used_ids,
165 shm_info.shm_tot,
166 shm_info.shm_rss,
167 shm_info.shm_swp,
168 shm_info.swap_attempts, shm_info.swap_successes);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000169 return;
170
171 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000172 printf("------ Shared Memory %s --------\n", "Segment Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100173 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
174 "shmid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000175 break;
176
177 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000178 printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100179 printf("%-10s %-10s %-20s %-20s %-20s\n",
180 "shmid", "owner", "attached", "detached", "changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000181 break;
182
183 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000184 printf("------ Shared Memory %s --------\n", "Creator/Last-op");
Denys Vlasenko69675782013-01-14 01:34:48 +0100185 printf("%-10s %-10s %-10s %-10s\n",
186 "shmid", "owner", "cpid", "lpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000187 break;
188
189 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000190 printf("------ Shared Memory %s --------\n", "Segments");
Denys Vlasenko69675782013-01-14 01:34:48 +0100191 printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
192 "key", "shmid", "owner", "perms", "bytes", "nattch",
193 "status");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000194 break;
195 }
196
197 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198 shmid = shmctl(id, SHM_STAT, &shmseg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000199 if (shmid < 0)
200 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000201 if (format == CREATOR) {
202 print_perms(shmid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000203 continue;
204 }
205 pw = getpwuid(ipcp->uid);
206 switch (format) {
207 case TIME:
208 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000209 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000210 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000211 printf("%-10d %-10d", shmid, ipcp->uid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000212 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000213 printf(" %-20.16s", shmseg.shm_atime
Denys Vlasenko69675782013-01-14 01:34:48 +0100214 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000215 printf(" %-20.16s", shmseg.shm_dtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100216 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000217 printf(" %-20.16s\n", shmseg.shm_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100218 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000219 break;
220 case PID:
221 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000222 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000223 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000224 printf("%-10d %-10d", shmid, ipcp->uid);
225 printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000226 break;
227
228 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000229 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000230 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000231 printf("%-10d %-10.10s", shmid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000232 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000233 printf("%-10d %-10d", shmid, ipcp->uid);
234 printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100235 /*
236 * earlier: int, Austin has size_t
237 */
238 (unsigned long) shmseg.shm_segsz,
239 /*
240 * glibc-2.1.3 and earlier has unsigned short;
241 * Austin has shmatt_t
242 */
243 (long) shmseg.shm_nattch,
244 ipcp->mode & SHM_DEST ? "dest" : " ",
245 ipcp->mode & SHM_LOCKED ? "locked" : " ");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000246 break;
247 }
248 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000249}
250
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100251static NOINLINE void do_sem(int format)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000252{
253 int maxid, semid, id;
254 struct semid_ds semary;
255 struct seminfo seminfo;
256 struct ipc_perm *ipcp = &semary.sem_perm;
257 struct passwd *pw;
258 union semun arg;
259
Dan Fandrichf3854f72011-06-03 20:25:17 +0200260 arg.array = (unsigned short *) (void *) &seminfo;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000261 maxid = semctl(0, 0, SEM_INFO, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000262 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000263 printf("kernel not configured for %s\n", "semaphores");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000264 return;
265 }
266
267 switch (format) {
268 case LIMITS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000269 printf("------ Semaphore %s --------\n", "Limits");
Dan Fandrichf3854f72011-06-03 20:25:17 +0200270 arg.array = (unsigned short *) (void *) &seminfo; /* damn union */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000271 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000272 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000273 printf("max number of arrays = %d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100274 "max semaphores per array = %d\n"
275 "max semaphores system wide = %d\n"
276 "max ops per semop call = %d\n"
277 "semaphore max value = %d\n",
278 seminfo.semmni,
279 seminfo.semmsl,
280 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000281 return;
282
283 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000284 printf("------ Semaphore %s --------\n", "Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100285 printf("used arrays = %d\n"
286 "allocated semaphores = %d\n",
287 seminfo.semusz, seminfo.semaem);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000288 return;
289
290 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000291 printf("------ Semaphore %s --------\n", "Arrays Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100292 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
293 "semid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000294 break;
295
296 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000297 printf("------ Shared Memory %s --------\n", "Operation/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100298 printf("%-8s %-10s %-26.24s %-26.24s\n",
299 "shmid", "owner", "last-op", "last-changed");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000300 break;
301
302 case PID:
303 break;
304
305 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000306 printf("------ Semaphore %s --------\n", "Arrays");
Denys Vlasenko69675782013-01-14 01:34:48 +0100307 printf("%-10s %-10s %-10s %-10s %-10s\n",
308 "key", "semid", "owner", "perms", "nsems");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000309 break;
310 }
311
312 for (id = 0; id <= maxid; id++) {
313 arg.buf = (struct semid_ds *) &semary;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000314 semid = semctl(id, 0, SEM_STAT, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000315 if (semid < 0)
316 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000317 if (format == CREATOR) {
318 print_perms(semid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000319 continue;
320 }
321 pw = getpwuid(ipcp->uid);
322 switch (format) {
323 case TIME:
324 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000325 printf("%-8d %-10.10s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000326 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000327 printf("%-8d %-10d", semid, ipcp->uid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000328 /* ctime uses static buffer: use separate calls */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000329 printf(" %-26.24s", semary.sem_otime
Denys Vlasenko69675782013-01-14 01:34:48 +0100330 ? ctime(&semary.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000331 printf(" %-26.24s\n", semary.sem_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100332 ? ctime(&semary.sem_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000333 break;
334 case PID:
335 break;
336
337 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000338 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000339 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000340 printf("%-10d %-10.9s", semid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000341 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000342 printf("%-10d %-9d", semid, ipcp->uid);
343 printf(" %-10o %-10ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100344 /*
345 * glibc-2.1.3 and earlier has unsigned short;
346 * glibc-2.1.91 has variation between
347 * unsigned short and unsigned long
348 * Austin prescribes unsigned short.
349 */
350 (long) semary.sem_nsems);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000351 break;
352 }
353 }
354}
355
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100356static NOINLINE void do_msg(int format)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000357{
358 int maxid, msqid, id;
359 struct msqid_ds msgque;
360 struct msginfo msginfo;
361 struct ipc_perm *ipcp = &msgque.msg_perm;
362 struct passwd *pw;
363
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000364 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000365 if (maxid < 0) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000366 printf("kernel not configured for %s\n", "message queues");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000367 return;
368 }
369
370 switch (format) {
371 case LIMITS:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000372 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000373 return;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000374 printf("------ Message%s --------\n", "s: Limits");
Denys Vlasenko69675782013-01-14 01:34:48 +0100375 printf("max queues system wide = %d\n"
376 "max size of message (bytes) = %d\n"
377 "default max size of queue (bytes) = %d\n",
378 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000379 return;
380
381 case STATUS:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000382 printf("------ Message%s --------\n", "s: Status");
Denys Vlasenko69675782013-01-14 01:34:48 +0100383 printf("allocated queues = %d\n"
384 "used headers = %d\n"
385 "used space = %d bytes\n",
386 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000387 return;
388
389 case CREATOR:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000390 printf("------ Message%s --------\n", " Queues: Creators/Owners");
Denys Vlasenko69675782013-01-14 01:34:48 +0100391 printf("%-10s %-10s %-10s %-10s %-10s %-10s\n",
392 "msqid", "perms", "cuid", "cgid", "uid", "gid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000393 break;
394
395 case TIME:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000396 printf("------ Message%s --------\n", " Queues Send/Recv/Change Times");
Denys Vlasenko69675782013-01-14 01:34:48 +0100397 printf("%-8s %-10s %-20s %-20s %-20s\n",
398 "msqid", "owner", "send", "recv", "change");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000399 break;
400
401 case PID:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000402 printf("------ Message%s --------\n", " Queues PIDs");
Denys Vlasenko69675782013-01-14 01:34:48 +0100403 printf("%-10s %-10s %-10s %-10s\n",
404 "msqid", "owner", "lspid", "lrpid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000405 break;
406
407 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000408 printf("------ Message%s --------\n", " Queues");
Denys Vlasenko69675782013-01-14 01:34:48 +0100409 printf("%-10s %-10s %-10s %-10s %-12s %-12s\n",
410 "key", "msqid", "owner", "perms", "used-bytes", "messages");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000411 break;
412 }
413
414 for (id = 0; id <= maxid; id++) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000415 msqid = msgctl(id, MSG_STAT, &msgque);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000416 if (msqid < 0)
417 continue;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000418 if (format == CREATOR) {
419 print_perms(msqid, ipcp);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000420 continue;
421 }
422 pw = getpwuid(ipcp->uid);
423 switch (format) {
424 case TIME:
425 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000426 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000427 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000428 printf("%-8d %-10d", msqid, ipcp->uid);
429 printf(" %-20.16s", msgque.msg_stime
Denys Vlasenko69675782013-01-14 01:34:48 +0100430 ? ctime(&msgque.msg_stime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000431 printf(" %-20.16s", msgque.msg_rtime
Denys Vlasenko69675782013-01-14 01:34:48 +0100432 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000433 printf(" %-20.16s\n", msgque.msg_ctime
Denys Vlasenko69675782013-01-14 01:34:48 +0100434 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000435 break;
436 case PID:
437 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000438 printf("%-8d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000439 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000440 printf("%-8d %-10d", msqid, ipcp->uid);
441 printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000442 break;
443
444 default:
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000445 printf("0x%08x ", ipcp->KEY);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000446 if (pw)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000447 printf("%-10d %-10.10s", msqid, pw->pw_name);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000448 else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000449 printf("%-10d %-10d", msqid, ipcp->uid);
450 printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
Denys Vlasenko69675782013-01-14 01:34:48 +0100451 /*
452 * glibc-2.1.3 and earlier has unsigned short;
453 * glibc-2.1.91 has variation between
454 * unsigned short, unsigned long
455 * Austin has msgqnum_t
456 */
457 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000458 break;
459 }
460 }
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000461}
462
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000463static void print_shm(int shmid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000464{
465 struct shmid_ds shmds;
466 struct ipc_perm *ipcp = &shmds.shm_perm;
467
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000468 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
James Byrne69374872019-07-02 11:35:03 +0200469 bb_simple_perror_msg("shmctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000470 return;
471 }
472
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000473 printf("\nShared memory Segment shmid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100474 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
475 "mode=%#o\taccess_perms=%#o\n"
476 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
477 shmid,
478 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
479 ipcp->mode, ipcp->mode & 0777,
480 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
481 (long) shmds.shm_nattch);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000482 printf("att_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100483 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000484 printf("det_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100485 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000486 printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000487}
488
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000489static void print_msg(int msqid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000490{
491 struct msqid_ds buf;
492 struct ipc_perm *ipcp = &buf.msg_perm;
493
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000494 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
James Byrne69374872019-07-02 11:35:03 +0200495 bb_simple_perror_msg("msgctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000496 return;
497 }
498
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000499 printf("\nMessage Queue msqid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100500 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
501 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
502 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
503 /*
504 * glibc-2.1.3 and earlier has unsigned short;
505 * glibc-2.1.91 has variation between
506 * unsigned short, unsigned long
507 * Austin has msgqnum_t (for msg_qbytes)
508 */
509 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
510 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000511
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000512 printf("send_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100513 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000514 printf("rcv_time=%-26.24s\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100515 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000516 printf("change_time=%-26.24s\n\n",
Denys Vlasenko69675782013-01-14 01:34:48 +0100517 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000518}
519
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000520static void print_sem(int semid)
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000521{
522 struct semid_ds semds;
523 struct ipc_perm *ipcp = &semds.sem_perm;
524 union semun arg;
525 unsigned int i;
526
527 arg.buf = &semds;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000528 if (semctl(semid, 0, IPC_STAT, arg)) {
James Byrne69374872019-07-02 11:35:03 +0200529 bb_simple_perror_msg("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000530 return;
531 }
532
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000533 printf("\nSemaphore Array semid=%d\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100534 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
535 "mode=%#o, access_perms=%#o\n"
536 "nsems = %ld\n"
537 "otime = %-26.24s\n",
538 semid,
539 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
540 ipcp->mode, ipcp->mode & 0777,
541 (long) semds.sem_nsems,
542 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000543 printf("ctime = %-26.24s\n"
Denys Vlasenko69675782013-01-14 01:34:48 +0100544 "%-10s %-10s %-10s %-10s %-10s\n",
545 ctime(&semds.sem_ctime),
546 "semnum", "value", "ncount", "zcount", "pid");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000547
548 arg.val = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000549 for (i = 0; i < semds.sem_nsems; i++) {
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000550 int val, ncnt, zcnt, pid;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000551
552 val = semctl(semid, i, GETVAL, arg);
553 ncnt = semctl(semid, i, GETNCNT, arg);
554 zcnt = semctl(semid, i, GETZCNT, arg);
555 pid = semctl(semid, i, GETPID, arg);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000556 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
James Byrne69374872019-07-02 11:35:03 +0200557 bb_simple_perror_msg_and_die("semctl");
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000558 }
Denys Vlasenko327f5502013-11-29 16:45:45 +0100559 printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000560 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000561 bb_putchar('\n');
Bernhard Reutner-Fischera38330c2006-04-03 11:25:48 +0000562}
Rob Landley6eb1e412005-06-20 04:30:36 +0000563
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +0200564//usage:#define ipcs_trivial_usage
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100565//usage: "[[-smq] -i SHMID] | [[-asmq] [-tcplu]]"
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +0200566//usage:#define ipcs_full_usage "\n\n"
Denys Vlasenko77982822018-03-08 13:23:06 +0100567//usage: " -i ID Show specific resource"
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +0200568//usage: "\nResource specification:"
569//usage: "\n -m Shared memory segments"
570//usage: "\n -q Message queues"
571//usage: "\n -s Semaphore arrays"
572//usage: "\n -a All (default)"
573//usage: "\nOutput format:"
574//usage: "\n -t Time"
575//usage: "\n -c Creator"
576//usage: "\n -p Pid"
577//usage: "\n -l Limits"
578//usage: "\n -u Summary"
579
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000580int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000581int ipcs_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000582{
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100583 int format = 0;
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000584 unsigned opt;
Denis Vlasenkoc7b9bad2006-09-19 17:40:31 +0000585 char *opt_i;
Rob Landley6eb1e412005-06-20 04:30:36 +0000586
Denis Vlasenkofe7cd642007-08-18 15:32:12 +0000587 opt = getopt32(argv, "i:aqsmtcplu", &opt_i);
Denys Vlasenko77982822018-03-08 13:23:06 +0100588#define flag_msg (1<<2)
589#define flag_sem (1<<3)
590#define flag_shm (1<<4)
591 if (opt & (1<<5)) format = TIME; // -t
592 if (opt & (1<<6)) format = CREATOR; // -c
593 if (opt & (1<<7)) format = PID; // -p
594 if (opt & (1<<8)) format = LIMITS; // -l
595 if (opt & (1<<9)) format = STATUS; // -u
Rob Landley6eb1e412005-06-20 04:30:36 +0000596
Denys Vlasenko77982822018-03-08 13:23:06 +0100597 if (opt & (1<<0)) { // -i
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100598 int id;
599
600 id = xatoi(opt_i);
Denys Vlasenko77982822018-03-08 13:23:06 +0100601 if (opt & flag_shm) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000602 print_shm(id);
Denys Vlasenko31f45c12022-01-04 23:31:58 +0100603 fflush_stdout_and_exit_SUCCESS();
Rob Landley6eb1e412005-06-20 04:30:36 +0000604 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100605 if (opt & flag_sem) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000606 print_sem(id);
Denys Vlasenko31f45c12022-01-04 23:31:58 +0100607 fflush_stdout_and_exit_SUCCESS();
Rob Landley6eb1e412005-06-20 04:30:36 +0000608 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100609 if (opt & flag_msg) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000610 print_msg(id);
Denys Vlasenko31f45c12022-01-04 23:31:58 +0100611 fflush_stdout_and_exit_SUCCESS();
Rob Landley6eb1e412005-06-20 04:30:36 +0000612 }
613 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000614 }
615
Denys Vlasenko77982822018-03-08 13:23:06 +0100616 if ((opt & (1<<1)) // -a
617 || !(opt & (flag_msg | flag_sem | flag_shm)) // none of -q,-s,-m == all
618 ) {
619 opt |= flag_msg | flag_sem | flag_shm;
620 }
621
Denis Vlasenko4daad902007-09-27 10:20:47 +0000622 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000623
Denys Vlasenko77982822018-03-08 13:23:06 +0100624 if (opt & flag_msg) {
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100625 do_msg(format);
Denys Vlasenko890bd5d2017-08-09 19:27:01 +0200626 bb_putchar('\n');
627 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100628 if (opt & flag_shm) {
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100629 do_shm(format);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000630 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000631 }
Denys Vlasenko77982822018-03-08 13:23:06 +0100632 if (opt & flag_sem) {
Denys Vlasenkof7fa8002018-03-08 13:10:23 +0100633 do_sem(format);
Denis Vlasenko4daad902007-09-27 10:20:47 +0000634 bb_putchar('\n');
Rob Landley6eb1e412005-06-20 04:30:36 +0000635 }
Denys Vlasenko31f45c12022-01-04 23:31:58 +0100636 fflush_stdout_and_exit_SUCCESS();
Rob Landley6eb1e412005-06-20 04:30:36 +0000637}