1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* krishna balasubramanian 1993
*
* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
* - added Native Language Support
*
* 1999-04-02 frank zago
* - can now remove several id's in the same call
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include "nls.h"
/* for getopt */
#include <unistd.h>
/* for tolower and isupper */
#include <ctype.h>
#ifndef HAVE_UNION_SEMUN
/* according to X/OPEN we have to define it ourselves */
union semun {
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
#endif
static void usage(char *);
char *execname;
typedef enum type_id {
SHM,
SEM,
MSG
} type_id;
static int
remove_ids(type_id type, int argc, char **argv) {
int id;
int ret = 0; /* for gcc */
char *end;
int nb_errors = 0;
union semun arg;
arg.val = 0;
while(argc) {
id = strtoul(argv[0], &end, 10);
if (*end != 0) {
printf (_("invalid id: %s\n"), argv[0]);
nb_errors ++;
} else {
switch(type) {
case SEM:
ret = semctl (id, 0, IPC_RMID, arg);
break;
case MSG:
ret = msgctl (id, IPC_RMID, NULL);
break;
case SHM:
ret = shmctl (id, IPC_RMID, NULL);
break;
}
if (ret) {
printf (_("cannot remove id %s (%s)\n"),
argv[0], strerror(errno));
nb_errors ++;
}
}
argc--;
argv++;
}
return(nb_errors);
}
static void deprecate_display_usage(void)
{
usage(execname);
printf (_("deprecated usage: %s {shm | msg | sem} id ...\n"),
execname);
}
static int deprecated_main(int argc, char **argv)
{
execname = argv[0];
if (argc < 3) {
deprecate_display_usage();
exit(1);
}
if (!strcmp(argv[1], "shm")) {
if (remove_ids(SHM, argc-2, &argv[2]))
exit(1);
}
else if (!strcmp(argv[1], "msg")) {
if (remove_ids(MSG, argc-2, &argv[2]))
exit(1);
}
else if (!strcmp(argv[1], "sem")) {
if (remove_ids(SEM, argc-2, &argv[2]))
exit(1);
}
else {
deprecate_display_usage();
printf (_("unknown resource type: %s\n"), argv[1]);
exit(1);
}
printf (_("resource(s) deleted\n"));
return 0;
}
/* print the new usage */
static void
usage(char *progname)
{
fprintf(stderr,
_("usage: %s [ [-q msqid] [-m shmid] [-s semid]\n"
" [-Q msgkey] [-M shmkey] [-S semkey] ... ]\n"),
progname);
}
int main(int argc, char **argv)
{
int c;
int error = 0;
char *prog = argv[0];
/* if the command is executed without parameters, do nothing */
if (argc == 1)
return 0;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
/* check to see if the command is being invoked in the old way if so
then run the old code */
if (strcmp(argv[1], "shm") == 0 ||
strcmp(argv[1], "msg") == 0 ||
strcmp(argv[1], "sem") == 0)
return deprecated_main(argc, argv);
/* process new syntax to conform with SYSV ipcrm */
while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) {
int result;
int id = 0;
int iskey = isupper(c);
/* needed to delete semaphores */
union semun arg;
arg.val = 0;
/* we don't need case information any more */
c = tolower(c);
/* make sure the option is in range */
if (c != 'q' && c != 'm' && c != 's') {
usage(prog);
error++;
return error;
}
if (iskey) {
/* keys are in hex or decimal */
key_t key = strtoul(optarg, NULL, 0);
if (key == IPC_PRIVATE) {
error++;
fprintf(stderr, _("%s: illegal key (%s)\n"),
prog, optarg);
continue;
}
/* convert key to id */
id = ((c == 'q') ? msgget(key, 0) :
(c == 'm') ? shmget(key, 0, 0) :
semget(key, 0, 0));
if (id < 0) {
char *errmsg;
error++;
switch(errno) {
case EACCES:
errmsg = _("permission denied for key");
break;
case EIDRM:
errmsg = _("already removed key");
break;
case ENOENT:
errmsg = _("invalid key");
break;
default:
errmsg = _("unknown error in key");
break;
}
fprintf(stderr, "%s: %s (%s)\n",
prog, errmsg, optarg);
continue;
}
} else {
/* ids are in decimal */
id = strtoul(optarg, NULL, 10);
}
result = ((c == 'q') ? msgctl(id, IPC_RMID, NULL) :
(c == 'm') ? shmctl(id, IPC_RMID, NULL) :
semctl(id, 0, IPC_RMID, arg));
if (result < 0) {
char *errmsg;
error++;
switch(errno) {
case EACCES:
case EPERM:
errmsg = iskey
? _("permission denied for key")
: _("permission denied for id");
break;
case EINVAL:
errmsg = iskey
? _("invalid key")
: _("invalid id");
break;
case EIDRM:
errmsg = iskey
? _("already removed key")
: _("already removed id");
break;
default:
errmsg = iskey
? _("unknown error in key")
: _("unknown error in id");
break;
}
fprintf(stderr, _("%s: %s (%s)\n"),
prog, errmsg, optarg);
continue;
}
}
/* print usage if we still have some arguments left over */
if (optind != argc) {
fprintf(stderr, _("%s: unknown argument: %s\n"),
prog, argv[optind]);
usage(prog);
}
/* exit value reflects the number of errors encountered */
return error;
}
|