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