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"
17 //usage: "\n -mM Remove memory segment after last detach"
18 //usage: "\n -qQ Remove message queue"
19 //usage: "\n -sS Remove semaphore"
23 /* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
24 /* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
30 #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
31 /* union semun is defined by including <sys/sem.h> */
33 /* according to X/OPEN we have to define it ourselves */
37 unsigned short *array;
38 struct seminfo *__buf;
42 #define IPCRM_LEGACY 1
47 typedef enum type_id {
53 static int remove_ids(type_id type, char **argv)
62 id = bb_strtoul(argv[0], NULL, 10);
63 if (errno || id > INT_MAX) {
64 bb_error_msg("invalid id: %s", argv[0]);
69 ret = semctl(id, 0, IPC_RMID, arg);
71 ret = msgctl(id, IPC_RMID, NULL);
73 ret = shmctl(id, IPC_RMID, NULL);
76 bb_perror_msg("can't remove id %s", argv[0]);
85 #endif /* IPCRM_LEGACY */
88 int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
89 int ipcrm_main(int argc, char **argv)
94 /* if the command is executed without parameters, do nothing */
98 /* check to see if the command is being invoked in the old way if so
99 then run the old code. Valid commands are msg, shm, sem. */
101 type_id what = 0; /* silence gcc */
105 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
106 || (argv[1][0] == 's'
107 && ((w = argv[1][1]) == 'h' || w == 'e')
108 && argv[1][2] == 'm')
109 ) && argv[1][3] == '\0'
121 if (remove_ids(what, &argv[2]))
122 fflush_stdout_and_exit(EXIT_FAILURE);
123 printf("resource(s) deleted\n");
127 #endif /* IPCRM_LEGACY */
129 /* process new syntax to conform with SYSV ipcrm */
130 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
133 int iskey = isupper(c);
135 /* needed to delete semaphores */
140 if ((c == '?') || (c == 'h')) {
144 /* we don't need case information any more */
147 /* make sure the option is in range: allowed are q, m, s */
148 if (c != 'q' && c != 'm' && c != 's') {
153 /* keys are in hex or decimal */
154 key_t key = xstrtoul(optarg, 0);
156 if (key == IPC_PRIVATE) {
158 bb_error_msg("illegal key (%s)", optarg);
162 /* convert key to id */
163 id = ((c == 'q') ? msgget(key, 0) :
164 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
172 errmsg = "permission denied for";
175 errmsg = "already removed";
181 errmsg = "unknown error in";
184 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
188 /* ids are in decimal */
192 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
193 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
194 semctl(id, 0, IPC_RMID, arg));
198 const char *const what = iskey ? "key" : "id";
204 errmsg = "permission denied for";
210 errmsg = "already removed";
213 errmsg = "unknown error in";
216 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
221 /* print usage if we still have some arguments left over */
222 if (optind != argc) {
226 /* exit value reflects the number of errors encountered */