file_cmds-220.4.tar.gz
[apple/file_cmds.git] / ipcrm / ipcrm.c
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
32 #ifndef lint
33 static const char rcsid[] =
34 "$Id: ipcrm.c,v 1.3 2005/02/03 07:31:33 josborne Exp $";
35 #endif /* not lint */
36
37 #include <ctype.h>
38 #include <err.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/ipc.h>
45 #include <sys/msg.h>
46 #include <sys/sem.h>
47 #include <sys/shm.h>
48
49 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
50 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
51 (x == 'M' ? "shared memory segment" : "semaphore"))
52
53 int signaled;
54
55 static void usage(void)
56 {
57 fprintf(stderr, "%s\n%s\n",
58 "usage: ipcrm [-q msqid] [-m shmid] [-s semid]",
59 " [-Q msgkey] [-M shmkey] [-S semkey] ...");
60 exit(1);
61 }
62
63 static int msgrm(key_t key, int id)
64 {
65 if (key) {
66 id = msgget(key, 0);
67 if (id == -1)
68 return -1;
69 }
70 return msgctl(id, IPC_RMID, NULL);
71 }
72
73 static int shmrm(key_t key, int id)
74 {
75 if (key) {
76 id = shmget(key, 0, 0);
77 if (id == -1)
78 return -1;
79 }
80 return shmctl(id, IPC_RMID, NULL);
81 }
82
83 static int semrm(key_t key, 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 static void not_configured(__unused int unused)
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 char *en;
108
109 errflg = 0;
110 signal(SIGSYS, not_configured);
111 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
112
113 signaled = 0;
114 switch (c) {
115 case 'q':
116 case 'm':
117 case 's':
118 target_id = (int)strtol(optarg, &en, 0);
119 if (*en) {
120 warnx("%s: '%s' is not a number",
121 IPC_TO_STRING(toupper(c)), optarg);
122 continue;
123 }
124 if (c == 'q')
125 result = msgrm(0, target_id);
126 else if (c == 'm')
127 result = shmrm(0, target_id);
128 else
129 result = semrm(0, target_id);
130 if (result < 0) {
131 errflg++;
132 if (!signaled)
133 warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id);
134 else
135 warnx("%ss are not configured in the running kernel",
136 IPC_TO_STRING(toupper(c)));
137 }
138 break;
139 case 'Q':
140 case 'M':
141 case 'S':
142 target_key = (key_t)strtol(optarg, &en, 0);
143 if (*en) {
144 warnx("%s: '%s' is not a number", IPC_TO_STRING(c), optarg);
145 continue;
146 }
147 if (target_key == IPC_PRIVATE) {
148 warnx("can't remove private %ss", IPC_TO_STRING(c));
149 continue;
150 }
151 if (c == 'Q')
152 result = msgrm(target_key, 0);
153 else if (c == 'M')
154 result = shmrm(target_key, 0);
155 else
156 result = semrm(target_key, 0);
157 if (result < 0) {
158 errflg++;
159 if (!signaled)
160 warn("%s key(%d): ", IPC_TO_STRING(c), target_key);
161 else
162 warnx("%ss are not configured in the running kernel",
163 IPC_TO_STRING(c));
164 }
165 break;
166 case ':':
167 fprintf(stderr, "option -%c requires an argument\n", optopt);
168 usage();
169 case '?':
170 fprintf(stderr, "unrecognized option: -%c\n", optopt);
171 usage();
172 }
173 }
174
175 if (optind != argc) {
176 fprintf(stderr, "unknown argument: %s\n", argv[optind]);
177 usage();
178 }
179 exit(errflg);
180 }