1 /* $OpenBSD: ipcrm.c,v 1.10 2005/12/19 19:13:50 millert Exp $*/
4 * Copyright (c) 1994 Adam Glass
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Adam Glass.
18 * 4. The name of the Author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL Adam Glass BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.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"))
54 int msgrm(key_t
, int);
55 int shmrm(key_t
, int);
56 int semrm(key_t
, int);
57 void not_configured(int);
62 extern char *__progname
;
63 fprintf(stderr
, "usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n"
64 " [-q msqid] [-S semkey] [-s semid] ...\n",
70 msgrm(key_t key
, int id
)
77 return (msgctl(id
, IPC_RMID
, NULL
));
81 shmrm(key_t key
, int id
)
84 id
= shmget(key
, 0, 0);
88 return (shmctl(id
, IPC_RMID
, NULL
));
92 semrm(key_t key
, int id
)
97 id
= semget(key
, 0, 0);
101 return (semctl(id
, 0, IPC_RMID
, arg
));
106 not_configured(int signo
)
112 main(int argc
, char *argv
[])
114 int c
, result
, errflg
, target_id
;
118 signal(SIGSYS
, not_configured
);
119 while ((c
= getopt(argc
, argv
, ":q:m:s:Q:M:S:")) != -1) {
125 target_id
= atoi(optarg
);
127 result
= msgrm(0, target_id
);
129 result
= shmrm(0, target_id
);
131 result
= semrm(0, target_id
);
136 IPC_TO_STR(toupper(c
)), target_id
);
138 warnx("%ss are not configured in the running kernel",
139 IPC_TO_STRING(toupper(c
)));
145 target_key
= atol(optarg
);
146 if (target_key
== IPC_PRIVATE
) {
147 warnx("can't remove private %ss", IPC_TO_STRING(c
));
151 result
= msgrm(target_key
, 0);
153 result
= shmrm(target_key
, 0);
155 result
= semrm(target_key
, 0);
159 warn("%skey(%ld)", IPC_TO_STR(c
),
162 warnx("%ss are not configured in the running kernel",
167 warnx("option -%c requires an argument", optopt
);
170 warnx("unrecognized option: -%c", optopt
);
175 if (optind
!= argc
) {
176 warnx("unknown argument: %s", argv
[optind
]);