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