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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/types.h>
38 static const char *progname
;
42 srandom( time( NULL
) );
46 int createShm(size_t size
, int permission
)
50 key_t key
= createKey();
52 if (-1 != (shmid
= shmget(key
, size
, permission
| IPC_CREAT
)))
58 int createMsg(int permission
)
62 key_t key
= createKey();
64 if (-1 != (msgid
= msgget(key
, permission
| IPC_CREAT
)))
70 int createSem(int nsems
, int permission
)
74 key_t key
= createKey();
76 if (-1 != (semid
= semget(key
, nsems
, permission
| IPC_CREAT
)))
84 FILE *out
= rc
== EXIT_FAILURE
? stderr
: stdout
;
86 fputs(_("\nUsage:\n"), out
);
88 _(" %s [options]\n"), progname
);
90 fputs(_("\nOptions:\n"), out
);
91 fputs(_(" -M <size> create shared memory segment of size <size>\n"
92 " -S <nsems> create semaphore array with <nsems> elements\n"
93 " -Q create message queue\n"
94 " -p <mode> permission for the resource (default is 0644)\n"), out
);
96 fputs(_("\nFor more information see ipcmk(1).\n"), out
);
101 int main(int argc
, char **argv
)
103 int permission
= 0644;
107 int doShm
= 0, doMsg
= 0, doSem
= 0;
109 progname
= program_invocation_short_name
;
113 setlocale(LC_ALL
, "");
114 bindtextdomain(PACKAGE
, LOCALEDIR
);
117 while((opt
= getopt(argc
, argv
, "hM:QS:p:")) != -1) {
127 nsems
= atoi(optarg
);
131 permission
= strtoul(optarg
, NULL
, 8);
137 doShm
= doMsg
= doSem
= 0;
142 if(!doShm
&& !doMsg
&& !doSem
)
147 if (-1 == (shmid
= createShm(size
, permission
)))
148 err(EXIT_FAILURE
, _("create share memory failed"));
150 printf(_("Shared memory id: %d\n"), shmid
);
155 if (-1 == (msgid
= createMsg(permission
)))
156 err(EXIT_FAILURE
, _("create message queue failed"));
158 printf(_("Message queue id: %d\n"), msgid
);
163 if (-1 == (semid
= createSem(nsems
, permission
)))
164 err(EXIT_FAILURE
, _("create semaphore failed"));
166 printf(_("Semaphore id: %d\n"), semid
);