]>
Commit | Line | Data |
---|---|---|
1b3f1330 HJ |
1 | /* |
2 | * ipcmk.c - used to create ad-hoc IPC segments | |
3 | * | |
4 | * Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com) | |
cc41c0a3 | 5 | * Copyright (C) 2008 Karel Zak <kzak@redhat.com> |
1b3f1330 HJ |
6 | * |
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. | |
11 | * | |
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. | |
16 | * | |
7cebf0bb SK |
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. | |
1b3f1330 HJ |
20 | */ |
21 | ||
1b3f1330 | 22 | #include <errno.h> |
003f4976 | 23 | #include <getopt.h> |
c20b6823 SK |
24 | #include <stddef.h> |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
1b3f1330 | 27 | #include <sys/ipc.h> |
1b3f1330 | 28 | #include <sys/msg.h> |
c20b6823 SK |
29 | #include <sys/sem.h> |
30 | #include <sys/shm.h> | |
b332ef44 | 31 | #include <sys/time.h> |
1b3f1330 | 32 | |
eb76ca98 | 33 | #include "c.h" |
c20b6823 | 34 | #include "nls.h" |
96f6ad88 | 35 | #include "randutils.h" |
c821a125 | 36 | #include "strutils.h" |
efb8854f | 37 | #include "closestream.h" |
f635c336 | 38 | |
d918c13c | 39 | static int create_shm(size_t size, int permission) |
1b3f1330 | 40 | { |
96f6ad88 KZ |
41 | key_t key; |
42 | ||
43 | random_get_bytes(&key, sizeof(key)); | |
f5b0684e | 44 | return shmget(key, size, permission | IPC_CREAT); |
1b3f1330 HJ |
45 | } |
46 | ||
d918c13c | 47 | static int create_msg(int permission) |
1b3f1330 | 48 | { |
96f6ad88 KZ |
49 | key_t key; |
50 | ||
51 | random_get_bytes(&key, sizeof(key)); | |
f5b0684e | 52 | return msgget(key, permission | IPC_CREAT); |
1b3f1330 HJ |
53 | } |
54 | ||
d918c13c | 55 | static int create_sem(int nsems, int permission) |
1b3f1330 | 56 | { |
96f6ad88 KZ |
57 | key_t key; |
58 | ||
59 | random_get_bytes(&key, sizeof(key)); | |
f5b0684e | 60 | return semget(key, nsems, permission | IPC_CREAT); |
1b3f1330 HJ |
61 | } |
62 | ||
6e1eda6f | 63 | static void __attribute__((__noreturn__)) usage(void) |
1b3f1330 | 64 | { |
6e1eda6f | 65 | FILE *out = stdout; |
7009af0e | 66 | fputs(USAGE_HEADER, out); |
f5b0684e | 67 | fprintf(out, _(" %s [options]\n"), program_invocation_short_name); |
003f4976 | 68 | |
451dbcfa BS |
69 | fputs(USAGE_SEPARATOR, out); |
70 | fputs(_("Create various IPC resources.\n"), out); | |
71 | ||
72 | fputs(USAGE_OPTIONS, out); | |
003f4976 | 73 | fputs(_(" -M, --shmem <size> create shared memory segment of size <size>\n"), out); |
7009af0e | 74 | fputs(_(" -S, --semaphore <number> create semaphore array with <number> elements\n"), out); |
003f4976 SK |
75 | fputs(_(" -Q, --queue create message queue\n"), out); |
76 | fputs(_(" -p, --mode <mode> permission for the resource (default is 0644)\n"), out); | |
77 | ||
7009af0e | 78 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 RM |
79 | printf(USAGE_HELP_OPTIONS(26)); |
80 | printf(USAGE_MAN_TAIL("ipcmk(1)")); | |
7009af0e | 81 | |
6e1eda6f | 82 | exit(EXIT_SUCCESS); |
1b3f1330 HJ |
83 | } |
84 | ||
85 | int main(int argc, char **argv) | |
86 | { | |
87 | int permission = 0644; | |
88 | int opt; | |
89 | size_t size = 0; | |
90 | int nsems = 0; | |
0eeec415 | 91 | int ask_shm = 0, ask_msg = 0, ask_sem = 0; |
003f4976 SK |
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'}, | |
99 | {NULL, 0, NULL, 0} | |
100 | }; | |
1b3f1330 | 101 | |
f635c336 KZ |
102 | setlocale(LC_ALL, ""); |
103 | bindtextdomain(PACKAGE, LOCALEDIR); | |
104 | textdomain(PACKAGE); | |
2c308875 | 105 | close_stdout_atexit(); |
f635c336 | 106 | |
003f4976 | 107 | while((opt = getopt_long(argc, argv, "hM:QS:p:Vh", longopts, NULL)) != -1) { |
1b3f1330 HJ |
108 | switch(opt) { |
109 | case 'M': | |
79f4c272 | 110 | size = strtosize_or_err(optarg, _("failed to parse size")); |
0eeec415 | 111 | ask_shm = 1; |
1b3f1330 HJ |
112 | break; |
113 | case 'Q': | |
0eeec415 | 114 | ask_msg = 1; |
1b3f1330 HJ |
115 | break; |
116 | case 'S': | |
20a39982 | 117 | nsems = strtos32_or_err(optarg, _("failed to parse elements")); |
0eeec415 | 118 | ask_sem = 1; |
1b3f1330 HJ |
119 | break; |
120 | case 'p': | |
121 | permission = strtoul(optarg, NULL, 8); | |
122 | break; | |
2c308875 | 123 | |
cc41c0a3 | 124 | case 'h': |
6e1eda6f | 125 | usage(); |
003f4976 | 126 | case 'V': |
2c308875 | 127 | print_version(EXIT_SUCCESS); |
1b3f1330 | 128 | default: |
677ec86c | 129 | errtryhelp(EXIT_FAILURE); |
1b3f1330 HJ |
130 | } |
131 | } | |
132 | ||
6e1eda6f RM |
133 | if(!ask_shm && !ask_msg && !ask_sem) { |
134 | warnx(_("bad usage")); | |
135 | errtryhelp(EXIT_FAILURE); | |
136 | } | |
0eeec415 | 137 | if (ask_shm) { |
1b3f1330 | 138 | int shmid; |
0eeec415 | 139 | if (-1 == (shmid = create_shm(size, permission))) |
f635c336 | 140 | err(EXIT_FAILURE, _("create share memory failed")); |
1b3f1330 | 141 | else |
f635c336 | 142 | printf(_("Shared memory id: %d\n"), shmid); |
1b3f1330 HJ |
143 | } |
144 | ||
0eeec415 | 145 | if (ask_msg) { |
1b3f1330 | 146 | int msgid; |
0eeec415 | 147 | if (-1 == (msgid = create_msg(permission))) |
f635c336 | 148 | err(EXIT_FAILURE, _("create message queue failed")); |
1b3f1330 | 149 | else |
f635c336 | 150 | printf(_("Message queue id: %d\n"), msgid); |
1b3f1330 HJ |
151 | } |
152 | ||
0eeec415 | 153 | if (ask_sem) { |
1b3f1330 | 154 | int semid; |
0eeec415 | 155 | if (-1 == (semid = create_sem(nsems, permission))) |
f635c336 | 156 | err(EXIT_FAILURE, _("create semaphore failed")); |
1b3f1330 | 157 | else |
f635c336 | 158 | printf(_("Semaphore id: %d\n"), semid); |
1b3f1330 HJ |
159 | } |
160 | ||
cc41c0a3 | 161 | return EXIT_SUCCESS; |
1b3f1330 | 162 | } |