2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994 Adam Glass
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass.
18 * 4. The name of the Author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #define _WANT_SYSVMSG_INTERNALS
39 #define _WANT_SYSVSEM_INTERNALS
42 #define _WANT_SYSVSHM_INTERNALS
57 static int rmverbose = 0;
64 "usage: ipcrm [-W] [-v[v]]\n"
65 " [-q msqid] [-m shmid] [-s semid]\n"
66 " [-Q msgkey] [-M shmkey] [-S semkey] ...\n");
71 msgrm(key_t key, int id)
74 if (key == -1 || id == -1) {
75 struct msqid_kernel *kxmsqids;
79 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
80 kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
81 kxmsqids = malloc(kxmsqids_len);
82 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
84 while (num-- && !signaled)
85 if (kxmsqids[num].u.msg_qbytes != 0) {
86 id = IXSEQ_TO_IPCID(num,
87 kxmsqids[num].u.msg_perm);
88 if (msgctl(id, IPC_RMID, NULL) < 0) {
90 warn("msqid(%d): ", id);
99 return signaled ? -1 : 0; /* errors maybe handled above */
108 return msgctl(id, IPC_RMID, NULL);
112 shmrm(key_t key, int id)
115 if (key == -1 || id == -1) {
116 struct shmid_kernel *kxshmids;
120 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
121 kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
122 kxshmids = malloc(kxshmids_len);
123 kget(X_SHMSEGS, kxshmids, kxshmids_len);
124 num = shminfo.shmmni;
125 while (num-- && !signaled)
126 if (kxshmids[num].u.shm_perm.mode & 0x0800) {
127 id = IXSEQ_TO_IPCID(num,
128 kxshmids[num].u.shm_perm);
129 if (shmctl(id, IPC_RMID, NULL) < 0) {
131 warn("shmid(%d): ", id);
140 return signaled ? -1 : 0; /* errors maybe handled above */
144 id = shmget(key, 0, 0);
149 return shmctl(id, IPC_RMID, NULL);
153 semrm(key_t key, int id)
157 if (key == -1 || id == -1) {
158 struct semid_kernel *kxsema;
162 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
163 kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
164 kxsema = malloc(kxsema_len);
165 kget(X_SEMA, kxsema, kxsema_len);
166 num = seminfo.semmni;
167 while (num-- && !signaled)
168 if ((kxsema[num].u.sem_perm.mode & SEM_ALLOC) != 0) {
169 id = IXSEQ_TO_IPCID(num,
170 kxsema[num].u.sem_perm);
171 if (semctl(id, 0, IPC_RMID, NULL) < 0) {
173 warn("semid(%d): ", id);
182 return signaled ? -1 : 0; /* errors maybe handled above */
186 id = semget(key, 0, 0);
191 return semctl(id, 0, IPC_RMID, arg);
195 not_configured(int signo __unused)
202 main(int argc, char *argv[])
204 int c, result, target_id;
207 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
222 signal(SIGSYS, not_configured);
223 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
230 target_id = atoi(optarg);
232 result = msgrm(0, target_id);
234 result = shmrm(0, target_id);
236 result = semrm(0, target_id);
241 IPC_TO_STR(toupper(c)), target_id);
244 "%ss are not configured "
245 "in the running kernel",
246 IPC_TO_STRING(toupper(c)));
252 target_key = atol(optarg);
253 if (target_key == IPC_PRIVATE) {
254 warnx("can't remove private %ss",
259 result = msgrm(target_key, 0);
261 result = shmrm(target_key, 0);
263 result = semrm(target_key, 0);
268 IPC_TO_STR(c), target_key);
270 warnx("%ss are not configured "
271 "in the running kernel",
277 /* Handled in other getopt() loop */
286 "option -%c requires an argument\n", optopt);
289 fprintf(stderr, "unrecognized option: -%c\n", optopt);
294 if (optind != argc) {
295 fprintf(stderr, "unknown argument: %s\n", argv[optind]);