void test_delete_job() {
    char* local_dirs = get_value("mapred.local.dir");
    char* job_dir = get_job_directory(TEST_ROOT "/local-2", username, "job_2");
    char* dont_touch = get_job_directory(TEST_ROOT "/local-2", username,
                                         DONT_TOUCH_FILE);
    char* task_dir = get_attempt_work_directory(TEST_ROOT "/local-2",
                     username, "job_2", "task_1");
    char buffer[100000];
    sprintf(buffer, "mkdir -p %s/who/let/the/dogs/out/who/who", task_dir);
    run(buffer);
    sprintf(buffer, "touch %s", dont_touch);
    run(buffer);

    // soft link to the canary file from the task directory
    sprintf(buffer, "ln -s %s %s/who/softlink", dont_touch, task_dir);
    run(buffer);
    // hard link to the canary file from the task directory
    sprintf(buffer, "ln %s %s/who/hardlink", dont_touch, task_dir);
    run(buffer);
    // create a dot file in the task directory
    sprintf(buffer, "touch %s/who/let/.dotfile", task_dir);
    run(buffer);
    // create a no permission file
    sprintf(buffer, "touch %s/who/let/protect", task_dir);
    run(buffer);
    sprintf(buffer, "chmod 000 %s/who/let/protect", task_dir);
    run(buffer);
    // create a no permission directory
    sprintf(buffer, "chmod 000 %s/who/let", task_dir);
    run(buffer);

    // delete task directory
    int ret = delete_as_user(username, local_dirs, "jobcache/job_2");
    if (ret != 0) {
        printf("FAIL: return code from delete_as_user is %d\n", ret);
        exit(1);
    }

    // check to make sure the task directory is gone
    if (access(task_dir, R_OK) == 0) {
        printf("FAIL: failed to delete the directory - %s\n", task_dir);
        exit(1);
    }
    // check to make sure the job directory is gone
    if (access(job_dir, R_OK) == 0) {
        printf("FAIL: didn't delete the directory - %s\n", job_dir);
        exit(1);
    }
    // but that the canary is not gone
    if (access(dont_touch, R_OK) != 0) {
        printf("FAIL: accidently deleted file %s\n", dont_touch);
        exit(1);
    }
    free(job_dir);
    free(task_dir);
    free(dont_touch);
    free(local_dirs);
}
void test_get_attempt_directory() {
  char *attempt_dir = get_attempt_work_directory("/tmp", "owen", "job_1",
						 "attempt_1");
  char *expected = "/tmp/taskTracker/owen/jobcache/job_1/attempt_1/work";
  if (strcmp(attempt_dir, expected) != 0) {
    printf("Fail get_attempt_work_directory got %s expected %s\n",
	   attempt_dir, expected);
  }
  free(attempt_dir);
}
void test_get_attempt_directory() {
  char *attempt_dir = get_attempt_work_directory("/tmp", "owen", "job_1",
						 "attempt_1");
  char *expected = "/tmp/usercache/owen/appcache/job_1/attempt_1";
  if (strcmp(attempt_dir, expected) != 0) {
    printf("Fail get_attempt_work_directory got %s expected %s\n",
	   attempt_dir, expected);
    exit(1);
  }
  free(attempt_dir);
}
Example #4
0
/**
 * Function to prepare the attempt directories for the task JVM.
 * It creates the task work and log directories.
 */
