Fix issues in nsenter.c and console handling

Signed-off-by: Michael Crosby <michael@docker.com>
This commit is contained in:
Michael Crosby 2014-08-06 22:04:28 -07:00
parent a48b001013
commit 5226b39d15
1 changed files with 7 additions and 20 deletions

View File

@ -71,7 +71,7 @@ int setns(int fd, int nstype)
void print_usage() void print_usage()
{ {
fprintf(stderr, fprintf(stderr,
"<binary> nsenter --nspid <pid> --containerjson <container_json> -- cmd1 arg1 arg2...\n"); "<binary> nsenter --nspid <pid> -- cmd1 arg1 arg2...\n");
} }
void nsenter() void nsenter()
@ -81,9 +81,10 @@ void nsenter()
get_args(&argc, &argv); get_args(&argc, &argv);
// Ignore if this is not for us. // Ignore if this is not for us.
if (argc < 6) { if (argc < 4) {
return; return;
} }
int found_nsenter = 0; int found_nsenter = 0;
for (c = 0; c < argc; ++c) { for (c = 0; c < argc; ++c) {
if (strcmp(argv[c], kNsEnter) == 0) { if (strcmp(argv[c], kNsEnter) == 0) {
@ -91,41 +92,31 @@ void nsenter()
break; break;
} }
} }
if (!found_nsenter) { if (!found_nsenter) {
return; return;
} }
static const struct option longopts[] = { static const struct option longopts[] = {
{"nspid", required_argument, NULL, 'n'}, {"nspid", required_argument, NULL, 'n'},
{"containerjson", optional_argument, NULL, 'c'}, {"console", required_argument, NULL, 't'},
{"console", optional_argument, NULL, 't'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
pid_t init_pid = -1; pid_t init_pid = -1;
char *init_pid_str = NULL; char *init_pid_str = NULL;
char *container_json = NULL;
char *console = NULL; char *console = NULL;
opterr = 0; while ((c = getopt_long_only(argc, argv, "n:c:", longopts, NULL)) != -1) {
while ((c =
getopt_long_only(argc, argv, "-n:s:c:", longopts,
NULL)) != -1) {
switch (c) { switch (c) {
case 'n': case 'n':
init_pid_str = optarg; init_pid_str = optarg;
break; break;
case 'c':
container_json = optarg;
break;
case 't': case 't':
console = optarg; console = optarg;
break; break;
} }
} }
if (strcmp(argv[optind - 2], kNsEnter) != 0) {
return;
}
if (init_pid_str == NULL) { if (init_pid_str == NULL) {
print_usage(); print_usage();
exit(1); exit(1);
@ -147,7 +138,6 @@ void nsenter()
fprintf(stderr, "setsid failed. Error: %s\n", strerror(errno)); fprintf(stderr, "setsid failed. Error: %s\n", strerror(errno));
exit(1); exit(1);
} }
// before we setns we need to dup the console // before we setns we need to dup the console
int consolefd = -1; int consolefd = -1;
if (console != NULL) { if (console != NULL) {
@ -159,7 +149,6 @@ void nsenter()
exit(1); exit(1);
} }
} }
// Setns on all supported namespaces. // Setns on all supported namespaces.
char ns_dir[PATH_MAX]; char ns_dir[PATH_MAX];
memset(ns_dir, 0, PATH_MAX); memset(ns_dir, 0, PATH_MAX);
@ -213,7 +202,6 @@ void nsenter()
exit(1); exit(1);
} }
} }
// Finish executing, let the Go runtime take over. // Finish executing, let the Go runtime take over.
return; return;
} else { } else {
@ -225,7 +213,6 @@ void nsenter()
strerror(errno)); strerror(errno));
exit(1); exit(1);
} }
// Forward the child's exit code or re-send its death signal. // Forward the child's exit code or re-send its death signal.
if (WIFEXITED(status)) { if (WIFEXITED(status)) {
exit(WEXITSTATUS(status)); exit(WEXITSTATUS(status));