blob: a93ceee11879a688a5a3f21aa6bc92efe54d8ef9 [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
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//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.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010017
Denys Vlasenkoc6ce1c92017-08-09 19:24:19 +020018//applet:IF_IPCRM(APPLET_NOEXEC(ipcrm, ipcrm, BB_DIR_USR_BIN, BB_SUID_DROP, ipcrm))
Denys Vlasenkodd898c92016-11-23 11:46:32 +010019
20//kbuild:lib-$(CONFIG_IPCRM) += ipcrm.o
Rob Landley6eb1e412005-06-20 04:30:36 +000021
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000023
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000024/* 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() */
Rob Landley6eb1e412005-06-20 04:30:36 +000026#include <sys/ipc.h>
27#include <sys/shm.h>
28#include <sys/msg.h>
29#include <sys/sem.h>
30
Denis Vlasenkoff131b92007-04-10 15:42:06 +000031#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000032/* union semun is defined by including <sys/sem.h> */
33#else
34/* according to X/OPEN we have to define it ourselves */
35union semun {
36 int val;
37 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000038 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000039 struct seminfo *__buf;
40};
41#endif
42
Denis Vlasenkoe3241842007-08-13 10:36:25 +000043#define IPCRM_LEGACY 1
44
45
46#if IPCRM_LEGACY
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000047
Rob Landley6eb1e412005-06-20 04:30:36 +000048typedef enum type_id {
49 SHM,
50 SEM,
51 MSG
52} type_id;
53
Denys Vlasenko756e95e2010-10-28 19:10:46 +020054static int remove_ids(type_id type, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000055{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000056 unsigned long id;
Rob Landley6eb1e412005-06-20 04:30:36 +000057 int nb_errors = 0;
58 union semun arg;
59
60 arg.val = 0;
61
Denys Vlasenko756e95e2010-10-28 19:10:46 +020062 while (argv[0]) {
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000063 id = bb_strtoul(argv[0], NULL, 10);
64 if (errno || id > INT_MAX) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000065 bb_error_msg("invalid id: %s", argv[0]);
66 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000067 } else {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020068 int ret = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000069 if (type == SEM)
70 ret = semctl(id, 0, IPC_RMID, arg);
71 else if (type == MSG)
72 ret = msgctl(id, IPC_RMID, NULL);
73 else if (type == SHM)
74 ret = shmctl(id, IPC_RMID, NULL);
Rob Landley6eb1e412005-06-20 04:30:36 +000075
76 if (ret) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010077 bb_perror_msg("can't remove id %s", argv[0]);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000078 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000079 }
80 }
Rob Landley6eb1e412005-06-20 04:30:36 +000081 argv++;
82 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000083
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000084 return nb_errors;
Rob Landley6eb1e412005-06-20 04:30:36 +000085}
Denis Vlasenkoe3241842007-08-13 10:36:25 +000086#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +000087
Denys Vlasenko7943be12017-08-09 19:20:08 +020088//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"
Rob Landley6eb1e412005-06-20 04:30:36 +000096
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000097int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley6eb1e412005-06-20 04:30:36 +000098int ipcrm_main(int argc, char **argv)
99{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000100 int c;
101 int error = 0;
Rob Landley6eb1e412005-06-20 04:30:36 +0000102
103 /* if the command is executed without parameters, do nothing */
104 if (argc == 1)
105 return 0;
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000106#if IPCRM_LEGACY
Rob Landley6eb1e412005-06-20 04:30:36 +0000107 /* check to see if the command is being invoked in the old way if so
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000108 then run the old code. Valid commands are msg, shm, sem. */
109 {
110 type_id what = 0; /* silence gcc */
111 char w;
112
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200113 w = argv[1][0];
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000114 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
115 || (argv[1][0] == 's'
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200116 && ((w = argv[1][1]) == 'h' || w == 'e')
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000117 && argv[1][2] == 'm')
118 ) && argv[1][3] == '\0'
119 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000120 if (argc < 3)
121 bb_show_usage();
122
123 if (w == 'h')
124 what = SHM;
125 else if (w == 'm')
126 what = MSG;
127 else if (w == 'e')
128 what = SEM;
129
Denys Vlasenko756e95e2010-10-28 19:10:46 +0200130 if (remove_ids(what, &argv[2]))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000131 fflush_stdout_and_exit(EXIT_FAILURE);
Denys Vlasenkod60752f2015-10-07 22:42:45 +0200132 puts("resource(s) deleted");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000133 return 0;
134 }
135 }
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000136#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +0000137
138 /* process new syntax to conform with SYSV ipcrm */
Denys Vlasenko7943be12017-08-09 19:20:08 +0200139 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000140 int result;
Denys Vlasenko7943be12017-08-09 19:20:08 +0200141 int id;
142 int iskey;
Rob Landley6eb1e412005-06-20 04:30:36 +0000143 /* needed to delete semaphores */
144 union semun arg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000145
Denys Vlasenko7943be12017-08-09 19:20:08 +0200146 if (c == '?') /* option not in the string */
147 bb_show_usage();
148
149 id = 0;
Rob Landley6eb1e412005-06-20 04:30:36 +0000150 arg.val = 0;
151
Denys Vlasenko7943be12017-08-09 19:20:08 +0200152 iskey = !(c & 0x20); /* uppercase? */
Rob Landley6eb1e412005-06-20 04:30:36 +0000153 if (iskey) {
154 /* keys are in hex or decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000155 key_t key = xstrtoul(optarg, 0);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000156
Rob Landley6eb1e412005-06-20 04:30:36 +0000157 if (key == IPC_PRIVATE) {
158 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000159 bb_error_msg("illegal key (%s)", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000160 continue;
161 }
162
Denys Vlasenko7943be12017-08-09 19:20:08 +0200163 c |= 0x20; /* lowercase. c is 'q', 'm' or 's' now */
Rob Landley6eb1e412005-06-20 04:30:36 +0000164 /* convert key to id */
165 id = ((c == 'q') ? msgget(key, 0) :
Denys Vlasenko69675782013-01-14 01:34:48 +0100166 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
Rob Landley6eb1e412005-06-20 04:30:36 +0000167
168 if (id < 0) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000169 const char *errmsg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000170
Rob Landley6eb1e412005-06-20 04:30:36 +0000171 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000172 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000173 case EACCES:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000174 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000175 break;
176 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000177 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000178 break;
179 case ENOENT:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000180 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000181 break;
182 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000183 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000184 break;
185 }
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000186 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000187 continue;
188 }
189 } else {
190 /* ids are in decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000191 id = xatoul(optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000192 }
193
194 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
Denys Vlasenko69675782013-01-14 01:34:48 +0100195 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
196 semctl(id, 0, IPC_RMID, arg));
Rob Landley6eb1e412005-06-20 04:30:36 +0000197
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198 if (result) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000199 const char *errmsg;
200 const char *const what = iskey ? "key" : "id";
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000201
Rob Landley6eb1e412005-06-20 04:30:36 +0000202 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000203 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000204 case EACCES:
205 case EPERM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000206 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000207 break;
208 case EINVAL:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000209 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000210 break;
211 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000212 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000213 break;
214 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000215 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000216 break;
217 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000218 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000219 continue;
220 }
221 }
222
223 /* print usage if we still have some arguments left over */
224 if (optind != argc) {
225 bb_show_usage();
226 }
227
228 /* exit value reflects the number of errors encountered */
229 return error;
230}