YARN-9667. Container-executor.c duplicates messages to stdout. Contributed by Peter Bacsko

This commit is contained in:
Jim Brennan 2020-10-12 15:41:33 +00:00
parent 3335fcfb05
commit 4bb0124c69
6 changed files with 40 additions and 75 deletions

View File

@ -256,20 +256,17 @@ static int write_pid_to_file_as_nm(const char* pid_file, pid_t pid) {
gid_t group = getegid();
if (change_effective_user(nm_uid, nm_gid) != 0) {
fprintf(ERRORFILE, "Could not change to effective users %d, %d\n", nm_uid, nm_gid);
fflush(ERRORFILE);
rc = -1;
goto cleanup;
}
temp_pid_file = concatenate("%s.tmp", "pid_file_path", 1, pid_file);
fprintf(LOGFILE, "Writing to tmp file %s\n", temp_pid_file);
fflush(LOGFILE);
// create with 700
int pid_fd = open(temp_pid_file, O_WRONLY|O_CREAT|O_EXCL, S_IRWXU);
if (pid_fd == -1) {
fprintf(LOGFILE, "Can't open file %s as node manager - %s\n", temp_pid_file,
strerror(errno));
fflush(LOGFILE);
rc = -1;
goto cleanup;
}
@ -282,7 +279,6 @@ static int write_pid_to_file_as_nm(const char* pid_file, pid_t pid) {
if (written == -1) {
fprintf(LOGFILE, "Failed to write pid to file %s as node manager - %s\n",
temp_pid_file, strerror(errno));
fflush(LOGFILE);
rc = -1;
goto cleanup;
}
@ -292,7 +288,6 @@ static int write_pid_to_file_as_nm(const char* pid_file, pid_t pid) {
if (rename(temp_pid_file, pid_file)) {
fprintf(LOGFILE, "Can't move pid file from %s to %s as node manager - %s\n",
temp_pid_file, pid_file, strerror(errno));
fflush(LOGFILE);
unlink(temp_pid_file);
rc = -1;
goto cleanup;
@ -320,7 +315,6 @@ static int write_exit_code_file_as_nm(const char* exit_code_file,
gid_t group = getegid();
if (change_effective_user(nm_uid, nm_gid) != 0) {
fprintf(ERRORFILE, "Could not change to effective users %d, %d\n", nm_uid, nm_gid);
fflush(ERRORFILE);
rc = -1;
goto cleanup;
}
@ -366,7 +360,6 @@ cleanup:
if (change_effective_user(user, group) != 0) {
fprintf(ERRORFILE,
"Could not change to effective users %d, %d\n", user, group);
fflush(ERRORFILE);
rc = -1;
}
@ -462,7 +455,6 @@ int is_feature_enabled(const char* feature_key, int default_value,
fprintf(LOGFILE, "Illegal value '%s' for '%s' in configuration. "
"Using default value: %d.\n", enabled_str, feature_key,
default_value);
fflush(LOGFILE);
free(enabled_str);
return default_value;
}
@ -834,7 +826,6 @@ int is_whitelisted(const char *user) {
struct passwd* check_user(const char *user) {
if (strcmp(user, "root") == 0) {
fprintf(LOGFILE, "Running as root is not allowed\n");
fflush(LOGFILE);
return NULL;
}
char *min_uid_str = get_section_value(MIN_USERID_KEY, &executor_cfg);
@ -845,7 +836,6 @@ struct passwd* check_user(const char *user) {
if (min_uid_str == end_ptr || *end_ptr != '\0') {
fprintf(LOGFILE, "Illegal value of %s for %s in configuration\n",
min_uid_str, MIN_USERID_KEY);
fflush(LOGFILE);
free(min_uid_str);
return NULL;
}
@ -854,13 +844,11 @@ struct passwd* check_user(const char *user) {
struct passwd *user_info = get_user_info(user);
if (NULL == user_info) {
fprintf(LOGFILE, "User %s not found\n", user);
fflush(LOGFILE);
return NULL;
}
if (user_info->pw_uid < min_uid && !is_whitelisted(user)) {
fprintf(LOGFILE, "Requested user %s is not whitelisted and has id %d,"
"which is below the minimum allowed %d\n", user, user_info->pw_uid, min_uid);
fflush(LOGFILE);
free(user_info);
return NULL;
}
@ -1029,7 +1017,6 @@ static int copy_file(int input, const char* in_filename,
if (out_fd == -1) {
fprintf(LOGFILE, "Can't open %s for output - %s\n", out_filename,
strerror(errno));
fflush(LOGFILE);
return -1;
}
@ -1311,8 +1298,6 @@ int run_docker(const char *command_file) {
if (execvp(docker_binary, args) != 0) {
fprintf(ERRORFILE, "Couldn't execute the container launch with args %s - %s",
docker_binary, strerror(errno));
fflush(LOGFILE);
fflush(ERRORFILE);
free(docker_binary);
free(args);
free(docker_command_with_binary);
@ -1333,32 +1318,28 @@ int create_script_paths(const char *work_dir,
*script_file_dest = get_container_launcher_file(work_dir);
if (script_file_dest == NULL) {
exit_code = OUT_OF_MEMORY;
fprintf(ERRORFILE, "Could not create script_file_dest");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create script_file_dest\n");
return exit_code;
}
*cred_file_dest = get_container_credentials_file(work_dir);
if (NULL == cred_file_dest) {
exit_code = OUT_OF_MEMORY;
fprintf(ERRORFILE, "Could not create cred_file_dest");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create cred_file_dest\n");
return exit_code;
}
// open launch script
*container_file_source = open_file_as_nm(script_name);
if (*container_file_source == -1) {
exit_code = INVALID_NM_ROOT_DIRS;
fprintf(ERRORFILE, "Could not open container file");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not open container file\n");
return exit_code;
}
// open credentials
*cred_file_source = open_file_as_nm(cred_file);
if (*cred_file_source == -1) {
exit_code = INVALID_ARGUMENT_NUMBER;
fprintf(ERRORFILE, "Could not open cred file");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not open cred file\n");
return exit_code;
}
@ -1377,22 +1358,19 @@ int create_local_dirs(const char * user, const char *app_id,
// create the user directory on all disks
int result = initialize_user(user, local_dirs);
if (result != 0) {
fprintf(ERRORFILE, "Could not create user dir");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create user dir\n");
return result;
}
// initializing log dirs
int log_create_result = create_log_dirs(app_id, log_dirs);
if (log_create_result != 0) {
fprintf(ERRORFILE, "Could not create log dirs");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create log dirs\n");
return log_create_result;
}
if (effective_user == 1) {
if (change_effective_user(user_detail->pw_uid, user_detail->pw_gid) != 0) {
fprintf(ERRORFILE, "Could not change to effective users %d, %d\n", user_detail->pw_uid, user_detail->pw_gid);
fflush(ERRORFILE);
goto cleanup;
}
} else {
@ -1417,8 +1395,7 @@ int create_local_dirs(const char * user, const char *app_id,
int directory_create_result = create_container_directories(user, app_id,
container_id, local_dirs, log_dirs, work_dir);
if (directory_create_result != 0) {
fprintf(ERRORFILE, "Could not create container dirs");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create container dirs\n");
exit_code = directory_create_result;
goto cleanup;
}
@ -1426,7 +1403,6 @@ int create_local_dirs(const char * user, const char *app_id,
// Copy script file with permissions 700
if (copy_file(container_file_source, script_name, script_file_dest,S_IRWXU) != 0) {
fprintf(ERRORFILE, "Could not create copy file %d %s\n", container_file_source, script_file_dest);
fflush(ERRORFILE);
exit_code = COULD_NOT_CREATE_SCRIPT_COPY;
goto cleanup;
}
@ -1435,15 +1411,13 @@ int create_local_dirs(const char * user, const char *app_id,
if (copy_file(cred_file_source, cred_file, cred_file_dest,
S_IRUSR | S_IWUSR) != 0) {
exit_code = COULD_NOT_CREATE_CREDENTIALS_FILE;
fprintf(ERRORFILE, "Could not copy file");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not copy file\n");
goto cleanup;
}
if (chdir(work_dir) != 0) {
fprintf(ERRORFILE, "Can't change directory to %s -%s\n", work_dir,
strerror(errno));
fflush(ERRORFILE);
goto cleanup;
}
exit_code = 0;
@ -1492,7 +1466,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
&container_file_source, &cred_file_source);
if (exit_code != 0) {
fprintf(ERRORFILE, "Could not create script path\n");
fflush(ERRORFILE);
goto pre_launch_cleanup;
}
@ -1503,7 +1476,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
container_file_source, cred_file_source);
if (exit_code != 0) {
fprintf(ERRORFILE, "Could not create local files and directories %d %d\n", container_file_source, cred_file_source);
fflush(ERRORFILE);
goto pre_launch_cleanup;
}
@ -1514,15 +1486,13 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
exit_code_file = get_exit_code_file(pid_file);
if (NULL == exit_code_file) {
exit_code = OUT_OF_MEMORY;
fprintf(ERRORFILE, "Container out of memory");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Container out of memory\n");
goto pre_launch_cleanup;
}
fprintf(LOGFILE, "Changing effective user to root...\n");
if (change_effective_user(0, user_gid) != 0) {
fprintf(ERRORFILE, "Could not change to effective users %d, %d\n", 0, user_gid);
fflush(ERRORFILE);
goto pre_launch_cleanup;
}
@ -1534,7 +1504,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
{
fprintf (ERRORFILE,
"Could not invoke docker %s.\n", docker_command_with_binary);
fflush(ERRORFILE);
exit_code = UNABLE_TO_EXECUTE_CONTAINER_SCRIPT;
goto post_launch_cleanup;
}
@ -1551,7 +1520,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
{
fprintf (ERRORFILE,
"Could not inspect docker to get pid %s.\n", docker_inspect_command);
fflush(ERRORFILE);
exit_code = UNABLE_TO_EXECUTE_CONTAINER_SCRIPT;
goto post_launch_cleanup;
}
@ -1579,8 +1547,7 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
if (pid_file == NULL
|| write_pid_to_file_as_nm(pid_file, (pid_t)pid) != 0) {
exit_code = WRITE_PIDFILE_FAILED;
fprintf(ERRORFILE, "Could not write pid to %s", pid_file);
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not write pid to %s\n", pid_file);
goto post_launch_cleanup;
}
@ -1593,7 +1560,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
if (pclose (wait_docker) != 0 || res <= 0) {
fprintf (ERRORFILE,
"Could not attach to docker; is container dead? %s.\n", docker_wait_command);
fflush(ERRORFILE);
}
if(exit_code != 0) {
fprintf(ERRORFILE, "Docker container exit code was not zero: %d\n",
@ -1607,22 +1573,18 @@ int launch_docker_container_as_user(const char * user, const char *app_id,
if(res < 1) {
fprintf(ERRORFILE, "%s %d %d\n",
"Unable to read from docker logs(ferror, feof):", ferror(logs), feof(logs));
fflush(ERRORFILE);
}
else {
fprintf(ERRORFILE, "%s\n", buffer);
fflush(ERRORFILE);
}
}
else {
fprintf(ERRORFILE, "%s\n", "Failed to get output of docker logs");
fprintf(ERRORFILE, "Command was '%s'\n", docker_logs_command);
fprintf(ERRORFILE, "%s\n", strerror(errno));
fflush(ERRORFILE);
}
if(pclose(logs) != 0) {
fprintf(ERRORFILE, "%s\n", "Failed to fetch docker logs");
fflush(ERRORFILE);
}
}
}
@ -1642,21 +1604,18 @@ post_launch_cleanup:
if (rm_docker == 0) {
fprintf(ERRORFILE,
"popen() failed: %s\n", strerror(errno));
fflush(ERRORFILE);
continue;
}
rc = pclose(rm_docker);
if (rc == -1) {
fprintf(ERRORFILE,
"pclose() failed: %s\n", strerror(errno));
fflush(ERRORFILE);
} else if (WIFEXITED(rc)) {
if (WEXITSTATUS(rc) == 0) {
break;
} else {
fprintf(ERRORFILE,
"docker rm command failed with exit status: %d\n", WEXITSTATUS(rc));
fflush(ERRORFILE);
}
}
}
@ -1665,7 +1624,6 @@ post_launch_cleanup:
// Tried 5 times and failed.
fprintf(ERRORFILE,
"Could not remove container after %d tries: %s\n", max_iterations, docker_rm_command);
fflush(ERRORFILE);
exit_code = UNABLE_TO_EXECUTE_CONTAINER_SCRIPT;
}
@ -1674,14 +1632,12 @@ pre_launch_cleanup:
if (exit_code_file != NULL && write_exit_code_file_as_nm(exit_code_file, exit_code) < 0) {
fprintf (ERRORFILE,
"Could not write exit code to file %s.\n", exit_code_file);
fflush(ERRORFILE);
}
// Drop root privileges
if (change_effective_user(prev_uid, user_gid) != 0) {
fprintf(ERRORFILE,
"Could not change to effective users %d, %d\n", prev_uid, user_gid);
fflush(ERRORFILE);
}
#if HAVE_FCLOSEALL
@ -1732,8 +1688,7 @@ int launch_container_as_user(const char *user, const char *app_id,
work_dir, script_name, cred_file, &script_file_dest, &cred_file_dest,
&container_file_source, &cred_file_source);
if (exit_code != 0) {
fprintf(ERRORFILE, "Could not create local files and directories");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create local files and directories\n");
goto cleanup;
}
@ -1782,8 +1737,7 @@ int launch_container_as_user(const char *user, const char *app_id,
0, script_file_dest, cred_file_dest,
container_file_source, cred_file_source);
if (exit_code != 0) {
fprintf(ERRORFILE, "Could not create local files and directories");
fflush(ERRORFILE);
fprintf(ERRORFILE, "Could not create local files and directories\n");
goto cleanup;
}
@ -1837,7 +1791,6 @@ int signal_container_as_user(const char *user, int pid, int sig) {
fprintf(LOGFILE,
"Error signalling process group %d with signal %d - %s\n",
-pid, sig, strerror(errno));
fflush(LOGFILE);
return UNABLE_TO_SIGNAL_CONTAINER;
} else {
return INVALID_CONTAINER_PID;

View File

@ -102,10 +102,20 @@ static void display_usage(FILE *stream) {
static void open_log_files() {
if (LOGFILE == NULL) {
LOGFILE = stdout;
if (setvbuf(LOGFILE, NULL, _IOLBF, BUFSIZ)) {
fprintf(LOGFILE, "Failed to invoke setvbuf() for LOGFILE: %s\n", strerror(errno));
fflush(LOGFILE);
exit(ERROR_CALLING_SETVBUF);
}
}
if (ERRORFILE == NULL) {
ERRORFILE = stderr;
if (setvbuf(ERRORFILE, NULL, _IOLBF, BUFSIZ)) {
fprintf(ERRORFILE, "Failed to invoke setvbuf() for ERRORFILE: %s\n", strerror(errno));
fflush(ERRORFILE);
exit(ERROR_CALLING_SETVBUF);
}
}
// There may be a process reading from stdout/stderr, and if it
@ -360,7 +370,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
fprintf(LOGFILE, "main : command provided %d\n", command);
fprintf(LOGFILE, "main : run as user is %s\n", cmd_input.run_as_user_name);
fprintf(LOGFILE, "main : requested yarn user is %s\n", cmd_input.yarn_user_name);
fflush(LOGFILE);
char * resources = NULL;// key,value pair describing resources
char * resources_key = NULL;
char * resources_value = NULL;
@ -369,7 +378,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
if (argc < 10) {
fprintf(ERRORFILE, "Too few arguments (%d vs 10) for initialize container\n",
argc);
fflush(ERRORFILE);
return INVALID_ARGUMENT_NUMBER;
}
cmd_input.app_id = argv[optind++];
@ -390,7 +398,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
if (!(argc == 14 || argc == 15)) {
fprintf(ERRORFILE, "Wrong number of arguments (%d vs 14 or 15) for"
" launch docker container\n", argc);
fflush(ERRORFILE);
return INVALID_ARGUMENT_NUMBER;
}
@ -413,7 +420,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
get_kv_value(resources, resources_value, strlen(resources)) < 0) {
fprintf(ERRORFILE, "Invalid arguments for cgroups resources: %s",
resources);
fflush(ERRORFILE);
free(resources_key);
free(resources_value);
return INVALID_ARGUMENT_NUMBER;
@ -443,7 +449,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
if (!(argc == 13 || argc == 14)) {
fprintf(ERRORFILE, "Wrong number of arguments (%d vs 13 or 14)"
" for launch container\n", argc);
fflush(ERRORFILE);
return INVALID_ARGUMENT_NUMBER;
}
@ -461,9 +466,8 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
if (get_kv_key(resources, resources_key, strlen(resources)) < 0 ||
get_kv_value(resources, resources_value, strlen(resources)) < 0) {
fprintf(ERRORFILE, "Invalid arguments for cgroups resources: %s",
fprintf(ERRORFILE, "Invalid arguments for cgroups resources: %s\n",
resources);
fflush(ERRORFILE);
free(resources_key);
free(resources_value);
return INVALID_ARGUMENT_NUMBER;
@ -489,7 +493,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
if (argc != 6) {
fprintf(ERRORFILE, "Wrong number of arguments (%d vs 6) for " \
"signal container\n", argc);
fflush(ERRORFILE);
return INVALID_ARGUMENT_NUMBER;
}
@ -498,14 +501,12 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
cmd_input.container_pid = strtol(option, &end_ptr, 10);
if (option == end_ptr || *end_ptr != '\0') {
fprintf(ERRORFILE, "Illegal argument for container pid %s\n", option);
fflush(ERRORFILE);
return INVALID_ARGUMENT_NUMBER;
}
option = argv[optind++];
cmd_input.signal = strtol(option, &end_ptr, 10);
if (option == end_ptr || *end_ptr != '\0') {
fprintf(ERRORFILE, "Illegal argument for signal %s\n", option);
fflush(ERRORFILE);
return INVALID_ARGUMENT_NUMBER;
}
@ -521,8 +522,7 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation)
*operation = RUN_AS_USER_LIST;
return 0;
default:
fprintf(ERRORFILE, "Invalid command %d not supported.",command);
fflush(ERRORFILE);
fprintf(ERRORFILE, "Invalid command %d not supported.\n",command);
return INVALID_COMMAND_PROVIDED;
}
}

View File

@ -138,8 +138,7 @@ int execute_regex_match(const char *regex_str, const char *input) {
regex_t regex;
int regex_match;
if (0 != regcomp(&regex, regex_str, REG_EXTENDED|REG_NOSUB)) {
fprintf(LOGFILE, "Unable to compile regex.");
fflush(LOGFILE);
fprintf(LOGFILE, "Unable to compile regex.\n");
exit(ERROR_COMPILING_REGEX);
}
regex_match = regexec(&regex, input, (size_t) 0, NULL, 0);

View File

@ -68,7 +68,8 @@ enum errorcodes {
DOCKER_IMAGE_INVALID = 40,
// DOCKER_CONTAINER_NAME_INVALID = 41, (NOT USED)
ERROR_COMPILING_REGEX = 42,
INVALID_CONTAINER_ID = 43
INVALID_CONTAINER_ID = 43,
ERROR_CALLING_SETVBUF = 44
};

View File

@ -167,7 +167,6 @@ static int validate_container_name(const char *container_name) {
}
}
fprintf(ERRORFILE, "Specified container_id=%s is invalid\n", container_name);
fflush(ERRORFILE);
return INVALID_DOCKER_CONTAINER_NAME;
}

View File

@ -1248,6 +1248,19 @@ int main(int argc, char **argv) {
LOGFILE = stdout;
ERRORFILE = stderr;
if (setvbuf(LOGFILE, NULL, _IOLBF, BUFSIZ)) {
fprintf(LOGFILE, "Failed to invoke setvbuf() for LOGFILE: %s\n", strerror(errno));
fflush(LOGFILE);
exit(ERROR_CALLING_SETVBUF);
}
if (setvbuf(ERRORFILE, NULL, _IOLBF, BUFSIZ)) {
fprintf(ERRORFILE, "Failed to invoke setvbuf() for ERRORFILE: %s\n", strerror(errno));
fflush(ERRORFILE);
exit(ERROR_CALLING_SETVBUF);
}
nm_uid = getuid();
printf("Attempting to clean up from any previous runs\n");