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