static int create_attempt_directories(const char* user, const char *job_id, 
					const char *task_id) {
  // create dirs as 0750
  const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
  if (job_id == NULL || task_id == NULL || user == NULL) {
    fprintf(LOGFILE, 
            "Either task_id is null or the user passed is null.\n");
    return -1;
  }
  int result = 0;

  char **local_dir = get_values(TT_SYS_DIR_KEY);

  if (local_dir == NULL) {
    fprintf(LOGFILE, "%s is not configured.\n", TT_SYS_DIR_KEY);
    return -1;
  }

  char **local_dir_ptr;
  for(local_dir_ptr = local_dir; *local_dir_ptr != NULL; ++local_dir_ptr) {
    char *task_dir = get_attempt_work_directory(*local_dir_ptr, user, job_id, 
                                                task_id);
    if (task_dir == NULL) {
      free_values(local_dir);
      return -1;
    }
    if (mkdirs(task_dir, perms) != 0) {
      // continue on to create other task directories
      free(task_dir);
    } else {
      free(task_dir);
    }
  }
  free_values(local_dir);

  // also make the directory for the task logs
  char *job_task_name = malloc(strlen(job_id) + strlen(task_id) + 2);
  if (job_task_name == NULL) {
    fprintf(LOGFILE, "Malloc of job task name failed\n");
    result = -1;
  } else {
    sprintf(job_task_name, "%s/%s", job_id, task_id);
    char *log_dir = get_job_log_directory(job_task_name);
    free(job_task_name);
    if (log_dir == NULL) {
      result = -1;
    } else if (mkdirs(log_dir, perms) != 0) {
      result = -1;
    }
    free(log_dir);
  }
  return result;
}
void test_run_task() {
  printf("\nTesting run task\n");
  if (seteuid(0) != 0) {
    printf("FAIL: seteuid to root failed - %s\n", strerror(errno));
    exit(1);
  }
  FILE* creds = fopen(TEST_ROOT "/creds.txt", "w");
  if (creds == NULL) {
    printf("FAIL: failed to create credentials file - %s\n", strerror(errno));
    exit(1);
  }
  if (fprintf(creds, "secret key\n") < 0) {
    printf("FAIL: fprintf failed - %s\n", strerror(errno));
    exit(1);
  }
  if (fclose(creds) != 0) {
    printf("FAIL: fclose failed - %s\n", strerror(errno));
    exit(1);
  }

  const char* script_name = TEST_ROOT "/task-script";
  FILE* script = fopen(script_name, "w");
  if (script == NULL) {
    printf("FAIL: failed to create script file - %s\n", strerror(errno));
    exit(1);
  }
  if (seteuid(user_detail->pw_uid) != 0) {
    printf("FAIL: failed to seteuid back to user - %s\n", strerror(errno));
    exit(1);
  }
  if (fprintf(script, "#!/bin/bash\n"
                     "touch foobar\n"
                     "exit 0") < 0) {
    printf("FAIL: fprintf failed - %s\n", strerror(errno));
    exit(1);
  }
  if (fclose(script) != 0) {
    printf("FAIL: fclose failed - %s\n", strerror(errno));
    exit(1);
  }
  fflush(stdout);
  fflush(stderr);
  char* task_dir = get_attempt_work_directory(TEST_ROOT "/local-1", 
					      username, "job_4", "task_1");
  pid_t child = fork();
  if (child == -1) {
    printf("FAIL: failed to fork process for init_job - %s\n", 
	   strerror(errno));
    exit(1);
  } else if (child == 0) {
    if (run_task_as_user(username, "job_4", "task_1", 
                         task_dir, script_name, TEST_ROOT "creds.txt") != 0) {
      printf("FAIL: failed in child\n");
      exit(42);
    }
    // should never return
    exit(1);
  }
  int status = 0;
  if (waitpid(child, &status, 0) <= 0) {
    printf("FAIL: failed waiting for process %d - %s\n", child, 
	   strerror(errno));
    exit(1);
  }
  if (access(TEST_ROOT "/logs/userlogs/job_4/task_1", R_OK) != 0) {
    printf("FAIL: failed to create task log directory\n");
    exit(1);
  }
  if (access(task_dir, R_OK) != 0) {
    printf("FAIL: failed to create task directory %s\n", task_dir);
    exit(1);
  }
  char buffer[100000];
  sprintf(buffer, "%s/foobar", task_dir);
  if (access(buffer, R_OK) != 0) {
    printf("FAIL: failed to create touch file %s\n", buffer);
    exit(1);
  }
  free(task_dir);
  task_dir = get_job_log_directory("logs", "job_4/task_1");
  if (access(task_dir, R_OK) != 0) {
    printf("FAIL: failed to create job log directory %s\n", task_dir);
    exit(1);
  }
  free(task_dir);
}
Example #6
0
/**
 * Function to prepare the attempt directories for the task JVM.
 * It creates the task work and log directories.
 */
