blob: cabd8b9e74a980e564c7f71e32159581cba133b8 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Rob Landley6eb1e412005-06-20 04:30:36 +00002/*
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +00003 * ipcrm.c - utility to allow removal of IPC objects and data structures.
Rob Landley6eb1e412005-06-20 04:30:36 +00004 *
5 * 01 Sept 2004 - Rodney Radford <rradford@mindspring.com>
6 * Adapted for busybox from util-linux-2.12a.
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley6eb1e412005-06-20 04:30:36 +00009 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010010//config:config IPCRM
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "ipcrm (2.9 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010012//config: default y
13//config: help
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.
17
18//applet:IF_IPCRM(APPLET(ipcrm, BB_DIR_USR_BIN, BB_SUID_DROP))
19
20//kbuild:lib-$(CONFIG_IPCRM) += ipcrm.o
Rob Landley6eb1e412005-06-20 04:30:36 +000021
Pere Orga5bc8c002011-04-11 03:29:49 +020022//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"
Pere Orga5bc8c002011-04-11 03:29:49 +020027//usage: "\n -mM Remove memory segment after last detach"
28//usage: "\n -qQ Remove message queue"
29//usage: "\n -sS Remove semaphore"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000032
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000033/* 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() */
Rob Landley6eb1e412005-06-20 04:30:36 +000035#include <sys/ipc.h>
36#include <sys/shm.h>
37#include <sys/msg.h>
38#include <sys/sem.h>
39
Denis Vlasenkoff131b92007-04-10 15:42:06 +000040#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000041/* union semun is defined by including <sys/sem.h> */
42#else
43/* according to X/OPEN we have to define it ourselves */
44union semun {
45 int val;
46 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000047 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000048 struct seminfo *__buf;
49};
50#endif
51
Denis Vlasenkoe3241842007-08-13 10:36:25 +000052#define IPCRM_LEGACY 1
53
54
55#if IPCRM_LEGACY
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000056
Rob Landley6eb1e412005-06-20 04:30:36 +000057typedef enum type_id {
58 SHM,
59 SEM,
60 MSG
61} type_id;
62
Denys Vlasenko756e95e2010-10-28 19:10:46 +020063static int remove_ids(type_id type, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000064{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000065 unsigned long id;
Rob Landley6eb1e412005-06-20 04:30:36 +000066 int nb_errors = 0;
67 union semun arg;
68
69 arg.val = 0;
70
Denys Vlasenko756e95e2010-10-28 19:10:46 +020071 while (argv[0]) {
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000072 id = bb_strtoul(argv[0], NULL, 10);
73 if (errno || id > INT_MAX) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000074 bb_error_msg("invalid id: %s", argv[0]);
75 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000076 } else {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020077 int ret = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000078 if (type == SEM)
79 ret = semctl(id, 0, IPC_RMID, arg);
80 else if (type == MSG)
81 ret = msgctl(id, IPC_RMID, NULL);
82 else if (type == SHM)
83 ret = shmctl(id, IPC_RMID, NULL);
Rob Landley6eb1e412005-06-20 04:30:36 +000084
85 if (ret) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010086 bb_perror_msg("can't remove id %s", argv[0]);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000087 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000088 }
89 }
Rob Landley6eb1e412005-06-20 04:30:36 +000090 argv++;
91 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000092
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000093 return nb_errors;
Rob Landley6eb1e412005-06-20 04:30:36 +000094}
Denis Vlasenkoe3241842007-08-13 10:36:25 +000095#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +000096
97
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000098int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley6eb1e412005-06-20 04:30:36 +000099int ipcrm_main(int argc, char **argv)
100{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101 int c;
102 int error = 0;
Rob Landley6eb1e412005-06-20 04:30:36 +0000103
104 /* if the command is executed without parameters, do nothing */
105 if (argc == 1)
106 return 0;
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000107#if IPCRM_LEGACY
Rob Landley6eb1e412005-06-20 04:30:36 +0000108 /* check to see if the command is being invoked in the old way if so
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000109 then run the old code. Valid commands are msg, shm, sem. */
110 {
111 type_id what = 0; /* silence gcc */
112 char w;
113
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200114 w = argv[1][0];
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000115 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
116 || (argv[1][0] == 's'
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200117 && ((w = argv[1][1]) == 'h' || w == 'e')
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000118 && argv[1][2] == 'm')
119 ) && argv[1][3] == '\0'
120 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000121 if (argc < 3)
122 bb_show_usage();
123
124 if (w == 'h')
125 what = SHM;
126 else if (w == 'm')
127 what = MSG;
128 else if (w == 'e')
129 what = SEM;
130
Denys Vlasenko756e95e2010-10-28 19:10:46 +0200131 if (remove_ids(what, &argv[2]))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000132 fflush_stdout_and_exit(EXIT_FAILURE);
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200133 puts("resource(s) deleted");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000134 return 0;
135 }
136 }
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000137#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +0000138
139 /* process new syntax to conform with SYSV ipcrm */
140 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
141 int result;
142 int id = 0;
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200143 int iskey = isupper(c);
Rob Landley6eb1e412005-06-20 04:30:36 +0000144
145 /* needed to delete semaphores */
146 union semun arg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000147
Rob Landley6eb1e412005-06-20 04:30:36 +0000148 arg.val = 0;
149
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000150 if ((c == '?') || (c == 'h')) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000151 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000152 }
153
154 /* we don't need case information any more */
155 c = tolower(c);
156
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000157 /* make sure the option is in range: allowed are q, m, s */
Rob Landley6eb1e412005-06-20 04:30:36 +0000158 if (c != 'q' && c != 'm' && c != 's') {
159 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000160 }
161
162 if (iskey) {
163 /* keys are in hex or decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000164 key_t key = xstrtoul(optarg, 0);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000165
Rob Landley6eb1e412005-06-20 04:30:36 +0000166 if (key == IPC_PRIVATE) {
167 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000168 bb_error_msg("illegal key (%s)", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000169 continue;
170 }
171
172 /* convert key to id */
173 id = ((c == 'q') ? msgget(key, 0) :
Denys Vlasenko69675782013-01-14 01:34:48 +0100174 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
Rob Landley6eb1e412005-06-20 04:30:36 +0000175
176 if (id < 0) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000177 const char *errmsg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000178
Rob Landley6eb1e412005-06-20 04:30:36 +0000179 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000180 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000181 case EACCES:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000182 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000183 break;
184 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000185 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000186 break;
187 case ENOENT:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000188 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000189 break;
190 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000191 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000192 break;
193 }
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000194 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000195 continue;
196 }
197 } else {
198 /* ids are in decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000199 id = xatoul(optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000200 }
201
202 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
Denys Vlasenko69675782013-01-14 01:34:48 +0100203 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
204 semctl(id, 0, IPC_RMID, arg));
Rob Landley6eb1e412005-06-20 04:30:36 +0000205
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000206 if (result) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000207 const char *errmsg;
208 const char *const what = iskey ? "key" : "id";
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000209
Rob Landley6eb1e412005-06-20 04:30:36 +0000210 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000211 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000212 case EACCES:
213 case EPERM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000214 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000215 break;
216 case EINVAL:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000217 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000218 break;
219 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000220 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000221 break;
222 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000223 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000224 break;
225 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000226 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000227 continue;
228 }
229 }
230
231 /* print usage if we still have some arguments left over */
232 if (optind != argc) {
233 bb_show_usage();
234 }
235
236 /* exit value reflects the number of errors encountered */
237 return error;
238}