blob: 6360c706f080237d046d907e4a7ca1647935c83a [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
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000043static int remove_ids(type_id type, int argc, char **argv)
44{
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000045 unsigned long id;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000046 int ret = 0; /* silence gcc */
Rob Landley6eb1e412005-06-20 04:30:36 +000047 int nb_errors = 0;
48 union semun arg;
49
50 arg.val = 0;
51
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000052 while (argc) {
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000053 id = bb_strtoul(argv[0], NULL, 10);
54 if (errno || id > INT_MAX) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000055 bb_error_msg("invalid id: %s", argv[0]);
56 nb_errors++;
Rob Landley6eb1e412005-06-20 04:30:36 +000057 } else {
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 }
70 argc--;
71 argv++;
72 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000073
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000074 return nb_errors;
Rob Landley6eb1e412005-06-20 04:30:36 +000075}
Denis Vlasenkoe3241842007-08-13 10:36:25 +000076#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +000077
78
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000079int ipcrm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley6eb1e412005-06-20 04:30:36 +000080int ipcrm_main(int argc, char **argv)
81{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000082 int c;
83 int error = 0;
Rob Landley6eb1e412005-06-20 04:30:36 +000084
85 /* if the command is executed without parameters, do nothing */
86 if (argc == 1)
87 return 0;
Denis Vlasenkoe3241842007-08-13 10:36:25 +000088#if IPCRM_LEGACY
Rob Landley6eb1e412005-06-20 04:30:36 +000089 /* check to see if the command is being invoked in the old way if so
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000090 then run the old code. Valid commands are msg, shm, sem. */
91 {
92 type_id what = 0; /* silence gcc */
93 char w;
94
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +000095 w=argv[1][0];
96 if ( ((w == 'm' && argv[1][1] == 's' && argv[1][2] == 'g')
97 || (argv[1][0] == 's'
98 && ((w=argv[1][1]) == 'h' || w == 'e')
99 && argv[1][2] == 'm')
100 ) && argv[1][3] == '\0'
101 ) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000102
103 if (argc < 3)
104 bb_show_usage();
105
106 if (w == 'h')
107 what = SHM;
108 else if (w == 'm')
109 what = MSG;
110 else if (w == 'e')
111 what = SEM;
112
113 if (remove_ids(what, argc-2, &argv[2]))
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000114 fflush_stdout_and_exit(EXIT_FAILURE);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000115 printf("resource(s) deleted\n");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000116 return 0;
117 }
118 }
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000119#endif /* IPCRM_LEGACY */
Rob Landley6eb1e412005-06-20 04:30:36 +0000120
121 /* process new syntax to conform with SYSV ipcrm */
122 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:h?")) != -1) {
123 int result;
124 int id = 0;
Denys Vlasenkof2cbb032009-10-23 03:16:08 +0200125 int iskey = isupper(c);
Rob Landley6eb1e412005-06-20 04:30:36 +0000126
127 /* needed to delete semaphores */
128 union semun arg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000129
Rob Landley6eb1e412005-06-20 04:30:36 +0000130 arg.val = 0;
131
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000132 if ((c == '?') || (c == 'h')) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000133 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000134 }
135
136 /* we don't need case information any more */
137 c = tolower(c);
138
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000139 /* make sure the option is in range: allowed are q, m, s */
Rob Landley6eb1e412005-06-20 04:30:36 +0000140 if (c != 'q' && c != 'm' && c != 's') {
141 bb_show_usage();
Rob Landley6eb1e412005-06-20 04:30:36 +0000142 }
143
144 if (iskey) {
145 /* keys are in hex or decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000146 key_t key = xstrtoul(optarg, 0);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000147
Rob Landley6eb1e412005-06-20 04:30:36 +0000148 if (key == IPC_PRIVATE) {
149 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000150 bb_error_msg("illegal key (%s)", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000151 continue;
152 }
153
154 /* convert key to id */
155 id = ((c == 'q') ? msgget(key, 0) :
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000156 (c == 'm') ? shmget(key, 0, 0) : semget(key, 0, 0));
Rob Landley6eb1e412005-06-20 04:30:36 +0000157
158 if (id < 0) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000159 const char *errmsg;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000160
Rob Landley6eb1e412005-06-20 04:30:36 +0000161 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000162 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000163 case EACCES:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000164 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000165 break;
166 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000167 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000168 break;
169 case ENOENT:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000170 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000171 break;
172 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000173 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000174 break;
175 }
Denis Vlasenko240a1cf2007-04-08 16:07:02 +0000176 bb_error_msg("%s %s (%s)", errmsg, "key", optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000177 continue;
178 }
179 } else {
180 /* ids are in decimal */
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000181 id = xatoul(optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000182 }
183
184 result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000185 (c == 'm') ? shmctl(id, IPC_RMID, NULL) :
186 semctl(id, 0, IPC_RMID, arg));
Rob Landley6eb1e412005-06-20 04:30:36 +0000187
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000188 if (result) {
Denis Vlasenko06c0a712007-01-29 22:51:44 +0000189 const char *errmsg;
190 const char *const what = iskey ? "key" : "id";
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000191
Rob Landley6eb1e412005-06-20 04:30:36 +0000192 error++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000193 switch (errno) {
Rob Landley6eb1e412005-06-20 04:30:36 +0000194 case EACCES:
195 case EPERM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000196 errmsg = "permission denied for";
Rob Landley6eb1e412005-06-20 04:30:36 +0000197 break;
198 case EINVAL:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000199 errmsg = "invalid";
Rob Landley6eb1e412005-06-20 04:30:36 +0000200 break;
201 case EIDRM:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000202 errmsg = "already removed";
Rob Landley6eb1e412005-06-20 04:30:36 +0000203 break;
204 default:
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000205 errmsg = "unknown error in";
Rob Landley6eb1e412005-06-20 04:30:36 +0000206 break;
207 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000208 bb_error_msg("%s %s (%s)", errmsg, what, optarg);
Rob Landley6eb1e412005-06-20 04:30:36 +0000209 continue;
210 }
211 }
212
213 /* print usage if we still have some arguments left over */
214 if (optind != argc) {
215 bb_show_usage();
216 }
217
218 /* exit value reflects the number of errors encountered */
219 return error;
220}