int create_attempt_directories(const char* user,
    const char * good_local_dirs, const char *job_id, const char *task_id) {
  // create dirs as 0750
  const mode_t perms = S_IRWXU | S_IRGRP | S_IXGRP;
  if (job_id == NULL || task_id == NULL || user == NULL) {
    fprintf(LOGFILE, 
            "Either task_id is null or the user passed is null.\n");
    return -1;
  }
  int result = 0;

  char **local_dir = get_mapred_local_dirs(good_local_dirs);

  if (local_dir == NULL) {
    fprintf(LOGFILE, "Good mapred local directories could not be obtained.\n");
    return INVALID_TT_ROOT;
  }

  char **local_dir_ptr;
  for(local_dir_ptr = local_dir; *local_dir_ptr != NULL; ++local_dir_ptr) {
    char *task_dir = get_attempt_work_directory(*local_dir_ptr, user, job_id, 
                                                task_id);
    if (task_dir == NULL) {
      free_values(local_dir);
      return -1;
    }
    if (mkdirs(task_dir, perms) != 0) {
      // continue on to create other task directories
      free(task_dir);
    } else {
      free(task_dir);
    }
  }

  // also make the directory for the task logs
  char *job_task_name = malloc(strlen(job_id) + strlen(task_id) + 2);
  char *real_task_dir = NULL; // target of symlink
  char *real_job_dir = NULL;  // parent dir of target of symlink
  char *random_local_dir = NULL;
  char *link_task_log_dir = NULL; // symlink
  if (job_task_name == NULL) {
    fprintf(LOGFILE, "Malloc of job task name failed\n");
    result = -1;
  } else {
    sprintf(job_task_name, "%s/%s", job_id, task_id);
    link_task_log_dir = get_job_log_directory(job_task_name);
    random_local_dir = get_random_local_dir(local_dir);
    if(random_local_dir == NULL) {
      result = -1;
      goto cleanup;
    }
    real_job_dir = malloc(strlen(random_local_dir) + strlen(USERLOGS) + 
                          strlen(job_id) + 3);
    if (real_job_dir == NULL) {
      fprintf(LOGFILE, "Malloc of real job directory failed\n");
      result = -1;
      goto cleanup;
    } 
    real_task_dir = malloc(strlen(random_local_dir) + strlen(USERLOGS) + 
                           strlen(job_id) + strlen(task_id) + 4);
    if (real_task_dir == NULL) {
      fprintf(LOGFILE, "Malloc of real task directory failed\n");
      result = -1;
      goto cleanup;
    }
    sprintf(real_job_dir, "%s/userlogs/%s", random_local_dir, job_id);
    result = create_directory_for_user(real_job_dir);
    if( result != 0) {
      result = -1;
      goto cleanup;
    }
    sprintf(real_task_dir, "%s/userlogs/%s/%s",
            random_local_dir, job_id, task_id);
    result = mkdirs(real_task_dir, perms); 
    if( result != 0) {
      result = -1; 
      goto cleanup;
    }
    result = symlink(real_task_dir, link_task_log_dir);
    if( result != 0) {
      fprintf(LOGFILE, "Failed to create symlink %s to %s - %s\n",
              link_task_log_dir, real_task_dir, strerror(errno));
      result = -1;
    }
  }

 cleanup:
  free(random_local_dir);
  free(job_task_name);
  free(link_task_log_dir);
  free(real_job_dir);
  free(real_task_dir);
  free_values(local_dir);

  return result;
}