ipcrm.c revision 1.10 1 1.10 thorpej /* $NetBSD: ipcrm.c,v 1.10 1999/08/25 05:15:39 thorpej Exp $ */
2 1.5 tls
3 1.1 cgd /*
4 1.2 glass * Copyright (c) 1994 Adam Glass
5 1.2 glass * All rights reserved.
6 1.1 cgd *
7 1.2 glass * Redistribution and use in source and binary forms, with or without
8 1.2 glass * modification, are permitted provided that the following conditions
9 1.2 glass * are met:
10 1.2 glass * 1. Redistributions of source code must retain the above copyright
11 1.2 glass * notice, this list of conditions and the following disclaimer.
12 1.4 glass * 2. Redistributions in binary form must reproduce the above copyright
13 1.4 glass * notice, this list of conditions and the following disclaimer in the
14 1.4 glass * documentation and/or other materials provided with the distribution.
15 1.4 glass * 3. All advertising materials mentioning features or use of this software
16 1.4 glass * must display the following acknowledgement:
17 1.4 glass * This product includes software developed by Adam Glass.
18 1.4 glass * 4. The name of the Author may not be used to endorse or promote products
19 1.2 glass * derived from this software without specific prior written permission.
20 1.2 glass *
21 1.2 glass * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22 1.2 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.2 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.2 glass * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE
25 1.2 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.2 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.2 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.2 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.2 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.2 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.2 glass * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.9 thorpej #include <sys/types.h>
35 1.9 thorpej #include <sys/ipc.h>
36 1.9 thorpej #include <sys/msg.h>
37 1.9 thorpej #include <sys/sem.h>
38 1.9 thorpej #include <sys/shm.h>
39 1.9 thorpej
40 1.1 cgd #include <stdio.h>
41 1.6 drochner #include <stdlib.h>
42 1.2 glass #include <unistd.h>
43 1.6 drochner #include <ctype.h>
44 1.2 glass #include <err.h>
45 1.2 glass #include <signal.h>
46 1.1 cgd
47 1.2 glass #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
48 1.2 glass #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
49 1.2 glass (x == 'M' ? "shared memory segment" : "semaphore"))
50 1.2 glass
51 1.10 thorpej int signaled;
52 1.2 glass
53 1.10 thorpej void usage __P((void));
54 1.10 thorpej int msgrm __P((key_t, int));
55 1.10 thorpej int shmrm __P((key_t, int));
56 1.10 thorpej int semrm __P((key_t, int));
57 1.10 thorpej void not_configured __P((void));
58 1.10 thorpej int main __P((int, char *[]));
59 1.6 drochner
60 1.10 thorpej void
61 1.10 thorpej usage()
62 1.4 glass {
63 1.10 thorpej fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n");
64 1.4 glass fprintf(stderr, " [-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
65 1.4 glass exit(1);
66 1.4 glass }
67 1.4 glass
68 1.10 thorpej int
69 1.10 thorpej msgrm(key, id)
70 1.10 thorpej key_t key;
71 1.10 thorpej int id;
72 1.10 thorpej {
73 1.10 thorpej if (key) {
74 1.10 thorpej id = msgget(key, 0);
75 1.10 thorpej if (id == -1)
76 1.10 thorpej return -1;
77 1.1 cgd }
78 1.10 thorpej return msgctl(id, IPC_RMID, NULL);
79 1.10 thorpej }
80 1.10 thorpej
81 1.10 thorpej int
82 1.10 thorpej shmrm(key, id)
83 1.10 thorpej key_t key;
84 1.10 thorpej int id;
85 1.10 thorpej {
86 1.10 thorpej if (key) {
87 1.10 thorpej id = shmget(key, 0, 0);
88 1.10 thorpej if (id == -1)
89 1.10 thorpej return -1;
90 1.10 thorpej }
91 1.10 thorpej return shmctl(id, IPC_RMID, NULL);
92 1.10 thorpej }
93 1.4 glass
94 1.10 thorpej int
95 1.10 thorpej semrm(key, id)
96 1.10 thorpej key_t key;
97 1.10 thorpej int id;
98 1.10 thorpej {
99 1.10 thorpej
100 1.10 thorpej if (key) {
101 1.10 thorpej id = semget(key, 0, 0);
102 1.10 thorpej if (id == -1)
103 1.10 thorpej return -1;
104 1.10 thorpej }
105 1.10 thorpej return semctl(id, 0, IPC_RMID, NULL);
106 1.10 thorpej }
107 1.10 thorpej
108 1.10 thorpej void
109 1.10 thorpej not_configured()
110 1.10 thorpej {
111 1.10 thorpej signaled++;
112 1.10 thorpej }
113 1.10 thorpej
114 1.10 thorpej int
115 1.10 thorpej main(argc, argv)
116 1.10 thorpej int argc;
117 1.10 thorpej char *argv[];
118 1.10 thorpej
119 1.10 thorpej {
120 1.10 thorpej int c, result, errflg, target_id;
121 1.10 thorpej key_t target_key;
122 1.10 thorpej
123 1.10 thorpej errflg = 0;
124 1.10 thorpej signal(SIGSYS, (void (*) __P((int))) not_configured);
125 1.10 thorpej while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
126 1.10 thorpej
127 1.10 thorpej signaled = 0;
128 1.10 thorpej switch (c) {
129 1.10 thorpej case 'q':
130 1.10 thorpej case 'm':
131 1.10 thorpej case 's':
132 1.10 thorpej target_id = atoi(optarg);
133 1.10 thorpej if (c == 'q')
134 1.10 thorpej result = msgrm(0, target_id);
135 1.10 thorpej else
136 1.10 thorpej if (c == 'm')
137 1.10 thorpej result = shmrm(0, target_id);
138 1.10 thorpej else
139 1.10 thorpej result = semrm(0, target_id);
140 1.10 thorpej if (result < 0) {
141 1.10 thorpej errflg++;
142 1.10 thorpej if (!signaled)
143 1.10 thorpej warn("%sid(%d): ",
144 1.10 thorpej IPC_TO_STR(toupper(c)), target_id);
145 1.10 thorpej else
146 1.10 thorpej warnx("%ss are not configured in "
147 1.10 thorpej "the running kernel",
148 1.10 thorpej IPC_TO_STRING(toupper(c)));
149 1.10 thorpej }
150 1.10 thorpej break;
151 1.10 thorpej case 'Q':
152 1.10 thorpej case 'M':
153 1.10 thorpej case 'S':
154 1.10 thorpej target_key = atol(optarg);
155 1.10 thorpej if (target_key == IPC_PRIVATE) {
156 1.10 thorpej warnx("can't remove private %ss",
157 1.10 thorpej IPC_TO_STRING(c));
158 1.10 thorpej continue;
159 1.10 thorpej }
160 1.10 thorpej if (c == 'Q')
161 1.10 thorpej result = msgrm(target_key, 0);
162 1.10 thorpej else
163 1.10 thorpej if (c == 'M')
164 1.10 thorpej result = shmrm(target_key, 0);
165 1.10 thorpej else
166 1.10 thorpej result = semrm(target_key, 0);
167 1.10 thorpej if (result < 0) {
168 1.10 thorpej errflg++;
169 1.10 thorpej if (!signaled)
170 1.10 thorpej warn("%skey(%ld): ",
171 1.10 thorpej IPC_TO_STR(c), (long) target_key);
172 1.10 thorpej else
173 1.10 thorpej warnx("%ss are not configured "
174 1.10 thorpej in the running kernel",
175 1.10 thorpej IPC_TO_STRING(c));
176 1.10 thorpej }
177 1.10 thorpej break;
178 1.10 thorpej case ':':
179 1.10 thorpej fprintf(stderr, "option -%c requires an argument\n",
180 1.10 thorpej optopt);
181 1.10 thorpej usage();
182 1.10 thorpej case '?':
183 1.10 thorpej fprintf(stderr, "unrecognized option: -%c\n", optopt);
184 1.10 thorpej usage();
185 1.10 thorpej }
186 1.10 thorpej }
187 1.10 thorpej
188 1.10 thorpej if (optind != argc) {
189 1.10 thorpej fprintf(stderr, "unknown argument: %s\n", argv[optind]);
190 1.10 thorpej usage();
191 1.10 thorpej }
192 1.10 thorpej exit(errflg);
193 1.1 cgd }
194