main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "task-controller.h"
  19. int main(int argc, char **argv) {
  20. int command;
  21. int next_option = 0;
  22. const char * job_id = NULL;
  23. const char * task_id = NULL;
  24. const char * tt_root = NULL;
  25. int exit_code = 0;
  26. const char * task_pid = NULL;
  27. const char* const short_options = "l:";
  28. const struct option long_options[] = { { "log", 1, NULL, 'l' }, { NULL, 0,
  29. NULL, 0 } };
  30. const char* log_file = NULL;
  31. //Minimum number of arguments required to run the task-controller
  32. //command-name user command tt-root
  33. if (argc < 3) {
  34. display_usage(stderr);
  35. return INVALID_ARGUMENT_NUMBER;
  36. }
  37. #ifndef HADOOP_CONF_DIR
  38. hadoop_conf_dir = (char *) malloc (sizeof(char) *
  39. (strlen(argv[0]) - strlen(EXEC_PATTERN)) + 1);
  40. strncpy(hadoop_conf_dir,argv[0],(strlen(argv[0]) - strlen(EXEC_PATTERN)));
  41. hadoop_conf_dir[(strlen(argv[0]) - strlen(EXEC_PATTERN))] = '\0';
  42. #endif
  43. do {
  44. next_option = getopt_long(argc, argv, short_options, long_options, NULL);
  45. switch (next_option) {
  46. case 'l':
  47. log_file = optarg;
  48. default:
  49. break;
  50. }
  51. } while (next_option != -1);
  52. if (log_file == NULL) {
  53. LOGFILE = stderr;
  54. } else {
  55. LOGFILE = fopen(log_file, "a");
  56. if (LOGFILE == NULL) {
  57. fprintf(stderr, "Unable to open LOGFILE : %s \n", log_file);
  58. LOGFILE = stderr;
  59. }
  60. if (LOGFILE != stderr) {
  61. if (chmod(log_file, S_IREAD | S_IEXEC | S_IWRITE | S_IROTH | S_IWOTH
  62. | S_IRGRP | S_IWGRP) < 0) {
  63. fprintf(stderr, "Unable to change permission of the log file %s \n",
  64. log_file);
  65. fprintf(stderr, "changing log file to stderr");
  66. LOGFILE = stderr;
  67. }
  68. }
  69. }
  70. //checks done for user name
  71. //checks done if the user is root or not.
  72. if (argv[optind] == NULL) {
  73. fprintf(LOGFILE, "Invalid user name \n");
  74. return INVALID_USER_NAME;
  75. }
  76. if (get_user_details(argv[optind]) != 0) {
  77. return INVALID_USER_NAME;
  78. }
  79. //implicit conversion to int instead of __gid_t and __uid_t
  80. if (user_detail->pw_gid == 0 || user_detail->pw_uid == 0) {
  81. fprintf(LOGFILE, "Cannot run tasks as super user\n");
  82. return SUPER_USER_NOT_ALLOWED_TO_RUN_TASKS;
  83. }
  84. optind = optind + 1;
  85. command = atoi(argv[optind++]);
  86. #ifdef DEBUG
  87. fprintf(LOGFILE, "main : command provided %d\n",command);
  88. fprintf(LOGFILE, "main : user is %s\n", user_detail->pw_name);
  89. #endif
  90. switch (command) {
  91. case LAUNCH_TASK_JVM:
  92. tt_root = argv[optind++];
  93. job_id = argv[optind++];
  94. task_id = argv[optind++];
  95. exit_code
  96. = run_task_as_user(user_detail->pw_name, job_id, task_id, tt_root);
  97. break;
  98. case TERMINATE_TASK_JVM:
  99. task_pid = argv[optind++];
  100. exit_code = kill_user_task(user_detail->pw_name, task_pid, SIGTERM);
  101. break;
  102. case KILL_TASK_JVM:
  103. task_pid = argv[optind++];
  104. exit_code = kill_user_task(user_detail->pw_name, task_pid, SIGKILL);
  105. break;
  106. default:
  107. exit_code = INVALID_COMMAND_PROVIDED;
  108. }
  109. fflush(LOGFILE);
  110. fclose(LOGFILE);
  111. return exit_code;
  112. }