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>
59 int msgrm(key_t, int);
60 int shmrm(key_t, int);
61 int semrm(key_t, int);
62 void not_configured(int);
69 "usage: ipcrm [-W] [-v[v]]\n"
70 " [-q msqid] [-m shmid] [-s semid]\n"
71 " [-Q msgkey] [-M shmkey] [-S semkey] ...\n");
76 msgrm(key_t key, int id)
79 if (key == -1 || id == -1) {
80 struct msqid_kernel *kxmsqids;
84 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
85 kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
86 kxmsqids = malloc(kxmsqids_len);
87 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
89 while (num-- && !signaled)
90 if (kxmsqids[num].u.msg_qbytes != 0) {
91 id = IXSEQ_TO_IPCID(num,
92 kxmsqids[num].u.msg_perm);
93 if (msgctl(id, IPC_RMID, NULL) < 0) {
95 warn("msqid(%d): ", id);
104 return signaled ? -1 : 0; /* errors maybe handled above */
113 return msgctl(id, IPC_RMID, NULL);
117 shmrm(key_t key, int id)
120 if (key == -1 || id == -1) {
121 struct shmid_kernel *kxshmids;
125 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
126 kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
127 kxshmids = malloc(kxshmids_len);
128 kget(X_SHMSEGS, kxshmids, kxshmids_len);
129 num = shminfo.shmmni;
130 while (num-- && !signaled)
131 if (kxshmids[num].u.shm_perm.mode & 0x0800) {
132 id = IXSEQ_TO_IPCID(num,
133 kxshmids[num].u.shm_perm);
134 if (shmctl(id, IPC_RMID, NULL) < 0) {
136 warn("shmid(%d): ", id);
145 return signaled ? -1 : 0; /* errors maybe handled above */
149 id = shmget(key, 0, 0);
154 return shmctl(id, IPC_RMID, NULL);
158 semrm(key_t key, int id)
162 if (key == -1 || id == -1) {
163 struct semid_kernel *kxsema;
167 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
168 kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
169 kxsema = malloc(kxsema_len);
170 kget(X_SEMA, kxsema, kxsema_len);
171 num = seminfo.semmni;
172 while (num-- && !signaled)
173 if ((kxsema[num].u.sem_perm.mode & SEM_ALLOC) != 0) {
174 id = IXSEQ_TO_IPCID(num,
175 kxsema[num].u.sem_perm);
176 if (semctl(id, IPC_RMID, NULL) < 0) {
178 warn("semid(%d): ", id);
187 return signaled ? -1 : 0; /* errors maybe handled above */
191 id = semget(key, 0, 0);
196 return semctl(id, 0, IPC_RMID, arg);
200 not_configured(int signo __unused)
207 main(int argc, char *argv[])
209 int c, result, target_id;
212 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
227 signal(SIGSYS, not_configured);
228 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
235 target_id = atoi(optarg);
237 result = msgrm(0, target_id);
239 result = shmrm(0, target_id);
241 result = semrm(0, target_id);
246 IPC_TO_STR(toupper(c)), target_id);
249 "%ss are not configured "
250 "in the running kernel",
251 IPC_TO_STRING(toupper(c)));
257 target_key = atol(optarg);
258 if (target_key == IPC_PRIVATE) {
259 warnx("can't remove private %ss",
264 result = msgrm(target_key, 0);
266 result = shmrm(target_key, 0);
268 result = semrm(target_key, 0);
273 IPC_TO_STR(c), target_key);
275 warnx("%ss are not configured "
276 "in the running kernel",
282 /* Handled in other getopt() loop */
291 "option -%c requires an argument\n", optopt);
294 fprintf(stderr, "unrecognized option: -%c\n", optopt);
299 if (optind != argc) {
300 fprintf(stderr, "unknown argument: %s\n", argv[optind]);