task-controller.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include <pwd.h>
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. //command definitions
  22. enum command {
  23. INITIALIZE_JOB = 0,
  24. LAUNCH_TASK_JVM = 1,
  25. SIGNAL_TASK = 2,
  26. DELETE_AS_USER = 3,
  27. DELETE_LOG_AS_USER = 4,
  28. RUN_COMMAND_AS_USER = 5
  29. };
  30. enum errorcodes {
  31. INVALID_ARGUMENT_NUMBER = 1,
  32. INVALID_USER_NAME, //2
  33. INVALID_COMMAND_PROVIDED, //3
  34. SUPER_USER_NOT_ALLOWED_TO_RUN_TASKS, //4
  35. INVALID_TT_ROOT, //5
  36. SETUID_OPER_FAILED, //6
  37. UNABLE_TO_EXECUTE_TASK_SCRIPT, //7
  38. UNABLE_TO_KILL_TASK, //8
  39. INVALID_TASK_PID, //9
  40. ERROR_RESOLVING_FILE_PATH, //10
  41. RELATIVE_PATH_COMPONENTS_IN_FILE_PATH, //11
  42. UNABLE_TO_STAT_FILE, //12
  43. FILE_NOT_OWNED_BY_TASKTRACKER, //13
  44. PREPARE_ATTEMPT_DIRECTORIES_FAILED, //14
  45. INITIALIZE_JOB_FAILED, //15
  46. PREPARE_TASK_LOGS_FAILED, //16
  47. INVALID_TT_LOG_DIR, //17
  48. OUT_OF_MEMORY, //18
  49. INITIALIZE_DISTCACHEFILE_FAILED, //19
  50. INITIALIZE_USER_FAILED, //20
  51. UNABLE_TO_BUILD_PATH, //21
  52. INVALID_TASKCONTROLLER_PERMISSIONS, //22
  53. PREPARE_JOB_LOGS_FAILED, //23
  54. INVALID_CONFIG_FILE, // 24
  55. };
  56. #define TT_GROUP_KEY "mapreduce.tasktracker.group"
  57. extern struct passwd *user_detail;
  58. // the log file for error messages
  59. extern FILE *LOGFILE;
  60. // get the executable's filename
  61. char* get_executable();
  62. int check_taskcontroller_permissions(char *executable_file);
  63. /**
  64. * delete a given log directory as a user
  65. */
  66. int delete_log_directory(const char *log_dir);
  67. // initialize the job directory
  68. int initialize_job(const char *user, const char *jobid,
  69. const char *credentials,
  70. const char *job_xml, char* const* args);
  71. // run the task as the user
  72. int run_task_as_user(const char * user, const char *jobid, const char *taskid,
  73. const char *work_dir, const char *script_name);
  74. // send a signal as the user
  75. int signal_user_task(const char *user, int pid, int sig);
  76. // delete a directory (or file) recursively as the user.
  77. int delete_as_user(const char *user,
  78. const char *dir_to_be_deleted);
  79. // run a command as the user
  80. int run_command_as_user(const char *user,
  81. char* const* args);
  82. // set the task tracker's uid and gid
  83. void set_tasktracker_uid(uid_t user, gid_t group);
  84. /**
  85. * Is the user a real user account?
  86. * Checks:
  87. * 1. Not root
  88. * 2. UID is above the minimum configured.
  89. * 3. Not in banned user list
  90. * Returns NULL on failure
  91. */
  92. struct passwd* check_user(const char *user);
  93. // set the user
  94. int set_user(const char *user);
  95. // methods to get the directories
  96. char *get_user_directory(const char *tt_root, const char *user);
  97. char *get_job_directory(const char * tt_root, const char *user,
  98. const char *jobid);
  99. char *get_attempt_work_directory(const char *tt_root, const char *user,
  100. const char *job_dir, const char *attempt_id);
  101. char *get_task_launcher_file(const char* work_dir);
  102. /**
  103. * Get the job log directory.
  104. * Ensures that the result is a realpath and that it is underneath the
  105. * tt log root.
  106. */
  107. char* get_job_log_directory(const char* jobid);
  108. char *get_task_log_dir(const char *log_dir, const char *job_id,
  109. const char *attempt_id);
  110. /**
  111. * Ensure that the given path and all of the parent directories are created
  112. * with the desired permissions.
  113. */
  114. int mkdirs(const char* path, mode_t perm);
  115. /**
  116. * Function to initialize the user directories of a user.
  117. */
  118. int initialize_user(const char *user);
  119. /**
  120. * Create a top level directory for the user.
  121. * It assumes that the parent directory is *not* writable by the user.
  122. * It creates directories with 02700 permissions owned by the user
  123. * and with the group set to the task tracker group.
  124. * return non-0 on failure
  125. */
  126. int create_directory_for_user(const char* path);
  127. int change_user(uid_t user, gid_t group);