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 //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"
16 //usage: "\n -mM Remove memory segment after last detach"
17 //usage: "\n -qQ Remove message queue"
18 //usage: "\n -sS Remove semaphore"
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() */
29 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
30 /* union semun is defined by including <sys/sem.h> */
32 /* according to X/OPEN we have to define it ourselves */
36 unsigned short *array;
37 struct seminfo *__buf;
41 #define IPCRM_LEGACY 1
46 typedef enum type_id {
52 static int remove_ids(type_id type, char **argv)
61 id = bb_strtoul(argv[0], NULL, 10);
62 if (errno || id > INT_MAX) {
63 bb_error_msg("invalid id: %s", argv[0]);
68 ret = semctl(id, 0, IPC_RMID, arg);
70 ret = msgctl(id, IPC_RMID, NULL);
72 ret = shmctl(id, IPC_RMID, NULL);
75 bb_perror_msg("can't remove id %s", argv[0]);
84 #endif /* IPCRM_LEGACY */
87 int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
88 int ipcrm_main(int argc, char **argv)
93 /* if the command is executed without parameters, do nothing */
97 /* check to see if the command is being invoked in the old way if so
98 then run the old code. Valid commands are msg, shm, sem. */
100 type_id what = 0; /* silence gcc */
104 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
105 || (argv[1][0] == 's'
106 && ((w = argv[1][1]) == 'h' || w == 'e')
107 && argv[1][2] == 'm')
108 ) && argv[1][3] == '\0'
120 if (remove_ids(what, &argv[2]))
121 fflush_stdout_and_exit(EXIT_FAILURE);
122 puts("resource(s) deleted");
126 #endif /* IPCRM_LEGACY */
128 /* process new syntax to conform with SYSV ipcrm */
129 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
132 int iskey = isupper(c);
134 /* needed to delete semaphores */
139 if ((c == '?') || (c == 'h')) {
143 /* we don't need case information any more */
146 /* make sure the option is in range: allowed are q, m, s */
147 if (c != 'q' && c != 'm' && c != 's') {
152 /* keys are in hex or decimal */
153 key_t key = xstrtoul(optarg, 0);
155 if (key == IPC_PRIVATE) {
157 bb_error_msg("illegal key (%s)", optarg);
161 /* convert key to id */
162 id = ((c == 'q') ? msgget(key, 0) :
163 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
171 errmsg = "permission denied for";
174 errmsg = "already removed";
180 errmsg = "unknown error in";
183 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
187 /* ids are in decimal */
191 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
192 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
193 semctl(id, 0, IPC_RMID, arg));
197 const char *const what = iskey ? "key" : "id";
203 errmsg = "permission denied for";
209 errmsg = "already removed";
212 errmsg = "unknown error in";
215 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
220 /* print usage if we still have some arguments left over */
221 if (optind != argc) {
225 /* exit value reflects the number of errors encountered */