2 * ipcmk.c - used to create ad-hoc IPC segments
4 * Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com)
5 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include "randutils.h"
37 #include "closestream.h"
39 static int create_shm(size_t size
, int permission
)
43 random_get_bytes(&key
, sizeof(key
));
44 return shmget(key
, size
, permission
| IPC_CREAT
);
47 static int create_msg(int permission
)
51 random_get_bytes(&key
, sizeof(key
));
52 return msgget(key
, permission
| IPC_CREAT
);
55 static int create_sem(int nsems
, int permission
)
59 random_get_bytes(&key
, sizeof(key
));
60 return semget(key
, nsems
, permission
| IPC_CREAT
);
63 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
65 fputs(USAGE_HEADER
, out
);
66 fprintf(out
, _(" %s [options]\n"), program_invocation_short_name
);
68 fputs(USAGE_SEPARATOR
, out
);
69 fputs(_("Create various IPC resources.\n"), out
);
71 fputs(USAGE_OPTIONS
, out
);
72 fputs(_(" -M, --shmem <size> create shared memory segment of size <size>\n"), out
);
73 fputs(_(" -S, --semaphore <number> create semaphore array with <number> elements\n"), out
);
74 fputs(_(" -Q, --queue create message queue\n"), out
);
75 fputs(_(" -p, --mode <mode> permission for the resource (default is 0644)\n"), out
);
77 fputs(USAGE_SEPARATOR
, out
);
78 fputs(USAGE_HELP
, out
);
79 fputs(USAGE_VERSION
, out
);
80 fprintf(out
, USAGE_MAN_TAIL("ipcmk(1)"));
82 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
85 int main(int argc
, char **argv
)
87 int permission
= 0644;
91 int ask_shm
= 0, ask_msg
= 0, ask_sem
= 0;
92 static const struct option longopts
[] = {
93 {"shmem", required_argument
, NULL
, 'M'},
94 {"semaphore", required_argument
, NULL
, 'S'},
95 {"queue", no_argument
, NULL
, 'Q'},
96 {"mode", required_argument
, NULL
, 'p'},
97 {"version", no_argument
, NULL
, 'V'},
98 {"help", no_argument
, NULL
, 'h'},
102 setlocale(LC_ALL
, "");
103 bindtextdomain(PACKAGE
, LOCALEDIR
);
105 atexit(close_stdout
);
107 while((opt
= getopt_long(argc
, argv
, "hM:QS:p:Vh", longopts
, NULL
)) != -1) {
110 size
= strtou64_or_err(optarg
, _("failed to parse size"));
117 nsems
= strtos32_or_err(optarg
, _("failed to parse elements"));
121 permission
= strtoul(optarg
, NULL
, 8);
127 printf(UTIL_LINUX_VERSION
);
130 ask_shm
= ask_msg
= ask_sem
= 0;
135 if(!ask_shm
&& !ask_msg
&& !ask_sem
)
140 if (-1 == (shmid
= create_shm(size
, permission
)))
141 err(EXIT_FAILURE
, _("create share memory failed"));
143 printf(_("Shared memory id: %d\n"), shmid
);
148 if (-1 == (msgid
= create_msg(permission
)))
149 err(EXIT_FAILURE
, _("create message queue failed"));
151 printf(_("Message queue id: %d\n"), msgid
);
156 if (-1 == (semid
= create_sem(nsems
, permission
)))
157 err(EXIT_FAILURE
, _("create semaphore failed"));
159 printf(_("Semaphore id: %d\n"), semid
);