Received: from msux-gh1-uea01.nsa.gov (msux-gh1-uea01.nsa.gov [63.239.67.1]) by tarius.tycho.ncsc.mil (8.13.1/8.13.1) with ESMTP id nA2HTIgF030298 for ; Mon, 2 Nov 2009 12:29:18 -0500 Received: from moss-lions.epoch.ncsc.mil (localhost [127.0.0.1]) by msux-gh1-uea01.nsa.gov (8.12.10/8.12.10) with ESMTP id nA2HSLNq000834 for ; Mon, 2 Nov 2009 17:28:21 GMT Received: from moss-lions.epoch.ncsc.mil (localhost.localdomain [127.0.0.1]) by moss-lions.epoch.ncsc.mil (8.14.3/8.14.3) with ESMTP id nA2HREAp007048 for ; Mon, 2 Nov 2009 12:27:14 -0500 Received: (from jwcart2@localhost) by moss-lions.epoch.ncsc.mil (8.14.3/8.14.3/Submit) id nA2HREVZ007046 for selinux@tycho.nsa.gov; Mon, 2 Nov 2009 12:27:14 -0500 Subject: Patch for Ticket #1 [1672486] (policycoreutils) From: Guido Trentalancia To: SELinux@tycho.nsa.gov Content-Type: multipart/mixed; boundary="=-S+dr3xlhgmP6mtSH0srn" Date: Mon, 02 Nov 2009 18:14:23 +0100 Message-Id: <1257182063.2916.22.camel@tesla.lan> Mime-Version: 1.0 Sender: owner-selinux@tycho.nsa.gov List-Id: selinux@tycho.nsa.gov --=-S+dr3xlhgmP6mtSH0srn Content-Type: text/plain Content-Transfer-Encoding: 7bit This patch is proposed to solve Ticket #1 [1672486] (command line binaries should support --version and --help). It adds handling of -h, -V and the long formats --help and --version to all binaries that are produced from C code. It does not tackle the issue for Python-based tools (e.g. semanage). Manual pages have also been updated accordingly. Guido Trentalancia diff -pru policycoreutils/load_policy/load_policy.8 policycoreutils-new/load_policy/load_policy.8 --- policycoreutils/load_policy/load_policy.8 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/load_policy/load_policy.8 2009-11-02 00:40:13.000000000 +0100 @@ -15,11 +15,25 @@ values in the policy file. .SH "OPTIONS" .TP +.B \-V +or +.B \-\-version +shows the current version of load_policy +.TP +.B \-h +or +.B \-\-help +shows the usage information for load_policy +.TP .B \-q +or +.B \-\-quiet suppress warning messages. .TP .B \-i -inital policy load. Only use this if this is the first time policy is being loaded since boot (usually called from initramfs). +or +.B \-\-init +initial policy load. Only use this if this is the first time policy is being loaded since boot (usually called from initramfs). .SH "EXIT STATUS" .TP diff -pru policycoreutils/load_policy/load_policy.c policycoreutils-new/load_policy/load_policy.c --- policycoreutils/load_policy/load_policy.c 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/load_policy/load_policy.c 2009-11-02 00:33:56.000000000 +0100 @@ -2,7 +2,7 @@ #include #include #include -#include +#include /* for getopt_long() form of getopt() */ #include #include #include @@ -13,19 +13,49 @@ #else #define _(msgid) (msgid) #endif +#define LOAD_POLICY_CONF_PROG_NAME "load_policy" /* default program name */ #ifndef PACKAGE #define PACKAGE "policycoreutils" /* the name of this package lang translation */ #endif void usage(char *progname) { - fprintf(stderr, _("usage: %s [-qi]\n"), progname); + fprintf(stderr, _("usage: %s -h | -V | [-qi]\n"), progname); + fprintf(stderr, _(" -h Show this message.\n")); + fprintf(stderr, _(" -V Show the version.\n")); + fprintf(stderr, _(" -q Suppress warning messages.\n")); + fprintf(stderr, _(" -i Inital policy load. Only use this if this is the first time policy is being loaded since boot.\n")); exit(1); } +static char *opt_program_name(char *argv0, char *def) +{ + if (argv0) { + if ((def = strrchr(argv0, '/'))) + ++def; + else + def = argv0; + + /* hack for libtool */ + if ((strlen(def) > strlen("lt-")) + && !memcmp("lt-", def, strlen("lt-"))) + def += 3; + } + + return (def); +} + int main(int argc, char **argv) { int ret, opt, quiet = 0, nargs, init=0, enforce=0; + char *program_name = NULL; + struct option long_options[] = { + {"quiet", no_argument, NULL, 'q'}, + {"init", no_argument, NULL, 'i'}, + {"version", no_argument, NULL, 'V'}, + {"help", no_argument, NULL, 'h'}, + {NULL, 0, NULL, 0} + }; #ifdef USE_NLS setlocale(LC_ALL, ""); @@ -33,11 +63,13 @@ int main(int argc, char **argv) textdomain(PACKAGE); #endif - while ((opt = getopt(argc, argv, "bqi")) > 0) { + program_name = opt_program_name(argv[0], LOAD_POLICY_CONF_PROG_NAME); + + while ((opt = getopt_long(argc, argv, "bqiVh", long_options, NULL)) != -1) { switch (opt) { case 'b': fprintf(stderr, "%s: Warning! The -b option is no longer supported, booleans are always preserved across reloads. Continuing...\n", - argv[0]); + program_name); break; case 'q': quiet = 1; @@ -46,8 +78,14 @@ int main(int argc, char **argv) case 'i': init = 1; break; + case 'V': + fprintf(stdout, + " %s version %s.\n", program_name, VERSION); + exit(EXIT_SUCCESS); + case 'h': + usage(program_name); default: - usage(argv[0]); + usage(program_name); } } diff -pru policycoreutils/load_policy/Makefile policycoreutils-new/load_policy/Makefile --- policycoreutils/load_policy/Makefile 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/load_policy/Makefile 2009-11-02 00:05:54.000000000 +0100 @@ -4,8 +4,9 @@ SBINDIR ?= $(PREFIX)/sbin MANDIR ?= $(PREFIX)/share/man LOCALEDIR ?= /usr/share/locale +VERSION = $(shell cat ../VERSION) CFLAGS ?= -Werror -Wall -W -override CFLAGS += $(LDFLAGS) -I$(PREFIX)/include -DUSE_NLS -DLOCALEDIR="\"$(LOCALEDIR)\"" -DPACKAGE="\"policycoreutils\"" +override CFLAGS += $(LDFLAGS) -I$(PREFIX)/include -DUSE_NLS -DLOCALEDIR="\"$(LOCALEDIR)\"" -DPACKAGE="\"policycoreutils\"" -DVERSION=\"$(VERSION)\" LDLIBS += -lsepol -lselinux -L$(PREFIX)/lib TARGETS=$(patsubst %.c,%,$(wildcard *.c)) diff -pru policycoreutils/newrole/newrole.1 policycoreutils-new/newrole/newrole.1 --- policycoreutils/newrole/newrole.1 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/newrole/newrole.1 2009-11-02 00:05:54.000000000 +0100 @@ -58,10 +58,16 @@ The new shell will be the shell specifie file. .PP The +.B -h +or +.B --help +shows the usage information for newrole. +.PP +The .B -V or .B --version -shows the current version of newrole +shows the current version of newrole. .PP .SH EXAMPLE .br diff -pru policycoreutils/newrole/newrole.c policycoreutils-new/newrole/newrole.c --- policycoreutils/newrole/newrole.c 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/newrole/newrole.c 2009-11-02 00:05:54.000000000 +0100 @@ -10,7 +10,7 @@ * * USAGE: * - * newrole [ -r role ] [ -t type ] [ -l level ] [ -V ] [ -- args ] + * newrole -h | -V | [ -r role ] [ -t type ] [ -l level ] [ -- args ] * * BUILD OPTIONS: * @@ -91,7 +91,7 @@ #endif /* USAGE_STRING describes the command-line args of this program. */ -#define USAGE_STRING "USAGE: newrole [ -r role ] [ -t type ] [ -l level ] [ -p ] [ -V ] [ -- args ]" +#define USAGE_STRING "USAGE: newrole -h | -V | [ -r role ] [ -t type ] [ -l level ] [ -p ] [ -- args ]" #ifdef USE_PAM #define PAM_SERVICE_CONFIG "/etc/selinux/newrole_pam.conf"; @@ -877,21 +877,24 @@ static int parse_command_line_arguments( {"level", 1, 0, 'l'}, {"preserve-environment", 0, 0, 'p'}, {"version", 0, 0, 'V'}, + {"help", 0, 0, 'h'}, {NULL, 0, 0, 0} }; *preserve_environment = 0; while (1) { - clflag = getopt_long(argc, argv, "r:t:l:pV", long_options, + clflag = getopt_long(argc, argv, "r:t:l:pVh", long_options, &flag_index); if (clflag == -1) break; switch (clflag) { + case 'h': + fprintf(stderr, "%s\n", USAGE_STRING); + exit(EXIT_SUCCESS); case 'V': printf("newrole: %s version %s\n", PACKAGE, VERSION); - exit(0); - break; + exit(EXIT_SUCCESS); case 'p': *preserve_environment = 1; break; diff -pru policycoreutils/restorecond/Makefile policycoreutils-new/restorecond/Makefile --- policycoreutils/restorecond/Makefile 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/restorecond/Makefile 2009-11-02 00:05:54.000000000 +0100 @@ -5,8 +5,9 @@ MANDIR = $(PREFIX)/share/man INITDIR = $(DESTDIR)/etc/rc.d/init.d SELINUXDIR = $(DESTDIR)/etc/selinux +VERSION = $(shell cat ../VERSION) CFLAGS ?= -g -Werror -Wall -W -override CFLAGS += -I$(PREFIX)/include -D_FILE_OFFSET_BITS=64 +override CFLAGS += -I$(PREFIX)/include -DVERSION=\"$(VERSION)\" -D_FILE_OFFSET_BITS=64 LDLIBS += -lselinux -L$(PREFIX)/lib all: restorecond diff -pru policycoreutils/restorecond/restorecond.8 policycoreutils-new/restorecond/restorecond.8 --- policycoreutils/restorecond/restorecond.8 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/restorecond/restorecond.8 2009-11-02 00:39:31.000000000 +0100 @@ -3,7 +3,7 @@ restorecond \- daemon that watches for file creation and then sets the default SELinux file context .SH "SYNOPSIS" -.B restorecond [\-d] +.B restorecond [\-d] [\-v] | \-h | \-V .P .SH "DESCRIPTION" @@ -17,8 +17,25 @@ the correct file context associated with .SH "OPTIONS" .TP .B \-d +or +.B \-\-debug Turns on debugging mode. Application will stay in the foreground and lots of debugs messages start printing. +.TP +.B \-v +or +.B \-\-verbose +Turns on verbose mode. Missing files are reported. +.TP +.B \-h +or +.B \-\-help +Shows the usage information. +.TP +.B \-V +or +.B \-\-version +Shows the version information. .SH "AUTHOR" This man page was written by Dan Walsh . diff -pru policycoreutils/restorecond/restorecond.c policycoreutils-new/restorecond/restorecond.c --- policycoreutils/restorecond/restorecond.c 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/restorecond/restorecond.c 2009-11-02 00:42:49.000000000 +0100 @@ -30,8 +30,10 @@ * and makes sure that there security context matches the systems defaults * * USAGE: - * restorecond [-d] [-v] - * + * restorecond -h | -V | [-d] [-v] + * + * -h Shows the usage information + * -V Shows the version * -d Run in debug mode * -v Run in verbose mode (Report missing files) * @@ -48,6 +50,7 @@ #include #include #include +#include /* for getopt_long() form of getopt() */ #include #include #include @@ -67,6 +70,8 @@ static int terminate = 0; #include #include +#define RESTORECOND_CONF_PROG_NAME "restorecond" /* default program name */ + /* size of the event structure, not counting name */ #define EVENT_SIZE (sizeof (struct inotify_event)) /* reasonable guess as to size of 1024 events */ @@ -374,8 +379,25 @@ static void term_handler() static void usage(char *program) { - printf("%s [-d] [-v] \n", program); - exit(0); + printf("%s [-d] [-v] | -h | -V \n", program); + exit(EXIT_SUCCESS); +} + +static char *opt_program_name(char *argv0, char *def) +{ + if (argv0) { + if ((def = strrchr(argv0, '/'))) + ++def; + else + def = argv0; + + /* hack for libtool */ + if ((strlen(def) > strlen("lt-")) + && !memcmp("lt-", def, strlen("lt-"))) + def += 3; + } + + return (def); } void exitApp(const char *msg) @@ -445,6 +467,14 @@ int main(int argc, char **argv) { int opt; struct sigaction sa; + char *program_name = NULL; + struct option long_options[] = { + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'V'}, + {"debug", no_argument, NULL, 'd'}, + {"verbose", no_argument, NULL, 'v'}, + {NULL, 0, NULL, 0} + }; #ifndef DEBUG /* Make sure we are root */ @@ -471,8 +501,17 @@ int main(int argc, char **argv) if (master_fd < 0) exitApp("inotify_init"); - while ((opt = getopt(argc, argv, "dv")) > 0) { + program_name = opt_program_name(argv[0], RESTORECOND_CONF_PROG_NAME); + + while ((opt = getopt_long(argc, argv, "hVdv", long_options, NULL)) != -1) { switch (opt) { + case 'h': + usage(program_name); + exit(EXIT_SUCCESS); + case 'V': + fprintf(stdout, + " %s version %s.\n", program_name, VERSION); + exit(EXIT_SUCCESS); case 'd': debug_mode = 1; break; @@ -480,7 +519,7 @@ int main(int argc, char **argv) verbose_mode = 1; break; case '?': - usage(argv[0]); + usage(program_name); } } read_config(master_fd); diff -pru policycoreutils/run_init/Makefile policycoreutils-new/run_init/Makefile --- policycoreutils/run_init/Makefile 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/run_init/Makefile 2009-11-02 00:05:54.000000000 +0100 @@ -8,8 +8,9 @@ LOCALEDIR ?= /usr/share/locale PAMH = $(shell ls /usr/include/security/pam_appl.h 2>/dev/null) AUDITH = $(shell ls /usr/include/libaudit.h 2>/dev/null) +VERSION = $(shell cat ../VERSION) CFLAGS ?= -Werror -Wall -W -override CFLAGS += -I$(PREFIX)/include -DUSE_NLS -DLOCALEDIR="\"$(LOCALEDIR)\"" -DPACKAGE="\"policycoreutils\"" +override CFLAGS += -I$(PREFIX)/include -DUSE_NLS -DLOCALEDIR="\"$(LOCALEDIR)\"" -DPACKAGE="\"policycoreutils\"" -DVERSION=\"$(VERSION)\" LDLIBS += -lselinux -L$(PREFIX)/lib ifeq (${PAMH}, /usr/include/security/pam_appl.h) override CFLAGS += -DUSE_PAM diff -pru policycoreutils/run_init/run_init.8 policycoreutils-new/run_init/run_init.8 --- policycoreutils/run_init/run_init.8 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/run_init/run_init.8 2009-11-02 01:18:22.000000000 +0100 @@ -3,12 +3,19 @@ run_init \- run an init script in the proper SELinux context .SH SYNOPSIS .B run_init -\fISCRIPT\fR [[\fIARGS\fR]...] +\fISCRIPT\fR [[\fIARGS\fR]...] | -h | -V .br .SH DESCRIPTION .PP Run a init script under the proper context, which is specified in /etc/selinux/POLICYTYPE/contexts/initrc_context. +.SH OPTIONS +.TP +.B \-h,\-\-help +Print the help message. +.TP +.B \-V,\-\-version +Print the version information. .SH FILES /etc/passwd - user account information .br diff -pru policycoreutils/run_init/run_init.c policycoreutils-new/run_init/run_init.c --- policycoreutils/run_init/run_init.c 2009-11-01 22:23:01.000000000 +0100 +++ policycoreutils-new/run_init/run_init.c 2009-11-02 01:18:27.000000000 +0100 @@ -8,7 +8,7 @@ * * USAGE: * - * run_init