]> git.saurik.com Git - apple/file_cmds.git/blob - ipcrm/ipcrm.c
567cfcbfbe602aa0243183f3a0ed762030a22ecd
[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.2.30.1 2005/02/26 03:36:38 nicolai 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 #if 0
46 #include <sys/msg.h>
47 #include <sys/sem.h>
48 #endif
49 #include <sys/shm.h>
50
51 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
52 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
53 (x == 'M' ? "shared memory segment" : "semaphore"))
54
55 int signaled;
56
57 void usage()
58 {
59 fprintf(stderr, "%s\n%s\n",
60 "usage: ipcrm [-q msqid] [-m shmid] [-s semid]",
61 " [-Q msgkey] [-M shmkey] [-S semkey] ...");
62 exit(1);
63 }
64
65 int msgrm(key, id)
66 key_t key;
67 int id;
68 {
69 #if 0
70 if (key) {
71 id = msgget(key, 0);
72 if (id == -1)
73 return -1;
74 }
75 #endif
76 return msgctl(id, IPC_RMID, NULL);
77 }
78
79 int shmrm(key, id)
80 key_t key;
81 int id;
82 {
83 if (key) {
84 id = shmget(key, 0, 0);
85 if (id == -1)
86 return -1;
87 }
88 return shmctl(id, IPC_RMID, NULL);
89 }
90
91 int semrm(key, id)
92 key_t key;
93 int id;
94 {
95 #if 0
96 union semun arg;
97
98 if (key) {
99 id = semget(key, 0, 0);
100 if (id == -1)
101 return -1;
102 }
103 return semctl(id, 0, IPC_RMID, arg);
104 #endif
105 }
106
107 void not_configured()
108 {
109 signaled++;
110 }
111
112 int main(argc, argv)
113 int argc;
114 char *argv[];
115
116 {
117 int c, result, errflg, target_id;
118 key_t target_key;
119
120 errflg = 0;
121 signal(SIGSYS, not_configured);
122 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
123
124 signaled = 0;
125 switch (c) {
126 case 'q':
127 case 'm':
128 case 's':
129 target_id = atoi(optarg);
130 if (c == 'q')
131 result = msgrm(0, target_id);
132 else if (c == 'm')
133 result = shmrm(0, target_id);
134 else
135 result = semrm(0, target_id);
136 if (result < 0) {
137 errflg++;
138 if (!signaled)
139 warn("%sid(%d): ", 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("%key(%ld): ", IPC_TO_STR(c), target_key);
163 else
164 warnx("%ss are not configured in the running kernel",
165 IPC_TO_STRING(c));
166 }
167 break;
168 case ':':
169 fprintf(stderr, "option -%c requires an argument\n", optopt);
170 usage();
171 case '?':
172 fprintf(stderr, "unrecognized option: -%c\n", optopt);
173 usage();
174 }
175 }
176
177 if (optind != argc) {
178 fprintf(stderr, "unknown argument: %s\n", argv[optind]);
179 usage();
180 }
181 exit(errflg);
182 }
183