"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 | /* |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 3 | * ipcrm.c - utility to allow removal of IPC objects and data structures. |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 4 | * |
| 5 | * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com> |
| 6 | * Adapted for busybox from util-linux-2.12a. |
| 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame^] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 12 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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() */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 15 | #include <sys/ipc.h> |
| 16 | #include <sys/shm.h> |
| 17 | #include <sys/msg.h> |
| 18 | #include <sys/sem.h> |
| 19 | |
Denis Vlasenko | ff131b9 | 2007-04-10 15:42:06 +0000 | [diff] [blame] | 20 | #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 21 | /* union semun is defined by including <sys/sem.h> */ |
| 22 | #else |
| 23 | /* according to X/OPEN we have to define it ourselves */ |
| 24 | union semun { |
| 25 | int val; |
| 26 | struct semid_ds *buf; |
Denis Vlasenko | 284d0fa | 2008-02-16 13:18:17 +0000 | [diff] [blame] | 27 | unsigned short *array; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 28 | struct seminfo *__buf; |
| 29 | }; |
| 30 | #endif |
| 31 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 32 | #define IPCRM_LEGACY 1 |
| 33 | |
| 34 | |
| 35 | #if IPCRM_LEGACY |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 36 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 37 | typedef enum type_id { |
| 38 | SHM, |
| 39 | SEM, |
| 40 | MSG |
| 41 | } type_id; |
| 42 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 43 | static int remove_ids(type_id type, int argc, char **argv) |
| 44 | { |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 45 | unsigned long id; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 46 | int ret = 0; /* silence gcc */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 47 | int nb_errors = 0; |
| 48 | union semun arg; |
| 49 | |
| 50 | arg.val = 0; |
| 51 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 52 | while (argc) { |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 53 | id = bb_strtoul(argv[0], NULL, 10); |
| 54 | if (errno || id > INT_MAX) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 55 | bb_error_msg("invalid id: %s", argv[0]); |
| 56 | nb_errors++; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 57 | } else { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 64 | |
| 65 | if (ret) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 66 | bb_perror_msg("can't remove id %s", argv[0]); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 67 | nb_errors++; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | argc--; |
| 71 | argv++; |
| 72 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 74 | return nb_errors; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 75 | } |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 76 | #endif /* IPCRM_LEGACY */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 77 | |
| 78 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 79 | int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 80 | int ipcrm_main(int argc, char **argv) |
| 81 | { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 82 | int c; |
| 83 | int error = 0; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 84 | |
| 85 | /* if the command is executed without parameters, do nothing */ |
| 86 | if (argc == 1) |
| 87 | return 0; |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 88 | #if IPCRM_LEGACY |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 89 | /* check to see if the command is being invoked in the old way if so |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 90 | then run the old code. Valid commands are msg, shm, sem. */ |
| 91 | { |
| 92 | type_id what = 0; /* silence gcc */ |
| 93 | char w; |
| 94 | |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 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 | ) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 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])) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 114 | fflush_stdout_and_exit(EXIT_FAILURE); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 115 | printf("resource(s) deleted\n"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | } |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 119 | #endif /* IPCRM_LEGACY */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 120 | |
| 121 | /* process new syntax to conform with SYSV ipcrm */ |
| 122 | while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) { |
| 123 | int result; |
| 124 | int id = 0; |
Denys Vlasenko | f2cbb03 | 2009-10-23 03:16:08 +0200 | [diff] [blame] | 125 | int iskey = isupper(c); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 126 | |
| 127 | /* needed to delete semaphores */ |
| 128 | union semun arg; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 129 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 130 | arg.val = 0; |
| 131 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 132 | if ((c == '?') || (c == 'h')) { |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 133 | bb_show_usage(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* we don't need case information any more */ |
| 137 | c = tolower(c); |
| 138 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 139 | /* make sure the option is in range: allowed are q, m, s */ |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 140 | if (c != 'q' && c != 'm' && c != 's') { |
| 141 | bb_show_usage(); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | if (iskey) { |
| 145 | /* keys are in hex or decimal */ |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 146 | key_t key = xstrtoul(optarg, 0); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 147 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 148 | if (key == IPC_PRIVATE) { |
| 149 | error++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 150 | bb_error_msg("illegal key (%s)", optarg); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
| 154 | /* convert key to id */ |
| 155 | id = ((c == 'q') ? msgget(key, 0) : |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 156 | (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0)); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 157 | |
| 158 | if (id < 0) { |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 159 | const char *errmsg; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 160 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 161 | error++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 162 | switch (errno) { |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 163 | case EACCES: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 164 | errmsg = "permission denied for"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 165 | break; |
| 166 | case EIDRM: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 167 | errmsg = "already removed"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 168 | break; |
| 169 | case ENOENT: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 170 | errmsg = "invalid"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 171 | break; |
| 172 | default: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 173 | errmsg = "unknown error in"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 174 | break; |
| 175 | } |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 176 | bb_error_msg("%s %s (%s)", errmsg, "key", optarg); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 177 | continue; |
| 178 | } |
| 179 | } else { |
| 180 | /* ids are in decimal */ |
Denis Vlasenko | c6ce873 | 2006-11-29 18:15:52 +0000 | [diff] [blame] | 181 | id = xatoul(optarg); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) : |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 185 | (c == 'm') ? shmctl(id, IPC_RMID, NULL) : |
| 186 | semctl(id, 0, IPC_RMID, arg)); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 187 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 188 | if (result) { |
Denis Vlasenko | 06c0a71 | 2007-01-29 22:51:44 +0000 | [diff] [blame] | 189 | const char *errmsg; |
| 190 | const char *const what = iskey ? "key" : "id"; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 191 | |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 192 | error++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 193 | switch (errno) { |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 194 | case EACCES: |
| 195 | case EPERM: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 196 | errmsg = "permission denied for"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 197 | break; |
| 198 | case EINVAL: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 199 | errmsg = "invalid"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 200 | break; |
| 201 | case EIDRM: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 202 | errmsg = "already removed"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 203 | break; |
| 204 | default: |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 205 | errmsg = "unknown error in"; |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 208 | bb_error_msg("%s %s (%s)", errmsg, what, optarg); |
Rob Landley | 6eb1e41 | 2005-06-20 04:30:36 +0000 | [diff] [blame] | 209 | continue; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /* print usage if we still have some arguments left over */ |
| 214 | if (optind != argc) { |
| 215 | bb_show_usage(); |
| 216 | } |
| 217 | |
| 218 | /* exit value reflects the number of errors encountered */ |
| 219 | return error; |
| 220 | } |