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