2 * ipcrm.c -- utility to allow removal of IPC objects and data structures.
4 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
5 * Adapted for busybox from util-linux-2.12a.
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.
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.
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
21 * --- Pre-busybox history from util-linux-2.12a ------------------------
23 * 1999-04-02 frank zago
24 * - can now remove several id's in the same call
26 * 1999-02-22 Arkadiusz Miÿkiewicz <misiek@pld.ORG.PL>
27 * - added Native Language Support
29 * Original author - krishna balasubramanian 1993
37 #include <sys/types.h>
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() */
48 /* for tolower and isupper */
53 #if defined (__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
54 /* union semun is defined by including <sys/sem.h> */
56 /* according to X/OPEN we have to define it ourselves */
60 unsigned short int *array;
61 struct seminfo *__buf;
65 typedef enum type_id {
72 remove_ids(type_id type, int argc, char **argv) {
74 int ret = 0; /* for gcc */
83 id = strtoul(argv[0], &end, 10);
86 bb_printf ("invalid id: %s\n", argv[0]);
91 ret = semctl (id, 0, IPC_RMID, arg);
95 ret = msgctl (id, IPC_RMID, NULL);
99 ret = shmctl (id, IPC_RMID, NULL);
104 bb_printf ("cannot remove id %s (%s)\n",
105 argv[0], strerror(errno));
116 static int deprecated_main(int argc, char **argv)
120 bb_fflush_stdout_and_exit(1);
123 if (!strcmp(argv[1], "shm")) {
124 if (remove_ids(SHM, argc-2, &argv[2]))
125 bb_fflush_stdout_and_exit(1);
127 else if (!strcmp(argv[1], "msg")) {
128 if (remove_ids(MSG, argc-2, &argv[2]))
129 bb_fflush_stdout_and_exit(1);
131 else if (!strcmp(argv[1], "sem")) {
132 if (remove_ids(SEM, argc-2, &argv[2]))
133 bb_fflush_stdout_and_exit(1);
136 bb_printf ("unknown resource type: %s\n", argv[1]);
138 bb_fflush_stdout_and_exit(1);
141 bb_printf ("resource(s) deleted\n");
146 int ipcrm_main(int argc, char **argv)
150 char *prog = argv[0];
152 /* if the command is executed without parameters, do nothing */
156 /* 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);
163 /* process new syntax to conform with SYSV ipcrm */
164 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
167 int iskey = isupper(c);
169 /* needed to delete semaphores */
173 if ((c == '?') || (c == 'h'))
179 /* we don't need case information any more */
182 /* make sure the option is in range */
183 if (c != 'q' && c != 'm' && c != 's') {
190 /* keys are in hex or decimal */
191 key_t key = strtoul(optarg, NULL, 0);
192 if (key == IPC_PRIVATE) {
194 bb_fprintf(stderr, "%s: illegal key (%s)\n",
199 /* convert key to id */
200 id = ((c == 'q') ? msgget(key, 0) :
201 (c == 'm') ? shmget(key, 0, 0) :
209 errmsg = "permission denied for key";
212 errmsg = "already removed key";
215 errmsg = "invalid key";
218 errmsg = "unknown error in key";
221 bb_fprintf(stderr, "%s: %s (%s)\n",
222 prog, errmsg, optarg);
226 /* ids are in decimal */
227 id = strtoul(optarg, NULL, 10);
230 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
231 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
232 semctl(id, 0, IPC_RMID, arg));
241 ? "permission denied for key"
242 : "permission denied for id";
251 ? "already removed key"
252 : "already removed id";
256 ? "unknown error in key"
257 : "unknown error in id";
260 bb_fprintf(stderr, "%s: %s (%s)\n",
261 prog, errmsg, optarg);
266 /* print usage if we still have some arguments left over */
267 if (optind != argc) {
271 /* exit value reflects the number of errors encountered */