]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/ipcmk.c
ipcmk: fix error codes and error messages
[thirdparty/util-linux.git] / sys-utils / ipcmk.c
1 /*
2 * ipcmk.c - used to create ad-hoc IPC segments
3 *
4 * Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com)
5 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
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 <err.h>
27 #include <time.h>
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
36 static const char *progname;
37
38 key_t createKey(void)
39 {
40 srandom( time( NULL ) );
41 return random();
42 }
43
44 int createShm(size_t size, int permission)
45 {
46 int result = -1;
47 int shmid;
48 key_t key = createKey();
49
50 if (-1 != (shmid = shmget(key, size, permission | IPC_CREAT)))
51 result = shmid;
52
53 return result;
54 }
55
56 int createMsg(int permission)
57 {
58 int result = -1;
59 int msgid;
60 key_t key = createKey();
61
62 if (-1 != (msgid = msgget(key, permission | IPC_CREAT)))
63 result = msgid;
64
65 return result;
66 }
67
68 int createSem(int nsems, int permission)
69 {
70 int result = -1;
71 int semid;
72 key_t key = createKey();
73
74 if (-1 != (semid = semget(key, nsems, permission | IPC_CREAT)))
75 result = semid;
76
77 return result;
78 }
79
80 void usage(int rc)
81 {
82 printf("\nUsage: %s [options]\n\n", progname);
83 printf(
84 " -M <size> create shared memory segment of size <size>\n"
85 " -S <nsems> create semaphore array with <nsems> elements\n"
86 " -Q create message queue\n"
87 " -p <mode> permission for the resource (default is 0644)\n");
88 printf("\nFor more information see ipcmk(1).\n\n");
89
90 exit(rc);
91 }
92
93 int main(int argc, char **argv)
94 {
95 int permission = 0644;
96 int opt;
97 size_t size = 0;
98 int nsems = 0;
99 int doShm = 0, doMsg = 0, doSem = 0;
100
101 progname = program_invocation_short_name;
102 if (!progname)
103 progname = "ipcmk";
104
105 while((opt = getopt(argc, argv, "hM:QS:p:")) != -1) {
106 switch(opt) {
107 case 'M':
108 size = atoi(optarg);
109 doShm = 1;
110 break;
111 case 'Q':
112 doMsg = 1;
113 break;
114 case 'S':
115 nsems = atoi(optarg);
116 doSem = 1;
117 break;
118 case 'p':
119 permission = strtoul(optarg, NULL, 8);
120 break;
121 case 'h':
122 usage(EXIT_SUCCESS);
123 break;
124 default:
125 doShm = doMsg = doSem = 0;
126 break;
127 }
128 }
129
130 if(!doShm && !doMsg && !doSem)
131 usage(EXIT_FAILURE);
132
133 if (doShm) {
134 int shmid;
135 if (-1 == (shmid = createShm(size, permission)))
136 err(EXIT_FAILURE, "create share memory failed");
137 else
138 printf("Shared memory id: %d\n", shmid);
139 }
140
141 if (doMsg) {
142 int msgid;
143 if (-1 == (msgid = createMsg(permission)))
144 err(EXIT_FAILURE, "create message queue failed");
145 else
146 printf("Message queue id: %d\n", msgid);
147 }
148
149 if (doSem) {
150 int semid;
151 if (-1 == (semid = createSem(nsems, permission)))
152 err(EXIT_FAILURE, "create semaphore failed");
153 else
154 printf("Semaphore id: %d\n", semid);
155 }
156
157 return EXIT_SUCCESS;
158 }