main.c 6.0 KB

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