2 * Copyright (c) 1994 Adam Glass
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #define _WANT_MSG_PROTOTYPES
38 #define _WANT_SEM_PROTOTYPES
39 #define _WANT_SHM_PROTOTYPES
58 static int rmverbose = 0;
65 "usage: ipcrm [-W] [-v[v]]\n"
66 " [-q msqid] [-m shmid] [-s semid]\n"
67 " [-Q msgkey] [-M shmkey] [-S semkey] ...\n");
72 msgrm(key_t key, int id)
75 if (key == -1 || id == -1) {
76 struct msqid_kernel *kxmsqids;
80 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
81 kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
82 kxmsqids = malloc(kxmsqids_len);
83 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
85 while (num-- && !signaled)
86 if (kxmsqids[num].u.msg_qbytes != 0) {
87 id = IXSEQ_TO_IPCID(num,
88 kxmsqids[num].u.msg_perm);
89 if (msgctl(id, IPC_RMID, NULL) < 0) {
91 warn("msqid(%d): ", id);
100 return signaled ? -1 : 0; /* errors maybe handled above */
109 return msgctl(id, IPC_RMID, NULL);
113 shmrm(key_t key, int id)
116 if (key == -1 || id == -1) {
117 struct shmid_kernel *kxshmids;
121 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
122 kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
123 kxshmids = malloc(kxshmids_len);
124 kget(X_SHMSEGS, kxshmids, kxshmids_len);
125 num = shminfo.shmmni;
126 while (num-- && !signaled)
127 if (kxshmids[num].u.shm_perm.mode & 0x0800) {
128 id = IXSEQ_TO_IPCID(num,
129 kxshmids[num].u.shm_perm);
130 if (shmctl(id, IPC_RMID, NULL) < 0) {
132 warn("shmid(%d): ", id);
141 return signaled ? -1 : 0; /* errors maybe handled above */
145 id = shmget(key, 0, 0);
150 return shmctl(id, IPC_RMID, NULL);
154 semrm(key_t key, int id)
158 if (key == -1 || id == -1) {
159 struct semid_kernel *kxsema;
163 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
164 kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
165 kxsema = malloc(kxsema_len);
166 kget(X_SEMA, kxsema, kxsema_len);
167 num = seminfo.semmni;
168 while (num-- && !signaled)
169 if ((kxsema[num].u.sem_perm.mode & SEM_ALLOC) != 0) {
170 id = IXSEQ_TO_IPCID(num,
171 kxsema[num].u.sem_perm);
172 if (semctl(id, 0, IPC_RMID, NULL) < 0) {
174 warn("semid(%d): ", id);
183 return signaled ? -1 : 0; /* errors maybe handled above */
187 id = semget(key, 0, 0);
192 return semctl(id, 0, IPC_RMID, arg);
196 not_configured(int signo __unused)
203 main(int argc, char *argv[])
205 int c, result, target_id;
208 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
223 signal(SIGSYS, not_configured);
224 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
231 target_id = atoi(optarg);
233 result = msgrm(0, target_id);
235 result = shmrm(0, target_id);
237 result = semrm(0, target_id);
242 IPC_TO_STR(toupper(c)), target_id);
245 "%ss are not configured "
246 "in the running kernel",
247 IPC_TO_STRING(toupper(c)));
253 target_key = atol(optarg);
254 if (target_key == IPC_PRIVATE) {
255 warnx("can't remove private %ss",
260 result = msgrm(target_key, 0);
262 result = shmrm(target_key, 0);
264 result = semrm(target_key, 0);
269 IPC_TO_STR(c), target_key);
271 warnx("%ss are not configured "
272 "in the running kernel",
278 /* Handled in other getopt() loop */
287 "option -%c requires an argument\n", optopt);
290 fprintf(stderr, "unrecognized option: -%c\n", optopt);
295 if (optind != argc) {
296 fprintf(stderr, "unknown argument: %s\n", argv[optind]);