1 /* vi: set sw=4 ts=4: */
3 * ipcrm.c - utility to allow removal of IPC objects and data structures.
5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
6 * Adapted for busybox from util-linux-2.12a.
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //config: bool "ipcrm"
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.
18 //applet:IF_IPCRM(APPLET(ipcrm, BB_DIR_USR_BIN, BB_SUID_DROP))
20 //kbuild:lib-$(CONFIG_IPCRM) += ipcrm.o
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"
27 //usage: "\n -mM Remove memory segment after last detach"
28 //usage: "\n -qQ Remove message queue"
29 //usage: "\n -sS Remove semaphore"
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() */
40 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
41 /* union semun is defined by including <sys/sem.h> */
43 /* according to X/OPEN we have to define it ourselves */
47 unsigned short *array;
48 struct seminfo *__buf;
52 #define IPCRM_LEGACY 1
57 typedef enum type_id {
63 static int remove_ids(type_id type, char **argv)
72 id = bb_strtoul(argv[0], NULL, 10);
73 if (errno || id > INT_MAX) {
74 bb_error_msg("invalid id: %s", argv[0]);
79 ret = semctl(id, 0, IPC_RMID, arg);
81 ret = msgctl(id, IPC_RMID, NULL);
83 ret = shmctl(id, IPC_RMID, NULL);
86 bb_perror_msg("can't remove id %s", argv[0]);
95 #endif /* IPCRM_LEGACY */
98 int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
99 int ipcrm_main(int argc, char **argv)
104 /* if the command is executed without parameters, do nothing */
108 /* check to see if the command is being invoked in the old way if so
109 then run the old code. Valid commands are msg, shm, sem. */
111 type_id what = 0; /* silence gcc */
115 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
116 || (argv[1][0] == 's'
117 && ((w = argv[1][1]) == 'h' || w == 'e')
118 && argv[1][2] == 'm')
119 ) && argv[1][3] == '\0'
131 if (remove_ids(what, &argv[2]))
132 fflush_stdout_and_exit(EXIT_FAILURE);
133 puts("resource(s) deleted");
137 #endif /* IPCRM_LEGACY */
139 /* process new syntax to conform with SYSV ipcrm */
140 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
143 int iskey = isupper(c);
145 /* needed to delete semaphores */
150 if ((c == '?') || (c == 'h')) {
154 /* we don't need case information any more */
157 /* make sure the option is in range: allowed are q, m, s */
158 if (c != 'q' && c != 'm' && c != 's') {
163 /* keys are in hex or decimal */
164 key_t key = xstrtoul(optarg, 0);
166 if (key == IPC_PRIVATE) {
168 bb_error_msg("illegal key (%s)", optarg);
172 /* convert key to id */
173 id = ((c == 'q') ? msgget(key, 0) :
174 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
182 errmsg = "permission denied for";
185 errmsg = "already removed";
191 errmsg = "unknown error in";
194 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
198 /* ids are in decimal */
202 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
203 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
204 semctl(id, 0, IPC_RMID, arg));
208 const char *const what = iskey ? "key" : "id";
214 errmsg = "permission denied for";
220 errmsg = "already removed";
223 errmsg = "unknown error in";
226 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
231 /* print usage if we still have some arguments left over */
232 if (optind != argc) {
236 /* exit value reflects the number of errors encountered */