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