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 (3.5 kb)"
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_NOEXEC(ipcrm, ipcrm, BB_DIR_USR_BIN, BB_SUID_DROP, ipcrm))
20
//kbuild:lib-$(CONFIG_IPCRM) += ipcrm.o
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() */
31
#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
32
/* union semun is defined by including <sys/sem.h> */
34
/* according to X/OPEN we have to define it ourselves */
38
unsigned short *array;
39
struct seminfo *__buf;
43
#define IPCRM_LEGACY 1
48
typedef enum type_id {
54
static int remove_ids(type_id type, char **argv)
63
id = bb_strtoul(argv[0], NULL, 10);
64
if (errno || id > INT_MAX) {
65
bb_error_msg("invalid id: %s", argv[0]);
70
ret = semctl(id, 0, IPC_RMID, arg);
72
ret = msgctl(id, IPC_RMID, NULL);
74
ret = shmctl(id, IPC_RMID, NULL);
77
bb_perror_msg("can't remove id %s", argv[0]);
86
#endif /* IPCRM_LEGACY */
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"
97
int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
98
int ipcrm_main(int argc, char **argv)
103
/* if the command is executed without parameters, do nothing */
107
/* check to see if the command is being invoked in the old way if so
108
then run the old code. Valid commands are msg, shm, sem. */
110
type_id what = 0; /* silence gcc */
114
if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
115
|| (argv[1][0] == 's'
116
&& ((w = argv[1][1]) == 'h' || w == 'e')
117
&& argv[1][2] == 'm')
118
) && argv[1][3] == '\0'
130
if (remove_ids(what, &argv[2]))
131
fflush_stdout_and_exit(EXIT_FAILURE);
132
puts("resource(s) deleted");
136
#endif /* IPCRM_LEGACY */
138
/* process new syntax to conform with SYSV ipcrm */
139
while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) {
143
/* needed to delete semaphores */
146
if (c == '?') /* option not in the string */
152
iskey = !(c & 0x20); /* uppercase? */
154
/* keys are in hex or decimal */
155
key_t key = xstrtoul(optarg, 0);
157
if (key == IPC_PRIVATE) {
159
bb_error_msg("illegal key (%s)", optarg);
163
c |= 0x20; /* lowercase. c is 'q', 'm' or 's' now */
164
/* convert key to id */
165
id = ((c == 'q') ? msgget(key, 0) :
166
(c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
174
errmsg = "permission denied for";
177
errmsg = "already removed";
183
errmsg = "unknown error in";
186
bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
190
/* ids are in decimal */
194
result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
195
(c == 'm') ? shmctl(id, IPC_RMID, NULL) :
196
semctl(id, 0, IPC_RMID, arg));
200
const char *const what = iskey ? "key" : "id";
206
errmsg = "permission denied for";
212
errmsg = "already removed";
215
errmsg = "unknown error in";
218
bb_error_msg("%s %s (%s)", errmsg, what, optarg);
223
/* print usage if we still have some arguments left over */
224
if (optind != argc) {
228
/* exit value reflects the number of errors encountered */