File: ipcmk.c

package info (click to toggle)
util-linux 2.41-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 95,208 kB
  • sloc: ansic: 179,016; sh: 22,689; yacc: 1,284; makefile: 528; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (302 lines) | stat: -rw-r--r-- 7,694 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 *  ipcmk.c - used to create ad-hoc IPC segments
 *
 *  Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com)
 *  Copyright (C) 2008 Karel Zak <kzak@redhat.com>
 *  
 *  2025 Prasanna Paithankar <paithankarprasanna@gmail.com>
 * 	- Added POSIX IPC support
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <errno.h>
#include <getopt.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/time.h>

#ifdef HAVE_MQUEUE_H
# include <mqueue.h>
#endif

#ifdef HAVE_SEMAPHORE_H
# include <semaphore.h>
#endif

#ifdef HAVE_SYS_MMAN_H
# include <sys/stat.h>
# include <sys/mman.h>
#endif

#include "c.h"
#include "nls.h"
#include "randutils.h"
#include "strutils.h"
#include "closestream.h"

static int create_shm(size_t size, int permission)
{
	key_t key;

	ul_random_get_bytes(&key, sizeof(key));
	return shmget(key, size, permission | IPC_CREAT);
}

#ifndef HAVE_SYS_MMAN_H
static int create_posix_shm(const char *name __attribute__((__unused__)),
			size_t size __attribute__((__unused__)),
			int permission __attribute__((__unused__)))
{
	warnx(_("POSIX shared memory is not supported"));
	return -1;
}
#else
static int create_posix_shm(const char *name, size_t size, int permission)
{
	int shmfd;

	if (-1 == (shmfd = shm_open(name, O_RDWR | O_CREAT, permission)))
		return -1;

	if (-1 == ftruncate(shmfd, size)) {
		close(shmfd);
		return -1;
	}

	close(shmfd);
	printf(_("POSIX shared memory name: %s\n"), name);
	return 0;
}
#endif

static int create_msg(int permission)
{
	key_t key;

	ul_random_get_bytes(&key, sizeof(key));
	return msgget(key, permission | IPC_CREAT);
}

#ifndef HAVE_MQUEUE_H
static int create_posix_msg(const char *name __attribute__((__unused__)),
			int permission __attribute__((__unused__)))
{
	warnx(_("POSIX message queue is not supported"));
	return -1;
}
#else
static int create_posix_msg(const char *name, int permission)
{
	mqd_t mqd;

	if (-1 == (mqd = mq_open(name, O_RDWR | O_CREAT, permission, NULL)))
		return -1;

	mq_close(mqd);
	printf(_("POSIX message queue name: %s\n"), name);
	return 0;
}
#endif

static int create_sem(int nsems, int permission)
{
	key_t key;

	ul_random_get_bytes(&key, sizeof(key));
	return semget(key, nsems, permission | IPC_CREAT);
}

#ifndef HAVE_SEMAPHORE_H
static int create_posix_sem(const char *name __attribute__((__unused__)),
			int permission __attribute__((__unused__)))
{
	warnx(_("POSIX semaphore is not supported"));
	return -1;
}
#else
static int create_posix_sem(const char *name, int permission)
{
	sem_t *sem;

	if (SEM_FAILED == (sem = sem_open(name, O_CREAT, permission, 0)))
		return -1;

	sem_close(sem);
	printf(_("POSIX semaphore name: %s\n"), name);
	return 0;
}
#endif

static void __attribute__((__noreturn__)) usage(void)
{
	FILE *out = stdout;
	fputs(USAGE_HEADER, out);
	fprintf(out, _(" %s [options]\n"), program_invocation_short_name);

	fputs(USAGE_SEPARATOR, out);
	fputs(_("Create various IPC resources.\n"), out);

	fputs(USAGE_OPTIONS, out);
	fputs(_(" -M, --shmem <size>       create shared memory segment of size <size>\n"), out);
	fputs(_(" -m, --posix-shmem <size> create POSIX shared memory segment of size <size>\n"), out);
	fputs(_(" -S, --semaphore <number> create semaphore array with <number> elements\n"), out);
	fputs(_(" -s, --posix-semaphore    create POSIX semaphore\n"), out);
	fputs(_(" -Q, --queue              create message queue\n"), out);
	fputs(_(" -q, --posix-mqueue       create POSIX message queue\n"), out);
	fputs(_(" -p, --mode <mode>        permission for the resource (default is 0644)\n"), out);
	fputs(_(" -n, --name <name>        name of the POSIX resource\n"), out);

	fputs(USAGE_SEPARATOR, out);
	fprintf(out, USAGE_HELP_OPTIONS(26));

	fputs(USAGE_ARGUMENTS, out);
	fprintf(out, USAGE_ARG_SIZE(_("<size>")));

	fputs(USAGE_SEPARATOR, out);
	fputs(_(" -n, --name <name> option is required for POSIX IPC\n"), out);

	fprintf(out, USAGE_MAN_TAIL("ipcmk(1)"));

	exit(EXIT_SUCCESS);
}

