YARN-7796. Container-executor fails with segfault on certain OS configurations. Contributed by Gergo Repas.

(cherry picked from commit e7642a3e6f)
This commit is contained in:
Miklos Szegedi 2018-01-23 21:07:05 -08:00
parent e4ad5f7f37
commit bf795560fb
1 changed files with 15 additions and 5 deletions

View File

@ -998,13 +998,20 @@ static int open_file_as_nm(const char* filename) {
static int copy_file(int input, const char* in_filename, static int copy_file(int input, const char* in_filename,
const char* out_filename, mode_t perm) { const char* out_filename, mode_t perm) {
const int buffer_size = 128*1024; const int buffer_size = 128*1024;
char buffer[buffer_size]; char* buffer = malloc(buffer_size);
if (buffer == NULL) {
fprintf(LOGFILE, "Failed to allocate buffer while copying file: %s -> %s",
in_filename, out_filename);
fflush(LOGFILE);
return -1;
}
int out_fd = open(out_filename, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, perm); int out_fd = open(out_filename, O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, perm);
if (out_fd == -1) { if (out_fd == -1) {
fprintf(LOGFILE, "Can't open %s for output - %s\n", out_filename, fprintf(LOGFILE, "Can't open %s for output - %s\n", out_filename,
strerror(errno)); strerror(errno));
fflush(LOGFILE); fflush(LOGFILE);
free(buffer);
return -1; return -1;
} }
@ -1014,15 +1021,18 @@ static int copy_file(int input, const char* in_filename,
while (pos < len) { while (pos < len) {
ssize_t write_result = write(out_fd, buffer + pos, len - pos); ssize_t write_result = write(out_fd, buffer + pos, len - pos);
if (write_result <= 0) { if (write_result <= 0) {
fprintf(LOGFILE, "Error writing to %s - %s\n", out_filename, fprintf(LOGFILE, "Error writing to %s - %s\n", out_filename,
strerror(errno)); strerror(errno));
close(out_fd); close(out_fd);
return -1; free(buffer);
return -1;
} }
pos += write_result; pos += write_result;
} }
len = read(input, buffer, buffer_size); len = read(input, buffer, buffer_size);
} }
free(buffer);
if (len < 0) { if (len < 0) {
fprintf(LOGFILE, "Failed to read file %s - %s\n", in_filename, fprintf(LOGFILE, "Failed to read file %s - %s\n", in_filename,
strerror(errno)); strerror(errno));