aboutsummaryrefslogtreecommitdiff
path: root/util-linux
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2006-08-28 23:31:54 +0000
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2006-08-28 23:31:54 +0000
commit73561cc75ac31e63bcab4b4ebfaf738e4ca225e6 (patch)
tree2a57564b8917a983365c765d4daa83032d9f0a17 /util-linux
parent6ce8dae1d5a201156a83201de6c49cdcc4c8df3e (diff)
downloadbusybox-w32-73561cc75ac31e63bcab4b4ebfaf738e4ca225e6.tar.gz
busybox-w32-73561cc75ac31e63bcab4b4ebfaf738e4ca225e6.tar.bz2
busybox-w32-73561cc75ac31e63bcab4b4ebfaf738e4ca225e6.zip
- pull from busybox_scratch: r15829:15850
Various fixes, cleanups and shrinkage: saves 952 Bytes: text data bss dec hex filename 1087742 15853 790632 1894227 1ce753 ../busybox/busybox.old 1086790 15853 790632 1893275 1ce39b busybox via: # scripts/bloat-o-meter ../busybox/busybox_unstripped.old busybox_unstripped function old new delta ipcrm_main 756 822 +66 getval - 61 +61 maybe_set_utc - 40 +40 udhcpc_main 2896 2912 +16 md5_hash_block 428 437 +9 opt 8 16 +8 qgravechar 106 110 +4 make_bitmap 292 295 +3 inflate_unzip 2056 2059 +3 add_partition 1412 1414 +2 __parsespent 156 158 +2 qrealloc 41 42 +1 format - 1 +1 catv_main 313 314 +1 watch_main 293 292 -1 varunset 81 80 -1 part 1 - -1 check_if_skip 837 836 -1 start_stop_daemon_main 840 837 -3 create_lost_and_found 175 172 -3 supress_non_delimited_lines 4 - -4 static.l 4 - -4 static.c 5 1 -4 bsd_sum_file 237 233 -4 eval2 338 332 -6 arithmetic_common 166 158 -8 cmpfunc 22 5 -17 cksum_main 294 275 -19 cmp_main 465 439 -26 dd_main 1535 1508 -27 rmmod_main 376 333 -43 cut_file 727 644 -83 ipcs_main 3809 3721 -88 cut_main 722 614 -108 date_main 1443 1263 -180 remove_ids 222 - -222 ------------------------------------------------------------------------------ (add/remove: 3/4 grow/shrink: 11/18 up/down: 217/-853) Total: -636 bytes
Diffstat (limited to 'util-linux')
-rw-r--r--util-linux/ipcrm.c216
-rw-r--r--util-linux/ipcs.c584
2 files changed, 363 insertions, 437 deletions
diff --git a/util-linux/ipcrm.c b/util-linux/ipcrm.c
index 2e164b8b3..735a8955b 100644
--- a/util-linux/ipcrm.c
+++ b/util-linux/ipcrm.c
@@ -1,56 +1,22 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * ipcrm.c -- utility to allow removal of IPC objects and data structures. 3 * ipcrm.c - utility to allow removal of IPC objects and data structures.
4 * 4 *
5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com> 5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
6 * Adapted for busybox from util-linux-2.12a. 6 * Adapted for busybox from util-linux-2.12a.
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * --- Pre-busybox history from util-linux-2.12a ------------------------
23 *
24 * 1999-04-02 frank zago
25 * - can now remove several id's in the same call
26 *
27 * 1999-02-22 Arkadiusz Mi˙kiewicz <misiek@pld.ORG.PL>
28 * - added Native Language Support
29 *
30 * Original author - krishna balasubramanian 1993
31 */ 9 */
32 10
33#include <stdio.h> 11#include "busybox.h"
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37 12
38#include <sys/types.h> 13/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
14/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
39#include <sys/ipc.h> 15#include <sys/ipc.h>
40#include <sys/shm.h> 16#include <sys/shm.h>
41#include <sys/msg.h> 17#include <sys/msg.h>
42#include <sys/sem.h> 18#include <sys/sem.h>
43 19
44/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
45/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
46/* for getopt */
47#include <unistd.h>
48
49/* for tolower and isupper */
50#include <ctype.h>
51
52#include "busybox.h"
53
54#if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) 20#if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
55/* union semun is defined by including <sys/sem.h> */ 21/* union semun is defined by including <sys/sem.h> */
56#else 22#else
@@ -63,164 +29,149 @@ union semun {
63}; 29};
64#endif 30#endif
65 31
32#ifndef CONFIG_IPCRM_DROP_LEGACY
33
66typedef enum type_id { 34typedef enum type_id {
67 SHM, 35 SHM,
68 SEM, 36 SEM,
69 MSG 37 MSG
70} type_id; 38} type_id;
71 39
72static int 40static int remove_ids(type_id type, int argc, char **argv)
73remove_ids(type_id type, int argc, char **argv) { 41{
74 int id; 42 int id;
75 int ret = 0; /* for gcc */ 43 int ret = 0; /* silence gcc */
76 char *end; 44 char *end;
77 int nb_errors = 0; 45 int nb_errors = 0;
78 union semun arg; 46 union semun arg;
79 47
80 arg.val = 0; 48 arg.val = 0;
81 49
82 while(argc) { 50 while (argc) {
83 51
84 id = strtoul(argv[0], &end, 10); 52 id = strtoul(argv[0], &end, 10);
85 53
86 if (*end != 0) { 54 if (*end != 0) {
87 bb_printf ("invalid id: %s\n", argv[0]); 55 bb_error_msg("invalid id: %s", argv[0]);
88 nb_errors ++; 56 nb_errors++;
89 } else { 57 } else {
90 switch(type) { 58 if (type == SEM)
91 case SEM: 59 ret = semctl(id, 0, IPC_RMID, arg);
92 ret = semctl (id, 0, IPC_RMID, arg); 60 else if (type == MSG)
93 break; 61 ret = msgctl(id, IPC_RMID, NULL);
94 62 else if (type == SHM)
95 case MSG: 63 ret = shmctl(id, IPC_RMID, NULL);
96 ret = msgctl (id, IPC_RMID, NULL);
97 break;
98
99 case SHM:
100 ret = shmctl (id, IPC_RMID, NULL);
101 break;
102 }
103 64
104 if (ret) { 65 if (ret) {
105 bb_printf ("cannot remove id %s (%s)\n", 66 bb_perror_msg("cannot remove id %s", argv[0]);
106 argv[0], strerror(errno)); 67 nb_errors++;
107 nb_errors ++;
108 } 68 }
109 } 69 }
110 argc--; 70 argc--;
111 argv++; 71 argv++;
112 } 72 }
113 73
114 return(nb_errors); 74 return (nb_errors);
115}
116
117static int deprecated_main(int argc, char **argv)
118{
119 if (argc < 3) {
120 bb_show_usage();
121 bb_fflush_stdout_and_exit(1);
122 }
123
124 if (!strcmp(argv[1], "shm")) {
125 if (remove_ids(SHM, argc-2, &argv[2]))
126 bb_fflush_stdout_and_exit(1);
127 }
128 else if (!strcmp(argv[1], "msg")) {
129 if (remove_ids(MSG, argc-2, &argv[2]))
130 bb_fflush_stdout_and_exit(1);
131 }
132 else if (!strcmp(argv[1], "sem")) {
133 if (remove_ids(SEM, argc-2, &argv[2]))
134 bb_fflush_stdout_and_exit(1);
135 }
136 else {
137 bb_printf ("unknown resource type: %s\n", argv[1]);
138 bb_show_usage();
139 bb_fflush_stdout_and_exit(1);
140 }
141
142 bb_printf ("resource(s) deleted\n");
143 return 0;
144} 75}
76#endif /* #ifndef CONFIG_IPCRM_DROP_LEGACY */
145 77
146 78
147int ipcrm_main(int argc, char **argv) 79int ipcrm_main(int argc, char **argv)
148{ 80{
149 int c; 81 int c;
150 int error = 0; 82 int error = 0;
151 char *prog = argv[0];
152 83
153 /* if the command is executed without parameters, do nothing */ 84 /* if the command is executed without parameters, do nothing */
154 if (argc == 1) 85 if (argc == 1)
155 return 0; 86 return 0;
156 87#ifndef CONFIG_IPCRM_DROP_LEGACY
157 /* check to see if the command is being invoked in the old way if so 88 /* check to see if the command is being invoked in the old way if so
158 then run the old code */ 89 then run the old code. Valid commands are msg, shm, sem. */
159 if (strcmp(argv[1], "shm") == 0 || 90 {
160 strcmp(argv[1], "msg") == 0 || 91 type_id what = 0; /* silence gcc */
161 strcmp(argv[1], "sem") == 0) 92 char w;
162 return deprecated_main(argc, argv); 93
94 if ((((w=argv[1][0]) == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
95 || (argv[1][0] == 's'
96 && ((w=argv[1][1]) == 'h' || w == 'e')
97 && argv[1][2] == 'm'))
98 && argv[1][3] == '\0') {
99
100 if (argc < 3)
101 bb_show_usage();
102
103 if (w == 'h')
104 what = SHM;
105 else if (w == 'm')
106 what = MSG;
107 else if (w == 'e')
108 what = SEM;
109
110 if (remove_ids(what, argc-2, &argv[2]))
111 bb_fflush_stdout_and_exit(1);
112 bb_printf("resource(s) deleted\n");
113 return 0;
114 }
115 }
116#endif /* #ifndef CONFIG_IPCRM_DROP_LEGACY */
163 117
164 /* process new syntax to conform with SYSV ipcrm */ 118 /* process new syntax to conform with SYSV ipcrm */
165 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) { 119 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
166 int result; 120 int result;
167 int id = 0; 121 int id = 0;
168 int iskey = isupper(c); 122 int iskey = (isupper)(c);
169 123
170 /* needed to delete semaphores */ 124 /* needed to delete semaphores */
171 union semun arg; 125 union semun arg;
126
172 arg.val = 0; 127 arg.val = 0;
173 128
174 if ((c == '?') || (c == 'h')) 129 if ((c == '?') || (c == 'h')) {
175 {
176 bb_show_usage(); 130 bb_show_usage();
177 return 0;
178 } 131 }
179 132
180 /* we don't need case information any more */ 133 /* we don't need case information any more */
181 c = tolower(c); 134 c = tolower(c);
182 135
183 /* make sure the option is in range */ 136 /* make sure the option is in range: allowed are q, m, s */
184 if (c != 'q' && c != 'm' && c != 's') { 137 if (c != 'q' && c != 'm' && c != 's') {
185 bb_show_usage(); 138 bb_show_usage();
186 error++;
187 return error;
188 } 139 }
189 140
190 if (iskey) { 141 if (iskey) {
191 /* keys are in hex or decimal */ 142 /* keys are in hex or decimal */
192 key_t key = strtoul(optarg, NULL, 0); 143 key_t key = strtoul(optarg, NULL, 0);
144
193 if (key == IPC_PRIVATE) { 145 if (key == IPC_PRIVATE) {
194 error++; 146 error++;
195 bb_fprintf(stderr, "%s: illegal key (%s)\n", 147 bb_error_msg("illegal key (%s)", optarg);
196 prog, optarg);
197 continue; 148 continue;
198 } 149 }
199 150
200 /* convert key to id */ 151 /* convert key to id */
201 id = ((c == 'q') ? msgget(key, 0) : 152 id = ((c == 'q') ? msgget(key, 0) :
202 (c == 'm') ? shmget(key, 0, 0) : 153 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
203 semget(key, 0, 0));
204 154
205 if (id < 0) { 155 if (id < 0) {
206 char *errmsg; 156 char *errmsg;
157 const char * const what = "key";
158
207 error++; 159 error++;
208 switch(errno) { 160 switch (errno) {
209 case EACCES: 161 case EACCES:
210 errmsg = "permission denied for key"; 162 errmsg = "permission denied for";
211 break; 163 break;
212 case EIDRM: 164 case EIDRM:
213 errmsg = "already removed key"; 165 errmsg = "already removed";
214 break; 166 break;
215 case ENOENT: 167 case ENOENT:
216 errmsg = "invalid key"; 168 errmsg = "invalid";
217 break; 169 break;
218 default: 170 default:
219 errmsg = "unknown error in key"; 171 errmsg = "unknown error in";
220 break; 172 break;
221 } 173 }
222 bb_fprintf(stderr, "%s: %s (%s)\n", 174 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
223 prog, errmsg, optarg);
224 continue; 175 continue;
225 } 176 }
226 } else { 177 } else {
@@ -229,37 +180,30 @@ int ipcrm_main(int argc, char **argv)
229 } 180 }
230 181
231 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) : 182 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
232 (c == 'm') ? shmctl(id, IPC_RMID, NULL) : 183 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
233 semctl(id, 0, IPC_RMID, arg)); 184 semctl(id, 0, IPC_RMID, arg));
234 185
235 if (result < 0) { 186 if (result) {
236 char *errmsg; 187 char *errmsg;
188 const char * const what = iskey ? "key" : "id";
189
237 error++; 190 error++;
238 switch(errno) { 191 switch (errno) {
239 case EACCES: 192 case EACCES:
240 case EPERM: 193 case EPERM:
241 errmsg = iskey 194 errmsg = "permission denied for";
242 ? "permission denied for key"
243 : "permission denied for id";
244 break; 195 break;
245 case EINVAL: 196 case EINVAL:
246 errmsg = iskey 197 errmsg = "invalid";
247 ? "invalid key"
248 : "invalid id";
249 break; 198 break;
250 case EIDRM: 199 case EIDRM:
251 errmsg = iskey 200 errmsg = "already removed";
252 ? "already removed key"
253 : "already removed id";
254 break; 201 break;
255 default: 202 default:
256 errmsg = iskey 203 errmsg = "unknown error in";
257 ? "unknown error in key"
258 : "unknown error in id";
259 break; 204 break;
260 } 205 }
261 bb_fprintf(stderr, "%s: %s (%s)\n", 206 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
262 prog, errmsg, optarg);
263 continue; 207 continue;
264 } 208 }
265 } 209 }
diff --git a/util-linux/ipcs.c b/util-linux/ipcs.c
index fbc75cbee..c7c837669 100644
--- a/util-linux/ipcs.c
+++ b/util-linux/ipcs.c
@@ -8,9 +8,7 @@
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */ 9 */
10 10
11#include <stdio.h> 11#include "busybox.h"
12#include <stdlib.h>
13#include <getopt.h>
14#include <errno.h> 12#include <errno.h>
15#include <time.h> 13#include <time.h>
16#include <pwd.h> 14#include <pwd.h>
@@ -25,15 +23,15 @@
25#include <sys/msg.h> 23#include <sys/msg.h>
26#include <sys/shm.h> 24#include <sys/shm.h>
27 25
28#include "busybox.h" 26
29 27
30/*-------------------------------------------------------------------*/ 28/*-------------------------------------------------------------------*/
31/* SHM_DEST and SHM_LOCKED are defined in kernel headers, 29/* SHM_DEST and SHM_LOCKED are defined in kernel headers,
32 but inside #ifdef __KERNEL__ ... #endif */ 30 but inside #ifdef __KERNEL__ ... #endif */
33#ifndef SHM_DEST 31#ifndef SHM_DEST
34/* shm_mode upper byte flags */ 32/* shm_mode upper byte flags */
35#define SHM_DEST 01000 /* segment will be destroyed on last detach */ 33#define SHM_DEST 01000 /* segment will be destroyed on last detach */
36#define SHM_LOCKED 02000 /* segment will not be swapped */ 34#define SHM_LOCKED 02000 /* segment will not be swapped */
37#endif 35#endif
38 36
39/* For older kernels the same holds for the defines below */ 37/* For older kernels the same holds for the defines below */
@@ -46,12 +44,12 @@
46#define SHM_STAT 13 44#define SHM_STAT 13
47#define SHM_INFO 14 45#define SHM_INFO 14
48struct shm_info { 46struct shm_info {
49 int used_ids; 47 int used_ids;
50 ulong shm_tot; /* total allocated shm */ 48 ulong shm_tot; /* total allocated shm */
51 ulong shm_rss; /* total resident shm */ 49 ulong shm_rss; /* total resident shm */
52 ulong shm_swp; /* total swapped shm */ 50 ulong shm_swp; /* total swapped shm */
53 ulong swap_attempts; 51 ulong swap_attempts;
54 ulong swap_successes; 52 ulong swap_successes;
55}; 53};
56#endif 54#endif
57 55
@@ -98,12 +96,14 @@ union semun {
98#define TIME 4 96#define TIME 4
99#define PID 5 97#define PID 5
100 98
99static char format;
101 100
102static void print_perms (int id, struct ipc_perm *ipcp) { 101static void print_perms(int id, struct ipc_perm *ipcp)
102{
103 struct passwd *pw; 103 struct passwd *pw;
104 struct group *gr; 104 struct group *gr;
105 105
106 bb_printf ("%-10d %-10o", id, ipcp->mode & 0777); 106 bb_printf("%-10d %-10o", id, ipcp->mode & 0777);
107 107
108 if ((pw = getpwuid(ipcp->cuid))) 108 if ((pw = getpwuid(ipcp->cuid)))
109 bb_printf(" %-10s", pw->pw_name); 109 bb_printf(" %-10s", pw->pw_name);
@@ -125,7 +125,7 @@ static void print_perms (int id, struct ipc_perm *ipcp) {
125} 125}
126 126
127 127
128static void do_shm (char format) 128static void do_shm(void)
129{ 129{
130 int maxid, shmid, id; 130 int maxid, shmid, id;
131 struct shmid_ds shmseg; 131 struct shmid_ds shmseg;
@@ -134,127 +134,125 @@ static void do_shm (char format)
134 struct ipc_perm *ipcp = &shmseg.shm_perm; 134 struct ipc_perm *ipcp = &shmseg.shm_perm;
135 struct passwd *pw; 135 struct passwd *pw;
136 136
137 maxid = shmctl (0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info); 137 maxid = shmctl(0, SHM_INFO, (struct shmid_ds *) (void *) &shm_info);
138 if (maxid < 0) { 138 if (maxid < 0) {
139 bb_printf ("kernel not configured for shared memory\n"); 139 bb_printf("kernel not configured for shared memory\n");
140 return; 140 return;
141 } 141 }
142 142
143 switch (format) { 143 switch (format) {
144 case LIMITS: 144 case LIMITS:
145 bb_printf ("------ Shared Memory Limits --------\n"); 145 bb_printf("------ Shared Memory Limits --------\n");
146 if ((shmctl (0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0 ) 146 if ((shmctl(0, IPC_INFO, (struct shmid_ds *) (void *) &shminfo)) < 0)
147 return; 147 return;
148 /* glibc 2.1.3 and all earlier libc's have ints as fields 148 /* glibc 2.1.3 and all earlier libc's have ints as fields
149 of struct shminfo; glibc 2.1.91 has unsigned long; ach */ 149 of struct shminfo; glibc 2.1.91 has unsigned long; ach */
150 bb_printf ("max number of segments = %lu\n" 150 bb_printf("max number of segments = %lu\n"
151 "max seg size (kbytes) = %lu\n" 151 "max seg size (kbytes) = %lu\n"
152 "max total shared memory (pages) = %lu\n" 152 "max total shared memory (pages) = %lu\n"
153 "min seg size (bytes) = %lu\n", 153 "min seg size (bytes) = %lu\n",
154 (unsigned long) shminfo.shmmni, 154 (unsigned long) shminfo.shmmni,
155 (unsigned long) (shminfo.shmmax >> 10), 155 (unsigned long) (shminfo.shmmax >> 10),
156 (unsigned long) shminfo.shmall, 156 (unsigned long) shminfo.shmall,
157 (unsigned long) shminfo.shmmin); 157 (unsigned long) shminfo.shmmin);
158 return; 158 return;
159 159
160 case STATUS: 160 case STATUS:
161 bb_printf ("------ Shared Memory Status --------\n" 161 bb_printf("------ Shared Memory Status --------\n"
162 "segments allocated %d\n" 162 "segments allocated %d\n"
163 "pages allocated %ld\n" 163 "pages allocated %ld\n"
164 "pages resident %ld\n" 164 "pages resident %ld\n"
165 "pages swapped %ld\n" 165 "pages swapped %ld\n"
166 "Swap performance: %ld attempts\t %ld successes\n", 166 "Swap performance: %ld attempts\t %ld successes\n",
167 shm_info.used_ids, 167 shm_info.used_ids,
168 shm_info.shm_tot, 168 shm_info.shm_tot,
169 shm_info.shm_rss, 169 shm_info.shm_rss,
170 shm_info.shm_swp, 170 shm_info.shm_swp,
171 shm_info.swap_attempts, shm_info.swap_successes); 171 shm_info.swap_attempts, shm_info.swap_successes);
172 return; 172 return;
173 173
174 case CREATOR: 174 case CREATOR:
175 bb_printf ("------ Shared Memory Segment Creators/Owners --------\n" 175 bb_printf("------ Shared Memory Segment Creators/Owners --------\n"
176 "%-10s %-10s %-10s %-10s %-10s %-10s\n", 176 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
177 "shmid","perms","cuid","cgid","uid","gid"); 177 "shmid", "perms", "cuid", "cgid", "uid", "gid");
178 break; 178 break;
179 179
180 case TIME: 180 case TIME:
181 bb_printf ("------ Shared Memory Attach/Detach/Change Times --------\n" 181 bb_printf("------ Shared Memory Attach/Detach/Change Times --------\n"
182 "%-10s %-10s %-20s %-20s %-20s\n", 182 "%-10s %-10s %-20s %-20s %-20s\n",
183 "shmid","owner","attached","detached","changed"); 183 "shmid", "owner", "attached", "detached", "changed");
184 break; 184 break;
185 185
186 case PID: 186 case PID:
187 bb_printf ("------ Shared Memory Creator/Last-op --------\n" 187 bb_printf("------ Shared Memory Creator/Last-op --------\n"
188 "%-10s %-10s %-10s %-10s\n", 188 "%-10s %-10s %-10s %-10s\n",
189 "shmid","owner","cpid","lpid"); 189 "shmid", "owner", "cpid", "lpid");
190 break; 190 break;
191 191
192 default: 192 default:
193 bb_printf ("------ Shared Memory Segments --------\n" 193 bb_printf("------ Shared Memory Segments --------\n"
194 "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n", 194 "%-10s %-10s %-10s %-10s %-10s %-10s %-12s\n",
195 "key","shmid","owner","perms","bytes","nattch","status"); 195 "key", "shmid", "owner", "perms", "bytes", "nattch",
196 "status");
196 break; 197 break;
197 } 198 }
198 199
199 for (id = 0; id <= maxid; id++) { 200 for (id = 0; id <= maxid; id++) {
200 shmid = shmctl (id, SHM_STAT, &shmseg); 201 shmid = shmctl(id, SHM_STAT, &shmseg);
201 if (shmid < 0) 202 if (shmid < 0)
202 continue; 203 continue;
203 if (format == CREATOR) { 204 if (format == CREATOR) {
204 print_perms (shmid, ipcp); 205 print_perms(shmid, ipcp);
205 continue; 206 continue;
206 } 207 }
207 pw = getpwuid(ipcp->uid); 208 pw = getpwuid(ipcp->uid);
208 switch (format) { 209 switch (format) {
209 case TIME: 210 case TIME:
210 if (pw) 211 if (pw)
211 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name); 212 bb_printf("%-10d %-10.10s", shmid, pw->pw_name);
212 else 213 else
213 bb_printf ("%-10d %-10d", shmid, ipcp->uid); 214 bb_printf("%-10d %-10d", shmid, ipcp->uid);
214 /* ctime uses static buffer: use separate calls */ 215 /* ctime uses static buffer: use separate calls */
215 bb_printf(" %-20.16s", shmseg.shm_atime 216 bb_printf(" %-20.16s", shmseg.shm_atime
216 ? ctime(&shmseg.shm_atime) + 4 : "Not set"); 217 ? ctime(&shmseg.shm_atime) + 4 : "Not set");
217 bb_printf(" %-20.16s", shmseg.shm_dtime 218 bb_printf(" %-20.16s", shmseg.shm_dtime
218 ? ctime(&shmseg.shm_dtime) + 4 : "Not set"); 219 ? ctime(&shmseg.shm_dtime) + 4 : "Not set");
219 bb_printf(" %-20.16s\n", shmseg.shm_ctime 220 bb_printf(" %-20.16s\n", shmseg.shm_ctime
220 ? ctime(&shmseg.shm_ctime) + 4 : "Not set"); 221 ? ctime(&shmseg.shm_ctime) + 4 : "Not set");
221 break; 222 break;
222 case PID: 223 case PID:
223 if (pw) 224 if (pw)
224 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name); 225 bb_printf("%-10d %-10.10s", shmid, pw->pw_name);
225 else 226 else
226 bb_printf ("%-10d %-10d", shmid, ipcp->uid); 227 bb_printf("%-10d %-10d", shmid, ipcp->uid);
227 bb_printf (" %-10d %-10d\n", 228 bb_printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
228 shmseg.shm_cpid, shmseg.shm_lpid);
229 break; 229 break;
230 230
231 default: 231 default:
232 bb_printf("0x%08x ",ipcp->KEY ); 232 bb_printf("0x%08x ", ipcp->KEY);
233 if (pw) 233 if (pw)
234 bb_printf ("%-10d %-10.10s", shmid, pw->pw_name); 234 bb_printf("%-10d %-10.10s", shmid, pw->pw_name);
235 else 235 else
236 bb_printf ("%-10d %-10d", shmid, ipcp->uid); 236 bb_printf("%-10d %-10d", shmid, ipcp->uid);
237 bb_printf ("%-10o %-10lu %-10ld %-6s %-6s\n", 237 bb_printf("%-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
238 ipcp->mode & 0777, 238 /*
239 /* 239 * earlier: int, Austin has size_t
240 * earlier: int, Austin has size_t 240 */
241 */ 241 (unsigned long) shmseg.shm_segsz,
242 (unsigned long) shmseg.shm_segsz, 242 /*
243 /* 243 * glibc-2.1.3 and earlier has unsigned short;
244 * glibc-2.1.3 and earlier has unsigned short; 244 * Austin has shmatt_t
245 * Austin has shmatt_t 245 */
246 */ 246 (long) shmseg.shm_nattch,
247 (long) shmseg.shm_nattch, 247 ipcp->mode & SHM_DEST ? "dest" : " ",
248 ipcp->mode & SHM_DEST ? "dest" : " ", 248 ipcp->mode & SHM_LOCKED ? "locked" : " ");
249 ipcp->mode & SHM_LOCKED ? "locked" : " ");
250 break; 249 break;
251 } 250 }
252 } 251 }
253 return;
254} 252}
255 253
256 254
257static void do_sem (char format) 255static void do_sem(void)
258{ 256{
259 int maxid, semid, id; 257 int maxid, semid, id;
260 struct semid_ds semary; 258 struct semid_ds semary;
@@ -263,107 +261,104 @@ static void do_sem (char format)
263 struct passwd *pw; 261 struct passwd *pw;
264 union semun arg; 262 union semun arg;
265 263
266 arg.array = (ushort *) (void *) &seminfo; 264 arg.array = (ushort *) (void *) &seminfo;
267 maxid = semctl (0, 0, SEM_INFO, arg); 265 maxid = semctl(0, 0, SEM_INFO, arg);
268 if (maxid < 0) { 266 if (maxid < 0) {
269 bb_printf ("kernel not configured for semaphores\n"); 267 bb_printf("kernel not configured for semaphores\n");
270 return; 268 return;
271 } 269 }
272 270
273 switch (format) { 271 switch (format) {
274 case LIMITS: 272 case LIMITS:
275 bb_printf ("------ Semaphore Limits --------\n"); 273 bb_printf("------ Semaphore Limits --------\n");
276 arg.array = (ushort *) (void *) &seminfo; /* damn union */ 274 arg.array = (ushort *) (void *) &seminfo; /* damn union */
277 if ((semctl (0, 0, IPC_INFO, arg)) < 0 ) 275 if ((semctl(0, 0, IPC_INFO, arg)) < 0)
278 return; 276 return;
279 bb_printf ("max number of arrays = %d\n" 277 bb_printf("max number of arrays = %d\n"
280 "max semaphores per array = %d\n" 278 "max semaphores per array = %d\n"
281 "max semaphores system wide = %d\n" 279 "max semaphores system wide = %d\n"
282 "max ops per semop call = %d\n" 280 "max ops per semop call = %d\n"
283 "semaphore max value = %d\n", 281 "semaphore max value = %d\n",
284 seminfo.semmni, 282 seminfo.semmni,
285 seminfo.semmsl, 283 seminfo.semmsl,
286 seminfo.semmns, 284 seminfo.semmns, seminfo.semopm, seminfo.semvmx);
287 seminfo.semopm,
288 seminfo.semvmx);
289 return; 285 return;
290 286
291 case STATUS: 287 case STATUS:
292 bb_printf ("------ Semaphore Status --------\n" 288 bb_printf("------ Semaphore Status --------\n"
293 "used arrays = %d\n" 289 "used arrays = %d\n"
294 "allocated semaphores = %d\n", 290 "allocated semaphores = %d\n",
295 seminfo.semusz, 291 seminfo.semusz, seminfo.semaem);
296 seminfo.semaem);
297 return; 292 return;
298 293
299 case CREATOR: 294 case CREATOR:
300 bb_printf ("------ Semaphore Arrays Creators/Owners --------\n" 295 bb_printf("------ Semaphore Arrays Creators/Owners --------\n"
301 "%-10s %-10s %-10s %-10s %-10s %-10s\n", 296 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
302 "semid","perms","cuid","cgid","uid","gid"); 297 "semid", "perms", "cuid", "cgid", "uid", "gid");
303 break; 298 break;
304 299
305 case TIME: 300 case TIME:
306 bb_printf ("------ Shared Memory Operation/Change Times --------\n" 301 bb_printf("------ Shared Memory Operation/Change Times --------\n"
307 "%-8s %-10s %-26.24s %-26.24s\n", 302 "%-8s %-10s %-26.24s %-26.24s\n",
308 "shmid","owner","last-op","last-changed"); 303 "shmid", "owner", "last-op", "last-changed");
309 break; 304 break;
310 305
311 case PID: 306 case PID:
312 break; 307 break;
313 308
314 default: 309 default:
315 bb_printf ("------ Semaphore Arrays --------\n" 310 bb_printf("------ Semaphore Arrays --------\n"
316 "%-10s %-10s %-10s %-10s %-10s\n", 311 "%-10s %-10s %-10s %-10s %-10s\n",
317 "key","semid","owner","perms","nsems"); 312 "key", "semid", "owner", "perms", "nsems");
318 break; 313 break;
319 } 314 }
320 315
321 for (id = 0; id <= maxid; id++) { 316 for (id = 0; id <= maxid; id++) {
322 arg.buf = (struct semid_ds *) &semary; 317 arg.buf = (struct semid_ds *) &semary;
323 semid = semctl (id, 0, SEM_STAT, arg); 318 semid = semctl(id, 0, SEM_STAT, arg);
324 if (semid < 0) 319 if (semid < 0)
325 continue; 320 continue;
326 if (format == CREATOR) { 321 if (format == CREATOR) {
327 print_perms (semid, ipcp); 322 print_perms(semid, ipcp);
328 continue; 323 continue;
329 } 324 }
330 pw = getpwuid(ipcp->uid); 325 pw = getpwuid(ipcp->uid);
331 switch (format) { 326 switch (format) {
332 case TIME: 327 case TIME:
333 if (pw) 328 if (pw)
334 bb_printf ("%-8d %-10.10s", semid, pw->pw_name); 329 bb_printf("%-8d %-10.10s", semid, pw->pw_name);
335 else 330 else
336 bb_printf ("%-8d %-10d", semid, ipcp->uid); 331 bb_printf("%-8d %-10d", semid, ipcp->uid);
337 bb_printf (" %-26.24s", semary.sem_otime 332 /* ctime uses static buffer: use separate calls */
338 ? ctime(&semary.sem_otime) : "Not set"); 333 bb_printf(" %-26.24s", semary.sem_otime
339 bb_printf (" %-26.24s\n", semary.sem_ctime 334 ? ctime(&semary.sem_otime) : "Not set");
340 ? ctime(&semary.sem_ctime) : "Not set"); 335 bb_printf(" %-26.24s\n", semary.sem_ctime
336 ? ctime(&semary.sem_ctime) : "Not set");
341 break; 337 break;
342 case PID: 338 case PID:
343 break; 339 break;
344 340
345 default: 341 default:
346 bb_printf("0x%08x ", ipcp->KEY); 342 bb_printf("0x%08x ", ipcp->KEY);
347 if (pw) 343 if (pw)
348 bb_printf ("%-10d %-10.9s", semid, pw->pw_name); 344 bb_printf("%-10d %-10.9s", semid, pw->pw_name);
349 else 345 else
350 bb_printf ("%-10d %-9d", semid, ipcp->uid); 346 bb_printf("%-10d %-9d", semid, ipcp->uid);
351 bb_printf ("%-10o %-10ld\n", 347 bb_printf("%-10o %-10ld\n", ipcp->mode & 0777,
352 ipcp->mode & 0777, 348 /*
353 /* 349 * glibc-2.1.3 and earlier has unsigned short;
354 * glibc-2.1.3 and earlier has unsigned short; 350 * glibc-2.1.91 has variation between
355 * glibc-2.1.91 has variation between 351 * unsigned short and unsigned long
356 * unsigned short and unsigned long 352 * Austin prescribes unsigned short.
357 * Austin prescribes unsigned short. 353 */
358 */ 354 (long) semary.sem_nsems);
359 (long) semary.sem_nsems);
360 break; 355 break;
361 } 356 }
362 } 357 }
363} 358}
364 359
365 360
366static void do_msg (char format) 361static void do_msg(void)
367{ 362{
368 int maxid, msqid, id; 363 int maxid, msqid, id;
369 struct msqid_ds msgque; 364 struct msqid_ds msgque;
@@ -371,178 +366,165 @@ static void do_msg (char format)
371 struct ipc_perm *ipcp = &msgque.msg_perm; 366 struct ipc_perm *ipcp = &msgque.msg_perm;
372 struct passwd *pw; 367 struct passwd *pw;
373 368
374 maxid = msgctl (0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo); 369 maxid = msgctl(0, MSG_INFO, (struct msqid_ds *) (void *) &msginfo);
375 if (maxid < 0) { 370 if (maxid < 0) {
376 bb_printf ("kernel not configured for message queues\n"); 371 bb_printf("kernel not configured for message queues\n");
377 return; 372 return;
378 } 373 }
379 374
380 switch (format) { 375 switch (format) {
381 case LIMITS: 376 case LIMITS:
382 if ((msgctl (0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0 ) 377 if ((msgctl(0, IPC_INFO, (struct msqid_ds *) (void *) &msginfo)) < 0)
383 return; 378 return;
384 bb_printf ("------ Messages: Limits --------\n" 379 bb_printf("------ Messages: Limits --------\n"
385 "max queues system wide = %d\n" 380 "max queues system wide = %d\n"
386 "max size of message (bytes) = %d\n" 381 "max size of message (bytes) = %d\n"
387 "default max size of queue (bytes) = %d\n", 382 "default max size of queue (bytes) = %d\n",
388 msginfo.msgmni, 383 msginfo.msgmni, msginfo.msgmax, msginfo.msgmnb);
389 msginfo.msgmax,
390 msginfo.msgmnb);
391 return; 384 return;
392 385
393 case STATUS: 386 case STATUS:
394 bb_printf ("------ Messages: Status --------\n" 387 bb_printf("------ Messages: Status --------\n"
395 "allocated queues = %d\n" 388 "allocated queues = %d\n"
396 "used headers = %d\n" 389 "used headers = %d\n"
397 "used space = %d bytes\n", 390 "used space = %d bytes\n",
398 msginfo.msgpool, 391 msginfo.msgpool, msginfo.msgmap, msginfo.msgtql);
399 msginfo.msgmap,
400 msginfo.msgtql);
401 return; 392 return;
402 393
403 case CREATOR: 394 case CREATOR:
404 bb_printf ("------ Message Queues: Creators/Owners --------\n" 395 bb_printf("------ Message Queues: Creators/Owners --------\n"
405 "%-10s %-10s %-10s %-10s %-10s %-10s\n", 396 "%-10s %-10s %-10s %-10s %-10s %-10s\n",
406 "msqid","perms","cuid","cgid","uid","gid"); 397 "msqid", "perms", "cuid", "cgid", "uid", "gid");
407 break; 398 break;
408 399
409 case TIME: 400 case TIME:
410 bb_printf ("------ Message Queues Send/Recv/Change Times --------\n" 401 bb_printf("------ Message Queues Send/Recv/Change Times --------\n"
411 "%-8s %-10s %-20s %-20s %-20s\n", 402 "%-8s %-10s %-20s %-20s %-20s\n",
412 "msqid","owner","send","recv","change"); 403 "msqid", "owner", "send", "recv", "change");
413 break; 404 break;
414 405
415 case PID: 406 case PID:
416 bb_printf ("------ Message Queues PIDs --------\n" 407 bb_printf("------ Message Queues PIDs --------\n"
417 "%-10s %-10s %-10s %-10s\n", 408 "%-10s %-10s %-10s %-10s\n",
418 "msqid","owner","lspid","lrpid"); 409 "msqid", "owner", "lspid", "lrpid");
419 break; 410 break;
420 411
421 default: 412 default:
422 bb_printf ("------ Message Queues --------\n" 413 bb_printf("------ Message Queues --------\n"
423 "%-10s %-10s %-10s %-10s %-12s %-12s\n", 414 "%-10s %-10s %-10s %-10s %-12s %-12s\n",
424 "key","msqid","owner","perms","used-bytes","messages"); 415 "key", "msqid", "owner", "perms", "used-bytes", "messages");
425 break; 416 break;
426 } 417 }
427 418
428 for (id = 0; id <= maxid; id++) { 419 for (id = 0; id <= maxid; id++) {
429 msqid = msgctl (id, MSG_STAT, &msgque); 420 msqid = msgctl(id, MSG_STAT, &msgque);
430 if (msqid < 0) 421 if (msqid < 0)
431 continue; 422 continue;
432 if (format == CREATOR) { 423 if (format == CREATOR) {
433 print_perms (msqid, ipcp); 424 print_perms(msqid, ipcp);
434 continue; 425 continue;
435 } 426 }
436 pw = getpwuid(ipcp->uid); 427 pw = getpwuid(ipcp->uid);
437 switch (format) { 428 switch (format) {
438 case TIME: 429 case TIME:
439 if (pw) 430 if (pw)
440 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name); 431 bb_printf("%-8d %-10.10s", msqid, pw->pw_name);
441 else 432 else
442 bb_printf ("%-8d %-10d", msqid, ipcp->uid); 433 bb_printf("%-8d %-10d", msqid, ipcp->uid);
443 bb_printf (" %-20.16s", msgque.msg_stime 434 bb_printf(" %-20.16s", msgque.msg_stime
444 ? ctime(&msgque.msg_stime) + 4 : "Not set"); 435 ? ctime(&msgque.msg_stime) + 4 : "Not set");
445 bb_printf (" %-20.16s", msgque.msg_rtime 436 bb_printf(" %-20.16s", msgque.msg_rtime
446 ? ctime(&msgque.msg_rtime) + 4 : "Not set"); 437 ? ctime(&msgque.msg_rtime) + 4 : "Not set");
447 bb_printf (" %-20.16s\n", msgque.msg_ctime 438 bb_printf(" %-20.16s\n", msgque.msg_ctime
448 ? ctime(&msgque.msg_ctime) + 4 : "Not set"); 439 ? ctime(&msgque.msg_ctime) + 4 : "Not set");
449 break; 440 break;
450 case PID: 441 case PID:
451 if (pw) 442 if (pw)
452 bb_printf ("%-8d %-10.10s", msqid, pw->pw_name); 443 bb_printf("%-8d %-10.10s", msqid, pw->pw_name);
453 else 444 else
454 bb_printf ("%-8d %-10d", msqid, ipcp->uid); 445 bb_printf("%-8d %-10d", msqid, ipcp->uid);
455 bb_printf (" %5d %5d\n", 446 bb_printf(" %5d %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
456 msgque.msg_lspid, msgque.msg_lrpid);
457 break; 447 break;
458 448
459 default: 449 default:
460 bb_printf( "0x%08x ",ipcp->KEY ); 450 bb_printf("0x%08x ", ipcp->KEY);
461 if (pw) 451 if (pw)
462 bb_printf ("%-10d %-10.10s", msqid, pw->pw_name); 452 bb_printf("%-10d %-10.10s", msqid, pw->pw_name);
463 else 453 else
464 bb_printf ("%-10d %-10d", msqid, ipcp->uid); 454 bb_printf("%-10d %-10d", msqid, ipcp->uid);
465 bb_printf (" %-10o %-12ld %-12ld\n", 455 bb_printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
466 ipcp->mode & 0777, 456 /*
467 /* 457 * glibc-2.1.3 and earlier has unsigned short;
468 * glibc-2.1.3 and earlier has unsigned short; 458 * glibc-2.1.91 has variation between
469 * glibc-2.1.91 has variation between 459 * unsigned short, unsigned long
470 * unsigned short, unsigned long 460 * Austin has msgqnum_t
471 * Austin has msgqnum_t 461 */
472 */ 462 (long) msgque.msg_cbytes, (long) msgque.msg_qnum);
473 (long) msgque.msg_cbytes,
474 (long) msgque.msg_qnum);
475 break; 463 break;
476 } 464 }
477 } 465 }
478 return;
479} 466}
480 467
481 468
482static void print_shm (int shmid) 469static void print_shm(int shmid)
483{ 470{
484 struct shmid_ds shmds; 471 struct shmid_ds shmds;
485 struct ipc_perm *ipcp = &shmds.shm_perm; 472 struct ipc_perm *ipcp = &shmds.shm_perm;
486 473
487 if (shmctl (shmid, IPC_STAT, &shmds) == -1) { 474 if (shmctl(shmid, IPC_STAT, &shmds) == -1) {
488 perror ("shmctl "); 475 bb_perror_msg("shmctl");
489 return; 476 return;
490 } 477 }
491 478
492 bb_printf ("\nShared memory Segment shmid=%d\n" 479 bb_printf("\nShared memory Segment shmid=%d\n"
493 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n" 480 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
494 "mode=%#o\taccess_perms=%#o\n" 481 "mode=%#o\taccess_perms=%#o\n"
495 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n" 482 "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
496 "att_time=%-26.24s\n" 483 shmid,
497 "det_time=%-26.24s\n" 484 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
498 "change_time=%-26.24s\n" 485 ipcp->mode, ipcp->mode & 0777,
499 "\n", 486 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
500 shmid, 487 (long) shmds.shm_nattch);
501 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, 488 bb_printf("att_time=%-26.24s\n",
502 ipcp->mode, ipcp->mode & 0777, 489 shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
503 (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid, 490 bb_printf("det_time=%-26.24s\n",
504 (long) shmds.shm_nattch, 491 shmds.shm_dtime ? ctime(&shmds.shm_dtime) : "Not set");
505 shmds.shm_atime ? ctime (&shmds.shm_atime) : "Not set", 492 bb_printf("change_time=%-26.24s\n\n", ctime(&shmds.shm_ctime));
506 shmds.shm_dtime ? ctime (&shmds.shm_dtime) : "Not set",
507 ctime (&shmds.shm_ctime));
508 return;
509} 493}
510 494
511 495
512static void print_msg (int msqid) 496static void print_msg(int msqid)
513{ 497{
514 struct msqid_ds buf; 498 struct msqid_ds buf;
515 struct ipc_perm *ipcp = &buf.msg_perm; 499 struct ipc_perm *ipcp = &buf.msg_perm;
516 500
517 if (msgctl (msqid, IPC_STAT, &buf) == -1) { 501 if (msgctl(msqid, IPC_STAT, &buf) == -1) {
518 perror ("msgctl "); 502 bb_perror_msg("msgctl");
519 return; 503 return;
520 } 504 }
521 505
522 bb_printf ("\nMessage Queue msqid=%d\n" 506 bb_printf("\nMessage Queue msqid=%d\n"
523 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n" 507 "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
524 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n" 508 "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
525 "send_time=%-26.24s\n" 509 msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
526 "rcv_time=%-26.24s\n" 510 /*
527 "change_time=%-26.24s\n" 511 * glibc-2.1.3 and earlier has unsigned short;
528 "\n", 512 * glibc-2.1.91 has variation between
529 msqid, 513 * unsigned short, unsigned long
530 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode, 514 * Austin has msgqnum_t (for msg_qbytes)
531 /* 515 */
532 * glibc-2.1.3 and earlier has unsigned short; 516 (long) buf.msg_cbytes, (long) buf.msg_qbytes,
533 * glibc-2.1.91 has variation between 517 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid);
534 * unsigned short, unsigned long 518
535 * Austin has msgqnum_t (for msg_qbytes) 519 bb_printf("send_time=%-26.24s\n",
536 */ 520 buf.msg_stime ? ctime(&buf.msg_stime) : "Not set");
537 (long) buf.msg_cbytes, (long) buf.msg_qbytes, 521 bb_printf("rcv_time=%-26.24s\n",
538 (long) buf.msg_qnum, buf.msg_lspid, buf.msg_lrpid, 522 buf.msg_rtime ? ctime(&buf.msg_rtime) : "Not set");
539 buf.msg_stime ? ctime (&buf.msg_stime) : "Not set", 523 bb_printf("change_time=%-26.24s\n\n",
540 buf.msg_rtime ? ctime (&buf.msg_rtime) : "Not set", 524 buf.msg_ctime ? ctime(&buf.msg_ctime) : "Not set");
541 buf.msg_ctime ? ctime (&buf.msg_ctime) : "Not set");
542 return;
543} 525}
544 526
545static void print_sem (int semid) 527static void print_sem(int semid)
546{ 528{
547 struct semid_ds semds; 529 struct semid_ds semds;
548 struct ipc_perm *ipcp = &semds.sem_perm; 530 struct ipc_perm *ipcp = &semds.sem_perm;
@@ -550,66 +532,69 @@ static void print_sem (int semid)
550 unsigned int i; 532 unsigned int i;
551 533
552 arg.buf = &semds; 534 arg.buf = &semds;
553 if (semctl (semid, 0, IPC_STAT, arg) < 0) { 535 if (semctl(semid, 0, IPC_STAT, arg)) {
554 perror ("semctl "); 536 bb_perror_msg("semctl");
555 return; 537 return;
556 } 538 }
557 539
558 bb_printf ("\nSemaphore Array semid=%d\n" 540 bb_printf("\nSemaphore Array semid=%d\n"
559 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n" 541 "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
560 "mode=%#o, access_perms=%#o\n" 542 "mode=%#o, access_perms=%#o\n"
561 "nsems = %ld\n" 543 "nsems = %ld\n"
562 "otime = %-26.24s\n" 544 "otime = %-26.24s\n",
563 "ctime = %-26.24s\n" 545 semid,
564 "%-10s %-10s %-10s %-10s %-10s\n", 546 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
565 semid, 547 ipcp->mode, ipcp->mode & 0777,
566 ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, 548 (long) semds.sem_nsems,
567 ipcp->mode, ipcp->mode & 0777, 549 semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
568 (long) semds.sem_nsems, 550 bb_printf("ctime = %-26.24s\n"
569 semds.sem_otime ? ctime (&semds.sem_otime) : "Not set", 551 "%-10s %-10s %-10s %-10s %-10s\n",
570 ctime (&semds.sem_ctime), 552 ctime(&semds.sem_ctime),
571 "semnum","value","ncount","zcount","pid"); 553 "semnum", "value", "ncount", "zcount", "pid");
572 554
573 arg.val = 0; 555 arg.val = 0;
574 for (i=0; i < semds.sem_nsems; i++) { 556 for (i = 0; i < semds.sem_nsems; i++) {
575 int val, ncnt, zcnt, pid; 557 int val, ncnt, zcnt, pid;
576 val = semctl (semid, i, GETVAL, arg); 558
577 ncnt = semctl (semid, i, GETNCNT, arg); 559 val = semctl(semid, i, GETVAL, arg);
578 zcnt = semctl (semid, i, GETZCNT, arg); 560 ncnt = semctl(semid, i, GETNCNT, arg);
579 pid = semctl (semid, i, GETPID, arg); 561 zcnt = semctl(semid, i, GETZCNT, arg);
562 pid = semctl(semid, i, GETPID, arg);
580 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) { 563 if (val < 0 || ncnt < 0 || zcnt < 0 || pid < 0) {
581 perror ("semctl "); 564 bb_perror_msg_and_die("semctl");
582 bb_fflush_stdout_and_exit (1);
583 } 565 }
584 bb_printf ("%-10d %-10d %-10d %-10d %-10d\n", 566 bb_printf("%-10d %-10d %-10d %-10d %-10d\n", i, val, ncnt, zcnt, pid);
585 i, val, ncnt, zcnt, pid);
586 } 567 }
587 bb_printf ("\n"); 568 bb_printf("\n");
588 return;
589} 569}
590 570
591int ipcs_main (int argc, char **argv) { 571int ipcs_main(int argc, char **argv)
592 int opt, msg = 0, sem = 0, shm = 0, id=0, print=0; 572{
593 char format = 0; 573 int opt, id = 0;
594 char options[] = "atclupsmqi:ih?"; 574 unsigned flags = 0;
595 575#define flag_print (1<<0)
596 while ((opt = getopt (argc, argv, options)) != -1) { 576#define flag_msg (1<<1)
577#define flag_sem (1<<2)
578#define flag_shm (1<<3)
579 const char *const options = "atclupsmqi:ih?";
580
581 while ((opt = getopt(argc, argv, options)) != -1) {
597 switch (opt) { 582 switch (opt) {
598 case 'i': 583 case 'i':
599 id = atoi (optarg); 584 id = atoi(optarg);
600 print = 1; 585 flags |= flag_print;
601 break; 586 break;
602 case 'a': 587 case 'a':
603 msg = shm = sem = 1; 588 flags |= flag_msg | flag_sem | flag_shm;
604 break; 589 break;
605 case 'q': 590 case 'q':
606 msg = 1; 591 flags |= flag_msg;
607 break; 592 break;
608 case 's': 593 case 's':
609 sem = 1; 594 flags |= flag_sem;
610 break; 595 break;
611 case 'm': 596 case 'm':
612 shm = 1; 597 flags |= flag_shm;
613 break; 598 break;
614 case 't': 599 case 't':
615 format = TIME; 600 format = TIME;
@@ -629,43 +614,40 @@ int ipcs_main (int argc, char **argv) {
629 case 'h': 614 case 'h':
630 case '?': 615 case '?':
631 bb_show_usage(); 616 bb_show_usage();
632 bb_fflush_stdout_and_exit (0);
633 } 617 }
634 } 618 }
635 619
636 if (print) { 620 if (flags & flag_print) {
637 if (shm) { 621 if (flags & flag_shm) {
638 print_shm (id); 622 print_shm(id);
639 bb_fflush_stdout_and_exit (0); 623 bb_fflush_stdout_and_exit(0);
640 } 624 }
641 if (sem) { 625 if (flags & flag_sem) {
642 print_sem (id); 626 print_sem(id);
643 bb_fflush_stdout_and_exit (0); 627 bb_fflush_stdout_and_exit(0);
644 } 628 }
645 if (msg) { 629 if (flags & flag_msg) {
646 print_msg (id); 630 print_msg(id);
647 bb_fflush_stdout_and_exit (0); 631 bb_fflush_stdout_and_exit(0);
648 } 632 }
649 bb_show_usage(); 633 bb_show_usage();
650 bb_fflush_stdout_and_exit (0);
651 } 634 }
652 635
653 if ( !shm && !msg && !sem) 636 if (!(flags & (flag_shm | flag_msg | flag_sem)))
654 msg = sem = shm = 1; 637 flags |= flag_msg | flag_shm | flag_sem;
655 bb_printf ("\n"); 638 bb_printf("\n");
656 639
657 if (shm) { 640 if (flags & flag_shm) {
658 do_shm (format); 641 do_shm();
659 bb_printf ("\n"); 642 bb_printf("\n");
660 } 643 }
661 if (sem) { 644 if (flags & flag_sem) {
662 do_sem (format); 645 do_sem();
663 bb_printf ("\n"); 646 bb_printf("\n");
664 } 647 }
665 if (msg) { 648 if (flags & flag_msg) {
666 do_msg (format); 649 do_msg();
667 bb_printf ("\n"); 650 bb_printf("\n");
668 } 651 }
669 return 0; 652 return EXIT_SUCCESS;
670} 653}
671