From: B, Ravi Date: Mon, 26 Sep 2016 12:54:08 +0000 (+0530) Subject: env: tool: add command line option to input lockfile path X-Git-Url: http://git.dujemihanovic.xyz/?a=commitdiff_plain;h=d40dbfb7405cb74946ba0a6e9058045fdb5bddaf;p=u-boot.git env: tool: add command line option to input lockfile path The default lockname is set to /var/lock. This limits the usage of this application where OS uses different lockfile location parameter. For example, In case of android, the default lock path location is /data. Hence by providing the command line option to input lockfile path will be useful to reuse the tool across multiple operating system. usage: ./fw_printenv -l Signed-off-by: Ravi Babu --- diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h index 436eca9ddc..05588ab6d5 100644 --- a/tools/env/fw_env.h +++ b/tools/env/fw_env.h @@ -63,6 +63,7 @@ struct env_opts { #endif int aes_flag; /* Is AES encryption used? */ uint8_t aes_key[AES_KEY_LENGTH]; + char *lockname; }; int parse_aes_key(char *key, uint8_t *bin_key); diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c index 7a17b28f40..443de36e43 100644 --- a/tools/env/fw_env_main.c +++ b/tools/env/fw_env_main.c @@ -46,6 +46,7 @@ static struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"script", required_argument, NULL, 's'}, {"noheader", required_argument, NULL, 'n'}, + {"lock", required_argument, NULL, 'l'}, {NULL, 0, NULL, 0} }; @@ -72,6 +73,7 @@ void usage_printenv(void) " -c, --config configuration file, default:" CONFIG_FILE "\n" #endif " -n, --noheader do not repeat variable name in output\n" + " -l, --lock lock node, default:/var/lock\n" "\n"); } @@ -88,6 +90,7 @@ void usage_setenv(void) #ifdef CONFIG_FILE " -c, --config configuration file, default:" CONFIG_FILE "\n" #endif + " -l, --lock lock node, default:/var/lock\n" " -s, --script batch mode to minimize writes\n" "\n" "Examples:\n" @@ -119,7 +122,7 @@ static void parse_common_args(int argc, char *argv[]) env_opts.config_file = CONFIG_FILE; #endif - while ((c = getopt_long(argc, argv, ":a:c:h", long_options, NULL)) != + while ((c = getopt_long(argc, argv, ":a:c:l:h", long_options, NULL)) != EOF) { switch (c) { case 'a': @@ -134,6 +137,9 @@ static void parse_common_args(int argc, char *argv[]) env_opts.config_file = optarg; break; #endif + case 'l': + env_opts.lockname = optarg; + break; case 'h': do_printenv ? usage_printenv() : usage_setenv(); exit(EXIT_SUCCESS); @@ -155,8 +161,8 @@ int parse_printenv_args(int argc, char *argv[]) parse_common_args(argc, argv); - while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) != - EOF) { + while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL)) + != EOF) { switch (c) { case 'n': noheader = 1; @@ -164,6 +170,7 @@ int parse_printenv_args(int argc, char *argv[]) case 'a': case 'c': case 'h': + case 'l': /* ignore common options */ break; default: /* '?' */ @@ -181,8 +188,8 @@ int parse_setenv_args(int argc, char *argv[]) parse_common_args(argc, argv); - while ((c = getopt_long(argc, argv, "a:c:ns:h", long_options, NULL)) != - EOF) { + while ((c = getopt_long(argc, argv, "a:c:ns:l:h", long_options, NULL)) + != EOF) { switch (c) { case 's': script_file = optarg; @@ -190,6 +197,7 @@ int parse_setenv_args(int argc, char *argv[]) case 'a': case 'c': case 'h': + case 'l': /* ignore common options */ break; default: /* '?' */ @@ -203,7 +211,7 @@ int parse_setenv_args(int argc, char *argv[]) int main(int argc, char *argv[]) { - const char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; + char *lockname = "/var/lock/" CMD_PRINTENV ".lock"; int lockfd = -1; int retval = EXIT_SUCCESS; char *_cmdname; @@ -235,6 +243,18 @@ int main(int argc, char *argv[]) argc -= optind; argv += optind; + if (env_opts.lockname) { + lockname = malloc(sizeof(env_opts.lockname) + + sizeof(CMD_PRINTENV) + 10); + if (!lockname) { + fprintf(stderr, "Unable allocate memory"); + exit(EXIT_FAILURE); + } + + sprintf(lockname, "%s/%s.lock", + env_opts.lockname, CMD_PRINTENV); + } + lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666); if (-1 == lockfd) { fprintf(stderr, "Error opening lock file %s\n", lockname); @@ -260,6 +280,9 @@ int main(int argc, char *argv[]) } } + if (env_opts.lockname) + free(lockname); + flock(lockfd, LOCK_UN); close(lockfd); return retval;