aboutsummaryrefslogtreecommitdiff
path: root/util-linux/ipcrm.c
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/ipcrm.c
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/ipcrm.c')
-rw-r--r--util-linux/ipcrm.c216
1 files changed, 80 insertions, 136 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 }