Annotation of src/usr.bin/ipcrm/ipcrm.c, Revision 1.8
1.8 ! deraadt 1: /* $OpenBSD: ipcrm.c,v 1.7 2004/06/29 17:05:41 mickey Exp $*/
1.3 deraadt 2:
1.1 deraadt 3: /*
4: * Copyright (c) 1994 Adam Glass
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
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.
20: *
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
31: * SUCH DAMAGE.
32: *
33: */
34:
35: #include <sys/types.h>
36: #include <sys/ipc.h>
37: #include <sys/msg.h>
38: #include <sys/sem.h>
39: #include <sys/shm.h>
1.3 deraadt 40: #include <stdio.h>
41: #include <unistd.h>
42: #include <stdlib.h>
43: #include <ctype.h>
44: #include <err.h>
45: #include <signal.h>
1.1 deraadt 46:
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"))
50:
51: int signaled;
52:
1.4 millert 53: void usage(void);
54: int msgrm(key_t, int);
55: int shmrm(key_t, int);
56: int semrm(key_t, int);
57: void not_configured(int);
1.3 deraadt 58:
59: void
1.5 deraadt 60: usage(void)
1.1 deraadt 61: {
1.7 mickey 62: extern char *__progname;
63: fprintf(stderr, "usage: %s [ [-q msqid] [-m shmid] [-s semid]\n"
64: " [-Q msgkey] [-M shmkey] [-S semkey] ...]\n",
65: __progname);
1.1 deraadt 66: exit(1);
67: }
68:
1.3 deraadt 69: int
1.5 deraadt 70: msgrm(key_t key, int id)
1.3 deraadt 71: {
72: if (key) {
73: id = msgget(key, 0);
74: if (id == -1)
75: return (-1);
76: }
77: return (msgctl(id, IPC_RMID, NULL));
78: }
79:
80: int
1.5 deraadt 81: shmrm(key_t key, int id)
1.3 deraadt 82: {
83: if (key) {
84: id = shmget(key, 0, 0);
85: if (id == -1)
86: return (-1);
1.1 deraadt 87: }
1.3 deraadt 88: return (shmctl(id, IPC_RMID, NULL));
89: }
90:
91: int
1.5 deraadt 92: semrm(key_t key, int id)
1.3 deraadt 93: {
94: union semun arg;
95:
96: if (key) {
97: id = semget(key, 0, 0);
98: if (id == -1)
99: return (-1);
100: }
101: return (semctl(id, 0, IPC_RMID, arg));
102: }
103:
1.8 ! deraadt 104: /* ARGSUSED */
1.3 deraadt 105: void
1.5 deraadt 106: not_configured(int signo)
1.3 deraadt 107: {
108: signaled++;
109: }
110:
111: int
1.5 deraadt 112: main(int argc, char *argv[])
1.3 deraadt 113: {
114: int c, result, errflg, target_id;
115: key_t target_key;
116:
117: errflg = 0;
118: signal(SIGSYS, not_configured);
119: while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
120: signaled = 0;
121: switch (c) {
122: case 'q':
123: case 'm':
124: case 's':
125: target_id = atoi(optarg);
126: if (c == 'q')
127: result = msgrm(0, target_id);
128: else if (c == 'm')
129: result = shmrm(0, target_id);
130: else
131: result = semrm(0, target_id);
132: if (result < 0) {
133: errflg++;
134: if (!signaled)
1.6 fgsch 135: warn("%sid(%d)",
1.3 deraadt 136: IPC_TO_STR(toupper(c)), target_id);
137: else
138: warnx("%ss are not configured in the running kernel",
139: IPC_TO_STRING(toupper(c)));
140: }
141: break;
142: case 'Q':
143: case 'M':
144: case 'S':
145: target_key = atol(optarg);
146: if (target_key == IPC_PRIVATE) {
147: warnx("can't remove private %ss", IPC_TO_STRING(c));
148: continue;
149: }
150: if (c == 'Q')
151: result = msgrm(target_key, 0);
152: else if (c == 'M')
153: result = shmrm(target_key, 0);
154: else
155: result = semrm(target_key, 0);
156: if (result < 0) {
157: errflg++;
158: if (!signaled)
1.6 fgsch 159: warn("%skey(%ld)", IPC_TO_STR(c),
1.3 deraadt 160: target_key);
161: else
162: warnx("%ss are not configured in the running kernel",
163: IPC_TO_STRING(c));
164: }
165: break;
166: case ':':
1.7 mickey 167: warnx("option -%c requires an argument", optopt);
1.3 deraadt 168: usage();
169: default:
1.7 mickey 170: warnx("unrecognized option: -%c", optopt);
1.3 deraadt 171: usage();
172: }
1.1 deraadt 173: }
174:
1.3 deraadt 175: if (optind != argc) {
1.7 mickey 176: warnx("unknown argument: %s", argv[optind]);
1.3 deraadt 177: usage();
178: }
179: exit(errflg);
1.1 deraadt 180: }