]> git.ipfire.org Git - thirdparty/util-linux.git/blame - sys-utils/ipcmk.c
ipcmk: remove camel casing
[thirdparty/util-linux.git] / sys-utils / ipcmk.c
CommitLineData
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 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26#include <time.h>
003f4976 27#include <getopt.h>
1b3f1330
HJ
28
29#include <unistd.h>
30#include <sys/types.h>
31#include <sys/ipc.h>
32#include <sys/shm.h>
33#include <sys/sem.h>
34#include <sys/msg.h>
35
f635c336 36#include "nls.h"
eb76ca98 37#include "c.h"
c821a125 38#include "strutils.h"
f635c336 39
0eeec415 40key_t create_key(void)
1b3f1330 41{
0eeec415 42 srandom(time(NULL));
1b3f1330
HJ
43 return random();
44}
45
0eeec415 46int create_shm(size_t size, int permission)
1b3f1330 47{
0eeec415 48 key_t key = create_key();
f5b0684e 49 return shmget(key, size, permission | IPC_CREAT);
1b3f1330
HJ
50}
51
0eeec415 52int create_msg(int permission)
1b3f1330 53{
0eeec415 54 key_t key = create_key();
f5b0684e 55 return msgget(key, permission | IPC_CREAT);
1b3f1330
HJ
56}
57
0eeec415 58int create_sem(int nsems, int permission)
1b3f1330 59{
0eeec415 60 key_t key = create_key();
f5b0684e 61 return semget(key, nsems, permission | IPC_CREAT);
1b3f1330
HJ
62}
63
003f4976 64static void __attribute__ ((__noreturn__)) usage(FILE * out)
1b3f1330 65{
003f4976 66 fprintf(out, USAGE_HEADER);
f5b0684e 67 fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
003f4976
SK
68 fprintf(out, USAGE_OPTIONS);
69
70 fputs(_(" -M, --shmem <size> create shared memory segment of size <size>\n"), out);
71 fputs(_(" -S, --semaphore <nsems> create semaphore array with <nsems> elements\n"), out);
72 fputs(_(" -Q, --queue create message queue\n"), out);
73 fputs(_(" -p, --mode <mode> permission for the resource (default is 0644)\n"), out);
74
75 fprintf(out, USAGE_HELP);
76 fprintf(out, USAGE_VERSION);
77 fprintf(out, USAGE_BEGIN_TAIL);
78 fprintf(out, USAGE_MAN_TAIL, "ipcmk(1)");
79 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
1b3f1330
HJ
80}
81
82int main(int argc, char **argv)
83{
84 int permission = 0644;
85 int opt;
86 size_t size = 0;
87 int nsems = 0;
0eeec415 88 int ask_shm = 0, ask_msg = 0, ask_sem = 0;
003f4976
SK
89 static const struct option longopts[] = {
90 {"shmem", required_argument, NULL, 'M'},
91 {"semaphore", required_argument, NULL, 'S'},
92 {"queue", no_argument, NULL, 'Q'},
93 {"mode", required_argument, NULL, 'p'},
94 {"version", no_argument, NULL, 'V'},
95 {"help", no_argument, NULL, 'h'},
96 {NULL, 0, NULL, 0}
97 };
1b3f1330 98
f635c336
KZ
99 setlocale(LC_ALL, "");
100 bindtextdomain(PACKAGE, LOCALEDIR);
101 textdomain(PACKAGE);
102
003f4976 103 while((opt = getopt_long(argc, argv, "hM:QS:p:Vh", longopts, NULL)) != -1) {
1b3f1330
HJ
104 switch(opt) {
105 case 'M':
c821a125 106 size = strtol_or_err(optarg, _("failed to parse size"));
0eeec415 107 ask_shm = 1;
1b3f1330
HJ
108 break;
109 case 'Q':
0eeec415 110 ask_msg = 1;
1b3f1330
HJ
111 break;
112 case 'S':
c821a125 113 nsems = strtol_or_err(optarg, _("failed to parse elements"));
0eeec415 114 ask_sem = 1;
1b3f1330
HJ
115 break;
116 case 'p':
117 permission = strtoul(optarg, NULL, 8);
118 break;
cc41c0a3 119 case 'h':
003f4976 120 usage(stdout);
cc41c0a3 121 break;
003f4976
SK
122 case 'V':
123 printf(UTIL_LINUX_VERSION);
124 return EXIT_SUCCESS;
1b3f1330 125 default:
0eeec415 126 ask_shm = ask_msg = ask_sem = 0;
1b3f1330
HJ
127 break;
128 }
129 }
130
0eeec415 131 if(!ask_shm && !ask_msg && !ask_sem)
003f4976 132 usage(stderr);
cc41c0a3 133
0eeec415 134 if (ask_shm) {
1b3f1330 135 int shmid;
0eeec415 136 if (-1 == (shmid = create_shm(size, permission)))
f635c336 137 err(EXIT_FAILURE, _("create share memory failed"));
1b3f1330 138 else
f635c336 139 printf(_("Shared memory id: %d\n"), shmid);
1b3f1330
HJ
140 }
141
0eeec415 142 if (ask_msg) {
1b3f1330 143 int msgid;
0eeec415 144 if (-1 == (msgid = create_msg(permission)))
f635c336 145 err(EXIT_FAILURE, _("create message queue failed"));
1b3f1330 146 else
f635c336 147 printf(_("Message queue id: %d\n"), msgid);
1b3f1330
HJ
148 }
149
0eeec415 150 if (ask_sem) {
1b3f1330 151 int semid;
0eeec415 152 if (-1 == (semid = create_sem(nsems, permission)))
f635c336 153 err(EXIT_FAILURE, _("create semaphore failed"));
1b3f1330 154 else
f635c336 155 printf(_("Semaphore id: %d\n"), semid);
1b3f1330
HJ
156 }
157
cc41c0a3 158 return EXIT_SUCCESS;
1b3f1330 159}