]>
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 | * | |
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> | |
27 | ||
28 | #include <unistd.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/ipc.h> | |
31 | #include <sys/shm.h> | |
32 | #include <sys/sem.h> | |
33 | #include <sys/msg.h> | |
34 | ||
f635c336 | 35 | #include "nls.h" |
eb76ca98 | 36 | #include "c.h" |
f635c336 | 37 | |
cc41c0a3 KZ |
38 | static const char *progname; |
39 | ||
1b3f1330 HJ |
40 | key_t createKey(void) |
41 | { | |
42 | srandom( time( NULL ) ); | |
43 | return random(); | |
44 | } | |
45 | ||
46 | int createShm(size_t size, int permission) | |
47 | { | |
48 | int result = -1; | |
49 | int shmid; | |
50 | key_t key = createKey(); | |
51 | ||
52 | if (-1 != (shmid = shmget(key, size, permission | IPC_CREAT))) | |
53 | result = shmid; | |
54 | ||
55 | return result; | |
56 | } | |
57 | ||
58 | int createMsg(int permission) | |
59 | { | |
60 | int result = -1; | |
61 | int msgid; | |
62 | key_t key = createKey(); | |
63 | ||
64 | if (-1 != (msgid = msgget(key, permission | IPC_CREAT))) | |
65 | result = msgid; | |
66 | ||
67 | return result; | |
68 | } | |
69 | ||
70 | int createSem(int nsems, int permission) | |
71 | { | |
72 | int result = -1; | |
73 | int semid; | |
74 | key_t key = createKey(); | |
75 | ||
76 | if (-1 != (semid = semget(key, nsems, permission | IPC_CREAT))) | |
77 | result = semid; | |
78 | ||
79 | return result; | |
80 | } | |
81 | ||
cc41c0a3 | 82 | void usage(int rc) |
1b3f1330 | 83 | { |
f635c336 KZ |
84 | printf(_("\nUsage: %s [options]\n\n"), progname); |
85 | printf(_( | |
cc41c0a3 KZ |
86 | " -M <size> create shared memory segment of size <size>\n" |
87 | " -S <nsems> create semaphore array with <nsems> elements\n" | |
88 | " -Q create message queue\n" | |
f635c336 KZ |
89 | " -p <mode> permission for the resource (default is 0644)\n")); |
90 | printf(_("\nFor more information see ipcmk(1).\n\n")); | |
cc41c0a3 KZ |
91 | |
92 | exit(rc); | |
1b3f1330 HJ |
93 | } |
94 | ||
95 | int main(int argc, char **argv) | |
96 | { | |
97 | int permission = 0644; | |
98 | int opt; | |
99 | size_t size = 0; | |
100 | int nsems = 0; | |
1b3f1330 HJ |
101 | int doShm = 0, doMsg = 0, doSem = 0; |
102 | ||
cc41c0a3 KZ |
103 | progname = program_invocation_short_name; |
104 | if (!progname) | |
105 | progname = "ipcmk"; | |
106 | ||
f635c336 KZ |
107 | setlocale(LC_ALL, ""); |
108 | bindtextdomain(PACKAGE, LOCALEDIR); | |
109 | textdomain(PACKAGE); | |
110 | ||
cc41c0a3 | 111 | while((opt = getopt(argc, argv, "hM:QS:p:")) != -1) { |
1b3f1330 HJ |
112 | switch(opt) { |
113 | case 'M': | |
114 | size = atoi(optarg); | |
115 | doShm = 1; | |
116 | break; | |
117 | case 'Q': | |
118 | doMsg = 1; | |
119 | break; | |
120 | case 'S': | |
121 | nsems = atoi(optarg); | |
122 | doSem = 1; | |
123 | break; | |
124 | case 'p': | |
125 | permission = strtoul(optarg, NULL, 8); | |
126 | break; | |
cc41c0a3 KZ |
127 | case 'h': |
128 | usage(EXIT_SUCCESS); | |
129 | break; | |
1b3f1330 HJ |
130 | default: |
131 | doShm = doMsg = doSem = 0; | |
132 | break; | |
133 | } | |
134 | } | |
135 | ||
cc41c0a3 KZ |
136 | if(!doShm && !doMsg && !doSem) |
137 | usage(EXIT_FAILURE); | |
138 | ||
1b3f1330 HJ |
139 | if (doShm) { |
140 | int shmid; | |
141 | if (-1 == (shmid = createShm(size, permission))) | |
f635c336 | 142 | err(EXIT_FAILURE, _("create share memory failed")); |
1b3f1330 | 143 | else |
f635c336 | 144 | printf(_("Shared memory id: %d\n"), shmid); |
1b3f1330 HJ |
145 | } |
146 | ||
147 | if (doMsg) { | |
148 | int msgid; | |
149 | if (-1 == (msgid = createMsg(permission))) | |
f635c336 | 150 | err(EXIT_FAILURE, _("create message queue failed")); |
1b3f1330 | 151 | else |
f635c336 | 152 | printf(_("Message queue id: %d\n"), msgid); |
1b3f1330 HJ |
153 | } |
154 | ||
155 | if (doSem) { | |
156 | int semid; | |
157 | if (-1 == (semid = createSem(nsems, permission))) | |
f635c336 | 158 | err(EXIT_FAILURE, _("create semaphore failed")); |
1b3f1330 | 159 | else |
f635c336 | 160 | printf(_("Semaphore id: %d\n"), semid); |
1b3f1330 HJ |
161 | } |
162 | ||
cc41c0a3 | 163 | return EXIT_SUCCESS; |
1b3f1330 | 164 | } |