diff --git a/src/includes/common-includes.h b/src/includes/common-includes.h index a350af7b16cb20cc8c50b008dfa01073d074e3ef..c7b8e364d9de7640c4c5fd44699e1fd593e2602a 100644 --- a/src/includes/common-includes.h +++ b/src/includes/common-includes.h @@ -11,6 +11,7 @@ #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> +#include <getopt.h> #include <sys/reboot.h> #define U_DISABLE_CURL diff --git a/src/runtime-config.c b/src/runtime-config.c index 0151c506a99d2175b2c9091f825c5bdc3d78d457..fb112a9ec4338ebb0b789d32fe6205a82f5d0c32 100644 --- a/src/runtime-config.c +++ b/src/runtime-config.c @@ -11,18 +11,86 @@ struct config extern struct config config; struct config config; +void print_usage() +{ + printf("Usage:\n"); + printf(" cryptops-api --root-device=DEV --crypt-name=LABEL " + " [--notify-command=COMMAND]\n"); +} + void set_config_from_arguments(int argc, char ** argv) { - if (argc < 4) + // Set defaults. + char * crypt_name = NULL; + char * notify_command = ""; + char * root_device = NULL; + + int c; + while (true) + { + static struct option long_options[] = + { + {"crypt-name" , required_argument, 0, 'c'}, + {"notify-command", required_argument, 0, 'n'}, + {"root-device" , required_argument, 0, 'r'}, + {0, 0, 0, 0} + }; + int option_index = 0; + + c = getopt_long(argc, argv, "c:n:r:", long_options, &option_index); + + // Check if we have dealt with all options. + if (c == -1) + { + break; + } + + switch (c) + { + case 'c': + crypt_name = optarg; + break; + case 'n': + notify_command = optarg; + break; + case 'r': + root_device = optarg; + break; + case '?': + // getopt_long already printed an error message. + break; + default: + abort(); + } + } + + // Print any remaining command line arguments (not options). + if (optind < argc) + { + printf("non-option ARGV-elements: "); + while (optind < argc) + { + printf("%s ", argv[optind++]); + } + putchar('\n'); + } + + if (root_device == NULL) + { + printf("Required option --root-device missing.\n"); + print_usage(); + exit(1); + } + if (crypt_name == NULL) { - printf("Not enough arguments. Usage:\n"); - printf(" cryptops-api ROOTDEV CRYPTNAME NOTIFY_COMMAND\n"); + printf("Required option --crypt-name missing.\n"); + print_usage(); exit(1); } - config.root_device = argv[1]; - config.mapped_device_name = argv[2]; - config.notify_command = argv[3]; + config.root_device = root_device; + config.mapped_device_name = crypt_name; + config.notify_command = notify_command; asprintf(&config.info_partition_device, "%s1", config.root_device); asprintf(&config.data_partition_device, "%s2", config.root_device);