"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 | * |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 11 | /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */ |
| 12 | /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */ |
| 13 | /* X/OPEN tells us to use <sys/{types,ipc,shm}.h> for shmctl() */ |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/ipc.h> |
| 16 | #include <sys/sem.h> |
| 17 | #include <sys/msg.h> |
| 18 | #include <sys/shm.h> |
| 19 | |
Denis Vlasenko | fad2b86 | 2007-05-31 22:16:38 +0000 | [diff] [blame] | 20 | #include "libbb.h" |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 21 | |
| 22 | /*-------------------------------------------------------------------*/ |
| 23 | /* SHM_DEST and SHM_LOCKED are defined in kernel headers, |
| 24 | but inside #ifdef __KERNEL__ ... #endif */ |
| 25 | #ifndef SHM_DEST |
| 26 | /* shm_mode upper byte flags */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 27 | #define SHM_DEST 01000 /* segment will be destroyed on last detach */ |
| 28 | #define SHM_LOCKED 02000 /* segment will not be swapped */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | /* For older kernels the same holds for the defines below */ |
| 32 | #ifndef MSG_STAT |
| 33 | #define MSG_STAT 11 |
| 34 | #define MSG_INFO 12 |
| 35 | #endif |
| 36 | |
| 37 | #ifndef SHM_STAT |
| 38 | #define SHM_STAT 13 |
| 39 | #define SHM_INFO 14 |
| 40 | struct shm_info { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 41 | int used_ids; |
| 42 | ulong shm_tot; /* total allocated shm */ |
| 43 | ulong shm_rss; /* total resident shm */ |
| 44 | ulong shm_swp; /* total swapped shm */ |
| 45 | ulong swap_attempts; |
| 46 | ulong swap_successes; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 47 | }; |
| 48 | #endif |
| 49 | |
| 50 | #ifndef SEM_STAT |
| 51 | #define SEM_STAT 18 |
| 52 | #define SEM_INFO 19 |
| 53 | #endif |
| 54 | |
| 55 | /* Some versions of libc only define IPC_INFO when __USE_GNU is defined. */ |
| 56 | #ifndef IPC_INFO |
| 57 | #define IPC_INFO 3 |
| 58 | #endif |
| 59 | /*-------------------------------------------------------------------*/ |
| 60 | |
| 61 | /* The last arg of semctl is a union semun, but where is it defined? |
| 62 | X/OPEN tells us to define it ourselves, but until recently |
| 63 | Linux include files would also define it. */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 64 | #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 65 | /* union semun is defined by including <sys/sem.h> */ |
| 66 | #else |
| 67 | /* according to X/OPEN we have to define it ourselves */ |
| 68 | union semun { |
| 69 | int val; |
| 70 | struct semid_ds *buf; |
| 71 | unsigned short int *array; |
| 72 | struct seminfo *__buf; |
| 73 | }; |
| 74 | #endif |
| 75 | |
| 76 | /* X/OPEN (Jan 1987) does not define fields key, seq in struct ipc_perm; |
| 77 | libc 4/5 does not mention struct ipc_term at all, but includes |
| 78 | <linux/ipc.h>, which defines a struct ipc_perm with such fields. |
| 79 | glibc-1.09 has no support for sysv ipc. |
| 80 | glibc 2 uses __key, __seq */ |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 81 | #if defined(__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1 |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 82 | #define KEY __key |
| 83 | #else |
| 84 | #define KEY key |
| 85 | #endif |
| 86 | |
| 87 | #define LIMITS 1 |
| 88 | #define STATUS 2 |
| 89 | #define CREATOR 3 |
| 90 | #define TIME 4 |
| 91 | #define PID 5 |
| 92 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 93 | static char format; |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 94 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 95 | static void print_perms(int id, struct ipc_perm *ipcp) |
| 96 | { |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 97 | struct passwd *pw; |
| 98 | struct group *gr; |
| 99 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 100 | printf("%-10d %-10o", id, ipcp->mode & 0777); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 101 | |
| 102 | if ((pw = getpwuid(ipcp->cuid))) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 103 | printf(" %-10s", pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 104 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 105 | printf(" %-10d", ipcp->cuid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 106 | if ((gr = getgrgid(ipcp->cgid))) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 107 | printf(" %-10s", gr->gr_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 108 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 109 | printf(" %-10d", ipcp->cgid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 110 | |
| 111 | if ((pw = getpwuid(ipcp->uid))) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 112 | printf(" %-10s", pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 113 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 114 | printf(" %-10d", ipcp->uid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 115 | if ((gr = getgrgid(ipcp->gid))) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 116 | printf(" %-10s\n", gr->gr_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 117 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 118 | printf(" %-10d\n", ipcp->gid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 122 | static void do_shm(void) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 123 | { |
| 124 | int maxid, shmid, id; |
| 125 | struct shmid_ds shmseg; |
| 126 | struct shm_info shm_info; |
| 127 | struct shminfo shminfo; |
| 128 | struct ipc_perm *ipcp = &shmseg.shm_perm; |
| 129 | struct passwd *pw; |
| 130 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 131 | maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 132 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 133 | printf("kernel not configured for %s\n", "shared memory"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 134 | return; |
| 135 | } |
| 136 | |
| 137 | switch (format) { |
| 138 | case LIMITS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 139 | printf("------ Shared Memory %s --------\n", "Limits"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 140 | if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 141 | return; |
| 142 | /* glibc 2.1.3 and all earlier libc's have ints as fields |
| 143 | of struct shminfo; glibc 2.1.91 has unsigned long; ach */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 144 | printf("max number of segments = %lu\n" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 145 | "max seg size (kbytes) = %lu\n" |
| 146 | "max total shared memory (pages) = %lu\n" |
| 147 | "min seg size (bytes) = %lu\n", |
| 148 | (unsigned long) shminfo.shmmni, |
| 149 | (unsigned long) (shminfo.shmmax >> 10), |
| 150 | (unsigned long) shminfo.shmall, |
| 151 | (unsigned long) shminfo.shmmin); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 152 | return; |
| 153 | |
| 154 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 155 | printf("------ Shared Memory %s --------\n", "Status"); |
| 156 | printf( "segments allocated %d\n" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 157 | "pages allocated %ld\n" |
| 158 | "pages resident %ld\n" |
| 159 | "pages swapped %ld\n" |
Denis Vlasenko | 5983914 | 2006-09-10 18:26:51 +0000 | [diff] [blame] | 160 | "Swap performance: %ld attempts\t%ld successes\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 161 | shm_info.used_ids, |
| 162 | shm_info.shm_tot, |
| 163 | shm_info.shm_rss, |
| 164 | shm_info.shm_swp, |
| 165 | shm_info.swap_attempts, shm_info.swap_successes); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 166 | return; |
| 167 | |
| 168 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 169 | printf("------ Shared Memory %s --------\n", "Segment Creators/Owners"); |
| 170 | printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 171 | "shmid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 172 | break; |
| 173 | |
| 174 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 175 | printf("------ Shared Memory %s --------\n", "Attach/Detach/Change Times"); |
| 176 | printf( "%-10s %-10s %-20s %-20s %-20s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 177 | "shmid", "owner", "attached", "detached", "changed"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 178 | break; |
| 179 | |
| 180 | case PID: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 181 | printf("------ Shared Memory %s --------\n", "Creator/Last-op"); |
| 182 | printf( "%-10s %-10s %-10s %-10s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 183 | "shmid", "owner", "cpid", "lpid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 184 | break; |
| 185 | |
| 186 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 187 | printf("------ Shared Memory %s --------\n", "Segments"); |
| 188 | printf( "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 189 | "key", "shmid", "owner", "perms", "bytes", "nattch", |
| 190 | "status"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | |
| 194 | for (id = 0; id <= maxid; id++) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 195 | shmid = shmctl(id, SHM_STAT, &shmseg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 196 | if (shmid < 0) |
| 197 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 198 | if (format == CREATOR) { |
| 199 | print_perms(shmid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 200 | continue; |
| 201 | } |
| 202 | pw = getpwuid(ipcp->uid); |
| 203 | switch (format) { |
| 204 | case TIME: |
| 205 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 206 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 207 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 208 | printf("%-10d %-10d", shmid, ipcp->uid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 209 | /* ctime uses static buffer: use separate calls */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 210 | printf(" %-20.16s", shmseg.shm_atime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 211 | ? ctime(&shmseg.shm_atime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 212 | printf(" %-20.16s", shmseg.shm_dtime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 213 | ? ctime(&shmseg.shm_dtime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 214 | printf(" %-20.16s\n", shmseg.shm_ctime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 215 | ? ctime(&shmseg.shm_ctime) + 4 : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 216 | break; |
| 217 | case PID: |
| 218 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 219 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 220 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 221 | printf("%-10d %-10d", shmid, ipcp->uid); |
| 222 | printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 223 | break; |
| 224 | |
| 225 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 226 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 227 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 228 | printf("%-10d %-10.10s", shmid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 229 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 230 | printf("%-10d %-10d", shmid, ipcp->uid); |
| 231 | printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777, |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 232 | /* |
| 233 | * earlier: int, Austin has size_t |
| 234 | */ |
| 235 | (unsigned long) shmseg.shm_segsz, |
| 236 | /* |
| 237 | * glibc-2.1.3 and earlier has unsigned short; |
| 238 | * Austin has shmatt_t |
| 239 | */ |
| 240 | (long) shmseg.shm_nattch, |
| 241 | ipcp->mode & SHM_DEST ? "dest" : " ", |
| 242 | ipcp->mode & SHM_LOCKED ? "locked" : " "); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | } |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 249 | static void do_sem(void) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 250 | { |
| 251 | int maxid, semid, id; |
| 252 | struct semid_ds semary; |
| 253 | struct seminfo seminfo; |
| 254 | struct ipc_perm *ipcp = &semary.sem_perm; |
| 255 | struct passwd *pw; |
| 256 | union semun arg; |
| 257 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 258 | arg.array = (ushort *) (void *) &seminfo; |
| 259 | maxid = semctl(0, 0, SEM_INFO, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 260 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 261 | printf("kernel not configured for %s\n", "semaphores"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 262 | return; |
| 263 | } |
| 264 | |
| 265 | switch (format) { |
| 266 | case LIMITS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 267 | printf("------ Semaphore %s --------\n", "Limits"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 268 | arg.array = (ushort *) (void *) &seminfo; /* damn union */ |
| 269 | if ((semctl(0, 0, IPC_INFO, arg)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 270 | return; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 271 | printf("max number of arrays = %d\n" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 272 | "max semaphores per array = %d\n" |
| 273 | "max semaphores system wide = %d\n" |
| 274 | "max ops per semop call = %d\n" |
| 275 | "semaphore max value = %d\n", |
| 276 | seminfo.semmni, |
| 277 | seminfo.semmsl, |
| 278 | seminfo.semmns, seminfo.semopm, seminfo.semvmx); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 279 | return; |
| 280 | |
| 281 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 282 | printf("------ Semaphore %s --------\n", "Status"); |
| 283 | printf( "used arrays = %d\n" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 284 | "allocated semaphores = %d\n", |
| 285 | seminfo.semusz, seminfo.semaem); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 286 | return; |
| 287 | |
| 288 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 289 | printf("------ Semaphore %s --------\n", "Arrays Creators/Owners"); |
| 290 | printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 291 | "semid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 292 | break; |
| 293 | |
| 294 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 295 | printf("------ Shared Memory %s --------\n", "Operation/Change Times"); |
| 296 | printf( "%-8s %-10s %-26.24s %-26.24s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 297 | "shmid", "owner", "last-op", "last-changed"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 298 | break; |
| 299 | |
| 300 | case PID: |
| 301 | break; |
| 302 | |
| 303 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 304 | printf("------ Semaphore %s --------\n", "Arrays"); |
| 305 | printf( "%-10s %-10s %-10s %-10s %-10s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 306 | "key", "semid", "owner", "perms", "nsems"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 307 | break; |
| 308 | } |
| 309 | |
| 310 | for (id = 0; id <= maxid; id++) { |
| 311 | arg.buf = (struct semid_ds *) &semary; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 312 | semid = semctl(id, 0, SEM_STAT, arg); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 313 | if (semid < 0) |
| 314 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 315 | if (format == CREATOR) { |
| 316 | print_perms(semid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 317 | continue; |
| 318 | } |
| 319 | pw = getpwuid(ipcp->uid); |
| 320 | switch (format) { |
| 321 | case TIME: |
| 322 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 323 | printf("%-8d %-10.10s", semid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 324 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 325 | printf("%-8d %-10d", semid, ipcp->uid); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 326 | /* ctime uses static buffer: use separate calls */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 327 | printf(" %-26.24s", semary.sem_otime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 328 | ? ctime(&semary.sem_otime) : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 329 | printf(" %-26.24s\n", semary.sem_ctime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 330 | ? ctime(&semary.sem_ctime) : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 331 | break; |
| 332 | case PID: |
| 333 | break; |
| 334 | |
| 335 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 336 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 337 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 338 | printf("%-10d %-10.9s", semid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 339 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 340 | printf("%-10d %-9d", semid, ipcp->uid); |
| 341 | printf(" %-10o %-10ld\n", ipcp->mode & 0777, |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 342 | /* |
| 343 | * glibc-2.1.3 and earlier has unsigned short; |
| 344 | * glibc-2.1.91 has variation between |
| 345 | * unsigned short and unsigned long |
| 346 | * Austin prescribes unsigned short. |
| 347 | */ |
| 348 | (long) semary.sem_nsems); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 355 | static void do_msg(void) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 356 | { |
| 357 | int maxid, msqid, id; |
| 358 | struct msqid_ds msgque; |
| 359 | struct msginfo msginfo; |
| 360 | struct ipc_perm *ipcp = &msgque.msg_perm; |
| 361 | struct passwd *pw; |
| 362 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 363 | maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 364 | if (maxid < 0) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 365 | printf("kernel not configured for %s\n", "message queues"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | |
| 369 | switch (format) { |
| 370 | case LIMITS: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 371 | if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0) |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 372 | return; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 373 | printf("------ Message%s --------\n", "s: Limits"); |
| 374 | printf( "max queues system wide = %d\n" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 375 | "max size of message (bytes) = %d\n" |
| 376 | "default max size of queue (bytes) = %d\n", |
| 377 | msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 378 | return; |
| 379 | |
| 380 | case STATUS: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 381 | printf("------ Message%s --------\n", "s: Status"); |
| 382 | printf( "allocated queues = %d\n" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 383 | "used headers = %d\n" |
| 384 | "used space = %d bytes\n", |
| 385 | msginfo.msgpool, msginfo.msgmap, msginfo.msgtql); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 386 | return; |
| 387 | |
| 388 | case CREATOR: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 389 | printf("------ Message%s --------\n", " Queues: Creators/Owners"); |
| 390 | printf( "%-10s %-10s %-10s %-10s %-10s %-10s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 391 | "msqid", "perms", "cuid", "cgid", "uid", "gid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case TIME: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 395 | printf("------ Message%s --------\n", " Queues Send/Recv/Change Times"); |
| 396 | printf( "%-8s %-10s %-20s %-20s %-20s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 397 | "msqid", "owner", "send", "recv", "change"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 398 | break; |
| 399 | |
| 400 | case PID: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 401 | printf("------ Message%s --------\n", " Queues PIDs"); |
| 402 | printf( "%-10s %-10s %-10s %-10s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 403 | "msqid", "owner", "lspid", "lrpid"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 404 | break; |
| 405 | |
| 406 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 407 | printf("------ Message%s --------\n", " Queues"); |
| 408 | printf( "%-10s %-10s %-10s %-10s %-12s %-12s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 409 | "key", "msqid", "owner", "perms", "used-bytes", "messages"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | |
| 413 | for (id = 0; id <= maxid; id++) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 414 | msqid = msgctl(id, MSG_STAT, &msgque); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 415 | if (msqid < 0) |
| 416 | continue; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 417 | if (format == CREATOR) { |
| 418 | print_perms(msqid, ipcp); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 419 | continue; |
| 420 | } |
| 421 | pw = getpwuid(ipcp->uid); |
| 422 | switch (format) { |
| 423 | case TIME: |
| 424 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 425 | printf("%-8d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 426 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 427 | printf("%-8d %-10d", msqid, ipcp->uid); |
| 428 | printf(" %-20.16s", msgque.msg_stime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 429 | ? ctime(&msgque.msg_stime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 430 | printf(" %-20.16s", msgque.msg_rtime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 431 | ? ctime(&msgque.msg_rtime) + 4 : "Not set"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 432 | printf(" %-20.16s\n", msgque.msg_ctime |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 433 | ? ctime(&msgque.msg_ctime) + 4 : "Not set"); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 434 | break; |
| 435 | case PID: |
| 436 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 437 | printf("%-8d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 438 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 439 | printf("%-8d %-10d", msqid, ipcp->uid); |
| 440 | printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 441 | break; |
| 442 | |
| 443 | default: |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 444 | printf("0x%08x ", ipcp->KEY); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 445 | if (pw) |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 446 | printf("%-10d %-10.10s", msqid, pw->pw_name); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 447 | else |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 448 | printf("%-10d %-10d", msqid, ipcp->uid); |
| 449 | printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777, |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 450 | /* |
| 451 | * glibc-2.1.3 and earlier has unsigned short; |
| 452 | * glibc-2.1.91 has variation between |
| 453 | * unsigned short, unsigned long |
| 454 | * Austin has msgqnum_t |
| 455 | */ |
| 456 | (long) msgque.msg_cbytes, (long) msgque.msg_qnum); |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | } |
Bernhard Reutner-Fischer | a38330c | 2006-04-03 11:25:48 +0000 | [diff] [blame] | 460 | } |
| 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) { |
| 469 | bb_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" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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 | |
| 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" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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); |
| 512 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 513 | printf("send_time=%-26.24s\n", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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", |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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" |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [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 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 560 | printf("%-10d %-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 | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 562 | puts(""); |
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 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 565 | int ipcs_main(int argc, char **argv); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 566 | int ipcs_main(int argc, char **argv) |
| 567 | { |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 568 | int id = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 569 | unsigned flags = 0; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 570 | unsigned opt; |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 571 | char *opt_i; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 572 | #define flag_print (1<<0) |
| 573 | #define flag_msg (1<<1) |
| 574 | #define flag_sem (1<<2) |
| 575 | #define flag_shm (1<<3) |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 576 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 577 | opt = getopt32(argv, "i:aqsmtcplu", &opt_i); |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 578 | if (opt & 0x1) { // -i |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 579 | id = xatoi(opt_i); |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 580 | flags |= flag_print; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 581 | } |
Denis Vlasenko | c7b9bad | 2006-09-19 17:40:31 +0000 | [diff] [blame] | 582 | if (opt & 0x2) flags |= flag_msg | flag_sem | flag_shm; // -a |
| 583 | if (opt & 0x4) flags |= flag_msg; // -q |
| 584 | if (opt & 0x8) flags |= flag_sem; // -s |
| 585 | if (opt & 0x10) flags |= flag_shm; // -m |
| 586 | if (opt & 0x20) format = TIME; // -t |
| 587 | if (opt & 0x40) format = CREATOR; // -c |
| 588 | if (opt & 0x80) format = PID; // -p |
| 589 | if (opt & 0x100) format = LIMITS; // -l |
| 590 | if (opt & 0x200) format = STATUS; // -u |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 591 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 592 | if (flags & flag_print) { |
| 593 | if (flags & flag_shm) { |
| 594 | print_shm(id); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 595 | fflush_stdout_and_exit(0); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 596 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 597 | if (flags & flag_sem) { |
| 598 | print_sem(id); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 599 | fflush_stdout_and_exit(0); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 600 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 601 | if (flags & flag_msg) { |
| 602 | print_msg(id); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 603 | fflush_stdout_and_exit(0); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 604 | } |
| 605 | bb_show_usage(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 608 | if (!(flags & (flag_shm | flag_msg | flag_sem))) |
| 609 | flags |= flag_msg | flag_shm | flag_sem; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 610 | puts(""); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 611 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 612 | if (flags & flag_shm) { |
| 613 | do_shm(); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 614 | puts(""); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 615 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 616 | if (flags & flag_sem) { |
| 617 | do_sem(); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 618 | puts(""); |
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_msg) { |
| 621 | do_msg(); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 622 | puts(""); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 623 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 624 | fflush_stdout_and_exit(0); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 625 | } |