Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/util-linux/ipcrm.c
- Timestamp:
- Nov 4, 2007, 3:16:40 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.5/mindi-busybox/util-linux/ipcrm.c
r821 r1765 1 /* vi: set sw=4 ts=4: */ 1 2 /* 2 * 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. 3 4 * 4 5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com> 5 6 * Adapted for busybox from util-linux-2.12a. 6 7 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * 21 * --- Pre-busybox history from util-linux-2.12a ------------------------ 22 * 23 * 1999-04-02 frank zago 24 * - can now remove several id's in the same call 25 * 26 * 1999-02-22 Arkadiusz Miÿkiewicz <misiek@pld.ORG.PL> 27 * - added Native Language Support 28 * 29 * Original author - krishna balasubramanian 1993 8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 30 9 */ 31 10 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <errno.h> 36 37 #include <sys/types.h> 11 #include "libbb.h" 12 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() */ 38 15 #include <sys/ipc.h> 39 16 #include <sys/shm.h> … … 41 18 #include <sys/sem.h> 42 19 43 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */ 44 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */ 45 /* for getopt */ 46 #include <unistd.h> 47 48 /* for tolower and isupper */ 49 #include <ctype.h> 50 51 #include "busybox.h" 52 53 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) 20 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) 54 21 /* union semun is defined by including <sys/sem.h> */ 55 22 #else … … 63 30 #endif 64 31 32 #define IPCRM_LEGACY 1 33 34 35 #if IPCRM_LEGACY 36 65 37 typedef enum type_id { 66 38 SHM, … … 69 41 } type_id; 70 42 71 static int 72 remove_ids(type_id type, int argc, char **argv) { 73 int id; 74 int ret = 0; /* for gcc */ 75 char *end; 43 static int remove_ids(type_id type, int argc, char **argv) 44 { 45 unsigned long id; 46 int ret = 0; /* silence gcc */ 76 47 int nb_errors = 0; 77 48 union semun arg; … … 79 50 arg.val = 0; 80 51 81 while(argc) { 82 83 id = strtoul(argv[0], &end, 10); 84 85 if (*end != 0) { 86 bb_printf ("invalid id: %s\n", argv[0]); 87 nb_errors ++; 52 while (argc) { 53 id = bb_strtoul(argv[0], NULL, 10); 54 if (errno || id > INT_MAX) { 55 bb_error_msg("invalid id: %s", argv[0]); 56 nb_errors++; 88 57 } else { 89 switch(type) { 90 case SEM: 91 ret = semctl (id, 0, IPC_RMID, arg); 92 break; 93 94 case MSG: 95 ret = msgctl (id, IPC_RMID, NULL); 96 break; 97 98 case SHM: 99 ret = shmctl (id, IPC_RMID, NULL); 100 break; 101 } 58 if (type == SEM) 59 ret = semctl(id, 0, IPC_RMID, arg); 60 else if (type == MSG) 61 ret = msgctl(id, IPC_RMID, NULL); 62 else if (type == SHM) 63 ret = shmctl(id, IPC_RMID, NULL); 102 64 103 65 if (ret) { 104 bb_printf ("cannot remove id %s (%s)\n", 105 argv[0], strerror(errno)); 106 nb_errors ++; 66 bb_perror_msg("cannot remove id %s", argv[0]); 67 nb_errors++; 107 68 } 108 69 } … … 111 72 } 112 73 113 return (nb_errors);74 return nb_errors; 114 75 } 115 116 static int deprecated_main(int argc, char **argv) 117 { 118 if (argc < 3) { 119 bb_show_usage(); 120 bb_fflush_stdout_and_exit(1); 121 } 122 123 if (!strcmp(argv[1], "shm")) { 124 if (remove_ids(SHM, argc-2, &argv[2])) 125 bb_fflush_stdout_and_exit(1); 126 } 127 else if (!strcmp(argv[1], "msg")) { 128 if (remove_ids(MSG, argc-2, &argv[2])) 129 bb_fflush_stdout_and_exit(1); 130 } 131 else if (!strcmp(argv[1], "sem")) { 132 if (remove_ids(SEM, argc-2, &argv[2])) 133 bb_fflush_stdout_and_exit(1); 134 } 135 else { 136 bb_printf ("unknown resource type: %s\n", argv[1]); 137 bb_show_usage(); 138 bb_fflush_stdout_and_exit(1); 139 } 140 141 bb_printf ("resource(s) deleted\n"); 142 return 0; 143 } 144 145 76 #endif /* IPCRM_LEGACY */ 77 78 79 int ipcrm_main(int argc, char **argv); 146 80 int ipcrm_main(int argc, char **argv) 147 81 { 148 int c; 149 int error = 0; 150 char *prog = argv[0]; 82 int c; 83 int error = 0; 151 84 152 85 /* if the command is executed without parameters, do nothing */ 153 86 if (argc == 1) 154 87 return 0; 155 88 #if IPCRM_LEGACY 156 89 /* check to see if the command is being invoked in the old way if so 157 then run the old code */ 158 if (strcmp(argv[1], "shm") == 0 || 159 strcmp(argv[1], "msg") == 0 || 160 strcmp(argv[1], "sem") == 0) 161 return deprecated_main(argc, argv); 90 then run the old code. Valid commands are msg, shm, sem. */ 91 { 92 type_id what = 0; /* silence gcc */ 93 char w; 94 95 w=argv[1][0]; 96 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g') 97 || (argv[1][0] == 's' 98 && ((w=argv[1][1]) == 'h' || w == 'e') 99 && argv[1][2] == 'm') 100 ) && argv[1][3] == '\0' 101 ) { 102 103 if (argc < 3) 104 bb_show_usage(); 105 106 if (w == 'h') 107 what = SHM; 108 else if (w == 'm') 109 what = MSG; 110 else if (w == 'e') 111 what = SEM; 112 113 if (remove_ids(what, argc-2, &argv[2])) 114 fflush_stdout_and_exit(1); 115 printf("resource(s) deleted\n"); 116 return 0; 117 } 118 } 119 #endif /* IPCRM_LEGACY */ 162 120 163 121 /* process new syntax to conform with SYSV ipcrm */ … … 165 123 int result; 166 124 int id = 0; 167 int iskey = isupper(c);125 int iskey = (isupper)(c); 168 126 169 127 /* needed to delete semaphores */ 170 128 union semun arg; 129 171 130 arg.val = 0; 172 131 173 if ((c == '?') || (c == 'h')) 174 { 132 if ((c == '?') || (c == 'h')) { 175 133 bb_show_usage(); 176 return 0;177 134 } 178 135 … … 180 137 c = tolower(c); 181 138 182 /* make sure the option is in range */139 /* make sure the option is in range: allowed are q, m, s */ 183 140 if (c != 'q' && c != 'm' && c != 's') { 184 141 bb_show_usage(); 185 error++;186 return error;187 142 } 188 143 189 144 if (iskey) { 190 145 /* keys are in hex or decimal */ 191 key_t key = strtoul(optarg, NULL, 0); 146 key_t key = xstrtoul(optarg, 0); 147 192 148 if (key == IPC_PRIVATE) { 193 149 error++; 194 bb_fprintf(stderr, "%s: illegal key (%s)\n", 195 prog, optarg); 150 bb_error_msg("illegal key (%s)", optarg); 196 151 continue; 197 152 } … … 199 154 /* convert key to id */ 200 155 id = ((c == 'q') ? msgget(key, 0) : 201 (c == 'm') ? shmget(key, 0, 0) : 202 semget(key, 0, 0)); 156 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0)); 203 157 204 158 if (id < 0) { 205 char *errmsg; 159 const char *errmsg; 160 206 161 error++; 207 switch (errno) {162 switch (errno) { 208 163 case EACCES: 209 errmsg = "permission denied for key";164 errmsg = "permission denied for"; 210 165 break; 211 166 case EIDRM: 212 errmsg = "already removed key";167 errmsg = "already removed"; 213 168 break; 214 169 case ENOENT: 215 errmsg = "invalid key";170 errmsg = "invalid"; 216 171 break; 217 172 default: 218 errmsg = "unknown error in key";173 errmsg = "unknown error in"; 219 174 break; 220 175 } 221 bb_fprintf(stderr, "%s: %s (%s)\n", 222 prog, errmsg, optarg); 176 bb_error_msg("%s %s (%s)", errmsg, "key", optarg); 223 177 continue; 224 178 } 225 179 } else { 226 180 /* ids are in decimal */ 227 id = strtoul(optarg, NULL, 10);181 id = xatoul(optarg); 228 182 } 229 183 230 184 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) : 231 (c == 'm') ? shmctl(id, IPC_RMID, NULL) : 232 semctl(id, 0, IPC_RMID, arg)); 233 234 if (result < 0) { 235 char *errmsg; 185 (c == 'm') ? shmctl(id, IPC_RMID, NULL) : 186 semctl(id, 0, IPC_RMID, arg)); 187 188 if (result) { 189 const char *errmsg; 190 const char *const what = iskey ? "key" : "id"; 191 236 192 error++; 237 switch (errno) {193 switch (errno) { 238 194 case EACCES: 239 195 case EPERM: 240 errmsg = iskey 241 ? "permission denied for key" 242 : "permission denied for id"; 196 errmsg = "permission denied for"; 243 197 break; 244 198 case EINVAL: 245 errmsg = iskey 246 ? "invalid key" 247 : "invalid id"; 199 errmsg = "invalid"; 248 200 break; 249 201 case EIDRM: 250 errmsg = iskey 251 ? "already removed key" 252 : "already removed id"; 202 errmsg = "already removed"; 253 203 break; 254 204 default: 255 errmsg = iskey 256 ? "unknown error in key" 257 : "unknown error in id"; 258 break; 259 } 260 bb_fprintf(stderr, "%s: %s (%s)\n", 261 prog, errmsg, optarg); 205 errmsg = "unknown error in"; 206 break; 207 } 208 bb_error_msg("%s %s (%s)", errmsg, what, optarg); 262 209 continue; 263 210 }
Note:
See TracChangeset
for help on using the changeset viewer.