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_SYSVMSG_INTERNALS
41 #define _WANT_SYSVSEM_INTERNALS
44 #define _WANT_SYSVSHM_INTERNALS
59 static int rmverbose = 0;
66 "usage: ipcrm [-W] [-v[v]]\n"
67 " [-q msqid] [-m shmid] [-s semid]\n"
68 " [-Q msgkey] [-M shmkey] [-S semkey] ...\n");
73 msgrm(key_t key, int id)
76 if (key == -1 || id == -1) {
77 struct msqid_kernel *kxmsqids;
81 kget(X_MSGINFO, &msginfo, sizeof(msginfo));
82 kxmsqids_len = sizeof(struct msqid_kernel) * msginfo.msgmni;
83 kxmsqids = malloc(kxmsqids_len);
84 kget(X_MSQIDS, kxmsqids, kxmsqids_len);
86 while (num-- && !signaled)
87 if (kxmsqids[num].u.msg_qbytes != 0) {
88 id = IXSEQ_TO_IPCID(num,
89 kxmsqids[num].u.msg_perm);
90 if (msgctl(id, IPC_RMID, NULL) < 0) {
92 warn("msqid(%d): ", id);
101 return signaled ? -1 : 0; /* errors maybe handled above */
110 return msgctl(id, IPC_RMID, NULL);
114 shmrm(key_t key, int id)
117 if (key == -1 || id == -1) {
118 struct shmid_kernel *kxshmids;
122 kget(X_SHMINFO, &shminfo, sizeof(shminfo));
123 kxshmids_len = sizeof(struct shmid_kernel) * shminfo.shmmni;
124 kxshmids = malloc(kxshmids_len);
125 kget(X_SHMSEGS, kxshmids, kxshmids_len);
126 num = shminfo.shmmni;
127 while (num-- && !signaled)
128 if (kxshmids[num].u.shm_perm.mode & 0x0800) {
129 id = IXSEQ_TO_IPCID(num,
130 kxshmids[num].u.shm_perm);
131 if (shmctl(id, IPC_RMID, NULL) < 0) {
133 warn("shmid(%d): ", id);
142 return signaled ? -1 : 0; /* errors maybe handled above */
146 id = shmget(key, 0, 0);
151 return shmctl(id, IPC_RMID, NULL);
155 semrm(key_t key, int id)
159 if (key == -1 || id == -1) {
160 struct semid_kernel *kxsema;
164 kget(X_SEMINFO, &seminfo, sizeof(seminfo));
165 kxsema_len = sizeof(struct semid_kernel) * seminfo.semmni;
166 kxsema = malloc(kxsema_len);
167 kget(X_SEMA, kxsema, kxsema_len);
168 num = seminfo.semmni;
169 while (num-- && !signaled)
170 if ((kxsema[num].u.sem_perm.mode & SEM_ALLOC) != 0) {
171 id = IXSEQ_TO_IPCID(num,
172 kxsema[num].u.sem_perm);
173 if (semctl(id, 0, IPC_RMID, NULL) < 0) {
175 warn("semid(%d): ", id);
184 return signaled ? -1 : 0; /* errors maybe handled above */
188 id = semget(key, 0, 0);
193 return semctl(id, 0, IPC_RMID, arg);
197 not_configured(int signo __unused)
204 main(int argc, char *argv[])
206 int c, result, target_id;
209 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
224 signal(SIGSYS, not_configured);
225 while ((c = getopt(argc, argv, "q:m:s:Q:M:S:vWy")) != -1) {
232 target_id = atoi(optarg);
234 result = msgrm(0, target_id);
236 result = shmrm(0, target_id);
238 result = semrm(0, target_id);
243 IPC_TO_STR(toupper(c)), target_id);
246 "%ss are not configured "
247 "in the running kernel",
248 IPC_TO_STRING(toupper(c)));
254 target_key = atol(optarg);
255 if (target_key == IPC_PRIVATE) {
256 warnx("can't remove private %ss",
261 result = msgrm(target_key, 0);
263 result = shmrm(target_key, 0);
265 result = semrm(target_key, 0);
270 IPC_TO_STR(c), target_key);
272 warnx("%ss are not configured "
273 "in the running kernel",
279 /* Handled in other getopt() loop */
288 "option -%c requires an argument\n", optopt);
291 fprintf(stderr, "unrecognized option: -%c\n", optopt);
296 if (optind != argc) {
297 fprintf(stderr, "unknown argument: %s\n", argv[optind]);