ipcrm.c revision 21673
1112918Sjeff/*
2112918Sjeff * Copyright (c) 1994 Adam Glass
3112918Sjeff * All rights reserved.
4112918Sjeff *
5112918Sjeff * Redistribution and use in source and binary forms, with or without
6112918Sjeff * modification, are permitted provided that the following conditions
7112918Sjeff * are met:
8112918Sjeff * 1. Redistributions of source code must retain the above copyright
9112918Sjeff *    notice, this list of conditions and the following disclaimer.
10112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
11112918Sjeff *    notice, this list of conditions and the following disclaimer in the
12112918Sjeff *    documentation and/or other materials provided with the distribution.
13112918Sjeff * 3. All advertising materials mentioning features or use of this software
14112918Sjeff *    must display the following acknowledgement:
15112918Sjeff *	This product includes software developed by Adam Glass.
16112918Sjeff * 4. The name of the Author may not be used to endorse or promote products
17112918Sjeff *    derived from this software without specific prior written permission.
18112918Sjeff *
19112918Sjeff * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
20112918Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21112918Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22112918Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL Adam Glass BE LIABLE
23112918Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24112918Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25112918Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26112918Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27112918Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28112918Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29112918Sjeff * SUCH DAMAGE.
30112918Sjeff *
31112918Sjeff * $FreeBSD: head/usr.bin/ipcrm/ipcrm.c 21673 1997-01-14 07:20:47Z jkh $
32112918Sjeff */
33112918Sjeff
34112918Sjeff#include <stdio.h>
35112918Sjeff#include <stdlib.h>
36112918Sjeff#include <unistd.h>
37112918Sjeff#include <err.h>
38112918Sjeff#include <signal.h>
39112918Sjeff#include <sys/types.h>
40112918Sjeff#include <sys/ipc.h>
41112918Sjeff#include <sys/msg.h>
42112918Sjeff#include <sys/sem.h>
43112918Sjeff#include <sys/shm.h>
44112918Sjeff
45112918Sjeff#define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
46112918Sjeff#define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
47112918Sjeff	(x == 'M' ? "shared memory segment" : "semaphore"))
48112918Sjeff
49112918Sjeffint signaled;
50112918Sjeff
51void usage()
52{
53        fprintf(stderr, "usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]\n");
54	fprintf(stderr, "        [-Q msgkey] [-M shmkey] [-S semkey] ...]\n");
55	exit(1);
56}
57
58int msgrm(key, id)
59    key_t key;
60    int id;
61{
62    if (key) {
63	id = msgget(key, 0);
64	if (id == -1)
65	    return -1;
66    }
67    return msgctl(id, IPC_RMID, NULL);
68}
69
70int shmrm(key, id)
71    key_t key;
72    int id;
73{
74    if (key) {
75	id = shmget(key, 0, 0);
76	if (id == -1)
77	    return -1;
78    }
79    return shmctl(id, IPC_RMID, NULL);
80}
81
82int semrm(key, id)
83    key_t key;
84    int id;
85{
86    union semun arg;
87
88    if (key) {
89	id = semget(key, 0, 0);
90	if (id == -1)
91	    return -1;
92    }
93    return semctl(id, 0, IPC_RMID, arg);
94}
95
96void not_configured()
97{
98    signaled++;
99}
100
101int main(argc, argv)
102    int argc;
103    char *argv[];
104
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("%key(%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