main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "configuration.h"
  19. #include "task-controller.h"
  20. #include <errno.h>
  21. #include <grp.h>
  22. #include <limits.h>
  23. #include <unistd.h>
  24. #include <signal.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #define _STRINGIFY(X) #X
  30. #define STRINGIFY(X) _STRINGIFY(X)
  31. #define CONF_FILENAME "taskcontroller.cfg"
  32. void display_usage(FILE *stream) {
  33. fprintf(stream,
  34. "Usage: task-controller user good-local-dirs command command-args\n");
  35. fprintf(stream, " where good-local-dirs is a comma separated list of " \
  36. "good mapred local directories.\n");
  37. fprintf(stream, "Commands:\n");
  38. fprintf(stream, " initialize job: %2d jobid credentials cmd args\n",
  39. INITIALIZE_JOB);
  40. fprintf(stream, " launch task: %2d jobid taskid task-script\n",
  41. LAUNCH_TASK_JVM);
  42. fprintf(stream, " signal task: %2d task-pid signal\n",
  43. SIGNAL_TASK);
  44. fprintf(stream, " delete as user: %2d relative-path\n",
  45. DELETE_AS_USER);
  46. fprintf(stream, " delete log: %2d relative-path\n",
  47. DELETE_LOG_AS_USER);
  48. fprintf(stream, " run command as user: %2d cmd args\n",
  49. RUN_COMMAND_AS_USER);
  50. fprintf(stream, " initialize task: %2d user relative-path jobid taskid\n",
  51. INITIALIZE_TASK);
  52. }
  53. int main(int argc, char **argv) {
  54. //Minimum number of arguments required to run the task-controller
  55. if (argc < 5) {
  56. display_usage(stdout);
  57. return INVALID_ARGUMENT_NUMBER;
  58. }
  59. LOGFILE = stdout;
  60. int command;
  61. const char * good_local_dirs = NULL;
  62. const char * job_id = NULL;
  63. const char * task_id = NULL;
  64. const char * cred_file = NULL;
  65. const char * script_file = NULL;
  66. const char * current_dir = NULL;
  67. const char * job_xml = NULL;
  68. int exit_code = 0;
  69. char * dir_to_be_deleted = NULL;
  70. char *executable_file = get_executable();
  71. #ifndef HADOOP_CONF_DIR
  72. #error HADOOP_CONF_DIR must be defined
  73. #endif
  74. char *orig_conf_file = STRINGIFY(HADOOP_CONF_DIR) "/" CONF_FILENAME;
  75. char *conf_file = realpath(orig_conf_file, NULL);
  76. if (conf_file == NULL) {
  77. fprintf(LOGFILE, "Configuration file %s not found.\n", orig_conf_file);
  78. return INVALID_CONFIG_FILE;
  79. }
  80. if (check_configuration_permissions(conf_file) != 0) {
  81. return INVALID_CONFIG_FILE;
  82. }
  83. read_config(conf_file);
  84. free(conf_file);
  85. // look up the task tracker group in the config file
  86. char *tt_group = get_value(TT_GROUP_KEY);
  87. if (tt_group == NULL) {
  88. fprintf(LOGFILE, "Can't get configured value for %s.\n", TT_GROUP_KEY);
  89. exit(INVALID_CONFIG_FILE);
  90. }
  91. struct group *group_info = getgrnam(tt_group);
  92. if (group_info == NULL) {
  93. fprintf(LOGFILE, "Can't get group information for %s - %s.\n", tt_group,
  94. strerror(errno));
  95. exit(INVALID_CONFIG_FILE);
  96. }
  97. set_tasktracker_uid(getuid(), group_info->gr_gid);
  98. // if we are running from a setuid executable, make the real uid root
  99. setuid(0);
  100. // set the real and effective group id to the task tracker group
  101. setgid(group_info->gr_gid);
  102. if (check_taskcontroller_permissions(executable_file) != 0) {
  103. fprintf(LOGFILE, "Invalid permissions on task-controller binary.\n");
  104. return INVALID_TASKCONTROLLER_PERMISSIONS;
  105. }
  106. //checks done for user name
  107. if (argv[optind] == NULL) {
  108. fprintf(LOGFILE, "Invalid user name \n");
  109. return INVALID_USER_NAME;
  110. }
  111. int ret = set_user(argv[optind]);
  112. if (ret != 0) {
  113. return ret;
  114. }
  115. optind = optind + 1;
  116. good_local_dirs = argv[optind];
  117. if (good_local_dirs == NULL) {
  118. return INVALID_TT_ROOT;
  119. }
  120. optind = optind + 1;
  121. command = atoi(argv[optind++]);
  122. fprintf(LOGFILE, "main : command provided %d\n",command);
  123. fprintf(LOGFILE, "main : user is %s\n", user_detail->pw_name);
  124. fprintf(LOGFILE, "Good mapred-local-dirs are %s\n", good_local_dirs);
  125. switch (command) {
  126. case INITIALIZE_JOB:
  127. if (argc < 8) {
  128. fprintf(LOGFILE, "Too few arguments (%d vs 8) for initialize job\n",
  129. argc);
  130. return INVALID_ARGUMENT_NUMBER;
  131. }
  132. job_id = argv[optind++];
  133. cred_file = argv[optind++];
  134. job_xml = argv[optind++];
  135. exit_code = initialize_job(user_detail->pw_name, good_local_dirs, job_id,
  136. cred_file, job_xml, argv + optind);
  137. break;
  138. case LAUNCH_TASK_JVM:
  139. if (argc < 8) {
  140. fprintf(LOGFILE, "Too few arguments (%d vs 8) for launch task\n", argc);
  141. return INVALID_ARGUMENT_NUMBER;
  142. }
  143. job_id = argv[optind++];
  144. task_id = argv[optind++];
  145. current_dir = argv[optind++];
  146. script_file = argv[optind++];
  147. exit_code = run_task_as_user(user_detail->pw_name, good_local_dirs, job_id,
  148. task_id, current_dir, script_file);
  149. break;
  150. case SIGNAL_TASK:
  151. if (argc < 6) {
  152. fprintf(LOGFILE, "Too few arguments (%d vs 6) for signal task\n", argc);
  153. return INVALID_ARGUMENT_NUMBER;
  154. } else {
  155. char* end_ptr = NULL;
  156. char* option = argv[optind++];
  157. int task_pid = strtol(option, &end_ptr, 10);
  158. if (option == end_ptr || *end_ptr != '\0') {
  159. fprintf(LOGFILE, "Illegal argument for task pid %s\n", option);
  160. return INVALID_ARGUMENT_NUMBER;
  161. }
  162. option = argv[optind++];
  163. int signal = strtol(option, &end_ptr, 10);
  164. if (option == end_ptr || *end_ptr != '\0') {
  165. fprintf(LOGFILE, "Illegal argument for signal %s\n", option);
  166. return INVALID_ARGUMENT_NUMBER;
  167. }
  168. exit_code = signal_user_task(user_detail->pw_name, task_pid, signal);
  169. }
  170. break;
  171. case DELETE_AS_USER:
  172. dir_to_be_deleted = argv[optind++];
  173. exit_code= delete_as_user(user_detail->pw_name, good_local_dirs,
  174. dir_to_be_deleted);
  175. break;
  176. case DELETE_LOG_AS_USER:
  177. dir_to_be_deleted = argv[optind++];
  178. exit_code= delete_log_directory(dir_to_be_deleted, good_local_dirs);
  179. break;
  180. case RUN_COMMAND_AS_USER:
  181. exit_code = run_command_as_user(user_detail->pw_name, argv + optind);
  182. break;
  183. case INITIALIZE_TASK:
  184. if (argc < 6) {
  185. fprintf(LOGFILE, "Too few arguments (%d vs 6) for initializing task\n", argc);
  186. return INVALID_ARGUMENT_NUMBER;
  187. }
  188. job_id = argv[optind++];
  189. task_id = argv[optind++];
  190. exit_code = initialize_task(user_detail->pw_name, good_local_dirs, job_id, task_id);
  191. break;
  192. default:
  193. exit_code = INVALID_COMMAND_PROVIDED;
  194. }
  195. fclose(LOGFILE);
  196. return exit_code;
  197. }