int main(int argc, char **argv)
{
	int permission = 0644;
	char *name = NULL;
	int opt;
	size_t size = 0;
	int nsems = 0;
	int ask_shm = 0, ask_msg = 0, ask_sem = 0, ask_pshm = 0, ask_pmsg = 0, ask_psem = 0;
	static const struct option longopts[] = {
		{"shmem", required_argument, NULL, 'M'},
		{"posix-shmem", required_argument, NULL, 'm'},
		{"semaphore", required_argument, NULL, 'S'},
		{"posix-semaphore", no_argument, NULL, 's'},
		{"queue", no_argument, NULL, 'Q'},
		{"posix-mqueue", no_argument, NULL, 'q'},
		{"mode", required_argument, NULL, 'p'},
		{"name", required_argument, NULL, 'n'},
		{"version", no_argument, NULL, 'V'},
		{"help", no_argument, NULL, 'h'},
		{NULL, 0, NULL, 0}
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
	close_stdout_atexit();

	while((opt = getopt_long(argc, argv, "hM:m:QqS:sp:n:Vh", longopts, NULL)) != -1) {
		switch(opt) {
		case 'M':
			size = strtosize_or_err(optarg, _("failed to parse size"));
			ask_shm = 1;
			break;
		case 'm':
			size = strtosize_or_err(optarg, _("failed to parse size"));
			ask_pshm = 1;
			break;
		case 'Q':
			ask_msg = 1;
			break;
		case 'q':
			ask_pmsg = 1;
			break;
		case 'S':
			nsems = strtos32_or_err(optarg, _("failed to parse elements"));
			ask_sem = 1;
			break;
		case 's':
			ask_psem = 1;
			break;
		case 'p':
		{
			char *end = NULL;
			errno = 0;
			permission = strtoul(optarg, &end, 8);
			if (errno || optarg == end || (end && *end))
				err(EXIT_FAILURE, _("failed to parse mode"));
			break;
		}
		case 'n':
			name = optarg;
			break;
		case 'h':
			usage();
		case 'V':
			print_version(EXIT_SUCCESS);
		default:
			errtryhelp(EXIT_FAILURE);
		}
	}

	if(!ask_shm && !ask_msg && !ask_sem && !ask_pshm && !ask_pmsg && !ask_psem) {
		warnx(_("bad usage"));
		errtryhelp(EXIT_FAILURE);
	}

	if ((ask_pshm + ask_pmsg + ask_psem > 0) && name == NULL) {
		warnx(_("name is required for POSIX IPC"));
		errtryhelp(EXIT_FAILURE);
	}

	if (ask_shm) {
		int shmid;
		if (-1 == (shmid = create_shm(size, permission)))
			err(EXIT_FAILURE, _("create share memory failed"));
		else
			printf(_("Shared memory id: %d\n"), shmid);
	}

	if (ask_pshm) {
		if (-1 == create_posix_shm(name, size, permission))
			err(EXIT_FAILURE, _("create POSIX shared memory failed"));
	}

	if (ask_msg) {
		int msgid;
		if (-1 == (msgid = create_msg(permission)))
			err(EXIT_FAILURE, _("create message queue failed"));
		else
			printf(_("Message queue id: %d\n"), msgid);
	}

	if (ask_pmsg) {
		if (-1 == create_posix_msg(name, permission))
			err(EXIT_FAILURE, _("create POSIX message queue failed"));
	}

	if (ask_sem) {
		int semid;
		if (-1 == (semid = create_sem(nsems, permission)))
			err(EXIT_FAILURE, _("create semaphore failed"));
		else
			printf(_("Semaphore id: %d\n"), semid);
	}

	if (ask_psem) {
		if (-1 == create_posix_sem(name, permission))
			err(EXIT_FAILURE, _("create POSIX semaphore failed"));
	}

	return EXIT_SUCCESS;
}