Commit 946a5852 authored by Kir Kolyshkin's avatar Kir Kolyshkin Committed by Pavel Emelyanov

criu: unify/improve bad argument messages

* Introduce a generic way to report that option argument is invalid
* Switch to using it from existing places
  (options --veth-pair, --port, -n)
* Check for invalid argument of -p and -t and report it.

Notes:

1) In order to correctly print long option name in case it was used
   instead of a short one, I had to move "struct option long_opts"
   to main() context, this is why the patch is so long.

2) pr_msg() (rather than pr_err()) is used to print errors, otherwise
   it is prefixed with that (crtools.c:123) prefix which makes it
   look weird.

3) Usage is not shown in case of error, otherwise an error message
   is lost in output.
Signed-off-by: 's avatarKir Kolyshkin <kir@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 8c0feee2
...@@ -83,21 +83,6 @@ int main(int argc, char *argv[]) ...@@ -83,21 +83,6 @@ int main(int argc, char *argv[])
int log_level = 0; int log_level = 0;
char *imgs_dir = "."; char *imgs_dir = ".";
char *work_dir = NULL; char *work_dir = NULL;
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
cr_pb_init();
restrict_uid(getuid(), getgid());
if (argc < 2)
goto usage;
init_opts();
if (init_service_fd())
return 1;
while (1) {
static const char short_opts[] = "dsRf:F:t:p:hcD:o:n:v::xVr:jlW:L:"; static const char short_opts[] = "dsRf:F:t:p:hcD:o:n:v::xVr:jlW:L:";
static struct option long_opts[] = { static struct option long_opts[] = {
{ "tree", required_argument, 0, 't' }, { "tree", required_argument, 0, 't' },
...@@ -138,6 +123,21 @@ int main(int argc, char *argv[]) ...@@ -138,6 +123,21 @@ int main(int argc, char *argv[])
{ }, { },
}; };
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
cr_pb_init();
restrict_uid(getuid(), getgid());
if (argc < 2)
goto usage;
init_opts();
if (init_service_fd())
return 1;
while (1) {
idx = -1;
opt = getopt_long(argc, argv, short_opts, long_opts, &idx); opt = getopt_long(argc, argv, short_opts, long_opts, &idx);
if (opt == -1) if (opt == -1)
break; break;
...@@ -154,9 +154,13 @@ int main(int argc, char *argv[]) ...@@ -154,9 +154,13 @@ int main(int argc, char *argv[])
break; break;
case 'p': case 'p':
pid = atoi(optarg); pid = atoi(optarg);
if (pid <= 0)
goto bad_arg;
break; break;
case 't': case 't':
tree_id = atoi(optarg); tree_id = atoi(optarg);
if (tree_id <= 0)
goto bad_arg;
break; break;
case 'c': case 'c':
opts.show_pages_content = true; opts.show_pages_content = true;
...@@ -184,7 +188,7 @@ int main(int argc, char *argv[]) ...@@ -184,7 +188,7 @@ int main(int argc, char *argv[])
break; break;
case 'n': case 'n':
if (parse_ns_string(optarg)) if (parse_ns_string(optarg))
return 1; goto bad_arg;
break; break;
case 'v': case 'v':
if (optarg) { if (optarg) {
...@@ -231,8 +235,7 @@ int main(int argc, char *argv[]) ...@@ -231,8 +235,7 @@ int main(int argc, char *argv[])
n->outside = strchr(optarg, '='); n->outside = strchr(optarg, '=');
if (n->outside == NULL) { if (n->outside == NULL) {
xfree(n); xfree(n);
pr_err("Invalid argument for --veth-pair\n"); goto bad_arg;
goto usage;
} }
*n->outside++ = '\0'; *n->outside++ = '\0';
...@@ -260,10 +263,8 @@ int main(int argc, char *argv[]) ...@@ -260,10 +263,8 @@ int main(int argc, char *argv[])
break; break;
case 52: case 52:
opts.ps_port = htons(atoi(optarg)); opts.ps_port = htons(atoi(optarg));
if (!opts.ps_port) { if (!opts.ps_port)
pr_err("Bad port\n"); goto bad_arg;
return 1;
}
break; break;
case 'j': case 'j':
opts.shell_job = true; opts.shell_job = true;
...@@ -469,4 +470,13 @@ usage: ...@@ -469,4 +470,13 @@ usage:
opt_pid_missing: opt_pid_missing:
pr_msg("No pid specified (-t option missing)\n"); pr_msg("No pid specified (-t option missing)\n");
return 1; return 1;
bad_arg:
if (idx < 0) /* short option */
pr_msg("Error: invalid argument for -%c: %s\n",
opt, optarg);
else /* long option */
pr_msg("Error: invalid argument for --%s: %s\n",
long_opts[idx].name, optarg);
return 1;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment