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