xref: /netbsd-src/usr.bin/ipcrm/ipcrm.c (revision 0449f26fed962105334ec76df58667eb3a784f05)
1*0449f26fSlukem /*	$NetBSD: ipcrm.c,v 1.16 2009/01/18 01:06:42 lukem Exp $	*/
29d225a17Stls 
33683d805Scgd /*
401b5cbdeSglass  * Copyright (c) 1994 Adam Glass
501b5cbdeSglass  * All rights reserved.
63683d805Scgd  *
701b5cbdeSglass  * Redistribution and use in source and binary forms, with or without
801b5cbdeSglass  * modification, are permitted provided that the following conditions
901b5cbdeSglass  * are met:
1001b5cbdeSglass  * 1. Redistributions of source code must retain the above copyright
1101b5cbdeSglass  *    notice, this list of conditions and the following disclaimer.
1273319ec0Sglass  * 2. Redistributions in binary form must reproduce the above copyright
1373319ec0Sglass  *    notice, this list of conditions and the following disclaimer in the
1473319ec0Sglass  *    documentation and/or other materials provided with the distribution.
1573319ec0Sglass  * 3. All advertising materials mentioning features or use of this software
1673319ec0Sglass  *    must display the following acknowledgement:
1773319ec0Sglass  *	This product includes software developed by Adam Glass.
1873319ec0Sglass  * 4. The name of the Author may not be used to endorse or promote products
1901b5cbdeSglass  *    derived from this software without specific prior written permission.
2001b5cbdeSglass  *
2101b5cbdeSglass  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
2201b5cbdeSglass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2301b5cbdeSglass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2401b5cbdeSglass  * ARE DISCLAIMED.  IN NO EVENT SHALL Adam Glass BE LIABLE
2501b5cbdeSglass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2601b5cbdeSglass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2701b5cbdeSglass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2801b5cbdeSglass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2901b5cbdeSglass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3001b5cbdeSglass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3101b5cbdeSglass  * SUCH DAMAGE.
323683d805Scgd  */
333683d805Scgd 
348c879eddSthorpej #include <sys/types.h>
358c879eddSthorpej #include <sys/ipc.h>
368c879eddSthorpej #include <sys/msg.h>
378c879eddSthorpej #include <sys/sem.h>
388c879eddSthorpej #include <sys/shm.h>
398c879eddSthorpej 
403683d805Scgd #include <stdio.h>
41dc05e9baSchristos #include <string.h>
425145a548Sdrochner #include <stdlib.h>
4301b5cbdeSglass #include <unistd.h>
445145a548Sdrochner #include <ctype.h>
4501b5cbdeSglass #include <err.h>
4601b5cbdeSglass #include <signal.h>
47dc05e9baSchristos #include <sys/sysctl.h>
483683d805Scgd 
4901b5cbdeSglass #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
5001b5cbdeSglass #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
5101b5cbdeSglass 	(x == 'M' ? "shared memory segment" : "semaphore"))
523683d805Scgd 
53dc05e9baSchristos static sig_atomic_t signaled;
5401b5cbdeSglass 
55dc05e9baSchristos static void	usage(void) __dead;
56dc05e9baSchristos static int	msgrm(key_t, int);
57dc05e9baSchristos static int	shmrm(key_t, int);
58dc05e9baSchristos static int	semrm(key_t, int);
59dc05e9baSchristos static int	msgrmall(void);
60dc05e9baSchristos static int	shmrmall(void);
61dc05e9baSchristos static int	semrmall(void);
62dc05e9baSchristos static void	not_configured(int);
635145a548Sdrochner 
64dc05e9baSchristos static void
usage(void)65dc05e9baSchristos usage(void)
6673319ec0Sglass {
67fbf47771Swiz 	(void)fprintf(stderr, "Usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n",
68dc05e9baSchristos 	    getprogname());
69fbf47771Swiz 	(void)fprintf(stderr, "\t[-q msqid] [-S semkey] [-s semid] ...\n");
7073319ec0Sglass 	exit(1);
7173319ec0Sglass }
7273319ec0Sglass 
73dc05e9baSchristos static int
msgrm(key_t key,int id)74dc05e9baSchristos msgrm(key_t key, int id)
753683d805Scgd {
7601b5cbdeSglass 	if (key) {
7701b5cbdeSglass 		id = msgget(key, 0);
7801b5cbdeSglass 		if (id == -1)
7901b5cbdeSglass 			return -1;
8001b5cbdeSglass 	}
8101b5cbdeSglass 	return msgctl(id, IPC_RMID, NULL);
823683d805Scgd }
833683d805Scgd 
84dc05e9baSchristos static int
shmrm(key_t key,int id)85dc05e9baSchristos shmrm(key_t key, int id)
863683d805Scgd {
8701b5cbdeSglass 	if (key) {
8801b5cbdeSglass 		id = shmget(key, 0, 0);
8901b5cbdeSglass 		if (id == -1)
9001b5cbdeSglass 			return -1;
9101b5cbdeSglass 	}
9201b5cbdeSglass 	return shmctl(id, IPC_RMID, NULL);
9301b5cbdeSglass }
943683d805Scgd 
95dc05e9baSchristos static int
semrm(key_t key,int id)96dc05e9baSchristos semrm(key_t key, int id)
9701b5cbdeSglass {
983683d805Scgd 
9901b5cbdeSglass 	if (key) {
10001b5cbdeSglass 		id = semget(key, 0, 0);
10101b5cbdeSglass 		if (id == -1)
10201b5cbdeSglass 			return -1;
1033683d805Scgd 	}
1046d162443Sthorpej 	return semctl(id, 0, IPC_RMID, NULL);
1053683d805Scgd }
1063683d805Scgd 
107dc05e9baSchristos static int
msgrmall(void)108dc05e9baSchristos msgrmall(void)
109dc05e9baSchristos {
110dc05e9baSchristos 	int mib[4];
111dc05e9baSchristos 	struct msg_sysctl_info *msgsi;
112*0449f26fSlukem 	int32_t i;
113*0449f26fSlukem 	size_t len;
114dc05e9baSchristos 	int result = 0;
115dc05e9baSchristos 
116dc05e9baSchristos 	mib[0] = CTL_KERN;
117dc05e9baSchristos 	mib[1] = KERN_SYSVIPC;
118dc05e9baSchristos 	mib[2] = KERN_SYSVIPC_INFO;
119dc05e9baSchristos 	mib[3] = KERN_SYSVIPC_MSG_INFO;
120dc05e9baSchristos 
121dc05e9baSchristos 	if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
122dc05e9baSchristos 		err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)");
123dc05e9baSchristos 
124dc05e9baSchristos 	if ((msgsi = malloc(len)) == NULL)
125dc05e9baSchristos 		err(1, "malloc");
126dc05e9baSchristos 	if (sysctl(mib, 4, msgsi, &len, NULL, 0) == -1) {
127dc05e9baSchristos 		free(msgsi);
128dc05e9baSchristos 		err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)");
129dc05e9baSchristos 	}
130dc05e9baSchristos 
131dc05e9baSchristos 	for (i = 0; i < msgsi->msginfo.msgmni; i++) {
132dc05e9baSchristos 		struct msgid_ds_sysctl *msgptr = &msgsi->msgids[i];
133dc05e9baSchristos 		if (msgptr->msg_qbytes != 0)
134dc05e9baSchristos 			result -= msgrm((key_t)0,
135dc05e9baSchristos 			    (int)IXSEQ_TO_IPCID(i, msgptr->msg_perm));
136dc05e9baSchristos 	}
137dc05e9baSchristos 	free(msgsi);
138dc05e9baSchristos 	return result;
139dc05e9baSchristos }
140dc05e9baSchristos 
141dc05e9baSchristos static int
shmrmall(void)142dc05e9baSchristos shmrmall(void)
143dc05e9baSchristos {
144dc05e9baSchristos 	int mib[4];
145dc05e9baSchristos 	struct shm_sysctl_info *shmsi;
146dc05e9baSchristos 	size_t i, len;
147dc05e9baSchristos 	int result = 0;
148dc05e9baSchristos 
149dc05e9baSchristos 	mib[0] = CTL_KERN;
150dc05e9baSchristos 	mib[1] = KERN_SYSVIPC;
151dc05e9baSchristos 	mib[2] = KERN_SYSVIPC_INFO;
152dc05e9baSchristos 	mib[3] = KERN_SYSVIPC_SHM_INFO;
153dc05e9baSchristos 
154dc05e9baSchristos 	if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
155dc05e9baSchristos 		err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)");
156dc05e9baSchristos 
157dc05e9baSchristos 	if ((shmsi = malloc(len)) == NULL)
158dc05e9baSchristos 		err(1, "malloc");
159dc05e9baSchristos 	if (sysctl(mib, 4, shmsi, &len, NULL, 0) == -1) {
160dc05e9baSchristos 		free(shmsi);
161dc05e9baSchristos 		err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)");
162dc05e9baSchristos 	}
163dc05e9baSchristos 
164dc05e9baSchristos 	for (i = 0; i < shmsi->shminfo.shmmni; i++) {
165dc05e9baSchristos 		struct shmid_ds_sysctl *shmptr = &shmsi->shmids[i];
166dc05e9baSchristos 		if (shmptr->shm_perm.mode & 0x0800)
167dc05e9baSchristos 			result -= shmrm((key_t)0,
168dc05e9baSchristos 			    (int)IXSEQ_TO_IPCID(i, shmptr->shm_perm));
169dc05e9baSchristos 	}
170dc05e9baSchristos 	free(shmsi);
171dc05e9baSchristos 	return result;
172dc05e9baSchristos }
173dc05e9baSchristos 
174dc05e9baSchristos static int
semrmall(void)175dc05e9baSchristos semrmall(void)
176dc05e9baSchristos {
177dc05e9baSchristos 	int mib[4];
178dc05e9baSchristos 	struct sem_sysctl_info *semsi;
179*0449f26fSlukem 	size_t len;
180*0449f26fSlukem 	int32_t i;
181dc05e9baSchristos 	int result = 0;
182dc05e9baSchristos 
183dc05e9baSchristos 	mib[0] = CTL_KERN;
184dc05e9baSchristos 	mib[1] = KERN_SYSVIPC;
185dc05e9baSchristos 	mib[2] = KERN_SYSVIPC_INFO;
186dc05e9baSchristos 	mib[3] = KERN_SYSVIPC_SEM_INFO;
187dc05e9baSchristos 
188dc05e9baSchristos 	if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1)
189dc05e9baSchristos 		err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)");
190dc05e9baSchristos 
191dc05e9baSchristos 	if ((semsi = malloc(len)) == NULL)
192dc05e9baSchristos 		err(1, "malloc");
193dc05e9baSchristos 	if (sysctl(mib, 4, semsi, &len, NULL, 0) == -1) {
194dc05e9baSchristos 		free(semsi);
195dc05e9baSchristos 		err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)");
196dc05e9baSchristos 	}
197dc05e9baSchristos 
198dc05e9baSchristos 	for (i = 0; i < semsi->seminfo.semmni; i++) {
199dc05e9baSchristos 		struct semid_ds_sysctl *semptr = &semsi->semids[i];
200dc05e9baSchristos 		if ((semptr->sem_perm.mode & SEM_ALLOC) != 0)
201dc05e9baSchristos 			result -= semrm((key_t)0,
202dc05e9baSchristos 			    (int)IXSEQ_TO_IPCID(i, semptr->sem_perm));
203dc05e9baSchristos 	}
204dc05e9baSchristos 	free(semsi);
205dc05e9baSchristos 	return result;
206dc05e9baSchristos }
207dc05e9baSchristos 
208dc05e9baSchristos static void
209dc05e9baSchristos /*ARGSUSED*/
not_configured(int n)210dc05e9baSchristos not_configured(int n)
21101b5cbdeSglass {
21201b5cbdeSglass 	signaled++;
2133683d805Scgd }
2143683d805Scgd 
215cff45b3bSthorpej int
main(int argc,char * argv[])216dc05e9baSchristos main(int argc, char *argv[])
21701b5cbdeSglass {
21801b5cbdeSglass 	int     c, result, errflg, target_id;
21901b5cbdeSglass 	key_t   target_key;
22001b5cbdeSglass 
221dc05e9baSchristos 	setprogname(argv[0]);
22201b5cbdeSglass 	errflg = 0;
223dc05e9baSchristos 	(void)signal(SIGSYS, not_configured);
2243d11ccecSthorpej 	while ((c = getopt(argc, argv, "q:m:s:Q:M:S:")) != -1) {
22501b5cbdeSglass 		signaled = 0;
226dc05e9baSchristos 		target_id = 0;
227dc05e9baSchristos 		target_key = 0;
228dc05e9baSchristos 		result = 0;
229dc05e9baSchristos 
23081b964b8Sdholland 		if (optarg != NULL && strcmp(optarg, "all") == 0) {
231dc05e9baSchristos 			switch (c) {
232dc05e9baSchristos 			case 'm':
233dc05e9baSchristos 			case 'M':
234dc05e9baSchristos 				result = shmrmall();
235dc05e9baSchristos 				break;
236dc05e9baSchristos 			case 'q':
237dc05e9baSchristos 			case 'Q':
238dc05e9baSchristos 				result = msgrmall();
239dc05e9baSchristos 				break;
240dc05e9baSchristos 			case 's':
241dc05e9baSchristos 			case 'S':
242dc05e9baSchristos 				result = semrmall();
243dc05e9baSchristos 				break;
244dc05e9baSchristos 			default:
245dc05e9baSchristos 				usage();
246dc05e9baSchristos 			}
247dc05e9baSchristos 		} else {
24801b5cbdeSglass 			switch (c) {
24901b5cbdeSglass 			case 'q':
25001b5cbdeSglass 			case 'm':
25101b5cbdeSglass 			case 's':
25201b5cbdeSglass 				target_id = atoi(optarg);
25301b5cbdeSglass 				break;
25401b5cbdeSglass 			case 'Q':
25501b5cbdeSglass 			case 'M':
25601b5cbdeSglass 			case 'S':
25701b5cbdeSglass 				target_key = atol(optarg);
25801b5cbdeSglass 				if (target_key == IPC_PRIVATE) {
259cff45b3bSthorpej 					warnx("can't remove private %ss",
260cff45b3bSthorpej 					    IPC_TO_STRING(c));
26101b5cbdeSglass 					continue;
26201b5cbdeSglass 				}
26301b5cbdeSglass 				break;
2643d11ccecSthorpej 			default:
26573319ec0Sglass 				usage();
2663683d805Scgd 			}
267dc05e9baSchristos 			switch (c) {
268dc05e9baSchristos 			case 'q':
269dc05e9baSchristos 				result = msgrm((key_t)0, target_id);
270dc05e9baSchristos 				break;
271dc05e9baSchristos 			case 'm':
272dc05e9baSchristos 				result = shmrm((key_t)0, target_id);
273dc05e9baSchristos 				break;
274dc05e9baSchristos 			case 's':
275dc05e9baSchristos 				result = semrm((key_t)0, target_id);
276dc05e9baSchristos 				break;
277dc05e9baSchristos 			case 'Q':
278dc05e9baSchristos 				result = msgrm(target_key, 0);
279dc05e9baSchristos 				break;
280dc05e9baSchristos 			case 'M':
281dc05e9baSchristos 				result = shmrm(target_key, 0);
282dc05e9baSchristos 				break;
283dc05e9baSchristos 			case 'S':
284dc05e9baSchristos 				result = semrm(target_key, 0);
285dc05e9baSchristos 				break;
286dc05e9baSchristos 			}
287dc05e9baSchristos 		}
288dc05e9baSchristos 		if (result < 0) {
289dc05e9baSchristos 			if (!signaled) {
290dc05e9baSchristos 				if (target_id) {
291dc05e9baSchristos 					warn("%sid(%d): ",
292dc05e9baSchristos 					    IPC_TO_STR(toupper(c)), target_id);
293dc05e9baSchristos 					errflg++;
294dc05e9baSchristos 				} else if (target_key) {
295dc05e9baSchristos 					warn("%skey(%ld): ", IPC_TO_STR(c),
296dc05e9baSchristos 					    (long)target_key);
297dc05e9baSchristos 					errflg++;
298dc05e9baSchristos 				}
299dc05e9baSchristos 			} else {
300dc05e9baSchristos 				errflg++;
301dc05e9baSchristos 				warnx("%ss are not configured in "
302dc05e9baSchristos 				    "the running kernel",
303dc05e9baSchristos 				    IPC_TO_STRING(toupper(c)));
304dc05e9baSchristos 			}
305dc05e9baSchristos 		}
3063683d805Scgd 	}
30773319ec0Sglass 
30801b5cbdeSglass 	if (optind != argc) {
309dc05e9baSchristos 		warnx("Unknown argument: %s", argv[optind]);
31073319ec0Sglass 		usage();
3113683d805Scgd 	}
312dc05e9baSchristos 	return errflg;
3133683d805Scgd }
314