kernel - close holes in autoconf's run_interrupt_driven_config_hooks()
[dragonfly.git] / usr.bin / ipcrm / ipcrm.c
blob3d2fd494cced0161e2f36852d268859365f46e4f
1 /*
2 * Copyright (c) 1994 Adam Glass
3 * All rights reserved.
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.
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.
31 * $FreeBSD: src/usr.bin/ipcrm/ipcrm.c,v 1.6 1999/08/28 01:02:14 peter Exp $
32 * $DragonFly: src/usr.bin/ipcrm/ipcrm.c,v 1.4 2004/11/19 18:35:29 liamfoy Exp $
35 #include <ctype.h>
36 #include <err.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/ipc.h>
43 #include <sys/msg.h>
44 #include <sys/sem.h>
45 #include <sys/shm.h>
47 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
48 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
49 (x == 'M' ? "shared memory segment" : "semaphore"))
51 volatile sig_atomic_t signaled;
53 static void usage(void);
54 static int msgrm(key_t, int);
55 static int shmrm(key_t, int);
56 static int semrm(key_t, int);
57 static void not_configured(int);
59 static void
60 usage(void)
62 fprintf(stderr, "%s\n%s\n",
63 "usage: ipcrm [-q msqid] [-m shmid] [-s semid]",
64 " [-Q msgkey] [-M shmkey] [-S semkey] ...");
65 exit(1);
68 static int
69 msgrm(key_t key, int id)
71 if (key) {
72 id = msgget(key, 0);
73 if (id == -1)
74 return -1;
76 return msgctl(id, IPC_RMID, NULL);
79 static int
80 shmrm(key_t key, int id)
82 if (key) {
83 id = shmget(key, 0, 0);
84 if (id == -1)
85 return -1;
87 return shmctl(id, IPC_RMID, NULL);
90 static int
91 semrm(key_t key, int id)
93 union semun arg;
95 if (key) {
96 id = semget(key, 0, 0);
97 if (id == -1)
98 return -1;
100 return semctl(id, 0, IPC_RMID, arg);
103 static void
104 not_configured(int signo __unused)
106 signaled++;
110 main(int argc, char **argv)
112 int c, result, errflg, target_id;
113 key_t target_key;
115 errflg = 0;
116 signal(SIGSYS, not_configured);
117 while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
119 signaled = 0;
120 switch (c) {
121 case 'q':
122 case 'm':
123 case 's':
124 target_id = atoi(optarg);
125 if (c == 'q')
126 result = msgrm(0, target_id);
127 else if (c == 'm')
128 result = shmrm(0, target_id);
129 else
130 result = semrm(0, target_id);
131 if (result < 0) {
132 errflg++;
133 if (!signaled)
134 warn("%sid(%d)", IPC_TO_STR(toupper(c)), target_id);
135 else
136 warnx("%ss are not configured in the running kernel",
137 IPC_TO_STRING(toupper(c)));
139 break;
140 case 'Q':
141 case 'M':
142 case 'S':
143 target_key = atol(optarg);
144 if (target_key == IPC_PRIVATE) {
145 warnx("can't remove private %ss", IPC_TO_STRING(c));
146 continue;
148 if (c == 'Q')
149 result = msgrm(target_key, 0);
150 else if (c == 'M')
151 result = shmrm(target_key, 0);
152 else
153 result = semrm(target_key, 0);
154 if (result < 0) {
155 errflg++;
156 if (!signaled)
157 warn("%s key %ld aborted", IPC_TO_STR(c), target_key);
158 else
159 warnx("%ss are not configured in the running kernel",
160 IPC_TO_STRING(c));
162 break;
163 case ':':
164 warnx("option -%c requires an argument\n", optopt);
165 usage();
166 case '?':
167 warnx("unrecognized option: -%c\n", optopt);
168 usage();
172 if (optind != argc) {
173 warnx("unknown argument: %s\n", argv[optind]);
174 usage();
176 exit(errflg);