From: Hayden James Date: Wed, 10 Sep 2008 13:31:36 +0000 (+0200) Subject: ipcmk: new command X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b3f13304381e0c78ebc904af02672c2aaf936e5;p=util-linux ipcmk: new command Small patch to create an application that can easily create ad-hoc ipc resources, along with man page. [kzak@redhat.com: - rename from ipccreat to ipcmk - minor coding style changes] Signed-off-by: Hayden James Signed-off-by: Karel Zak --- diff --git a/sys-utils/.gitignore b/sys-utils/.gitignore index 02d1ee43..fb6e433b 100644 --- a/sys-utils/.gitignore +++ b/sys-utils/.gitignore @@ -4,6 +4,7 @@ dmesg flock ipcrm ipcs +ipcmk ldattach readprofile renice diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am index 9e6ecc69..0dbbb482 100644 --- a/sys-utils/Makefile.am +++ b/sys-utils/Makefile.am @@ -2,10 +2,10 @@ include $(top_srcdir)/config/include-Makefile.am bin_PROGRAMS = sbin_PROGRAMS = -usrbinexec_PROGRAMS = flock ipcrm ipcs renice setsid +usrbinexec_PROGRAMS = flock ipcrm ipcs ipcmk renice setsid usrsbinexec_PROGRAMS = readprofile -dist_man_MANS = flock.1 ipcrm.1 ipcs.1 renice.1 setsid.1 \ +dist_man_MANS = flock.1 ipcrm.1 ipcs.1 ipcmk.1 renice.1 setsid.1 \ readprofile.1 if LINUX diff --git a/sys-utils/ipcmk.1 b/sys-utils/ipcmk.1 new file mode 100644 index 00000000..640c2e85 --- /dev/null +++ b/sys-utils/ipcmk.1 @@ -0,0 +1,39 @@ +.\" Copyright 2008 Hayden A. James (hayden.james@gmail.com) +.\" May be distributed under the GNU General Public License +.TH "IPCMK" "1" "21 March 2008" "ipcmk" "Linux Programmer's Manual" +.SH "NAME" +ipcmk \- create various ipc resources +.SH "SYNOPSIS" +.B ipcmk [ \-M \fIsize\fR ] [ \-p \fImode\fR ] +.br +.B ipcmk [ \-S \fInsems\fR ] [ \-p \fImode\fR ] +.br +.B ipcmk [ \-Q ] [ \-p \fImode\fR ] +.SH "DESCRIPTION" +.B ipcmk +allows you to create shared memory segments, message queues or semaphore arrays. +.SH "OPTIONS" +Resources may be specified as follows: +.TP +.BI \-M " size" +shared memory segment of size \fB\fIsize\fR\fR +.TP +.BI \-S " nsems" +semaphore array with \fB\fInsems\fR\fR elements +.TP +.BI \-Q +message queue +.TP +Other options +.TP +.BI \-p " mode" +permission for the resource (default is 0644) +.PP +.SH "SEE ALSO" +.BR ipcrm (1), +.BR ipcs (1) +.SH "AUTHOR" +Hayden A. James (hayden.james@gmail.com) +.SH "AVAILABILITY" +The ipcmk command is part of the util\-linux\-ng package and is available from +ftp://ftp.kernel.org/pub/linux/utils/util\-linux\-ng/. diff --git a/sys-utils/ipcmk.c b/sys-utils/ipcmk.c new file mode 100644 index 00000000..de50888e --- /dev/null +++ b/sys-utils/ipcmk.c @@ -0,0 +1,140 @@ +/* + * ipcmk.c - used to create ad-hoc IPC segments + * + * Copyright (C) 2008 Hayden A. James (hayden.james@gmail.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +key_t createKey(void) +{ + srandom( time( NULL ) ); + return random(); +} + +int createShm(size_t size, int permission) +{ + int result = -1; + int shmid; + key_t key = createKey(); + + if (-1 != (shmid = shmget(key, size, permission | IPC_CREAT))) + result = shmid; + + return result; +} + +int createMsg(int permission) +{ + int result = -1; + int msgid; + key_t key = createKey(); + + if (-1 != (msgid = msgget(key, permission | IPC_CREAT))) + result = msgid; + + return result; +} + +int createSem(int nsems, int permission) +{ + int result = -1; + int semid; + key_t key = createKey(); + + if (-1 != (semid = semget(key, nsems, permission | IPC_CREAT))) + result = semid; + + return result; +} + +void usage(char *progname) +{ + fprintf(stderr, "usage: %s [-M size] [-S nsems] [-Q] [-p permission]\n", progname); +} + +int main(int argc, char **argv) +{ + int permission = 0644; + int opt; + size_t size = 0; + int nsems = 0; + + int doShm = 0, doMsg = 0, doSem = 0; + + while((opt = getopt(argc, argv, "M:QS:p:")) != -1) { + switch(opt) { + case 'M': + size = atoi(optarg); + doShm = 1; + break; + case 'Q': + doMsg = 1; + break; + case 'S': + nsems = atoi(optarg); + doSem = 1; + break; + case 'p': + permission = strtoul(optarg, NULL, 8); + break; + default: + doShm = doMsg = doSem = 0; + break; + } + } + + if (doShm) { + int shmid; + if (-1 == (shmid = createShm(size, permission))) + fprintf(stderr, "%s\n", strerror(errno)); + else + fprintf(stdout, "%s%d\n", "Shared memory id: ", shmid); + } + + if (doMsg) { + int msgid; + if (-1 == (msgid = createMsg(permission))) + fprintf(stderr, "%s\n", strerror(errno)); + else + fprintf(stdout, "%s%d\n", "Message queue id: ", msgid); + } + + if (doSem) { + int semid; + if (-1 == (semid = createSem(nsems, permission))) + fprintf(stderr, "%s\n", strerror(errno)); + else + fprintf(stdout, "%s%d\n", "Semaphore id: ", semid); + } + + if(!doShm && !doMsg && !doSem) + usage(argv[0]); + + return 0; +} diff --git a/sys-utils/ipcrm.1 b/sys-utils/ipcrm.1 index b94e6f14..f9dd676a 100644 --- a/sys-utils/ipcrm.1 +++ b/sys-utils/ipcrm.1 @@ -114,6 +114,7 @@ compatibility the previous syntax is still supported. .SH SEE ALSO .nh .BR ipcs (1), +.BR ipcmk (1), .BR msgctl (2), .BR msgget (2), .BR semctl (2), diff --git a/sys-utils/ipcs.1 b/sys-utils/ipcs.1 index f570d712..d51bce63 100644 --- a/sys-utils/ipcs.1 +++ b/sys-utils/ipcs.1 @@ -53,7 +53,8 @@ limits .B \-u summary .SH SEE ALSO -.BR ipcrm (1) +.BR ipcrm (1), +.BR ipcmk (1) .SH CONFORMING TO The Linux ipcs utility is not fully compatible to the POSIX ipcs utility. The Linux version does not support the