xref: /freebsd/usr.bin/ipcrm/ipcrm.c (revision 6607310b740c499e273ae5bbf1f308ed91b61c05)
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 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
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>
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 
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);
58 
59 void usage(void)
60 {
61 	fprintf(stderr, "%s\n%s\n",
62 		"usage: ipcrm [-q msqid] [-m shmid] [-s semid]",
63 		"             [-Q msgkey] [-M shmkey] [-S semkey] ...");
64 	exit(1);
65 }
66 
67 int msgrm(key_t key, int id)
68 {
69     if (key) {
70 	id = msgget(key, 0);
71 	if (id == -1)
72 	    return -1;
73     }
74     return msgctl(id, IPC_RMID, NULL);
75 }
76 
77 int shmrm(key_t key, int id)
78 {
79     if (key) {
80 	id = shmget(key, 0, 0);
81 	if (id == -1)
82 	    return -1;
83     }
84     return shmctl(id, IPC_RMID, NULL);
85 }
86 
87 int semrm(key_t key, int id)
88 {
89     union semun arg;
90 
91     if (key) {
92 	id = semget(key, 0, 0);
93 	if (id == -1)
94 	    return -1;
95     }
96     return semctl(id, 0, IPC_RMID, arg);
97 }
98 
99 void not_configured(int signo __unused)
100 {
101     signaled++;
102 }
103 
104 int main(int argc, char *argv[])
105 {
106     int c, result, errflg, target_id;
107     key_t target_key;
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 = atoi(optarg);
119 	    if (c == 'q')
120 		result = msgrm(0, target_id);
121 	    else if (c == 'm')
122 		result = shmrm(0, target_id);
123 	    else
124 		result = semrm(0, target_id);
125 	    if (result < 0) {
126 		errflg++;
127 		if (!signaled)
128 		    warn("%sid(%d): ", IPC_TO_STR(toupper(c)), target_id);
129 		else
130 		    warnx("%ss are not configured in the running kernel",
131 			  IPC_TO_STRING(toupper(c)));
132 	    }
133 	    break;
134 	case 'Q':
135 	case 'M':
136 	case 'S':
137 	    target_key = atol(optarg);
138 	    if (target_key == IPC_PRIVATE) {
139 		warnx("can't remove private %ss", IPC_TO_STRING(c));
140 		continue;
141 	    }
142 	    if (c == 'Q')
143 		result = msgrm(target_key, 0);
144 	    else if (c == 'M')
145 		result = shmrm(target_key, 0);
146 	    else
147 		result = semrm(target_key, 0);
148 	    if (result < 0) {
149 		errflg++;
150 		if (!signaled)
151 		    warn("%ss(%ld): ", IPC_TO_STR(c), target_key);
152 		else
153 		    warnx("%ss are not configured in the running kernel",
154 			  IPC_TO_STRING(c));
155 	    }
156 	    break;
157 	case ':':
158 	    fprintf(stderr, "option -%c requires an argument\n", optopt);
159 	    usage();
160 	case '?':
161 	    fprintf(stderr, "unrecognized option: -%c\n", optopt);
162 	    usage();
163 	}
164     }
165 
166     if (optind != argc) {
167 	    fprintf(stderr, "unknown argument: %s\n", argv[optind]);
168 	    usage();
169     }
170     exit(errflg);
171 }
172 
173