"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 2 | /* |
| 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 10 | //config:config IPCS |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "ipcs (11 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 12 | //config: default y |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: The ipcs utility is used to provide information on the currently |
| 15 | //config: allocated System V interprocess (IPC) objects in the system. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 16 | |
Denys Vlasenko | c6ce1c9 | 2017-08-09 19:24:19 +0200 | [diff] [blame] | 17 | //applet:IF_IPCS(APPLET_NOEXEC(ipcs, ipcs, BB_DIR_USR_BIN, BB_SUID_DROP, ipcs)) |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 18 | |
| 19 | //kbuild:lib-$(CONFIG_IPCS) += ipcs.o |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 20 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 21 | /* 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 Vlasenko | fad2b86 | 2007-05-31 22:16:38 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 31 | |
| 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 37 | #define SHM_DEST 01000 /* segment will be destroyed on last detach */ |
| 38 | #define SHM_LOCKED 02000 /* segment will not be swapped */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 39 | #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 |
| 50 | struct shm_info { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 51 | int used_ids; |
Dan Fandrich | f3854f7 | 2011-06-03 20:25:17 +0200 | [diff] [blame] | 52 | 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 Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 57 | }; |
| 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 Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 74 | #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 75 | /* union semun is defined by including <sys/sem.h> */ |
| 76 | #else |
| 77 | /* according to X/OPEN we have to define it ourselves */ |
| 78 | union semun { |
| 79 | int val; |
| 80 | struct semid_ds *buf; |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 81 | unsigned short *array; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 82 | 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 Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 91 | #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1 |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 92 | #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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 103 | static void print_perms(int id, struct ipc_perm *ipcp) |
| 104 | { |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 105 | struct passwd *pw; |
| 106 | struct group *gr; |
| 107 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 108 | printf("%-10d %-10o", id, ipcp->mode & 0777); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | 1acdc89 | 2007-09-27 10:17:53 +0000 | [diff] [blame] | 110 | 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 1acdc89 | 2007-09-27 10:17:53 +0000 | [diff] [blame] | 117 | 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 125 | static NOINLINE void do_shm(int format) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 126 | { |
| 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 134 | maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 135 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 136 | printf("kernel not configured for %s\n", "shared memory"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | switch (format) { |
| 141 | case LIMITS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 142 | printf("------ Shared Memory %s --------\n", "Limits"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 143 | if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 144 | return; |
| 145 | /* glibc 2.1.3 and all earlier libc's have ints as fields |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 146 | * of struct shminfo; glibc 2.1.91 has unsigned long; ach */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 147 | printf("max number of segments = %lu\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 148 | "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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
| 157 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 158 | printf("------ Shared Memory %s --------\n", "Status"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 159 | printf("segments allocated %d\n" |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 160 | "pages allocated %lu\n" |
| 161 | "pages resident %lu\n" |
| 162 | "pages swapped %lu\n" |
| 163 | "Swap performance: %lu attempts\t%lu successes\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 164 | 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 169 | return; |
| 170 | |
| 171 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 172 | printf("------ Shared Memory %s --------\n", "Segment Creators/Owners"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 173 | printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", |
| 174 | "shmid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 175 | break; |
| 176 | |
| 177 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 178 | printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 179 | printf("%-10s %-10s %-20s %-20s %-20s\n", |
| 180 | "shmid", "owner", "attached", "detached", "changed"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 181 | break; |
| 182 | |
| 183 | case PID: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 184 | printf("------ Shared Memory %s --------\n", "Creator/Last-op"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 185 | printf("%-10s %-10s %-10s %-10s\n", |
| 186 | "shmid", "owner", "cpid", "lpid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 187 | break; |
| 188 | |
| 189 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 190 | printf("------ Shared Memory %s --------\n", "Segments"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 191 | printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n", |
| 192 | "key", "shmid", "owner", "perms", "bytes", "nattch", |
| 193 | "status"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 194 | break; |
| 195 | } |
| 196 | |
| 197 | for (id = 0; id <= maxid; id++) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 198 | shmid = shmctl(id, SHM_STAT, &shmseg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 199 | if (shmid < 0) |
| 200 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 201 | if (format == CREATOR) { |
| 202 | print_perms(shmid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 203 | continue; |
| 204 | } |
| 205 | pw = getpwuid(ipcp->uid); |
| 206 | switch (format) { |
| 207 | case TIME: |
| 208 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 209 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 210 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 211 | printf("%-10d %-10d", shmid, ipcp->uid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 212 | /* ctime uses static buffer: use separate calls */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 213 | printf(" %-20.16s", shmseg.shm_atime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 214 | ? ctime(&shmseg.shm_atime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 215 | printf(" %-20.16s", shmseg.shm_dtime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 216 | ? ctime(&shmseg.shm_dtime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 217 | printf(" %-20.16s\n", shmseg.shm_ctime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 218 | ? ctime(&shmseg.shm_ctime) + 4 : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 219 | break; |
| 220 | case PID: |
| 221 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 222 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 223 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 224 | printf("%-10d %-10d", shmid, ipcp->uid); |
| 225 | printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 226 | break; |
| 227 | |
| 228 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 229 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 230 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 231 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 232 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 233 | printf("%-10d %-10d", shmid, ipcp->uid); |
| 234 | printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 235 | /* |
| 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | } |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 251 | static NOINLINE void do_sem(int format) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 252 | { |
| 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 Fandrich | f3854f7 | 2011-06-03 20:25:17 +0200 | [diff] [blame] | 260 | arg.array = (unsigned short *) (void *) &seminfo; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 261 | maxid = semctl(0, 0, SEM_INFO, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 262 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 263 | printf("kernel not configured for %s\n", "semaphores"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 264 | return; |
| 265 | } |
| 266 | |
| 267 | switch (format) { |
| 268 | case LIMITS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 269 | printf("------ Semaphore %s --------\n", "Limits"); |
Dan Fandrich | f3854f7 | 2011-06-03 20:25:17 +0200 | [diff] [blame] | 270 | arg.array = (unsigned short *) (void *) &seminfo; /* damn union */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 271 | if ((semctl(0, 0, IPC_INFO, arg)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 272 | return; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 273 | printf("max number of arrays = %d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 274 | "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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 281 | return; |
| 282 | |
| 283 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 284 | printf("------ Semaphore %s --------\n", "Status"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 285 | printf("used arrays = %d\n" |
| 286 | "allocated semaphores = %d\n", |
| 287 | seminfo.semusz, seminfo.semaem); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 288 | return; |
| 289 | |
| 290 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 291 | printf("------ Semaphore %s --------\n", "Arrays Creators/Owners"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 292 | printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", |
| 293 | "semid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 294 | break; |
| 295 | |
| 296 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 297 | printf("------ Shared Memory %s --------\n", "Operation/Change Times"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 298 | printf("%-8s %-10s %-26.24s %-26.24s\n", |
| 299 | "shmid", "owner", "last-op", "last-changed"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | case PID: |
| 303 | break; |
| 304 | |
| 305 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 306 | printf("------ Semaphore %s --------\n", "Arrays"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 307 | printf("%-10s %-10s %-10s %-10s %-10s\n", |
| 308 | "key", "semid", "owner", "perms", "nsems"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 309 | break; |
| 310 | } |
| 311 | |
| 312 | for (id = 0; id <= maxid; id++) { |
| 313 | arg.buf = (struct semid_ds *) &semary; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 314 | semid = semctl(id, 0, SEM_STAT, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 315 | if (semid < 0) |
| 316 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 317 | if (format == CREATOR) { |
| 318 | print_perms(semid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 319 | continue; |
| 320 | } |
| 321 | pw = getpwuid(ipcp->uid); |
| 322 | switch (format) { |
| 323 | case TIME: |
| 324 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 325 | printf("%-8d %-10.10s", semid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 326 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 327 | printf("%-8d %-10d", semid, ipcp->uid); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 328 | /* ctime uses static buffer: use separate calls */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 329 | printf(" %-26.24s", semary.sem_otime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 330 | ? ctime(&semary.sem_otime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 331 | printf(" %-26.24s\n", semary.sem_ctime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 332 | ? ctime(&semary.sem_ctime) : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 333 | break; |
| 334 | case PID: |
| 335 | break; |
| 336 | |
| 337 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 338 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 339 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 340 | printf("%-10d %-10.9s", semid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 341 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 342 | printf("%-10d %-9d", semid, ipcp->uid); |
| 343 | printf(" %-10o %-10ld\n", ipcp->mode & 0777, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 344 | /* |
| 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 356 | static NOINLINE void do_msg(int format) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 357 | { |
| 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 364 | maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 365 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 366 | printf("kernel not configured for %s\n", "message queues"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
| 370 | switch (format) { |
| 371 | case LIMITS: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 372 | if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 373 | return; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 374 | printf("------ Message%s --------\n", "s: Limits"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 375 | 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 379 | return; |
| 380 | |
| 381 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 382 | printf("------ Message%s --------\n", "s: Status"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 383 | 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 387 | return; |
| 388 | |
| 389 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 390 | printf("------ Message%s --------\n", " Queues: Creators/Owners"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 391 | printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", |
| 392 | "msqid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 396 | printf("------ Message%s --------\n", " Queues Send/Recv/Change Times"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 397 | printf("%-8s %-10s %-20s %-20s %-20s\n", |
| 398 | "msqid", "owner", "send", "recv", "change"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 399 | break; |
| 400 | |
| 401 | case PID: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 402 | printf("------ Message%s --------\n", " Queues PIDs"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 403 | printf("%-10s %-10s %-10s %-10s\n", |
| 404 | "msqid", "owner", "lspid", "lrpid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 405 | break; |
| 406 | |
| 407 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 408 | printf("------ Message%s --------\n", " Queues"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 409 | printf("%-10s %-10s %-10s %-10s %-12s %-12s\n", |
| 410 | "key", "msqid", "owner", "perms", "used-bytes", "messages"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | |
| 414 | for (id = 0; id <= maxid; id++) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 415 | msqid = msgctl(id, MSG_STAT, &msgque); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 416 | if (msqid < 0) |
| 417 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 418 | if (format == CREATOR) { |
| 419 | print_perms(msqid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 420 | continue; |
| 421 | } |
| 422 | pw = getpwuid(ipcp->uid); |
| 423 | switch (format) { |
| 424 | case TIME: |
| 425 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 426 | printf("%-8d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 427 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 428 | printf("%-8d %-10d", msqid, ipcp->uid); |
| 429 | printf(" %-20.16s", msgque.msg_stime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 430 | ? ctime(&msgque.msg_stime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 431 | printf(" %-20.16s", msgque.msg_rtime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 432 | ? ctime(&msgque.msg_rtime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 433 | printf(" %-20.16s\n", msgque.msg_ctime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 434 | ? ctime(&msgque.msg_ctime) + 4 : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 435 | break; |
| 436 | case PID: |
| 437 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 438 | printf("%-8d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 439 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 440 | printf("%-8d %-10d", msqid, ipcp->uid); |
| 441 | printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 442 | break; |
| 443 | |
| 444 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 445 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 446 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 447 | printf("%-10d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 448 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 449 | printf("%-10d %-10d", msqid, ipcp->uid); |
| 450 | printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 451 | /* |
| 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 458 | break; |
| 459 | } |
| 460 | } |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 463 | static void print_shm(int shmid) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 464 | { |
| 465 | struct shmid_ds shmds; |
| 466 | struct ipc_perm *ipcp = &shmds.shm_perm; |
| 467 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 468 | if (shmctl(shmid, IPC_STAT, &shmds) == -1) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 469 | bb_simple_perror_msg("shmctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 473 | printf("\nShared memory Segment shmid=%d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 474 | "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 Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 482 | printf("att_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 483 | shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 484 | printf("det_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 485 | shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 486 | printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime)); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 489 | static void print_msg(int msqid) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 490 | { |
| 491 | struct msqid_ds buf; |
| 492 | struct ipc_perm *ipcp = &buf.msg_perm; |
| 493 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 494 | if (msgctl(msqid, IPC_STAT, &buf) == -1) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 495 | bb_simple_perror_msg("msgctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 496 | return; |
| 497 | } |
| 498 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 499 | printf("\nMessage Queue msqid=%d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 500 | "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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 511 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 512 | printf("send_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 513 | buf.msg_stime ? ctime(&buf.msg_stime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 514 | printf("rcv_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 515 | buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 516 | printf("change_time=%-26.24s\n\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 517 | buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 520 | static void print_sem(int semid) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 521 | { |
| 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-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 528 | if (semctl(semid, 0, IPC_STAT, arg)) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 529 | bb_simple_perror_msg("semctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 533 | printf("\nSemaphore Array semid=%d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 534 | "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 Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 543 | printf("ctime = %-26.24s\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 544 | "%-10s %-10s %-10s %-10s %-10s\n", |
| 545 | ctime(&semds.sem_ctime), |
| 546 | "semnum", "value", "ncount", "zcount", "pid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 547 | |
| 548 | arg.val = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 549 | for (i = 0; i < semds.sem_nsems; i++) { |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 550 | int val, ncnt, zcnt, pid; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 551 | |
| 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-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 556 | if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 557 | bb_simple_perror_msg_and_die("semctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 558 | } |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 559 | printf("%-10u %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 560 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 561 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 562 | } |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 563 | |
Denys Vlasenko | c6ce1c9 | 2017-08-09 19:24:19 +0200 | [diff] [blame] | 564 | //usage:#define ipcs_trivial_usage |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 565 | //usage: "[[-smq] -i SHMID] | [[-asmq] [-tcplu]]" |
Denys Vlasenko | c6ce1c9 | 2017-08-09 19:24:19 +0200 | [diff] [blame] | 566 | //usage:#define ipcs_full_usage "\n\n" |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 567 | //usage: " -i ID Show specific resource" |
Denys Vlasenko | c6ce1c9 | 2017-08-09 19:24:19 +0200 | [diff] [blame] | 568 | //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 Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 580 | int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 581 | int ipcs_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 582 | { |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 583 | int format = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 584 | unsigned opt; |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 585 | char *opt_i; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 586 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 587 | opt = getopt32(argv, "i:aqsmtcplu", &opt_i); |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 588 | #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 Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 596 | |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 597 | if (opt & (1<<0)) { // -i |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 598 | int id; |
| 599 | |
| 600 | id = xatoi(opt_i); |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 601 | if (opt & flag_shm) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 602 | print_shm(id); |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 603 | fflush_stdout_and_exit_SUCCESS(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 604 | } |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 605 | if (opt & flag_sem) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 606 | print_sem(id); |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 607 | fflush_stdout_and_exit_SUCCESS(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 608 | } |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 609 | if (opt & flag_msg) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 610 | print_msg(id); |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 611 | fflush_stdout_and_exit_SUCCESS(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 612 | } |
| 613 | bb_show_usage(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 616 | 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 Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 622 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 623 | |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 624 | if (opt & flag_msg) { |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 625 | do_msg(format); |
Denys Vlasenko | 890bd5d | 2017-08-09 19:27:01 +0200 | [diff] [blame] | 626 | bb_putchar('\n'); |
| 627 | } |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 628 | if (opt & flag_shm) { |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 629 | do_shm(format); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 630 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 631 | } |
Denys Vlasenko | 7798282 | 2018-03-08 13:23:06 +0100 | [diff] [blame] | 632 | if (opt & flag_sem) { |
Denys Vlasenko | f7fa800 | 2018-03-08 13:10:23 +0100 | [diff] [blame] | 633 | do_sem(format); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 634 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 635 | } |
Denys Vlasenko | 31f45c1 | 2022-01-04 23:31:58 +0100 | [diff] [blame] | 636 | fflush_stdout_and_exit_SUCCESS(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 637 | } |