From 19c221a82758291b81d945de3f9cf00639d7469a Mon Sep 17 00:00:00 2001 From: Ivana Hutarova Varekova Date: Thu, 28 Apr 2011 10:25:17 +0200 Subject: [PATCH] Add -h option to cgexec tool This patch adds option -h to cgexec tool Signed-off-by: Ivana Hutarova Varekova Signed-off-by: Jan Safranek --- doc/man/cgexec.1 | 6 +++++- src/tools/cgexec.c | 35 ++++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/doc/man/cgexec.1 b/doc/man/cgexec.1 index bf197bdb..e9d38d20 100644 --- a/doc/man/cgexec.1 +++ b/doc/man/cgexec.1 @@ -7,7 +7,7 @@ cgexec \- run the task in given control groups .SH SYNOPSIS -\fBcgexec\fR [\fB-g\fR <\fIcontrollers>:] [--sticky] \fBcommand\fR [\fIarguments\fR] +\fBcgexec\fR [\fB-h\fR] [\fB-g\fR <\fIcontrollers>:] [--sticky] \fBcommand\fR [\fIarguments\fR] .SH DESCRIPTION The \fBcgexec\fR @@ -31,6 +31,10 @@ If this option is not used then \fBcgexec\fR will automatically place the task to the right cgroup based on \fB/etc/cgrules.conf\fR. +.TP +.B -h, --help +Display this help and exit. + .TP .B --sticky If running the task \fBcommand\fR with this option, the daemon of diff --git a/src/tools/cgexec.c b/src/tools/cgexec.c index 8e0b4d8c..f03f1fa0 100644 --- a/src/tools/cgexec.c +++ b/src/tools/cgexec.c @@ -35,10 +35,25 @@ #include "tools-common.h" static struct option longopts[] = { - {"sticky", no_argument, NULL, 's'}, + {"sticky", no_argument, NULL, 's'}, + {"help", no_argument, NULL, 'h'}, {0, 0, 0, 0} }; +static void usage(int status, const char *program_name) +{ + if (status != 0) + fprintf(stderr, "Wrong input parameters," + " try %s --help' for more information.\n", + program_name); + else { + printf("Usage: %s [-h] [-g :] " + "[--sticky] command [arguments] ...\n", + program_name); + } +} + + int main(int argc, char *argv[]) { int ret = 0, i; @@ -50,17 +65,9 @@ int main(int argc, char *argv[]) int c; struct cgroup_group_spec *cgroup_list[CG_HIER_MAX]; - if (argc < 2) { - fprintf(stderr, "Usage is %s" - " [-g :]" - " [--sticky] command [arguments] \n", - argv[0]); - exit(2); - } - memset(cgroup_list, 0, sizeof(cgroup_list)); - while ((c = getopt_long(argc, argv, "+g:s", longopts, NULL)) > 0) { + while ((c = getopt_long(argc, argv, "+g:sh", longopts, NULL)) > 0) { switch (c) { case 'g': ret = parse_cgroup_spec(cgroup_list, optarg, @@ -75,16 +82,18 @@ int main(int argc, char *argv[]) case 's': flag_child |= CGROUP_DAEMON_UNCHANGE_CHILDREN; break; + case 'h': + usage(0, argv[0]); + exit(0); default: - fprintf(stderr, "Invalid command line option\n"); + usage(1, argv[0]); exit(1); - break; } } /* Executable name */ if (!argv[optind]) { - fprintf(stderr, "No command specified\n"); + usage(1, argv[0]); exit(1); } -- 2.47.2