"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 |
| 11 | //config: bool "ipcs" |
| 12 | //config: default y |
| 13 | //config: select PLATFORM_LINUX |
| 14 | //config: help |
| 15 | //config: The ipcs utility is used to provide information on the currently |
| 16 | //config: allocated System V interprocess (IPC) objects in the system. |
| 17 | |
| 18 | //applet:IF_IPCS(APPLET(ipcs, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 19 | |
| 20 | //kbuild:lib-$(CONFIG_IPCS) += ipcs.o |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 21 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 22 | //usage:#define ipcs_trivial_usage |
| 23 | //usage: "[[-smq] -i shmid] | [[-asmq] [-tcplu]]" |
| 24 | //usage:#define ipcs_full_usage "\n\n" |
| 25 | //usage: " -i Show specific resource" |
| 26 | //usage: "\nResource specification:" |
| 27 | //usage: "\n -m Shared memory segments" |
| 28 | //usage: "\n -q Message queues" |
| 29 | //usage: "\n -s Semaphore arrays" |
| 30 | //usage: "\n -a All (default)" |
| 31 | //usage: "\nOutput format:" |
| 32 | //usage: "\n -t Time" |
| 33 | //usage: "\n -c Creator" |
| 34 | //usage: "\n -p Pid" |
| 35 | //usage: "\n -l Limits" |
| 36 | //usage: "\n -u Summary" |
| 37 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 38 | /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */ |
| 39 | /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */ |
| 40 | /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */ |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/ipc.h> |
| 43 | #include <sys/sem.h> |
| 44 | #include <sys/msg.h> |
| 45 | #include <sys/shm.h> |
| 46 | |
Denis Vlasenko | fad2b86 | 2007-05-31 22:16:38 +0000 | [diff] [blame] | 47 | #include "libbb.h" |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 48 | |
| 49 | /*-------------------------------------------------------------------*/ |
| 50 | /* SHM_DEST and SHM_LOCKED are defined in kernel headers, |
| 51 | but inside #ifdef __KERNEL__ ... #endif */ |
| 52 | #ifndef SHM_DEST |
| 53 | /* shm_mode upper byte flags */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 54 | #define SHM_DEST 01000 /* segment will be destroyed on last detach */ |
| 55 | #define SHM_LOCKED 02000 /* segment will not be swapped */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | /* For older kernels the same holds for the defines below */ |
| 59 | #ifndef MSG_STAT |
| 60 | #define MSG_STAT 11 |
| 61 | #define MSG_INFO 12 |
| 62 | #endif |
| 63 | |
| 64 | #ifndef SHM_STAT |
| 65 | #define SHM_STAT 13 |
| 66 | #define SHM_INFO 14 |
| 67 | struct shm_info { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 68 | int used_ids; |
Dan Fandrich | f3854f7 | 2011-06-03 20:25:17 +0200 | [diff] [blame] | 69 | unsigned long shm_tot; /* total allocated shm */ |
| 70 | unsigned long shm_rss; /* total resident shm */ |
| 71 | unsigned long shm_swp; /* total swapped shm */ |
| 72 | unsigned long swap_attempts; |
| 73 | unsigned long swap_successes; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 74 | }; |
| 75 | #endif |
| 76 | |
| 77 | #ifndef SEM_STAT |
| 78 | #define SEM_STAT 18 |
| 79 | #define SEM_INFO 19 |
| 80 | #endif |
| 81 | |
| 82 | /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */ |
| 83 | #ifndef IPC_INFO |
| 84 | #define IPC_INFO 3 |
| 85 | #endif |
| 86 | /*-------------------------------------------------------------------*/ |
| 87 | |
| 88 | /* The last arg of semctl is a union semun, but where is it defined? |
| 89 | X/OPEN tells us to define it ourselves, but until recently |
| 90 | Linux include files would also define it. */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 91 | #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 92 | /* union semun is defined by including <sys/sem.h> */ |
| 93 | #else |
| 94 | /* according to X/OPEN we have to define it ourselves */ |
| 95 | union semun { |
| 96 | int val; |
| 97 | struct semid_ds *buf; |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 98 | unsigned short *array; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 99 | struct seminfo *__buf; |
| 100 | }; |
| 101 | #endif |
| 102 | |
| 103 | /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm; |
| 104 | libc 4/5 does not mention struct ipc_term at all, but includes |
| 105 | <linux/ipc.h>, which defines a struct ipc_perm with such fields. |
| 106 | glibc-1.09 has no support for sysv ipc. |
| 107 | glibc 2 uses __key, __seq */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 108 | #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1 |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 109 | #define KEY __key |
| 110 | #else |
| 111 | #define KEY key |
| 112 | #endif |
| 113 | |
| 114 | #define LIMITS 1 |
| 115 | #define STATUS 2 |
| 116 | #define CREATOR 3 |
| 117 | #define TIME 4 |
| 118 | #define PID 5 |
| 119 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 120 | static char format; |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 121 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 122 | static void print_perms(int id, struct ipc_perm *ipcp) |
| 123 | { |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 124 | struct passwd *pw; |
| 125 | struct group *gr; |
| 126 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 127 | printf("%-10d %-10o", id, ipcp->mode & 0777); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 1acdc89 | 2007-09-27 10:17:53 +0000 | [diff] [blame] | 129 | pw = getpwuid(ipcp->cuid); |
| 130 | if (pw) printf(" %-10s", pw->pw_name); |
| 131 | else printf(" %-10d", ipcp->cuid); |
| 132 | gr = getgrgid(ipcp->cgid); |
| 133 | if (gr) printf(" %-10s", gr->gr_name); |
| 134 | else printf(" %-10d", ipcp->cgid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 135 | |
Denis Vlasenko | 1acdc89 | 2007-09-27 10:17:53 +0000 | [diff] [blame] | 136 | pw = getpwuid(ipcp->uid); |
| 137 | if (pw) printf(" %-10s", pw->pw_name); |
| 138 | else printf(" %-10d", ipcp->uid); |
| 139 | gr = getgrgid(ipcp->gid); |
| 140 | if (gr) printf(" %-10s\n", gr->gr_name); |
| 141 | else printf(" %-10d\n", ipcp->gid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
Denys Vlasenko | a7bb3c1 | 2009-10-08 12:28:08 +0200 | [diff] [blame] | 145 | static NOINLINE void do_shm(void) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 146 | { |
| 147 | int maxid, shmid, id; |
| 148 | struct shmid_ds shmseg; |
| 149 | struct shm_info shm_info; |
| 150 | struct shminfo shminfo; |
| 151 | struct ipc_perm *ipcp = &shmseg.shm_perm; |
| 152 | struct passwd *pw; |
| 153 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 154 | maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 155 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 156 | printf("kernel not configured for %s\n", "shared memory"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
| 160 | switch (format) { |
| 161 | case LIMITS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 162 | printf("------ Shared Memory %s --------\n", "Limits"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 163 | if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 164 | return; |
| 165 | /* 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] | 166 | * of struct shminfo; glibc 2.1.91 has unsigned long; ach */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 167 | printf("max number of segments = %lu\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 168 | "max seg size (kbytes) = %lu\n" |
| 169 | "max total shared memory (pages) = %lu\n" |
| 170 | "min seg size (bytes) = %lu\n", |
| 171 | (unsigned long) shminfo.shmmni, |
| 172 | (unsigned long) (shminfo.shmmax >> 10), |
| 173 | (unsigned long) shminfo.shmall, |
| 174 | (unsigned long) shminfo.shmmin); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 175 | return; |
| 176 | |
| 177 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 178 | printf("------ Shared Memory %s --------\n", "Status"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 179 | printf("segments allocated %d\n" |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 180 | "pages allocated %lu\n" |
| 181 | "pages resident %lu\n" |
| 182 | "pages swapped %lu\n" |
| 183 | "Swap performance: %lu attempts\t%lu successes\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 184 | shm_info.used_ids, |
| 185 | shm_info.shm_tot, |
| 186 | shm_info.shm_rss, |
| 187 | shm_info.shm_swp, |
| 188 | shm_info.swap_attempts, shm_info.swap_successes); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 189 | return; |
| 190 | |
| 191 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 192 | printf("------ Shared Memory %s --------\n", "Segment Creators/Owners"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 193 | printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", |
| 194 | "shmid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 195 | break; |
| 196 | |
| 197 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 198 | printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 199 | printf("%-10s %-10s %-20s %-20s %-20s\n", |
| 200 | "shmid", "owner", "attached", "detached", "changed"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 201 | break; |
| 202 | |
| 203 | case PID: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 204 | printf("------ Shared Memory %s --------\n", "Creator/Last-op"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 205 | printf("%-10s %-10s %-10s %-10s\n", |
| 206 | "shmid", "owner", "cpid", "lpid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 210 | printf("------ Shared Memory %s --------\n", "Segments"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 211 | printf("%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n", |
| 212 | "key", "shmid", "owner", "perms", "bytes", "nattch", |
| 213 | "status"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | |
| 217 | for (id = 0; id <= maxid; id++) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 218 | shmid = shmctl(id, SHM_STAT, &shmseg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 219 | if (shmid < 0) |
| 220 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 221 | if (format == CREATOR) { |
| 222 | print_perms(shmid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 223 | continue; |
| 224 | } |
| 225 | pw = getpwuid(ipcp->uid); |
| 226 | switch (format) { |
| 227 | case TIME: |
| 228 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 229 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 230 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 231 | printf("%-10d %-10d", shmid, ipcp->uid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 232 | /* ctime uses static buffer: use separate calls */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 233 | printf(" %-20.16s", shmseg.shm_atime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 234 | ? ctime(&shmseg.shm_atime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 235 | printf(" %-20.16s", shmseg.shm_dtime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 236 | ? ctime(&shmseg.shm_dtime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 237 | printf(" %-20.16s\n", shmseg.shm_ctime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 238 | ? ctime(&shmseg.shm_ctime) + 4 : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 239 | break; |
| 240 | case PID: |
| 241 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 242 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 243 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 244 | printf("%-10d %-10d", shmid, ipcp->uid); |
| 245 | printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 246 | break; |
| 247 | |
| 248 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 249 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 250 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 251 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 252 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 253 | printf("%-10d %-10d", shmid, ipcp->uid); |
| 254 | printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 255 | /* |
| 256 | * earlier: int, Austin has size_t |
| 257 | */ |
| 258 | (unsigned long) shmseg.shm_segsz, |
| 259 | /* |
| 260 | * glibc-2.1.3 and earlier has unsigned short; |
| 261 | * Austin has shmatt_t |
| 262 | */ |
| 263 | (long) shmseg.shm_nattch, |
| 264 | ipcp->mode & SHM_DEST ? "dest" : " ", |
| 265 | ipcp->mode & SHM_LOCKED ? "locked" : " "); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | } |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 272 | static NOINLINE void do_sem(void) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 273 | { |
| 274 | int maxid, semid, id; |
| 275 | struct semid_ds semary; |
| 276 | struct seminfo seminfo; |
| 277 | struct ipc_perm *ipcp = &semary.sem_perm; |
| 278 | struct passwd *pw; |
| 279 | union semun arg; |
| 280 | |
Dan Fandrich | f3854f7 | 2011-06-03 20:25:17 +0200 | [diff] [blame] | 281 | arg.array = (unsigned short *) (void *) &seminfo; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 282 | maxid = semctl(0, 0, SEM_INFO, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 283 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 284 | printf("kernel not configured for %s\n", "semaphores"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | |
| 288 | switch (format) { |
| 289 | case LIMITS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 290 | printf("------ Semaphore %s --------\n", "Limits"); |
Dan Fandrich | f3854f7 | 2011-06-03 20:25:17 +0200 | [diff] [blame] | 291 | arg.array = (unsigned short *) (void *) &seminfo; /* damn union */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 292 | if ((semctl(0, 0, IPC_INFO, arg)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 293 | return; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 294 | printf("max number of arrays = %d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 295 | "max semaphores per array = %d\n" |
| 296 | "max semaphores system wide = %d\n" |
| 297 | "max ops per semop call = %d\n" |
| 298 | "semaphore max value = %d\n", |
| 299 | seminfo.semmni, |
| 300 | seminfo.semmsl, |
| 301 | seminfo.semmns, seminfo.semopm, seminfo.semvmx); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 302 | return; |
| 303 | |
| 304 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 305 | printf("------ Semaphore %s --------\n", "Status"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 306 | printf("used arrays = %d\n" |
| 307 | "allocated semaphores = %d\n", |
| 308 | seminfo.semusz, seminfo.semaem); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 309 | return; |
| 310 | |
| 311 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 312 | printf("------ Semaphore %s --------\n", "Arrays Creators/Owners"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 313 | printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", |
| 314 | "semid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 315 | break; |
| 316 | |
| 317 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 318 | printf("------ Shared Memory %s --------\n", "Operation/Change Times"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 319 | printf("%-8s %-10s %-26.24s %-26.24s\n", |
| 320 | "shmid", "owner", "last-op", "last-changed"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | case PID: |
| 324 | break; |
| 325 | |
| 326 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 327 | printf("------ Semaphore %s --------\n", "Arrays"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 328 | printf("%-10s %-10s %-10s %-10s %-10s\n", |
| 329 | "key", "semid", "owner", "perms", "nsems"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | |
| 333 | for (id = 0; id <= maxid; id++) { |
| 334 | arg.buf = (struct semid_ds *) &semary; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 335 | semid = semctl(id, 0, SEM_STAT, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 336 | if (semid < 0) |
| 337 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 338 | if (format == CREATOR) { |
| 339 | print_perms(semid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 340 | continue; |
| 341 | } |
| 342 | pw = getpwuid(ipcp->uid); |
| 343 | switch (format) { |
| 344 | case TIME: |
| 345 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 346 | printf("%-8d %-10.10s", semid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 347 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 348 | printf("%-8d %-10d", semid, ipcp->uid); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 349 | /* ctime uses static buffer: use separate calls */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 350 | printf(" %-26.24s", semary.sem_otime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 351 | ? ctime(&semary.sem_otime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 352 | printf(" %-26.24s\n", semary.sem_ctime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 353 | ? ctime(&semary.sem_ctime) : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 354 | break; |
| 355 | case PID: |
| 356 | break; |
| 357 | |
| 358 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 359 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 360 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 361 | printf("%-10d %-10.9s", semid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 362 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 363 | printf("%-10d %-9d", semid, ipcp->uid); |
| 364 | printf(" %-10o %-10ld\n", ipcp->mode & 0777, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 365 | /* |
| 366 | * glibc-2.1.3 and earlier has unsigned short; |
| 367 | * glibc-2.1.91 has variation between |
| 368 | * unsigned short and unsigned long |
| 369 | * Austin prescribes unsigned short. |
| 370 | */ |
| 371 | (long) semary.sem_nsems); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 378 | static NOINLINE void do_msg(void) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 379 | { |
| 380 | int maxid, msqid, id; |
| 381 | struct msqid_ds msgque; |
| 382 | struct msginfo msginfo; |
| 383 | struct ipc_perm *ipcp = &msgque.msg_perm; |
| 384 | struct passwd *pw; |
| 385 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 386 | maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 387 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 388 | printf("kernel not configured for %s\n", "message queues"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 389 | return; |
| 390 | } |
| 391 | |
| 392 | switch (format) { |
| 393 | case LIMITS: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 394 | if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 395 | return; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 396 | printf("------ Message%s --------\n", "s: Limits"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 397 | printf("max queues system wide = %d\n" |
| 398 | "max size of message (bytes) = %d\n" |
| 399 | "default max size of queue (bytes) = %d\n", |
| 400 | msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 401 | return; |
| 402 | |
| 403 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 404 | printf("------ Message%s --------\n", "s: Status"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 405 | printf("allocated queues = %d\n" |
| 406 | "used headers = %d\n" |
| 407 | "used space = %d bytes\n", |
| 408 | msginfo.msgpool, msginfo.msgmap, msginfo.msgtql); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 409 | return; |
| 410 | |
| 411 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 412 | printf("------ Message%s --------\n", " Queues: Creators/Owners"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 413 | printf("%-10s %-10s %-10s %-10s %-10s %-10s\n", |
| 414 | "msqid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 415 | break; |
| 416 | |
| 417 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 418 | printf("------ Message%s --------\n", " Queues Send/Recv/Change Times"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 419 | printf("%-8s %-10s %-20s %-20s %-20s\n", |
| 420 | "msqid", "owner", "send", "recv", "change"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 421 | break; |
| 422 | |
| 423 | case PID: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 424 | printf("------ Message%s --------\n", " Queues PIDs"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 425 | printf("%-10s %-10s %-10s %-10s\n", |
| 426 | "msqid", "owner", "lspid", "lrpid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 430 | printf("------ Message%s --------\n", " Queues"); |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 431 | printf("%-10s %-10s %-10s %-10s %-12s %-12s\n", |
| 432 | "key", "msqid", "owner", "perms", "used-bytes", "messages"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | |
| 436 | for (id = 0; id <= maxid; id++) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 437 | msqid = msgctl(id, MSG_STAT, &msgque); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 438 | if (msqid < 0) |
| 439 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 440 | if (format == CREATOR) { |
| 441 | print_perms(msqid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 442 | continue; |
| 443 | } |
| 444 | pw = getpwuid(ipcp->uid); |
| 445 | switch (format) { |
| 446 | case TIME: |
| 447 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 448 | printf("%-8d %-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("%-8d %-10d", msqid, ipcp->uid); |
| 451 | printf(" %-20.16s", msgque.msg_stime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 452 | ? ctime(&msgque.msg_stime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 453 | printf(" %-20.16s", msgque.msg_rtime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 454 | ? ctime(&msgque.msg_rtime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 455 | printf(" %-20.16s\n", msgque.msg_ctime |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 456 | ? ctime(&msgque.msg_ctime) + 4 : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 457 | break; |
| 458 | case PID: |
| 459 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 460 | printf("%-8d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 461 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 462 | printf("%-8d %-10d", msqid, ipcp->uid); |
| 463 | printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 464 | break; |
| 465 | |
| 466 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 467 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 468 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 469 | printf("%-10d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 470 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 471 | printf("%-10d %-10d", msqid, ipcp->uid); |
| 472 | printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777, |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 473 | /* |
| 474 | * glibc-2.1.3 and earlier has unsigned short; |
| 475 | * glibc-2.1.91 has variation between |
| 476 | * unsigned short, unsigned long |
| 477 | * Austin has msgqnum_t |
| 478 | */ |
| 479 | (long) msgque.msg_cbytes, (long) msgque.msg_qnum); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | } |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 486 | static void print_shm(int shmid) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 487 | { |
| 488 | struct shmid_ds shmds; |
| 489 | struct ipc_perm *ipcp = &shmds.shm_perm; |
| 490 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 491 | if (shmctl(shmid, IPC_STAT, &shmds) == -1) { |
| 492 | bb_perror_msg("shmctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 493 | return; |
| 494 | } |
| 495 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 496 | printf("\nShared memory Segment shmid=%d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 497 | "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n" |
| 498 | "mode=%#o\taccess_perms=%#o\n" |
| 499 | "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n", |
| 500 | shmid, |
| 501 | ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, |
| 502 | ipcp->mode, ipcp->mode & 0777, |
| 503 | (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid, |
| 504 | (long) shmds.shm_nattch); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 505 | printf("att_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 506 | shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 507 | printf("det_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 508 | shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 509 | printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime)); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 513 | static void print_msg(int msqid) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 514 | { |
| 515 | struct msqid_ds buf; |
| 516 | struct ipc_perm *ipcp = &buf.msg_perm; |
| 517 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 518 | if (msgctl(msqid, IPC_STAT, &buf) == -1) { |
| 519 | bb_perror_msg("msgctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 520 | return; |
| 521 | } |
| 522 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 523 | printf("\nMessage Queue msqid=%d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 524 | "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n" |
| 525 | "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n", |
| 526 | msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode, |
| 527 | /* |
| 528 | * glibc-2.1.3 and earlier has unsigned short; |
| 529 | * glibc-2.1.91 has variation between |
| 530 | * unsigned short, unsigned long |
| 531 | * Austin has msgqnum_t (for msg_qbytes) |
| 532 | */ |
| 533 | (long) buf.msg_cbytes, (long) buf.msg_qbytes, |
| 534 | (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 535 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 536 | printf("send_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 537 | buf.msg_stime ? ctime(&buf.msg_stime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 538 | printf("rcv_time=%-26.24s\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 539 | buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 540 | printf("change_time=%-26.24s\n\n", |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 541 | buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 544 | static void print_sem(int semid) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 545 | { |
| 546 | struct semid_ds semds; |
| 547 | struct ipc_perm *ipcp = &semds.sem_perm; |
| 548 | union semun arg; |
| 549 | unsigned int i; |
| 550 | |
| 551 | arg.buf = &semds; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 552 | if (semctl(semid, 0, IPC_STAT, arg)) { |
| 553 | bb_perror_msg("semctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 554 | return; |
| 555 | } |
| 556 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 557 | printf("\nSemaphore Array semid=%d\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 558 | "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n" |
| 559 | "mode=%#o, access_perms=%#o\n" |
| 560 | "nsems = %ld\n" |
| 561 | "otime = %-26.24s\n", |
| 562 | semid, |
| 563 | ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, |
| 564 | ipcp->mode, ipcp->mode & 0777, |
| 565 | (long) semds.sem_nsems, |
| 566 | semds.sem_otime ? ctime(&semds.sem_otime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 567 | printf("ctime = %-26.24s\n" |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 568 | "%-10s %-10s %-10s %-10s %-10s\n", |
| 569 | ctime(&semds.sem_ctime), |
| 570 | "semnum", "value", "ncount", "zcount", "pid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 571 | |
| 572 | arg.val = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 573 | for (i = 0; i < semds.sem_nsems; i++) { |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 574 | int val, ncnt, zcnt, pid; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 575 | |
| 576 | val = semctl(semid, i, GETVAL, arg); |
| 577 | ncnt = semctl(semid, i, GETNCNT, arg); |
| 578 | zcnt = semctl(semid, i, GETZCNT, arg); |
| 579 | pid = semctl(semid, i, GETPID, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 580 | if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 581 | bb_perror_msg_and_die("semctl"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 582 | } |
Denys Vlasenko | 327f550 | 2013-11-29 16:45:45 +0100 | [diff] [blame] | 583 | 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] | 584 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 585 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 586 | } |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 587 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 588 | int ipcs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 589 | int ipcs_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 590 | { |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 591 | int id = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 592 | unsigned flags = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 593 | unsigned opt; |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 594 | char *opt_i; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 595 | #define flag_print (1<<0) |
| 596 | #define flag_msg (1<<1) |
| 597 | #define flag_sem (1<<2) |
| 598 | #define flag_shm (1<<3) |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 599 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 600 | opt = getopt32(argv, "i:aqsmtcplu", &opt_i); |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 601 | if (opt & 0x1) { // -i |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 602 | id = xatoi(opt_i); |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 603 | flags |= flag_print; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 604 | } |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 605 | if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a |
| 606 | if (opt & 0x4) flags |= flag_msg; // -q |
| 607 | if (opt & 0x8) flags |= flag_sem; // -s |
| 608 | if (opt & 0x10) flags |= flag_shm; // -m |
| 609 | if (opt & 0x20) format = TIME; // -t |
| 610 | if (opt & 0x40) format = CREATOR; // -c |
| 611 | if (opt & 0x80) format = PID; // -p |
| 612 | if (opt & 0x100) format = LIMITS; // -l |
| 613 | if (opt & 0x200) format = STATUS; // -u |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 614 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 615 | if (flags & flag_print) { |
| 616 | if (flags & flag_shm) { |
| 617 | print_shm(id); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 618 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 619 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 620 | if (flags & flag_sem) { |
| 621 | print_sem(id); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 622 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 623 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 624 | if (flags & flag_msg) { |
| 625 | print_msg(id); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 626 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 627 | } |
| 628 | bb_show_usage(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 631 | if (!(flags & (flag_shm | flag_msg | flag_sem))) |
| 632 | flags |= flag_msg | flag_shm | flag_sem; |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 633 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 634 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 635 | if (flags & flag_shm) { |
| 636 | do_shm(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 637 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 638 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 639 | if (flags & flag_sem) { |
| 640 | do_sem(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 641 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 642 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 643 | if (flags & flag_msg) { |
| 644 | do_msg(); |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 645 | bb_putchar('\n'); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 646 | } |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 647 | fflush_stdout_and_exit(EXIT_SUCCESS); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 648 | } |