From d6697da5e854355ac3718a85006b73315d0702aa Mon Sep 17 00:00:00 2001 From: Eric Yang Date: Mon, 5 Aug 2019 13:59:12 -0400 Subject: [PATCH] YARN-9667. Use setbuf with line buffer to reduce fflush complexity in container-executor. Contributed by Peter Bacsko --- .../impl/container-executor.c | 225 +++--------------- .../native/container-executor/impl/main.c | 25 +- .../impl/modules/devices/devices-module.c | 1 - .../native/container-executor/impl/util.c | 3 +- .../native/container-executor/impl/util.h | 3 +- .../impl/utils/docker-util.c | 1 - .../test/test-container-executor.c | 12 + 7 files changed, 57 insertions(+), 213 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c index 1e117b69c85..69dee3578d0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/container-executor.c @@ -143,7 +143,6 @@ int check_executor_permissions(char *executable_file) { fprintf(ERRORFILE, "Error resolving the canonical name for the executable : %s!", strerror(errno)); - fflush(ERRORFILE); return -1; } @@ -152,7 +151,6 @@ int check_executor_permissions(char *executable_file) { if (stat(resolved_path, &filestat) != 0) { fprintf(ERRORFILE, "Could not stat the executable : %s!.\n", strerror(errno)); - fflush(ERRORFILE); return -1; } @@ -163,14 +161,12 @@ int check_executor_permissions(char *executable_file) { if (binary_euid != 0) { fprintf(LOGFILE, "The container-executor binary should be user-owned by root.\n"); - fflush(LOGFILE); return -1; } if (binary_gid != getgid()) { fprintf(LOGFILE, "The configured nodemanager group %d is different from" " the group of the executable %d\n", getgid(), binary_gid); - fflush(LOGFILE); return -1; } @@ -180,14 +176,12 @@ int check_executor_permissions(char *executable_file) { fprintf(LOGFILE, "The container-executor binary should not have write or execute " "for others.\n"); - fflush(LOGFILE); return -1; } // Binary should be setuid executable if ((filestat.st_mode & S_ISUID) == 0) { fprintf(LOGFILE, "The container-executor binary should be set setuid.\n"); - fflush(LOGFILE); return -1; } @@ -207,13 +201,11 @@ static int change_effective_user(uid_t user, gid_t group) { if (setegid(group) != 0) { fprintf(LOGFILE, "Failed to set effective group id %d - %s\n", group, strerror(errno)); - fflush(LOGFILE); return -1; } if (seteuid(user) != 0) { fprintf(LOGFILE, "Failed to set effective user id %d - %s\n", user, strerror(errno)); - fflush(LOGFILE); return -1; } return 0; @@ -238,7 +230,6 @@ static int write_pid_to_cgroup_as_root(const char* cgroup_file, pid_t pid) { if (cgroup_fd == -1) { fprintf(LOGFILE, "Can't open file %s as node manager - %s\n", cgroup_file, strerror(errno)); - fflush(LOGFILE); rc = -1; goto cleanup; } @@ -251,7 +242,6 @@ static int write_pid_to_cgroup_as_root(const char* cgroup_file, pid_t pid) { if (written == -1) { fprintf(LOGFILE, "Failed to write pid to file %s - %s\n", cgroup_file, strerror(errno)); - fflush(LOGFILE); rc = -1; goto cleanup; } @@ -277,20 +267,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; } @@ -303,7 +290,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; } @@ -313,7 +299,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; @@ -341,7 +326,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; } @@ -357,7 +341,6 @@ static int write_exit_code_file_as_nm(const char* exit_code_file, if (ecode_fd == -1) { fprintf(LOGFILE, "Can't open file %s - %s\n", tmp_ecode_file, strerror(errno)); - fflush(LOGFILE); rc = -1; goto cleanup; } @@ -369,7 +352,6 @@ static int write_exit_code_file_as_nm(const char* exit_code_file, if (written == -1) { fprintf(LOGFILE, "Failed to write exit code to file %s - %s\n", tmp_ecode_file, strerror(errno)); - fflush(LOGFILE); rc = -1; goto cleanup; } @@ -379,7 +361,6 @@ static int write_exit_code_file_as_nm(const char* exit_code_file, if (rename(tmp_ecode_file, exit_code_file)) { fprintf(LOGFILE, "Can't move exit code file from %s to %s - %s\n", tmp_ecode_file, exit_code_file, strerror(errno)); - fflush(LOGFILE); unlink(tmp_ecode_file); rc = -1; goto cleanup; @@ -390,7 +371,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; } @@ -409,7 +389,6 @@ static int wait_and_get_exit_code(pid_t pid) { if (waitpid_result < 0) { fprintf(LOGFILE, "error waiting for process %" PRId64 " - %s\n", (int64_t)pid, strerror(errno)); - fflush(LOGFILE); return -1; } @@ -419,7 +398,6 @@ static int wait_and_get_exit_code(pid_t pid) { exit_code = 0x80 + WTERMSIG(child_status); } else { fprintf(LOGFILE, "Unable to determine exit status for pid %" PRId64 "\n", (int64_t)pid); - fflush(LOGFILE); } return exit_code; @@ -455,7 +433,6 @@ int change_user(uid_t user, gid_t group) { fprintf(LOGFILE, "unable to reacquire root - %s\n", strerror(errno)); fprintf(LOGFILE, "Real: %d:%d; Effective: %d:%d\n", getuid(), getgid(), geteuid(), getegid()); - fflush(LOGFILE); return SETUID_OPER_FAILED; } if (setgid(group) != 0) { @@ -463,14 +440,12 @@ int change_user(uid_t user, gid_t group) { strerror(errno)); fprintf(LOGFILE, "Real: %d:%d; Effective: %d:%d\n", getuid(), getgid(), geteuid(), getegid()); - fflush(LOGFILE); return SETUID_OPER_FAILED; } if (setuid(user) != 0) { fprintf(LOGFILE, "unable to set user to %d - %s\n", user, strerror(errno)); fprintf(LOGFILE, "Real: %d:%d; Effective: %d:%d\n", getuid(), getgid(), geteuid(), getegid()); - fflush(LOGFILE); return SETUID_OPER_FAILED; } @@ -496,7 +471,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; } @@ -551,7 +525,6 @@ char *concatenate(char *concat_pattern, char *return_path_name, if (arg == NULL) { fprintf(LOGFILE, "One of the arguments passed for %s is null.\n", return_path_name); - fflush(LOGFILE); return NULL; } strlen_args += strlen(arg); @@ -564,7 +537,6 @@ char *concatenate(char *concat_pattern, char *return_path_name, return_path = (char *) malloc(str_len); if (return_path == NULL) { fprintf(LOGFILE, "Unable to allocate memory for %s.\n", return_path_name); - fflush(LOGFILE); return NULL; } va_start(ap, numArgs); @@ -614,12 +586,10 @@ int check_nm_local_dir(uid_t caller_uid, const char *nm_root) { int err = stat(nm_root, &info); if (err < 0) { fprintf(LOGFILE, "Error checking file stats for %s %d %s.\n", nm_root, err, strerror(errno)); - fflush(LOGFILE); return 1; } if (caller_uid != info.st_uid) { fprintf(LOGFILE, "Permission mismatch for %s for caller uid: %d, owner uid: %d.\n", nm_root, caller_uid, info.st_uid); - fflush(LOGFILE); return 1; } return 0; @@ -685,8 +655,7 @@ int mkdirs(const char* path, mode_t perm) { } npath = strdup(path); if (npath == NULL) { - fprintf(LOGFILE, "Not enough memory to copy path string"); - fflush(LOGFILE); + fprintf(LOGFILE, "Not enough memory to copy path string\n"); return -1; } /* Skip leading slashes. */ @@ -729,7 +698,6 @@ int create_validate_dir(const char* npath, mode_t perm, const char* path, if (errno != EEXIST || stat(npath, &sb) != 0) { fprintf(LOGFILE, "Can't create directory %s - %s\n", npath, strerror(errno)); - fflush(LOGFILE); return -1; } // The directory npath should exist. @@ -750,14 +718,12 @@ int create_validate_dir(const char* npath, mode_t perm, const char* path, int check_dir(const char* npath, mode_t st_mode, mode_t desired, int finalComponent) { if (!S_ISDIR(st_mode)) { fprintf(LOGFILE, "Path %s is file not dir\n", npath); - fflush(LOGFILE); return -1; } else if (finalComponent == 1) { int filePermInt = st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); int desiredInt = desired & (S_IRWXU | S_IRWXG | S_IRWXO); if (filePermInt != desiredInt) { fprintf(LOGFILE, "Path %s has permission %o but needs permission %o.\n", npath, filePermInt, desiredInt); - fflush(LOGFILE); return -1; } } @@ -777,7 +743,6 @@ static int create_container_directories(const char* user, const char *app_id, user_detail == NULL || user_detail->pw_name == NULL) { fprintf(LOGFILE, "Either app_id, container_id or the user passed is null.\n"); - fflush(LOGFILE); return ERROR_CREATE_CONTAINER_DIRECTORIES_ARGUMENTS; } @@ -810,7 +775,6 @@ static int create_container_directories(const char* user, const char *app_id, char *combined_name = malloc(strlen(app_id) + strlen(container_id) + 2); if (combined_name == NULL) { fprintf(LOGFILE, "Malloc of combined name failed\n"); - fflush(LOGFILE); result = OUT_OF_MEMORY; } else { sprintf(combined_name, "%s/%s", app_id, container_id); @@ -823,7 +787,6 @@ static int create_container_directories(const char* user, const char *app_id, } if (strstr(container_log_dir, "..") != 0) { fprintf(LOGFILE, "Unsupported container log directory path detected.\n"); - fflush(LOGFILE); container_log_dir = NULL; } if (container_log_dir == NULL) { @@ -870,8 +833,7 @@ static struct passwd* get_user_info(const char* user) { } struct passwd* buffer = malloc(sizeof(struct passwd) + string_size); if (NULL == buffer) { - fprintf(LOGFILE, "Failed malloc in get_user_info"); - fflush(LOGFILE); + fprintf(LOGFILE, "Failed malloc in get_user_info\n"); return NULL; } if (getpwnam_r(user, buffer, ((char*)buffer) + sizeof(struct passwd), @@ -879,7 +841,6 @@ static struct passwd* get_user_info(const char* user) { free(buffer); fprintf(LOGFILE, "Can't get user information %s - %s\n", user, strerror(errno)); - fflush(LOGFILE); return NULL; } return result; @@ -911,7 +872,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); @@ -922,7 +882,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; } @@ -931,13 +890,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; } @@ -952,7 +909,6 @@ struct passwd* check_user(const char *user) { free_values(banned_users); } fprintf(LOGFILE, "Requested user %s is banned\n", user); - fflush(LOGFILE); return NULL; } } @@ -983,7 +939,6 @@ int set_user(const char *user) { if (initgroups(user, user_detail->pw_gid) != 0) { fprintf(LOGFILE, "Error setting supplementary groups for user %s: %s\n", user, strerror(errno)); - fflush(LOGFILE); return -1; } @@ -1005,7 +960,6 @@ static int change_owner(const char* path, uid_t user, gid_t group) { if (chown(path, user, group) != 0) { fprintf(LOGFILE, "Can't chown %s to %d:%d - %s\n", path, user, group, strerror(errno)); - fflush(LOGFILE); return -1; } return 0; @@ -1022,7 +976,6 @@ static int change_owner(const char* path, uid_t user, gid_t group) { if (chown(path, user, group) != 0) { fprintf(LOGFILE, "Can't chown %s to %d:%d - %s\n", path, user, group, strerror(errno)); - fflush(LOGFILE); return -1; } return change_effective_user(old_user, old_group); @@ -1054,24 +1007,20 @@ int create_directory_for_user(const char* path) { if (change_owner(path, user, nm_gid) != 0) { fprintf(LOGFILE, "Failed to chown %s to %d:%d: %s\n", path, user, nm_gid, strerror(errno)); - fflush(LOGFILE); ret = -1; } else if (chmod(path, permissions) != 0) { fprintf(LOGFILE, "Can't chmod %s to add the sticky bit - %s\n", path, strerror(errno)); - fflush(LOGFILE); ret = -1; } } else { fprintf(LOGFILE, "Failed to create directory %s - %s\n", path, strerror(errno)); - fflush(LOGFILE); ret = -1; } } if (change_effective_user(user, group) != 0) { fprintf(LOGFILE, "Failed to change user to %i - %i\n", user, group); - fflush(LOGFILE); ret = -1; } @@ -1092,7 +1041,6 @@ static int open_file_as_nm(const char* filename) { if (result == -1) { fprintf(LOGFILE, "Can't open file %s as node manager - %s\n", filename, strerror(errno)); - fflush(LOGFILE); } if (change_effective_user(user, group)) { result = -1; @@ -1111,9 +1059,8 @@ static int copy_file(int input, const char* in_filename, const int buffer_size = 128*1024; char* buffer = malloc(buffer_size); if (buffer == NULL) { - fprintf(LOGFILE, "Failed to allocate buffer while copying file: %s -> %s", + fprintf(LOGFILE, "Failed to allocate buffer while copying file: %s -> %s\n", in_filename, out_filename); - fflush(LOGFILE); return -1; } @@ -1121,7 +1068,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); free(buffer); return -1; } @@ -1134,7 +1080,6 @@ static int copy_file(int input, const char* in_filename, if (write_result <= 0) { fprintf(LOGFILE, "Error writing to %s - %s\n", out_filename, strerror(errno)); - fflush(LOGFILE); close(out_fd); free(buffer); return -1; @@ -1148,14 +1093,12 @@ static int copy_file(int input, const char* in_filename, if (len < 0) { fprintf(LOGFILE, "Failed to read file %s - %s\n", in_filename, strerror(errno)); - fflush(LOGFILE); close(out_fd); return -1; } if (close(out_fd) != 0) { fprintf(LOGFILE, "Failed to close file %s - %s\n", out_filename, strerror(errno)); - fflush(LOGFILE); return -1; } close(input); @@ -1174,7 +1117,6 @@ int initialize_user(const char *user, char* const* local_dirs) { user_dir = get_user_directory(*local_dir_ptr, user); if (user_dir == NULL) { fprintf(LOGFILE, "Couldn't get userdir directory for %s.\n", user); - fflush(LOGFILE); failed = 1; break; } @@ -1210,7 +1152,6 @@ int create_log_dirs(const char *app_id, char * const * log_dirs) { if (any_one_app_log_dir == NULL) { fprintf(LOGFILE, "Did not create any app-log directories\n"); - fflush(LOGFILE); return -1; } free(any_one_app_log_dir); @@ -1234,20 +1175,17 @@ char *init_log_path(const char *container_log_dir, const char *logfile) { if (change_owner(tmp_buffer, user_detail->pw_uid, nm_gid) != 0) { fprintf(ERRORFILE, "Failed to chown %s to %d:%d: %s\n", tmp_buffer, user_detail->pw_uid, nm_gid, strerror(errno)); - fflush(ERRORFILE); free(tmp_buffer); tmp_buffer = NULL; } else if (chmod(tmp_buffer, permissions) != 0) { fprintf(ERRORFILE, "Can't chmod %s - %s\n", tmp_buffer, strerror(errno)); - fflush(ERRORFILE); free(tmp_buffer); tmp_buffer = NULL; } } else { fprintf(ERRORFILE, "Failed to create file %s - %s\n", tmp_buffer, strerror(errno)); - fflush(ERRORFILE); free(tmp_buffer); tmp_buffer = NULL; } @@ -1266,7 +1204,6 @@ int create_container_log_dirs(const char *container_id, const char *app_id, fprintf(LOGFILE, "Failed to get container log directory name! Log root directory: %s, App id: %s, Container id: %s\n", *log_root, app_id, container_id); - fflush(LOGFILE); continue; } @@ -1274,7 +1211,6 @@ int create_container_log_dirs(const char *container_id, const char *app_id, if (result != 0 && container_log_dir != NULL) { fprintf(LOGFILE, "Unsupported container log directory path (%s) detected.\n", container_log_dir); - fflush(LOGFILE); free(container_log_dir); container_log_dir = NULL; continue; @@ -1283,7 +1219,6 @@ int create_container_log_dirs(const char *container_id, const char *app_id, if (create_directory_for_user(container_log_dir) != 0) { fprintf(LOGFILE, "Failed to create container log directory (%s)!\n", container_log_dir); - fflush(LOGFILE); free(container_log_dir); return -1; } @@ -1297,7 +1232,6 @@ int create_container_log_dirs(const char *container_id, const char *app_id, if (!created_any_dir) { fprintf(LOGFILE, "Did not create any container log directory.\n"); - fflush(LOGFILE); return -1; } return 0; @@ -1313,7 +1247,6 @@ int initialize_app(const char *user, const char *app_id, char* const* args) { if (app_id == NULL || user == NULL || user_detail == NULL || user_detail->pw_name == NULL) { fprintf(LOGFILE, "Either app_id is null or the user passed is null.\n"); - fflush(LOGFILE); return INVALID_ARGUMENT_NUMBER; } @@ -1366,7 +1299,6 @@ int initialize_app(const char *user, const char *app_id, if (primary_app_dir == NULL) { fprintf(LOGFILE, "Did not create any app directories\n"); - fflush(LOGFILE); return -1; } @@ -1397,13 +1329,11 @@ int initialize_app(const char *user, const char *app_id, } if (chdir(primary_app_dir) != 0) { fprintf(LOGFILE, "Failed to chdir to app dir - %s\n", strerror(errno)); - fflush(LOGFILE); return -1; } execvp(args[0], args); fprintf(ERRORFILE, "Failure to exec app initialization process - %s\n", strerror(errno)); - fflush(ERRORFILE); return -1; } @@ -1413,21 +1343,18 @@ char **construct_docker_command(const char *command_file) { uid_t user = geteuid(); gid_t group = getegid(); if (change_effective_user(nm_uid, nm_gid) != 0) { - fprintf(ERRORFILE, "Cannot change effective user to nm"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Cannot change effective user to nm\n"); exit(SETUID_OPER_FAILED); } ret = get_docker_command(command_file, &CFG, &buffer); if (ret != 0) { fprintf(ERRORFILE, "Error constructing docker command, docker error code=%d, error message='%s'\n", ret, get_docker_error_message(ret)); - fflush(ERRORFILE); exit(DOCKER_RUN_FAILED); } if (change_effective_user(user, group)) { - fprintf(ERRORFILE, "Cannot change effective user from nm back to original"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Cannot change effective user from nm back to original\n"); exit(SETUID_OPER_FAILED); } @@ -1443,8 +1370,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\n", docker_binary, strerror(errno)); - fflush(LOGFILE); - fflush(ERRORFILE); exit_code = DOCKER_RUN_FAILED; } else { exit_code = 0; @@ -1661,7 +1586,6 @@ int exec_container(const char *command_file) { if (ret != 0) { fprintf(ERRORFILE, "Couldn't execute the container launch with args %s - %s\n", binary, strerror(errno)); - fflush(ERRORFILE); exit_code = DOCKER_EXEC_FAILED; } else { exit_code = 0; @@ -1697,8 +1621,6 @@ int exec_docker_command(char *docker_command, char **argv, int argc) { // will only get here if execvp fails fprintf(ERRORFILE, "Couldn't execute the container launch with args %s - %s\n", docker_binary, strerror(errno)); - fflush(LOGFILE); - fflush(ERRORFILE); free(docker_binary); free(args); @@ -1717,16 +1639,14 @@ 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; } @@ -1734,15 +1654,13 @@ int create_script_paths(const char *work_dir, *keystore_file_dest = get_container_keystore_file(work_dir); if (NULL == keystore_file_dest) { exit_code = OUT_OF_MEMORY; - fprintf(ERRORFILE, "Could not create keystore_file_dest"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not create keystore_file_dest\n"); return exit_code; } *truststore_file_dest = get_container_truststore_file(work_dir); if (NULL == truststore_file_dest) { exit_code = OUT_OF_MEMORY; - fprintf(ERRORFILE, "Could not create truststore_file_dest"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not create truststore_file_dest\n"); return exit_code; } } @@ -1751,16 +1669,14 @@ int create_script_paths(const char *work_dir, *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_NM_ROOT_DIRS; - fprintf(ERRORFILE, "Could not open cred file"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not open cred file\n"); return exit_code; } @@ -1769,16 +1685,14 @@ int create_script_paths(const char *work_dir, *keystore_file_source = open_file_as_nm(keystore_file); if (*keystore_file_source == -1) { exit_code = INVALID_NM_ROOT_DIRS; - fprintf(ERRORFILE, "Could not open keystore file"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not open keystore file\n"); return exit_code; } // open truststore *truststore_file_source = open_file_as_nm(truststore_file); if (*truststore_file_source == -1) { exit_code = INVALID_NM_ROOT_DIRS; - fprintf(ERRORFILE, "Could not open truststore file"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not open truststore file\n"); return exit_code; } } @@ -1802,22 +1716,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 { @@ -1833,8 +1744,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; } @@ -1842,7 +1752,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 %s %s (%d)\n", script_name, script_file_dest, container_file_source); - fflush(ERRORFILE); exit_code = COULD_NOT_CREATE_SCRIPT_COPY; goto cleanup; } @@ -1851,8 +1760,7 @@ 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_COPY; - fprintf(ERRORFILE, "Could not copy file"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not copy file\n"); goto cleanup; } @@ -1861,8 +1769,7 @@ int create_local_dirs(const char * user, const char *app_id, if (copy_file(keystore_file_source, keystore_file, keystore_file_dest, S_IRUSR | S_IWUSR) != 0) { exit_code = COULD_NOT_CREATE_KEYSTORE_COPY; - fprintf(ERRORFILE, "Could not copy file"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not copy file\n"); goto cleanup; } @@ -1870,8 +1777,7 @@ int create_local_dirs(const char * user, const char *app_id, if (copy_file(truststore_file_source, truststore_file, truststore_file_dest, S_IRUSR | S_IWUSR) != 0) { exit_code = COULD_NOT_CREATE_TRUSTSTORE_COPY; - fprintf(ERRORFILE, "Could not copy file"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not copy file\n"); goto cleanup; } } @@ -1879,7 +1785,6 @@ int create_local_dirs(const char * user, const char *app_id, 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; @@ -1895,14 +1800,12 @@ int create_user_filecache_dirs(const char * user, char* const* local_dirs) { char* filecache_dir = get_user_filecache_directory(*ldir_p, user); if (filecache_dir == NULL) { fprintf(LOGFILE, "Couldn't get user filecache directory for %s.\n", user); - fflush(LOGFILE); rc = INITIALIZE_USER_FAILED; break; } if (0 != mkdir(filecache_dir, permissions) && EEXIST != errno) { fprintf(LOGFILE, "Failed to create directory %s - %s\n", filecache_dir, strerror(errno)); - fflush(LOGFILE); free(filecache_dir); rc = INITIALIZE_USER_FAILED; break; @@ -1967,7 +1870,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id, &keystore_file_dest, &truststore_file_dest, &container_file_source, &cred_file_source, &keystore_file_source, &truststore_file_source); if (exit_code != 0) { fprintf(ERRORFILE, "Could not create script path\n"); - fflush(ERRORFILE); goto cleanup; } @@ -1978,21 +1880,18 @@ int launch_docker_container_as_user(const char * user, const char *app_id, container_file_source, cred_file_source, keystore_file_source, truststore_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 cleanup; } exit_code = create_user_filecache_dirs(user, local_dirs); if (exit_code != 0) { - fprintf(ERRORFILE, "Could not create user filecache directory"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not create user filecache directory\n"); goto cleanup; } exit_code = create_yarn_sysfs(user, app_id, container_id, work_dir, local_dirs); if (exit_code != 0) { - fprintf(ERRORFILE, "Could not create user yarn sysfs directory"); - fflush(ERRORFILE); + fprintf(ERRORFILE, "Could not create user yarn sysfs directory\n"); exit(-1); goto cleanup; } @@ -2004,15 +1903,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 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 cleanup; } @@ -2035,7 +1932,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id, if (child_pid == -1) { fprintf (ERRORFILE, "Could not invoke docker %s.\n", docker_command_with_binary); - fflush(ERRORFILE); exit_code = UNABLE_TO_EXECUTE_CONTAINER_SCRIPT; goto cleanup; } @@ -2088,14 +1984,12 @@ int launch_docker_container_as_user(const char * user, const char *app_id, while (count < max_retries) { fprintf(LOGFILE, "Inspecting docker container...\n"); fprintf(LOGFILE, "Docker inspect command: %s\n", docker_inspect_command); - fflush(LOGFILE); FILE* inspect_docker = popen(docker_inspect_command, "r"); res = fscanf (inspect_docker, "%d", &pid); fprintf(LOGFILE, "pid from docker inspect: %d\n", pid); if (pclose (inspect_docker) != 0 || res <= 0) { fprintf (ERRORFILE, "Could not inspect docker to get pid %s.\n", docker_inspect_command); - fflush(ERRORFILE); exit_code = UNABLE_TO_EXECUTE_CONTAINER_SCRIPT; } else { if (pid != 0) { @@ -2128,7 +2022,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 cleanup; } @@ -2139,13 +2032,11 @@ 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 cleanup; } fprintf(LOGFILE, "Waiting for docker container to finish.\n"); - fflush(LOGFILE); // wait for pid to finish #ifdef __linux @@ -2155,7 +2046,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id, } if (dir_exists(proc_pid_path) == -1) { fprintf(ERRORFILE, "Error occurred checking %s\n", proc_pid_path); - fflush(ERRORFILE); } #else while (kill(pid,0) == 0) { @@ -2177,7 +2067,6 @@ int launch_docker_container_as_user(const char * user, const char *app_id, res = fscanf (inspect_exitcode_docker, "%d", &exit_code); if (pclose (inspect_exitcode_docker) != 0 || res <= 0) { fprintf (ERRORFILE, "Could not inspect docker to get Exit code %s.\n", docker_inspect_exitcode_command); - fflush(ERRORFILE); exit_code = -1; } else { if (exit_code != 0) { @@ -2188,23 +2077,19 @@ int launch_docker_container_as_user(const char * user, const char *app_id, count++; } fprintf(LOGFILE, "Exit code from docker inspect: %d\n", exit_code); - fflush(LOGFILE); 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); } fprintf(LOGFILE, "Wrote the exit code %d to %s\n", exit_code, exit_code_file); - fflush(LOGFILE); // 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 @@ -2261,8 +2146,7 @@ int launch_container_as_user(const char *user, const char *app_id, work_dir, script_name, cred_file, https, keystore_file, truststore_file, &script_file_dest, &cred_file_dest, &keystore_file_dest, &truststore_file_dest, &container_file_source, &cred_file_source, &keystore_file_source, &truststore_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; } @@ -2311,13 +2195,11 @@ int launch_container_as_user(const char *user, const char *app_id, 0, script_file_dest, cred_file_dest, keystore_file_dest, truststore_file_dest, container_file_source, cred_file_source, keystore_file_source, truststore_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; } fprintf(LOGFILE, "Launching container...\n"); - fflush(LOGFILE); #if HAVE_FCLOSEALL fcloseall(); @@ -2332,9 +2214,8 @@ int launch_container_as_user(const char *user, const char *app_id, umask(0027); if (execlp(script_file_dest, script_file_dest, NULL) != 0) { - fprintf(LOGFILE, "Couldn't execute the container launch file %s - %s", + fprintf(LOGFILE, "Couldn't execute the container launch file %s - %s\n", script_file_dest, strerror(errno)); - fflush(LOGFILE); exit_code = UNABLE_TO_EXECUTE_CONTAINER_SCRIPT; goto cleanup; } @@ -2362,7 +2243,6 @@ int signal_container_as_user(const char *user, int pid, int sig) { if (kill(-pid,0) < 0) { fprintf(LOGFILE, "Error signalling not exist process group %d " "with signal %d\n", pid, sig); - fflush(LOGFILE); return INVALID_CONTAINER_PID; } @@ -2371,14 +2251,12 @@ 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; } } fprintf(LOGFILE, "Killing process group %d with %d\n", pid, sig); - fflush(LOGFILE); return 0; } @@ -2392,7 +2270,6 @@ static int rmdir_as_nm(const char* path) { if (ret == 0) { if (rmdir(path) != 0 && errno != ENOENT) { fprintf(LOGFILE, "rmdir of %s failed - %s\n", path, strerror(errno)); - fflush(LOGFILE); ret = -1; } } @@ -2489,7 +2366,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, ret = -ret; fprintf(LOGFILE, "is_dir_helper(%s) failed: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } else if (ret == 0) { // is_dir_helper determined that the path is not a directory. @@ -2497,7 +2373,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, if (ret) { fprintf(LOGFILE, "failed to unlink %s: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); } goto done; } @@ -2514,7 +2389,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, goto done; } fprintf(LOGFILE, "chmod(%s) failed: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } fd = open_helper(dirfd, name); @@ -2526,7 +2400,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, goto done; } fprintf(LOGFILE, "error opening %s: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } if (fstat(fd, &stat) < 0) { @@ -2536,14 +2409,12 @@ static int recursive_unlink_helper(int dirfd, const char *name, goto done; } fprintf(LOGFILE, "failed to stat %s: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } if (!(S_ISDIR(stat.st_mode))) { ret = unlink_helper(dirfd, name, 0); if (ret) { fprintf(LOGFILE, "failed to unlink %s: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } } else { @@ -2557,7 +2428,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, goto done; } fprintf(LOGFILE, "chmod(%s) failed: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } } @@ -2569,7 +2439,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, goto done; } fprintf(LOGFILE, "fopendir(%s) failed: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } while (1) { @@ -2582,7 +2451,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, ret = errno; if (ret && ret != ENOENT) { fprintf(LOGFILE, "readdir(%s) failed: %s\n", fullpath, strerror(ret)); - fflush(LOGFILE); goto done; } break; @@ -2596,7 +2464,7 @@ static int recursive_unlink_helper(int dirfd, const char *name, if (asprintf(&new_fullpath, "%s/%s", fullpath, de->d_name) < 0) { fprintf(LOGFILE, "Failed to allocate string for %s/%s.\n", fullpath, de->d_name); - fflush(LOGFILE); + ret = ENOMEM; goto done; } @@ -2610,7 +2478,6 @@ static int recursive_unlink_helper(int dirfd, const char *name, ret = unlink_helper(dirfd, name, AT_REMOVEDIR); if (ret) { fprintf(LOGFILE, "failed to rmdir %s: %s\n", name, strerror(ret)); - fflush(LOGFILE); goto done; } } @@ -2643,14 +2510,12 @@ static int delete_path(const char *full_path, /* Return an error if the path is null. */ if (full_path == NULL) { fprintf(LOGFILE, "Path is null\n"); - fflush(LOGFILE); return PATH_TO_DELETE_IS_NULL; } ret = recursive_unlink_children(full_path); if (ret != 0) { fprintf(LOGFILE, "Error while deleting %s: %d (%s)\n", full_path, ret, strerror(ret)); - fflush(LOGFILE); return -1; } @@ -2668,7 +2533,6 @@ static int delete_path(const char *full_path, if (ret != ENOENT) { fprintf(LOGFILE, "Couldn't delete directory %s - %s\n", full_path, strerror(ret)); - fflush(LOGFILE); return -1; } } @@ -2704,14 +2568,12 @@ int delete_as_user(const char *user, continue; } else { fprintf(LOGFILE, "Could not stat %s - %s\n", *ptr, strerror(errno)); - fflush(LOGFILE); return -1; } } if (!S_ISDIR(sb.st_mode)) { if (!subDirEmptyStr) { fprintf(LOGFILE, "baseDir \"%s\" is a file and cannot contain subdir \"%s\".\n", *ptr, subdir); - fflush(LOGFILE); return -1; } full_path = strdup(*ptr); @@ -2746,7 +2608,6 @@ int list_as_user(const char *target_dir) { // If directory doesn't exist or can't be accessed, error out fprintf(LOGFILE, "Could not stat %s - %s\n", target_dir, strerror(errno)); - fflush(LOGFILE); ret = -1; } else if (!S_ISDIR(sb.st_mode)) { // If it's not a directory, list it as the only file @@ -2774,13 +2635,11 @@ int list_as_user(const char *target_dir) { if (errno != 0) { fprintf(LOGFILE, "Could not read directory %s - %s\n", target_dir, strerror(errno)); - fflush(LOGFILE); ret = -1; } } else { fprintf(LOGFILE, "Could not open directory %s - %s\n", target_dir, strerror(errno)); - fflush(LOGFILE); ret = -1; } } @@ -2808,8 +2667,7 @@ void chown_dir_contents(const char *dir_path, uid_t uid, gid_t gid) { if (result > 0 && result < len) { change_owner(path_tmp, uid, gid); } else { - fprintf(LOGFILE, "Ignored %s/%s due to length", dir_path, ep->d_name); - fflush(LOGFILE); + fprintf(LOGFILE, "Ignored %s/%s due to length\n", dir_path, ep->d_name); } } } @@ -2826,7 +2684,6 @@ int is_empty(char *target_dir) { if (!dir) { fprintf(LOGFILE, "Could not open directory %s - %s\n", target_dir, strerror(errno)); - fflush(LOGFILE); return 0; } while ((entry = readdir(dir)) != NULL) { @@ -2837,7 +2694,6 @@ int is_empty(char *target_dir) { continue; } fprintf(LOGFILE, "Directory is not empty %s\n", target_dir); - fflush(LOGFILE); return 0; } return 1; @@ -2852,7 +2708,6 @@ int is_empty(char *target_dir) { int mount_cgroup(const char *pair, const char *hierarchy) { #ifndef __linux fprintf(LOGFILE, "Failed to mount cgroup controller, not supported\n"); - fflush(LOGFILE); return -1; #else size_t len = strlen(pair); @@ -2863,13 +2718,12 @@ int mount_cgroup(const char *pair, const char *hierarchy) { if (controller == NULL || mount_path == NULL) { fprintf(LOGFILE, "Failed to mount cgroup controller; not enough memory\n"); - fflush(LOGFILE); + result = OUT_OF_MEMORY; goto cleanup; } if (hierarchy == NULL || strstr(hierarchy, "..") != NULL) { fprintf(LOGFILE, "Unsupported cgroup hierarhy path detected.\n"); - fflush(LOGFILE); result = INVALID_COMMAND_PROVIDED; goto cleanup; } @@ -2877,13 +2731,11 @@ int mount_cgroup(const char *pair, const char *hierarchy) { get_kv_value(pair, mount_path, len) < 0) { fprintf(LOGFILE, "Failed to mount cgroup controller; invalid option: %s\n", pair); - fflush(LOGFILE); result = -1; } else { if (strstr(mount_path, "..") != NULL) { fprintf(LOGFILE, "Unsupported cgroup mount path detected. %s\n", mount_path); - fflush(LOGFILE); result = INVALID_COMMAND_PROVIDED; goto cleanup; } @@ -2903,7 +2755,6 @@ int mount_cgroup(const char *pair, const char *hierarchy) { if (stat(hier_path, &sb) == 0 && (sb.st_uid != nm_uid || sb.st_gid != nm_gid)) { fprintf(LOGFILE, "cgroup hierarchy %s already owned by another user %d\n", hier_path, sb.st_uid); - fflush(LOGFILE); result = INVALID_COMMAND_PROVIDED; goto cleanup; } @@ -2914,7 +2765,6 @@ int mount_cgroup(const char *pair, const char *hierarchy) { } else { fprintf(LOGFILE, "Failed to mount cgroup controller %s at %s - %s\n", controller, mount_path, strerror(errno)); - fflush(LOGFILE); // if controller is already mounted, don't stop trying to mount others if (errno != EBUSY) { result = -1; @@ -2942,8 +2792,7 @@ static int run_traffic_control(const char *opts[], char *command_file) { } //too many args to tc if (i == max_tc_args - 1) { - fprintf(LOGFILE, "too many args to tc"); - fflush(LOGFILE); + fprintf(LOGFILE, "too many args to tc\n"); return TRAFFIC_CONTROL_EXECUTION_FAILED; } args[i++] = command_file; @@ -2954,7 +2803,6 @@ static int run_traffic_control(const char *opts[], char *command_file) { int exit_code = wait_and_get_exit_code(child_pid); if (exit_code != 0) { fprintf(LOGFILE, "failed to execute tc command!\n"); - fflush(LOGFILE); return TRAFFIC_CONTROL_EXECUTION_FAILED; } return 0; @@ -2962,7 +2810,6 @@ static int run_traffic_control(const char *opts[], char *command_file) { execv(TC_BIN, (char**)args); //if we reach here, exec failed fprintf(LOGFILE, "failed to execute tc command! error: %s\n", strerror(errno)); - fflush(LOGFILE); return TRAFFIC_CONTROL_EXECUTION_FAILED; } } @@ -3088,7 +2935,6 @@ int clean_docker_cgroups_internal(const char *mount_table, const char* container_id) { #ifndef __linux fprintf(LOGFILE, "Failed to clean cgroups, not supported\n"); - fflush(LOGFILE); return -1; #else const char * cgroup_mount_type = "cgroup"; @@ -3102,20 +2948,17 @@ int clean_docker_cgroups_internal(const char *mount_table, if (!mount_table || mount_table[0] == 0) { fprintf(ERRORFILE, "clean_docker_cgroups: Invalid mount table\n"); - fflush(ERRORFILE); rc = -1; goto cleanup; } if (!yarn_hierarchy || yarn_hierarchy[0] == 0) { fprintf(ERRORFILE, "clean_docker_cgroups: Invalid yarn_hierarchy\n"); - fflush(ERRORFILE); rc = -1; goto cleanup; } if (!validate_container_id(container_id)) { fprintf(ERRORFILE, "clean_docker_cgroups: Invalid container_id: %s\n", (container_id == NULL) ? "null" : container_id); - fflush(ERRORFILE); rc = -1; goto cleanup; } @@ -3123,7 +2966,6 @@ int clean_docker_cgroups_internal(const char *mount_table, if (fp == NULL) { fprintf(ERRORFILE, "clean_docker_cgroups: failed to open %s, error %d: %s\n", mount_table, errno, strerror(errno)); - fflush(ERRORFILE); rc = -1; goto cleanup; } @@ -3137,7 +2979,6 @@ int clean_docker_cgroups_internal(const char *mount_table, ret = sscanf(lineptr, " %ms %ms %*s %*s %*s %*s", &mnt_type, &mnt_dir); if (ret != 2) { fprintf(ERRORFILE, "clean_docker_cgroups: Failed to parse line: %s\n", lineptr); - fflush(ERRORFILE); rc = -1; break; } @@ -3146,7 +2987,6 @@ int clean_docker_cgroups_internal(const char *mount_table, } if ((mnt_dir == NULL) || (mnt_dir[0] == 0)) { fprintf(ERRORFILE, "clean_docker_cgroups: skipping mount entry with invalid mnt_dir\n"); - fflush(ERRORFILE); continue; } @@ -3154,7 +2994,6 @@ int clean_docker_cgroups_internal(const char *mount_table, full_path = make_string("%s/%s/%s", mnt_dir, yarn_hierarchy, container_id); if (full_path == NULL) { fprintf(ERRORFILE, "clean_docker_cgroups: Failed to allocate cgroup path.\n"); - fflush(ERRORFILE); rc = -1; break; } @@ -3163,7 +3002,6 @@ int clean_docker_cgroups_internal(const char *mount_table, if (!verify_path_safety(full_path)) { fprintf(ERRORFILE, "clean_docker_cgroups: skipping invalid path: %s\n", full_path); - fflush(ERRORFILE); continue; } @@ -3171,7 +3009,6 @@ int clean_docker_cgroups_internal(const char *mount_table, if ((ret == -1) && (errno != ENOENT)) { fprintf(ERRORFILE, "clean_docker_cgroups: Failed to rmdir cgroup, %s (error=%s)\n", full_path, strerror(errno)); - fflush(ERRORFILE); rc = -1; continue; } @@ -3179,7 +3016,6 @@ int clean_docker_cgroups_internal(const char *mount_table, if (ferror(fp)) { fprintf(ERRORFILE, "clean_docker_cgroups: Error reading %s, error=%d (%s) \n", mount_table, errno, strerror(errno)); - fflush(ERRORFILE); rc = -1; } @@ -3220,7 +3056,6 @@ int remove_docker_container(char**argv, int argc) { if (child_pid == -1) { fprintf (ERRORFILE, "Failed to fork for docker remove command\n"); - fflush(ERRORFILE); return DOCKER_RUN_FAILED; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/main.c b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/main.c index 8507ff89dee..f890bd77ab2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/main.c +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/main.c @@ -130,10 +130,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 @@ -232,7 +242,6 @@ static void assert_valid_setup(char *argv0) { static void display_feature_disabled_message(const char* name) { fprintf(ERRORFILE, "Feature disabled: %s\n", name); - fflush(ERRORFILE); } /* Use to store parsed input parmeters for various operations */ @@ -450,7 +459,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; @@ -459,14 +467,12 @@ 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++]; cmd_input.container_id = argv[optind++]; if (!validate_container_id(cmd_input.container_id)) { fprintf(ERRORFILE, "Invalid container id %s\n", cmd_input.container_id); - fflush(ERRORFILE); return INVALID_CONTAINER_ID; } cmd_input.cred_file = argv[optind++]; @@ -481,7 +487,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation) if (!(argc >= 14 && argc <= 17)) { fprintf(ERRORFILE, "Wrong number of arguments (%d vs 14 - 17) for" " launch docker container\n", argc); - fflush(ERRORFILE); return INVALID_ARGUMENT_NUMBER; } @@ -525,7 +530,6 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation) if (!(argc >= 14 && argc <= 17)) { fprintf(ERRORFILE, "Wrong number of arguments (%d vs 14 - 17)" " for launch container\n", argc); - fflush(ERRORFILE); return INVALID_ARGUMENT_NUMBER; } @@ -550,9 +554,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; @@ -578,7 +581,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; } @@ -587,14 +589,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; } @@ -615,8 +615,7 @@ static int validate_run_as_user_commands(int argc, char **argv, int *operation) *operation = RUN_AS_USER_SYNC_YARN_SYSFS; 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; } } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/modules/devices/devices-module.c b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/modules/devices/devices-module.c index 4063baeaeca..f57a93ee00f 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/modules/devices/devices-module.c +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/modules/devices/devices-module.c @@ -61,7 +61,6 @@ static int is_block_device(const char* value) { char* block_path = malloc(max_path_size); if (block_path == NULL) { fprintf(ERRORFILE, "Failed to allocate memory for sys device path string.\n"); - fflush(ERRORFILE); goto cleanup; } if (snprintf(block_path, max_path_size, "/sys/dev/block/%s", diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.c b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.c index 1753954cb2e..11654284af2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.c +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.c @@ -141,8 +141,7 @@ int execute_regex_match(const char *regex_str, const char *input) { regex_t regex; int regex_match; if (0 != regcomp(®ex, 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(®ex, input, (size_t) 0, NULL, 0); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.h b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.h index 5702b0c9d50..caa1c5f44f2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.h +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/util.h @@ -71,7 +71,8 @@ enum errorcodes { INVALID_CONTAINER_ID = 43, DOCKER_EXEC_FAILED = 44, COULD_NOT_CREATE_KEYSTORE_COPY = 45, - COULD_NOT_CREATE_TRUSTSTORE_COPY = 46 + COULD_NOT_CREATE_TRUSTSTORE_COPY = 46, + ERROR_CALLING_SETVBUF = 47 }; /* Macros for min/max. */ diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/utils/docker-util.c b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/utils/docker-util.c index 4abee027df6..17114338e72 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/utils/docker-util.c +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/impl/utils/docker-util.c @@ -318,7 +318,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; } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/test/test-container-executor.c b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/test/test-container-executor.c index 86a5d782325..2ff43c01755 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/test/test-container-executor.c +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/native/container-executor/test/test-container-executor.c @@ -1539,6 +1539,18 @@ 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");