blob: 274050cdf2f078d469ccfaa20efc5b07f1d0375d [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 */
10
Pere Orga5bc8c002011-04-11 03:29:49 +020011//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"
Pere Orga5bc8c002011-04-11 03:29:49 +020016//usage: "\n -mM Remove memory segment after last detach"
17//usage: "\n -qQ Remove message queue"
18//usage: "\n -sS Remove semaphore"
19
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000021
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000022/* 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() */
Rob Landley6eb1e412005-06-20 04:30:36 +000024#include <sys/ipc.h>
25#include <sys/shm.h>
26#include <sys/msg.h>
27#include <sys/sem.h>
28
Denis Vlasenkoff131b92007-04-10 15:42:06 +000029#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000030/* union semun is defined by including <sys/sem.h> */
31#else
32/* according to X/OPEN we have to define it ourselves */
33union semun {
34 int val;
35 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000036 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000037 struct seminfo *__buf;
38};
39#endif
40
Denis Vlasenkoe3241842007-08-13 10:36:25 +000041#define IPCRM_LEGACY 1
42
43
44#if IPCRM_LEGACY
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000045
Rob Landley6eb1e412005-06-20 04:30:36 +000046typedef enum type_id {
47 SHM,
48 SEM,
49 MSG
50} type_id;
51
Denys Vlasenko756e95e2010-10-28 19:10:46 +020052static int remove_ids(type_id type, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000053{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000054 unsigned long id;
Rob Landley6eb1e412005-06-20 04:30:36 +000055 int nb_errors = 0;
56 union semun arg;
57
58 arg.val = 0;
59
Denys Vlasenko756e95e2010-10-28 19:10:46 +020060 while (argv[0]) {
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000061 id = bb_strtoul(argv[0], NULL, 10);
62 if (errno || id > INT_MAX) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000063 bb_error_msg("invalid id: %s", argv[0]);
64 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000065 } else {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020066 int ret = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000067 if (type == SEM)
68 ret = semctl(id, 0, IPC_RMID, arg);
69 else if (type == MSG)
70 ret = msgctl(id, IPC_RMID, NULL);
71 else if (type == SHM)
72 ret = shmctl(id, IPC_RMID, NULL);
Rob Landley6eb1e412005-06-20 04:30:36 +000073
74 if (ret) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010075 bb_perror_msg("can't remove id %s", argv[0]);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000076 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000077 }
78 }
Rob Landley6eb1e412005-06-20 04:30:36 +000079 argv++;
80 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000081
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000082 return nb_errors;
Rob Landley6eb1e412005-06-20 04:30:36 +000083}
Denis Vlasenkoe3241842007-08-13 10:36:25 +000084#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +000085
86
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000087int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley6eb1e412005-06-20 04:30:36 +000088int ipcrm_main(int argc, char **argv)
89{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000090 int c;
91 int error = 0;
Rob Landley6eb1e412005-06-20 04:30:36 +000092
93 /* if the command is executed without parameters, do nothing */
94 if (argc == 1)
95 return 0;
Denis Vlasenkoe3241842007-08-13 10:36:25 +000096#if IPCRM_LEGACY
Rob Landley6eb1e412005-06-20 04:30:36 +000097 /* check to see if the command is being invoked in the old way if so
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000098 then run the old code. Valid commands are msg, shm, sem. */
99 {
100 type_id what = 0; /* silence gcc */
101 char w;
102
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200103 w = argv[1][0];
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000104 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
105 || (argv[1][0] == 's'
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200106 && ((w = argv[1][1]) == 'h' || w == 'e')
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000107 && argv[1][2] == 'm')
108 ) && argv[1][3] == '\0'
109 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000110 if (argc < 3)
111 bb_show_usage();
112
113 if (w == 'h')
114 what = SHM;
115 else if (w == 'm')
116 what = MSG;
117 else if (w == 'e')
118 what = SEM;
119
Denys Vlasenko756e95e2010-10-28 19:10:46 +0200120 if (remove_ids(what, &argv[2]))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000121 fflush_stdout_and_exit(EXIT_FAILURE);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000122 printf("resource(s) deleted\n");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000123 return 0;
124 }
125 }
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000126#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +0000127
128 /* process new syntax to conform with SYSV ipcrm */
129 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
130 int result;
131 int id = 0;
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200132 int iskey = isupper(c);
Rob Landley6eb1e412005-06-20 04:30:36 +0000133
134 /* needed to delete semaphores */
135 union semun arg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000136
Rob Landley6eb1e412005-06-20 04:30:36 +0000137 arg.val = 0;
138
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000139 if ((c == '?') || (c == 'h')) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000140 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000141 }
142
143 /* we don't need case information any more */
144 c = tolower(c);
145
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000146 /* make sure the option is in range: allowed are q, m, s */
Rob Landley6eb1e412005-06-20 04:30:36 +0000147 if (c != 'q' && c != 'm' && c != 's') {
148 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000149 }
150
151 if (iskey) {
152 /* keys are in hex or decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000153 key_t key = xstrtoul(optarg, 0);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000154
Rob Landley6eb1e412005-06-20 04:30:36 +0000155 if (key == IPC_PRIVATE) {
156 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000157 bb_error_msg("illegal key (%s)", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000158 continue;
159 }
160
161 /* convert key to id */
162 id = ((c == 'q') ? msgget(key, 0) :
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000163 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
Rob Landley6eb1e412005-06-20 04:30:36 +0000164
165 if (id < 0) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000166 const char *errmsg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000167
Rob Landley6eb1e412005-06-20 04:30:36 +0000168 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000169 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000170 case EACCES:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000171 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000172 break;
173 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000174 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000175 break;
176 case ENOENT:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000177 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000178 break;
179 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000180 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000181 break;
182 }
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000183 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000184 continue;
185 }
186 } else {
187 /* ids are in decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000188 id = xatoul(optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000189 }
190
191 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000192 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
193 semctl(id, 0, IPC_RMID, arg));
Rob Landley6eb1e412005-06-20 04:30:36 +0000194
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000195 if (result) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000196 const char *errmsg;
197 const char *const what = iskey ? "key" : "id";
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198
Rob Landley6eb1e412005-06-20 04:30:36 +0000199 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000200 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000201 case EACCES:
202 case EPERM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000203 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000204 break;
205 case EINVAL:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000206 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000207 break;
208 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000209 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000210 break;
211 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000212 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000213 break;
214 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000215 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000216 continue;
217 }
218 }
219
220 /* print usage if we still have some arguments left over */
221 if (optind != argc) {
222 bb_show_usage();
223 }
224
225 /* exit value reflects the number of errors encountered */
226 return error;
227}