1 /* $NetBSD: ipcrm.c,v 1.16 2009/01/18 01:06:42 lukem 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
34 #include <sys/types.h>
47 #include <sys/sysctl.h>
53 #define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
54 #define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
55 (x == 'M' ? "shared memory segment" : "semaphore"))
57 static sig_atomic_t signaled
;
59 static void usage(void) __dead
;
60 static int msgrm(key_t
, int);
61 static int shmrm(key_t
, int);
62 static int semrm(key_t
, int);
63 static int msgrmall(void);
64 static int shmrmall(void);
65 static int semrmall(void);
66 static void not_configured(int);
71 (void)fprintf(stderr
, "Usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n",
73 (void)fprintf(stderr
, "\t[-q msqid] [-S semkey] [-s semid] ...\n");
78 msgrm(key_t key
, int id
)
86 return msgctl(id
, IPC_RMID
, NULL
);
94 shmrm(key_t key
, int id
)
97 id
= shmget(key
, 0, 0);
101 return shmctl(id
, IPC_RMID
, NULL
);
105 semrm(key_t key
, int id
)
109 id
= semget(key
, 0, 0);
113 return semctl(id
, 0, IPC_RMID
, NULL
);
120 struct msg_sysctl_info
*msgsi
;
126 mib
[1] = KERN_SYSVIPC
;
127 mib
[2] = KERN_SYSVIPC_INFO
;
128 mib
[3] = KERN_SYSVIPC_MSG_INFO
;
130 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) == -1)
131 err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)");
133 if ((msgsi
= malloc(len
)) == NULL
)
135 if (sysctl(mib
, 4, msgsi
, &len
, NULL
, 0) == -1) {
137 err(1, "sysctl(KERN_SYSVIPC_MSG_INFO)");
140 for (i
= 0; i
< msgsi
->msginfo
.msgmni
; i
++) {
141 struct msgid_ds_sysctl
*msgptr
= &msgsi
->msgids
[i
];
142 if (msgptr
->msg_qbytes
!= 0)
143 result
-= msgrm((key_t
)0,
144 (int)IXSEQ_TO_IPCID(i
, msgptr
->msg_perm
));
154 struct shm_sysctl_info
*shmsi
;
159 mib
[1] = KERN_SYSVIPC
;
160 mib
[2] = KERN_SYSVIPC_INFO
;
161 mib
[3] = KERN_SYSVIPC_SHM_INFO
;
163 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) == -1)
164 err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)");
166 if ((shmsi
= malloc(len
)) == NULL
)
168 if (sysctl(mib
, 4, shmsi
, &len
, NULL
, 0) == -1) {
170 err(1, "sysctl(KERN_SYSVIPC_SHM_INFO)");
173 for (i
= 0; i
< shmsi
->shminfo
.shmmni
; i
++) {
174 struct shmid_ds_sysctl
*shmptr
= &shmsi
->shmids
[i
];
175 if (shmptr
->shm_perm
.mode
& 0x0800)
176 result
-= shmrm((key_t
)0,
177 (int)IXSEQ_TO_IPCID(i
, shmptr
->shm_perm
));
187 struct sem_sysctl_info
*semsi
;
193 mib
[1] = KERN_SYSVIPC
;
194 mib
[2] = KERN_SYSVIPC_INFO
;
195 mib
[3] = KERN_SYSVIPC_SEM_INFO
;
197 if (sysctl(mib
, 4, NULL
, &len
, NULL
, 0) == -1)
198 err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)");
200 if ((semsi
= malloc(len
)) == NULL
)
202 if (sysctl(mib
, 4, semsi
, &len
, NULL
, 0) == -1) {
204 err(1, "sysctl(KERN_SYSVIPC_SEM_INFO)");
207 for (i
= 0; i
< semsi
->seminfo
.semmni
; i
++) {
208 struct semid_ds_sysctl
*semptr
= &semsi
->semids
[i
];
209 if ((semptr
->sem_perm
.mode
& SEM_ALLOC
) != 0)
210 result
-= semrm((key_t
)0,
211 (int)IXSEQ_TO_IPCID(i
, semptr
->sem_perm
));
219 not_configured(int n
)
225 main(int argc
, char *argv
[])
227 int c
, result
, errflg
, target_id
;
230 setprogname(argv
[0]);
232 (void)signal(SIGSYS
, not_configured
);
233 while ((c
= getopt(argc
, argv
, "q:m:s:Q:M:S:")) != -1) {
239 if (optarg
!= NULL
&& strcmp(optarg
, "all") == 0) {
261 target_id
= atoi(optarg
);
266 target_key
= atol(optarg
);
267 if (target_key
== IPC_PRIVATE
) {
268 warnx("can't remove private %ss",
278 result
= msgrm((key_t
)0, target_id
);
281 result
= shmrm((key_t
)0, target_id
);
284 result
= semrm((key_t
)0, target_id
);
287 result
= msgrm(target_key
, 0);
290 result
= shmrm(target_key
, 0);
293 result
= semrm(target_key
, 0);
301 IPC_TO_STR(toupper(c
)), target_id
);
303 } else if (target_key
) {
304 warn("%skey(%ld): ", IPC_TO_STR(c
),
310 warnx("%ss are not configured in "
311 "the running kernel",
312 IPC_TO_STRING(toupper(c
)));
317 if (optind
!= argc
) {
318 warnx("Unknown argument: %s", argv
[optind
]);