GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ipcrm/ipcrm.c Lines: 0 62 0.0 %
Date: 2017-11-07 Branches: 0 54 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: ipcrm.c,v 1.12 2015/07/26 22:17:34 chl Exp $*/
2
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>
40
#include <stdio.h>
41
#include <unistd.h>
42
#include <limits.h>
43
#include <stdlib.h>
44
#include <ctype.h>
45
#include <err.h>
46
#include <signal.h>
47
48
#define IPC_TO_STR(x) (x == 'Q' ? "msq" : (x == 'M' ? "shm" : "sem"))
49
#define IPC_TO_STRING(x) (x == 'Q' ? "message queue" : \
50
	(x == 'M' ? "shared memory segment" : "semaphore"))
51
52
int signaled;
53
54
void	usage(void);
55
int	msgrm(key_t, int);
56
int	shmrm(key_t, int);
57
int	semrm(key_t, int);
58
void	not_configured(int);
59
60
void
61
usage(void)
62
{
63
	extern char *__progname;
64
	fprintf(stderr, "usage: %s [-M shmkey] [-m shmid] [-Q msgkey]\n"
65
			"         [-q msqid] [-S semkey] [-s semid] ...\n",
66
		__progname);
67
	exit(1);
68
}
69
70
int
71
msgrm(key_t key, int id)
72
{
73
	if (key) {
74
		id = msgget(key, 0);
75
		if (id == -1)
76
			return (-1);
77
	}
78
	return (msgctl(id, IPC_RMID, NULL));
79
}
80
81
int
82
shmrm(key_t key, int id)
83
{
84
	if (key) {
85
		id = shmget(key, 0, 0);
86
		if (id == -1)
87
			return (-1);
88
	}
89
	return (shmctl(id, IPC_RMID, NULL));
90
}
91
92
int
93
semrm(key_t key, int id)
94
{
95
	union semun arg;
96
97
	if (key) {
98
		id = semget(key, 0, 0);
99
		if (id == -1)
100
			return (-1);
101
	}
102
	return (semctl(id, 0, IPC_RMID, arg));
103
}
104
105
/* ARGSUSED */
106
void
107
not_configured(int signo)
108
{
109
	signaled++;
110
}
111
112
int
113
main(int argc, char *argv[])
114
{
115
	int c, result, errflg, target_id;
116
	const char *errstr;
117
	key_t target_key;
118
119
	errflg = 0;
120
	signal(SIGSYS, not_configured);
121
	while ((c = getopt(argc, argv, ":q:m:s:Q:M:S:")) != -1) {
122
		signaled = 0;
123
		switch (c) {
124
		case 'q':
125
		case 'm':
126
		case 's':
127
			target_id = strtonum(optarg, 0, INT_MAX, &errstr);
128
			if (errstr)
129
				errx(1, "-%c %s: %s\n", c, optarg, errstr);
130
			if (c == 'q')
131
				result = msgrm(0, target_id);
132
			else if (c == 'm')
133
				result = shmrm(0, target_id);
134
			else
135
				result = semrm(0, target_id);
136
			if (result < 0) {
137
				errflg++;
138
				if (!signaled)
139
					warn("%sid(%d)",
140
					    IPC_TO_STR(toupper(c)), target_id);
141
				else
142
					warnx("%ss are not configured in the running kernel",
143
					    IPC_TO_STRING(toupper(c)));
144
			}
145
			break;
146
		case 'Q':
147
		case 'M':
148
		case 'S':
149
			target_key = atol(optarg);
150
			if (target_key == IPC_PRIVATE) {
151
				warnx("can't remove private %ss", IPC_TO_STRING(c));
152
				continue;
153
			}
154
			if (c == 'Q')
155
				result = msgrm(target_key, 0);
156
			else if (c == 'M')
157
				result = shmrm(target_key, 0);
158
			else
159
				result = semrm(target_key, 0);
160
			if (result < 0) {
161
				errflg++;
162
				if (!signaled)
163
					warn("%skey(%ld)", IPC_TO_STR(c),
164
					    target_key);
165
				else
166
					warnx("%ss are not configured in the running kernel",
167
					    IPC_TO_STRING(c));
168
			}
169
			break;
170
		case ':':
171
			warnx("option -%c requires an argument", optopt);
172
			usage();
173
		default:
174
			warnx("unrecognized option: -%c", optopt);
175
			usage();
176
		}
177
	}
178
179
	if (optind != argc) {
180
		warnx("unknown argument: %s", argv[optind]);
181
		usage();
182
	}
183
	exit(errflg);
184
}