]>
Commit | Line | Data |
---|---|---|
c59d3020 A |
1 | /* |
2 | * Copyright (c) 1994 Adam Glass | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by Adam Glass. | |
16 | * 4. The name of the Author may not be used to endorse or promote products | |
17 | * derived from this software without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
00337e45 | 32 | #include <sys/cdefs.h> |
c59d3020 | 33 | #ifndef lint |
00337e45 | 34 | __used static const char rcsid[] = |
864a4b6e | 35 | "$Id: ipcrm.c,v 1.3 2005/02/03 07:31:33 josborne Exp $"; |
c59d3020 A |
36 | #endif /* not lint */ |
37 | ||
38 | #include <ctype.h> | |
39 | #include <err.h> | |
40 | #include <signal.h> | |
41 | #include <stdio.h> | |
42 | #include <stdlib.h> | |
43 | #include <unistd.h> | |
44 | #include <sys/types.h> | |
45 | #include <sys/ipc.h> | |
c59d3020 A |
46 | #include <sys/msg.h> |
47 | #include <sys/sem.h> | |
c59d3020 A |
48 | #include <sys/shm.h> |
49 | ||
50 | #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem")) | |
51 | #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \ | |
52 | (x == 'M' ? "shared memory segment" : "semaphore")) | |
53 | ||
54 | int signaled; | |
55 | ||
4d0bb651 | 56 | static void usage(void) |
c59d3020 A |
57 | { |
58 | fprintf(stderr, "%s\n%s\n", | |
59 | "usage: ipcrm [-q msqid] [-m shmid] [-s semid]", | |
60 | " [-Q msgkey] [-M shmkey] [-S semkey] ..."); | |
61 | exit(1); | |
62 | } | |
63 | ||
4d0bb651 | 64 | static int msgrm(key_t key, int id) |
c59d3020 | 65 | { |
c59d3020 A |
66 | if (key) { |
67 | id = msgget(key, 0); | |
68 | if (id == -1) | |
69 | return -1; | |
70 | } | |
c59d3020 A |
71 | return msgctl(id, IPC_RMID, NULL); |
72 | } | |
73 | ||
4d0bb651 | 74 | static int shmrm(key_t key, int id) |
c59d3020 A |
75 | { |
76 | if (key) { | |
77 | id = shmget(key, 0, 0); | |
78 | if (id == -1) | |
79 | return -1; | |
80 | } | |
81 | return shmctl(id, IPC_RMID, NULL); | |
82 | } | |
83 | ||
4d0bb651 | 84 | static int semrm(key_t key, int id) |
c59d3020 | 85 | { |
c59d3020 A |
86 | if (key) { |
87 | id = semget(key, 0, 0); | |
88 | if (id == -1) | |
89 | return -1; | |
90 | } | |
e0055cbe | 91 | return semctl(id, 0, IPC_RMID); |
c59d3020 A |
92 | } |
93 | ||
4d0bb651 | 94 | static void not_configured(__unused int unused) |
c59d3020 A |
95 | { |
96 | signaled++; | |
97 | } | |
98 | ||
99 | int main(argc, argv) | |
100 | int argc; | |
101 | char *argv[]; | |
102 | ||
103 | { | |
104 | int c, result, errflg, target_id; | |
105 | key_t target_key; | |
864a4b6e | 106 | char *en; |
c59d3020 A |
107 | |
108 | errflg = 0; | |
109 | signal(SIGSYS, not_configured); | |
110 | while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) { | |
111 | ||
112 | signaled = 0; | |
113 | switch (c) { | |
114 | case 'q': | |
115 | case 'm': | |
116 | case 's': | |
4d0bb651 | 117 | target_id = (int)strtol(optarg, &en, 0); |
864a4b6e A |
118 | if (*en) { |
119 | warnx("%s: '%s' is not a number", | |
120 | IPC_TO_STRING(toupper(c)), optarg); | |
121 | continue; | |
122 | } | |
c59d3020 A |
123 | if (c == 'q') |
124 | result = msgrm(0, target_id); | |
125 | else if (c == 'm') | |
126 | result = shmrm(0, target_id); | |
127 | else | |
128 | result = semrm(0, target_id); | |
129 | if (result < 0) { | |
130 | errflg++; | |
131 | if (!signaled) | |
132 | warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id); | |
133 | else | |
134 | warnx("%ss are not configured in the running kernel", | |
135 | IPC_TO_STRING(toupper(c))); | |
136 | } | |
137 | break; | |
138 | case 'Q': | |
139 | case 'M': | |
140 | case 'S': | |
4d0bb651 | 141 | target_key = (key_t)strtol(optarg, &en, 0); |
864a4b6e A |
142 | if (*en) { |
143 | warnx("%s: '%s' is not a number", IPC_TO_STRING(c), optarg); | |
144 | continue; | |
145 | } | |
c59d3020 A |
146 | if (target_key == IPC_PRIVATE) { |
147 | warnx("can't remove private %ss", IPC_TO_STRING(c)); | |
148 | continue; | |
149 | } | |
150 | if (c == 'Q') | |
151 | result = msgrm(target_key, 0); | |
152 | else if (c == 'M') | |
153 | result = shmrm(target_key, 0); | |
154 | else | |
155 | result = semrm(target_key, 0); | |
156 | if (result < 0) { | |
157 | errflg++; | |
158 | if (!signaled) | |
686e1a44 | 159 | warn("%s key(%d): ", IPC_TO_STRING(c), target_key); |
c59d3020 A |
160 | else |
161 | warnx("%ss are not configured in the running kernel", | |
162 | IPC_TO_STRING(c)); | |
163 | } | |
164 | break; | |
165 | case ':': | |
166 | fprintf(stderr, "option -%c requires an argument\n", optopt); | |
167 | usage(); | |
168 | case '?': | |
169 | fprintf(stderr, "unrecognized option: -%c\n", optopt); | |
170 | usage(); | |
171 | } | |
172 | } | |
173 | ||
174 | if (optind != argc) { | |
175 | fprintf(stderr, "unknown argument: %s\n", argv[optind]); | |
176 | usage(); | |
177 | } | |
178 | exit(errflg); | |
179 | } |