1 /* $OpenBSD: ipcrm.c,v 1.13 2023/03/08 04:43:11 guenther Exp $*/
2
3 /*
4 * Copyright (c) 1994 Adam Glass
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 */
34
35 #include <sys/types.h>
36 #include <sys/ipc.h>
37 #include <sys/msg.h>
38 #include <sys/sem.h>
39 #include <sys/shm.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <ctype.h>
45 #include <err.h>
46 #include <signal.h>
47
48 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
49 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
50 (x == 'M' ? "shared memory segment" : "semaphore"))
51
52 int signaled;
53
54 void usage(void);
55 int msgrm(key_t, int);
56 int shmrm(key_t, int);
57 int semrm(key_t, int);
58 void not_configured(int);
59
60 void
usage(void)61 usage(void)
62 {
63 extern char *__progname;
64 fprintf(stderr, "usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n"
65 " [-q msqid] [-S semkey] [-s semid] ...\n",
66 __progname);
67 exit(1);
68 }
69
70 int
msgrm(key_t key,int id)71 msgrm(key_t key, int id)
72 {
73 if (key) {
74 id = msgget(key, 0);
75 if (id == -1)
76 return (-1);
77 }
78 return (msgctl(id, IPC_RMID, NULL));
79 }
80
81 int
shmrm(key_t key,int id)82 shmrm(key_t key, int id)
83 {
84 if (key) {
85 id = shmget(key, 0, 0);
86 if (id == -1)
87 return (-1);
88 }
89 return (shmctl(id, IPC_RMID, NULL));
90 }
91
92 int
semrm(key_t key,int id)93 semrm(key_t key, int id)
94 {
95 union semun arg;
96
97 if (key) {
98 id = semget(key, 0, 0);
99 if (id == -1)
100 return (-1);
101 }
102 return (semctl(id, 0, IPC_RMID, arg));
103 }
104
105 void
not_configured(int signo)106 not_configured(int signo)
107 {
108 signaled++;
109 }
110
111 int
main(int argc,char * argv[])112 main(int argc, char *argv[])
113 {
114 int c, result, errflg, target_id;
115 const char *errstr;
116 key_t target_key;
117
118 errflg = 0;
119 signal(SIGSYS, not_configured);
120 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
121 signaled = 0;
122 switch (c) {
123 case 'q':
124 case 'm':
125 case 's':
126 target_id = strtonum(optarg, 0, INT_MAX, &errstr);
127 if (errstr)
128 errx(1, "-%c %s: %s\n", c, optarg, errstr);
129 if (c == 'q')
130 result = msgrm(0, target_id);
131 else if (c == 'm')
132 result = shmrm(0, target_id);
133 else
134 result = semrm(0, target_id);
135 if (result < 0) {
136 errflg++;
137 if (!signaled)
138 warn("%sid(%d)",
139 IPC_TO_STR(toupper(c)), target_id);
140 else
141 warnx("%ss are not configured in the running kernel",
142 IPC_TO_STRING(toupper(c)));
143 }
144 break;
145 case 'Q':
146 case 'M':
147 case 'S':
148 target_key = atol(optarg);
149 if (target_key == IPC_PRIVATE) {
150 warnx("can't remove private %ss", IPC_TO_STRING(c));
151 continue;
152 }
153 if (c == 'Q')
154 result = msgrm(target_key, 0);
155 else if (c == 'M')
156 result = shmrm(target_key, 0);
157 else
158 result = semrm(target_key, 0);
159 if (result < 0) {
160 errflg++;
161 if (!signaled)
162 warn("%skey(%ld)", IPC_TO_STR(c),
163 target_key);
164 else
165 warnx("%ss are not configured in the running kernel",
166 IPC_TO_STRING(c));
167 }
168 break;
169 case ':':
170 warnx("option -%c requires an argument", optopt);
171 usage();
172 default:
173 warnx("unrecognized option: -%c", optopt);
174 usage();
175 }
176 }
177
178 if (optind != argc) {
179 warnx("unknown argument: %s", argv[optind]);
180 usage();
181 }
182 exit(errflg);
183 }
184