From a7912aa1423314fe3a7d49352f306993b9778f8a Mon Sep 17 00:00:00 2001 From: Jarkko Sakkinen Date: Tue, 22 Oct 2013 22:26:26 +0300 Subject: [PATCH] Add parameter name back to the chsmack error message This patch add parameter name back to the error message. Instead of relaying on hazardous longindex parameter of getopt_long(), a look-up table is constructed to map short option to the corresponding struct option entry. Additionally, the basename of the application is added to the error message. Also usage message is converted to use basename instead of the full path name. (cherry picked from commit d98a04ff7ccbcfcf36d9d4d43de68448b70da9fa) --- utils/chsmack.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/utils/chsmack.c b/utils/chsmack.c index 1a20c81..ecb3a87 100644 --- a/utils/chsmack.c +++ b/utils/chsmack.c @@ -31,12 +31,12 @@ int main(int argc, char *argv[]) { - static struct option long_options[] = { + static struct option options[] = { {"access", required_argument, 0, 'a'}, {"exec", required_argument, 0, 'e'}, {"mmap", required_argument, 0, 'm'}, {"transmute", no_argument, 0, 't'}, - {0, 0, 0, 0} + {NULL, 0, 0, 0} }; /* Buffers are zeroed automatically by keeping them static variables. @@ -45,6 +45,7 @@ int main(int argc, char *argv[]) static char access_buf[SMACK_LABEL_LEN + 1]; static char exec_buf[SMACK_LABEL_LEN + 1]; static char mmap_buf[SMACK_LABEL_LEN + 1]; + static int options_map[128]; int transmute_flag = 0; int option_flag = 0; @@ -52,14 +53,18 @@ int main(int argc, char *argv[]) int c; int i; - while ((c = getopt_long(argc, argv, "a:e:m:t", long_options, + for (i = 0; options[i].name != NULL; i++) + options_map[options[i].val] = i; + + while ((c = getopt_long(argc, argv, "a:e:m:t", options, NULL)) != -1) { if ((c == 'a' || c == 'e' || c == 'm') && strnlen(optarg, SMACK_LABEL_LEN + 1) == (SMACK_LABEL_LEN + 1)) { - fprintf(stderr, "label \"%s\" " + fprintf(stderr, "%s: %s: \"%s\" " \ "exceeds %d characters.\n", - optarg, SMACK_LABEL_LEN); + basename(argv[0]), options[options_map[c]].name, optarg, + SMACK_LABEL_LEN); exit(1); } @@ -77,7 +82,7 @@ int main(int argc, char *argv[]) transmute_flag = 1; break; default: - printf("Usage: %s [options] \n", argv[0]); + printf("Usage: %s [options] \n", basename(argv[0])); printf("options:\n"); printf(" [--access|-a] set security.SMACK64\n"); printf(" [--exec|-e] set security.SMACK64EXEC\n"); -- 2.7.4