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 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #define _WANT_MSG_PROTOTYPES
40 #define _WANT_SEM_PROTOTYPES
41 #define _WANT_SHM_PROTOTYPES
60 static int rmverbose = 0;
67 "usage: ipcrm [-W] [-v[v]]\n"
68 " [-q msqid] [-m shmid] [-s semid]\n"
69 " [-Q msgkey] [-M shmkey] [-S semkey] ...\n");
74 msgrm(key_t key, int id)
77 if (key == -1 || id == -1) {
78 struct msqid_kernel *kxmsqids;
82 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
83 kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
84 kxmsqids = malloc(kxmsqids_len);
85 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
87 while (num-- && !signaled)
88 if (kxmsqids[num].u.msg_qbytes != 0) {
89 id = IXSEQ_TO_IPCID(num,
90 kxmsqids[num].u.msg_perm);
91 if (msgctl(id, IPC_RMID, NULL) < 0) {
93 warn("msqid(%d): ", id);
102 return signaled ? -1 : 0; /* errors maybe handled above */
111 return msgctl(id, IPC_RMID, NULL);
115 shmrm(key_t key, int id)
118 if (key == -1 || id == -1) {
119 struct shmid_kernel *kxshmids;
123 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
124 kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
125 kxshmids = malloc(kxshmids_len);
126 kget(X_SHMSEGS, kxshmids, kxshmids_len);
127 num = shminfo.shmmni;
128 while (num-- && !signaled)
129 if (kxshmids[num].u.shm_perm.mode & 0x0800) {
130 id = IXSEQ_TO_IPCID(num,
131 kxshmids[num].u.shm_perm);
132 if (shmctl(id, IPC_RMID, NULL) < 0) {
134 warn("shmid(%d): ", id);
143 return signaled ? -1 : 0; /* errors maybe handled above */
147 id = shmget(key, 0, 0);
152 return shmctl(id, IPC_RMID, NULL);
156 semrm(key_t key, int id)
160 if (key == -1 || id == -1) {
161 struct semid_kernel *kxsema;
165 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
166 kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
167 kxsema = malloc(kxsema_len);
168 kget(X_SEMA, kxsema, kxsema_len);
169 num = seminfo.semmni;
170 while (num-- && !signaled)
171 if ((kxsema[num].u.sem_perm.mode & SEM_ALLOC) != 0) {
172 id = IXSEQ_TO_IPCID(num,
173 kxsema[num].u.sem_perm);
174 if (semctl(id, 0, IPC_RMID, NULL) < 0) {
176 warn("semid(%d): ", id);
185 return signaled ? -1 : 0; /* errors maybe handled above */
189 id = semget(key, 0, 0);
194 return semctl(id, 0, IPC_RMID, arg);
198 not_configured(int signo __unused)
205 main(int argc, char *argv[])
207 int c, result, target_id;
210 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
225 signal(SIGSYS, not_configured);
226 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
233 target_id = atoi(optarg);
235 result = msgrm(0, target_id);
237 result = shmrm(0, target_id);
239 result = semrm(0, target_id);
244 IPC_TO_STR(toupper(c)), target_id);
247 "%ss are not configured "
248 "in the running kernel",
249 IPC_TO_STRING(toupper(c)));
255 target_key = atol(optarg);
256 if (target_key == IPC_PRIVATE) {
257 warnx("can't remove private %ss",
262 result = msgrm(target_key, 0);
264 result = shmrm(target_key, 0);
266 result = semrm(target_key, 0);
271 IPC_TO_STR(c), target_key);
273 warnx("%ss are not configured "
274 "in the running kernel",
280 /* Handled in other getopt() loop */
289 "option -%c requires an argument\n", optopt);
292 fprintf(stderr, "unrecognized option: -%c\n", optopt);
297 if (optind != argc) {
298 fprintf(stderr, "unknown argument: %s\n", argv[optind]);