blob: e597ed637eb709ce49a67792b1ec947641bc1fa1 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Rob Landley6eb1e412005-06-20 04:30:36 +000012
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000013/* X/OPEN tells us to use <sys/{types,ipc,sem}.h> for semctl() */
14/* X/OPEN tells us to use <sys/{types,ipc,msg}.h> for msgctl() */
Rob Landley6eb1e412005-06-20 04:30:36 +000015#include <sys/ipc.h>
16#include <sys/shm.h>
17#include <sys/msg.h>
18#include <sys/sem.h>
19
Denis Vlasenkoff131b92007-04-10 15:42:06 +000020#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
Rob Landley6eb1e412005-06-20 04:30:36 +000021/* union semun is defined by including <sys/sem.h> */
22#else
23/* according to X/OPEN we have to define it ourselves */
24union semun {
25 int val;
26 struct semid_ds *buf;
Denis Vlasenko284d0fa2008-02-16 13:18:17 +000027 unsigned short *array;
Rob Landley6eb1e412005-06-20 04:30:36 +000028 struct seminfo *__buf;
29};
30#endif
31
Denis Vlasenkoe3241842007-08-13 10:36:25 +000032#define IPCRM_LEGACY 1
33
34
35#if IPCRM_LEGACY
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000036
Rob Landley6eb1e412005-06-20 04:30:36 +000037typedef enum type_id {
38 SHM,
39 SEM,
40 MSG
41} type_id;
42
Denys Vlasenko756e95e2010-10-28 19:10:46 +020043static int remove_ids(type_id type, char **argv)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000045 unsigned long id;
Rob Landley6eb1e412005-06-20 04:30:36 +000046 int nb_errors = 0;
47 union semun arg;
48
49 arg.val = 0;
50
Denys Vlasenko756e95e2010-10-28 19:10:46 +020051 while (argv[0]) {
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000052 id = bb_strtoul(argv[0], NULL, 10);
53 if (errno || id > INT_MAX) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000054 bb_error_msg("invalid id: %s", argv[0]);
55 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000056 } else {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020057 int ret = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000058 if (type == SEM)
59 ret = semctl(id, 0, IPC_RMID, arg);
60 else if (type == MSG)
61 ret = msgctl(id, IPC_RMID, NULL);
62 else if (type == SHM)
63 ret = shmctl(id, IPC_RMID, NULL);
Rob Landley6eb1e412005-06-20 04:30:36 +000064
65 if (ret) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010066 bb_perror_msg("can't remove id %s", argv[0]);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000067 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000068 }
69 }
Rob Landley6eb1e412005-06-20 04:30:36 +000070 argv++;
71 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000072
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000073 return nb_errors;
Rob Landley6eb1e412005-06-20 04:30:36 +000074}
Denis Vlasenkoe3241842007-08-13 10:36:25 +000075#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +000076
77
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000078int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley6eb1e412005-06-20 04:30:36 +000079int ipcrm_main(int argc, char **argv)
80{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000081 int c;
82 int error = 0;
Rob Landley6eb1e412005-06-20 04:30:36 +000083
84 /* if the command is executed without parameters, do nothing */
85 if (argc == 1)
86 return 0;
Denis Vlasenkoe3241842007-08-13 10:36:25 +000087#if IPCRM_LEGACY
Rob Landley6eb1e412005-06-20 04:30:36 +000088 /* check to see if the command is being invoked in the old way if so
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000089 then run the old code. Valid commands are msg, shm, sem. */
90 {
91 type_id what = 0; /* silence gcc */
92 char w;
93
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020094 w = argv[1][0];
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000095 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
96 || (argv[1][0] == 's'
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020097 && ((w = argv[1][1]) == 'h' || w == 'e')
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000098 && argv[1][2] == 'm')
99 ) && argv[1][3] == '\0'
100 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101 if (argc < 3)
102 bb_show_usage();
103
104 if (w == 'h')
105 what = SHM;
106 else if (w == 'm')
107 what = MSG;
108 else if (w == 'e')
109 what = SEM;
110
Denys Vlasenko756e95e2010-10-28 19:10:46 +0200111 if (remove_ids(what, &argv[2]))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000112 fflush_stdout_and_exit(EXIT_FAILURE);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000113 printf("resource(s) deleted\n");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000114 return 0;
115 }
116 }
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000117#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +0000118
119 /* process new syntax to conform with SYSV ipcrm */
120 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
121 int result;
122 int id = 0;
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200123 int iskey = isupper(c);
Rob Landley6eb1e412005-06-20 04:30:36 +0000124
125 /* needed to delete semaphores */
126 union semun arg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000127
Rob Landley6eb1e412005-06-20 04:30:36 +0000128 arg.val = 0;
129
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000130 if ((c == '?') || (c == 'h')) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000131 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000132 }
133
134 /* we don't need case information any more */
135 c = tolower(c);
136
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000137 /* make sure the option is in range: allowed are q, m, s */
Rob Landley6eb1e412005-06-20 04:30:36 +0000138 if (c != 'q' && c != 'm' && c != 's') {
139 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000140 }
141
142 if (iskey) {
143 /* keys are in hex or decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000144 key_t key = xstrtoul(optarg, 0);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000145
Rob Landley6eb1e412005-06-20 04:30:36 +0000146 if (key == IPC_PRIVATE) {
147 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000148 bb_error_msg("illegal key (%s)", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000149 continue;
150 }
151
152 /* convert key to id */
153 id = ((c == 'q') ? msgget(key, 0) :
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000154 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
Rob Landley6eb1e412005-06-20 04:30:36 +0000155
156 if (id < 0) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000157 const char *errmsg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000158
Rob Landley6eb1e412005-06-20 04:30:36 +0000159 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000160 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000161 case EACCES:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000162 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000163 break;
164 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000165 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000166 break;
167 case ENOENT:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000168 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000169 break;
170 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000171 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000172 break;
173 }
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000174 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000175 continue;
176 }
177 } else {
178 /* ids are in decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000179 id = xatoul(optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000180 }
181
182 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000183 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
184 semctl(id, 0, IPC_RMID, arg));
Rob Landley6eb1e412005-06-20 04:30:36 +0000185
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000186 if (result) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000187 const char *errmsg;
188 const char *const what = iskey ? "key" : "id";
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000189
Rob Landley6eb1e412005-06-20 04:30:36 +0000190 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000191 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000192 case EACCES:
193 case EPERM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000194 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000195 break;
196 case EINVAL:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000197 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000198 break;
199 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000200 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000201 break;
202 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000203 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000204 break;
205 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000206 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000207 continue;
208 }
209 }
210
211 /* print usage if we still have some arguments left over */
212 if (optind != argc) {
213 bb_show_usage();
214 }
215
216 /* exit value reflects the number of errors encountered */
217 return error;
218}