1
0

test-task-controller.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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 <fcntl.h>
  22. #include <unistd.h>
  23. #include <signal.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/stat.h>
  28. #include <sys/wait.h>
  29. #define TEST_ROOT "/tmp/test-task-controller"
  30. #define DONT_TOUCH_FILE "dont-touch-me"
  31. static char* username = NULL;
  32. /**
  33. * Run the command using the effective user id.
  34. * It can't use system, since bash seems to copy the real user id into the
  35. * effective id.
  36. */
  37. void run(const char *cmd) {
  38. fflush(stdout);
  39. fflush(stderr);
  40. pid_t child = fork();
  41. if (child == -1) {
  42. printf("FAIL: failed to fork - %s\n", strerror(errno));
  43. } else if (child == 0) {
  44. char *cmd_copy = strdup(cmd);
  45. char *ptr;
  46. int words = 1;
  47. for(ptr = strchr(cmd_copy, ' '); ptr; ptr = strchr(ptr+1, ' ')) {
  48. words += 1;
  49. }
  50. char **argv = malloc(sizeof(char *) * (words + 1));
  51. ptr = strtok(cmd_copy, " ");
  52. int i = 0;
  53. argv[i++] = ptr;
  54. while (ptr != NULL) {
  55. ptr = strtok(NULL, " ");
  56. argv[i++] = ptr;
  57. }
  58. if (execvp(argv[0], argv) != 0) {
  59. printf("FAIL: exec failed in child %s - %s\n", cmd, strerror(errno));
  60. exit(42);
  61. }
  62. } else {
  63. int status = 0;
  64. if (waitpid(child, &status, 0) <= 0) {
  65. printf("FAIL: failed waiting for child process %s pid %d - %s\n",
  66. cmd, child, strerror(errno));
  67. exit(1);
  68. }
  69. if (!WIFEXITED(status)) {
  70. printf("FAIL: process %s pid %d did not exit\n", cmd, child);
  71. exit(1);
  72. }
  73. if (WEXITSTATUS(status) != 0) {
  74. printf("FAIL: process %s pid %d exited with error status %d\n", cmd,
  75. child, WEXITSTATUS(status));
  76. exit(1);
  77. }
  78. }
  79. }
  80. int write_config_file(char *file_name) {
  81. FILE *file;
  82. file = fopen(file_name, "w");
  83. if (file == NULL) {
  84. printf("Failed to open %s.\n", file_name);
  85. return EXIT_FAILURE;
  86. }
  87. fprintf(file, "mapred.local.dir=" TEST_ROOT "/local-1");
  88. int i;
  89. for(i=2; i < 5; ++i) {
  90. fprintf(file, "," TEST_ROOT "/local-%d", i);
  91. }
  92. fprintf(file, "\n");
  93. fprintf(file, "hadoop.log.dir=" TEST_ROOT "/logs\n");
  94. fclose(file);
  95. return 0;
  96. }
  97. void create_tt_roots() {
  98. char** tt_roots = get_values("mapred.local.dir");
  99. char** tt_root;
  100. for(tt_root=tt_roots; *tt_root != NULL; ++tt_root) {
  101. if (mkdir(*tt_root, 0755) != 0) {
  102. printf("FAIL: Can't create directory %s - %s\n", *tt_root,
  103. strerror(errno));
  104. exit(1);
  105. }
  106. char buffer[100000];
  107. sprintf(buffer, "%s/taskTracker", *tt_root);
  108. if (mkdir(buffer, 0755) != 0) {
  109. printf("FAIL: Can't create directory %s - %s\n", buffer,
  110. strerror(errno));
  111. exit(1);
  112. }
  113. }
  114. free_values(tt_roots);
  115. }
  116. void test_get_user_directory() {
  117. char *user_dir = get_user_directory("/tmp", "user");
  118. char *expected = "/tmp/taskTracker/user";
  119. if (strcmp(user_dir, expected) != 0) {
  120. printf("test_get_user_directory expected %s got %s\n", user_dir, expected);
  121. exit(1);
  122. }
  123. free(user_dir);
  124. }
  125. void test_get_job_directory() {
  126. char *expected = "/tmp/taskTracker/user/jobcache/job_200906101234_0001";
  127. char *job_dir = (char *) get_job_directory("/tmp", "user",
  128. "job_200906101234_0001");
  129. if (strcmp(job_dir, expected) != 0) {
  130. exit(1);
  131. }
  132. free(job_dir);
  133. }
  134. void test_get_attempt_directory() {
  135. char *attempt_dir = get_attempt_work_directory("/tmp", "owen", "job_1",
  136. "attempt_1");
  137. char *expected = "/tmp/taskTracker/owen/jobcache/job_1/attempt_1/work";
  138. if (strcmp(attempt_dir, expected) != 0) {
  139. printf("Fail get_attempt_work_directory got %s expected %s\n",
  140. attempt_dir, expected);
  141. }
  142. free(attempt_dir);
  143. }
  144. void test_get_task_launcher_file() {
  145. char *expected_file = ("/tmp/taskTracker/user/jobcache/job_200906101234_0001"
  146. "/taskjvm.sh");
  147. char *job_dir = get_job_directory("/tmp", "user",
  148. "job_200906101234_0001");
  149. char *task_file = get_task_launcher_file(job_dir);
  150. if (strcmp(task_file, expected_file) != 0) {
  151. printf("failure to match expected task file %s vs %s\n", task_file,
  152. expected_file);
  153. exit(1);
  154. }
  155. free(job_dir);
  156. free(task_file);
  157. }
  158. void test_get_job_log_dir() {
  159. char *expected = TEST_ROOT "/logs/userlogs/job_200906101234_0001";
  160. char *logdir = get_job_log_directory("job_200906101234_0001");
  161. if (strcmp(logdir, expected) != 0) {
  162. printf("Fail get_job_log_dir got %s expected %s\n", logdir, expected);
  163. exit(1);
  164. }
  165. free(logdir);
  166. }
  167. void test_get_task_log_dir() {
  168. char *logdir = get_job_log_directory("job_5/task_4");
  169. char *expected = TEST_ROOT "/logs/userlogs/job_5/task_4";
  170. if (strcmp(logdir, expected) != 0) {
  171. printf("FAIL: get_task_log_dir expected %s got %s\n", logdir, expected);
  172. }
  173. free(logdir);
  174. }
  175. void test_check_user() {
  176. printf("\nTesting test_check_user\n");
  177. struct passwd *user = check_user(username);
  178. if (user == NULL) {
  179. printf("FAIL: failed check for user %s\n", username);
  180. exit(1);
  181. }
  182. free(user);
  183. if (check_user("lp") != NULL) {
  184. printf("FAIL: failed check for system user lp\n");
  185. exit(1);
  186. }
  187. if (check_user("root") != NULL) {
  188. printf("FAIL: failed check for system user root\n");
  189. exit(1);
  190. }
  191. if (check_user("mapred") != NULL) {
  192. printf("FAIL: failed check for hadoop user mapred\n");
  193. exit(1);
  194. }
  195. }
  196. void test_check_configuration_permissions() {
  197. printf("\nTesting check_configuration_permissions\n");
  198. if (check_configuration_permissions("/etc/passwd") != 0) {
  199. printf("FAIL: failed permission check on /etc/passwd\n");
  200. exit(1);
  201. }
  202. if (check_configuration_permissions(TEST_ROOT) == 0) {
  203. printf("FAIL: failed permission check on %s\n", TEST_ROOT);
  204. exit(1);
  205. }
  206. }
  207. void test_delete_task() {
  208. if (initialize_user(username)) {
  209. printf("FAIL: failed to initialized user %s\n", username);
  210. exit(1);
  211. }
  212. char* job_dir = get_job_directory(TEST_ROOT "/local-2", username, "job_1");
  213. char* dont_touch = get_job_directory(TEST_ROOT "/local-2", username,
  214. DONT_TOUCH_FILE);
  215. char* task_dir = get_attempt_work_directory(TEST_ROOT "/local-2",
  216. username, "job_1", "task_1");
  217. char buffer[100000];
  218. sprintf(buffer, "mkdir -p %s/who/let/the/dogs/out/who/who", task_dir);
  219. run(buffer);
  220. sprintf(buffer, "touch %s", dont_touch);
  221. run(buffer);
  222. // soft link to the canary file from the task directory
  223. sprintf(buffer, "ln -s %s %s/who/softlink", dont_touch, task_dir);
  224. run(buffer);
  225. // hard link to the canary file from the task directory
  226. sprintf(buffer, "ln %s %s/who/hardlink", dont_touch, task_dir);
  227. run(buffer);
  228. // create a dot file in the task directory
  229. sprintf(buffer, "touch %s/who/let/.dotfile", task_dir);
  230. run(buffer);
  231. // create a no permission file
  232. sprintf(buffer, "touch %s/who/let/protect", task_dir);
  233. run(buffer);
  234. sprintf(buffer, "chmod 000 %s/who/let/protect", task_dir);
  235. run(buffer);
  236. // create a no permission directory
  237. sprintf(buffer, "chmod 000 %s/who/let", task_dir);
  238. run(buffer);
  239. // delete task directory
  240. int ret = delete_as_user(username, "jobcache/job_1/task_1");
  241. if (ret != 0) {
  242. printf("FAIL: return code from delete_as_user is %d\n", ret);
  243. exit(1);
  244. }
  245. // check to make sure the task directory is gone
  246. if (access(task_dir, R_OK) == 0) {
  247. printf("FAIL: failed to delete the directory - %s\n", task_dir);
  248. exit(1);
  249. }
  250. // check to make sure the job directory is not gone
  251. if (access(job_dir, R_OK) != 0) {
  252. printf("FAIL: accidently deleted the directory - %s\n", job_dir);
  253. exit(1);
  254. }
  255. // but that the canary is not gone
  256. if (access(dont_touch, R_OK) != 0) {
  257. printf("FAIL: accidently deleted file %s\n", dont_touch);
  258. exit(1);
  259. }
  260. sprintf(buffer, "chmod -R 700 %s", job_dir);
  261. run(buffer);
  262. sprintf(buffer, "rm -fr %s", job_dir);
  263. run(buffer);
  264. free(job_dir);
  265. free(task_dir);
  266. free(dont_touch);
  267. }
  268. void test_delete_job() {
  269. char* job_dir = get_job_directory(TEST_ROOT "/local-2", username, "job_2");
  270. char* dont_touch = get_job_directory(TEST_ROOT "/local-2", username,
  271. DONT_TOUCH_FILE);
  272. char* task_dir = get_attempt_work_directory(TEST_ROOT "/local-2",
  273. username, "job_2", "task_1");
  274. char buffer[100000];
  275. sprintf(buffer, "mkdir -p %s/who/let/the/dogs/out/who/who", task_dir);
  276. run(buffer);
  277. sprintf(buffer, "touch %s", dont_touch);
  278. run(buffer);
  279. // soft link to the canary file from the task directory
  280. sprintf(buffer, "ln -s %s %s/who/softlink", dont_touch, task_dir);
  281. run(buffer);
  282. // hard link to the canary file from the task directory
  283. sprintf(buffer, "ln %s %s/who/hardlink", dont_touch, task_dir);
  284. run(buffer);
  285. // create a dot file in the task directory
  286. sprintf(buffer, "touch %s/who/let/.dotfile", task_dir);
  287. run(buffer);
  288. // create a no permission file
  289. sprintf(buffer, "touch %s/who/let/protect", task_dir);
  290. run(buffer);
  291. sprintf(buffer, "chmod 000 %s/who/let/protect", task_dir);
  292. run(buffer);
  293. // create a no permission directory
  294. sprintf(buffer, "chmod 000 %s/who/let", task_dir);
  295. run(buffer);
  296. // delete task directory
  297. int ret = delete_as_user(username, "jobcache/job_2");
  298. if (ret != 0) {
  299. printf("FAIL: return code from delete_as_user is %d\n", ret);
  300. exit(1);
  301. }
  302. // check to make sure the task directory is gone
  303. if (access(task_dir, R_OK) == 0) {
  304. printf("FAIL: failed to delete the directory - %s\n", task_dir);
  305. exit(1);
  306. }
  307. // check to make sure the job directory is gone
  308. if (access(job_dir, R_OK) == 0) {
  309. printf("FAIL: didn't delete the directory - %s\n", job_dir);
  310. exit(1);
  311. }
  312. // but that the canary is not gone
  313. if (access(dont_touch, R_OK) != 0) {
  314. printf("FAIL: accidently deleted file %s\n", dont_touch);
  315. exit(1);
  316. }
  317. free(job_dir);
  318. free(task_dir);
  319. free(dont_touch);
  320. }
  321. void test_delete_user() {
  322. printf("\nTesting delete_user\n");
  323. char* job_dir = get_job_directory(TEST_ROOT "/local-1", username, "job_3");
  324. if (mkdirs(job_dir, 0700) != 0) {
  325. exit(1);
  326. }
  327. char buffer[100000];
  328. sprintf(buffer, "%s/local-1/taskTracker/%s", TEST_ROOT, username);
  329. if (access(buffer, R_OK) != 0) {
  330. printf("FAIL: directory missing before test\n");
  331. exit(1);
  332. }
  333. if (delete_as_user(username, "") != 0) {
  334. exit(1);
  335. }
  336. if (access(buffer, R_OK) == 0) {
  337. printf("FAIL: directory not deleted\n");
  338. exit(1);
  339. }
  340. if (access(TEST_ROOT "/local-1", R_OK) != 0) {
  341. printf("FAIL: local-1 directory does not exist\n");
  342. exit(1);
  343. }
  344. free(job_dir);
  345. }
  346. void test_delete_log_directory() {
  347. printf("\nTesting delete_log_directory\n");
  348. char *job_log_dir = get_job_log_directory("job_1");
  349. if (job_log_dir == NULL) {
  350. exit(1);
  351. }
  352. if (create_directory_for_user(job_log_dir) != 0) {
  353. exit(1);
  354. }
  355. free(job_log_dir);
  356. char *task_log_dir = get_job_log_directory("job_1/task_2");
  357. if (task_log_dir == NULL) {
  358. exit(1);
  359. }
  360. if (mkdirs(task_log_dir, 0700) != 0) {
  361. exit(1);
  362. }
  363. if (access(TEST_ROOT "/logs/userlogs/job_1/task_2", R_OK) != 0) {
  364. printf("FAIL: can't access task directory - %s\n", strerror(errno));
  365. exit(1);
  366. }
  367. if (delete_log_directory("job_1/task_2") != 0) {
  368. printf("FAIL: can't delete task directory\n");
  369. exit(1);
  370. }
  371. if (access(TEST_ROOT "/logs/userlogs/job_1/task_2", R_OK) == 0) {
  372. printf("FAIL: task directory not deleted\n");
  373. exit(1);
  374. }
  375. if (access(TEST_ROOT "/logs/userlogs/job_1", R_OK) != 0) {
  376. printf("FAIL: job directory not deleted - %s\n", strerror(errno));
  377. exit(1);
  378. }
  379. if (delete_log_directory("job_1") != 0) {
  380. printf("FAIL: can't delete task directory\n");
  381. exit(1);
  382. }
  383. if (access(TEST_ROOT "/logs/userlogs/job_1", R_OK) == 0) {
  384. printf("FAIL: job directory not deleted\n");
  385. exit(1);
  386. }
  387. free(task_log_dir);
  388. }
  389. void run_test_in_child(const char* test_name, void (*func)()) {
  390. printf("\nRunning test %s in child process\n", test_name);
  391. fflush(stdout);
  392. fflush(stderr);
  393. pid_t child = fork();
  394. if (child == -1) {
  395. printf("FAIL: fork failed\n");
  396. exit(1);
  397. } else if (child == 0) {
  398. func();
  399. exit(0);
  400. } else {
  401. int status = 0;
  402. if (waitpid(child, &status, 0) == -1) {
  403. printf("FAIL: waitpid %d failed - %s\n", child, strerror(errno));
  404. exit(1);
  405. }
  406. if (!WIFEXITED(status)) {
  407. printf("FAIL: child %d didn't exit - %d\n", child, status);
  408. exit(1);
  409. }
  410. if (WEXITSTATUS(status) != 0) {
  411. printf("FAIL: child %d exited with bad status %d\n",
  412. child, WEXITSTATUS(status));
  413. exit(1);
  414. }
  415. }
  416. }
  417. void test_signal_task() {
  418. printf("\nTesting signal_task\n");
  419. fflush(stdout);
  420. fflush(stderr);
  421. pid_t child = fork();
  422. if (child == -1) {
  423. printf("FAIL: fork failed\n");
  424. exit(1);
  425. } else if (child == 0) {
  426. if (change_user(user_detail->pw_uid, user_detail->pw_gid) != 0) {
  427. exit(1);
  428. }
  429. sleep(3600);
  430. exit(0);
  431. } else {
  432. printf("Child task launched as %d\n", child);
  433. if (signal_user_task(username, child, SIGQUIT) != 0) {
  434. exit(1);
  435. }
  436. int status = 0;
  437. if (waitpid(child, &status, 0) == -1) {
  438. printf("FAIL: waitpid failed - %s\n", strerror(errno));
  439. exit(1);
  440. }
  441. if (!WIFSIGNALED(status)) {
  442. printf("FAIL: child wasn't signalled - %d\n", status);
  443. exit(1);
  444. }
  445. if (WTERMSIG(status) != SIGQUIT) {
  446. printf("FAIL: child was killed with %d instead of %d\n",
  447. WTERMSIG(status), SIGQUIT);
  448. exit(1);
  449. }
  450. }
  451. }
  452. void test_signal_task_group() {
  453. printf("\nTesting group signal_task\n");
  454. fflush(stdout);
  455. fflush(stderr);
  456. pid_t child = fork();
  457. if (child == -1) {
  458. printf("FAIL: fork failed\n");
  459. exit(1);
  460. } else if (child == 0) {
  461. setpgrp();
  462. if (change_user(user_detail->pw_uid, user_detail->pw_gid) != 0) {
  463. exit(1);
  464. }
  465. sleep(3600);
  466. exit(0);
  467. }
  468. printf("Child task launched as %d\n", child);
  469. if (signal_user_task(username, child, SIGKILL) != 0) {
  470. exit(1);
  471. }
  472. int status = 0;
  473. if (waitpid(child, &status, 0) == -1) {
  474. printf("FAIL: waitpid failed - %s\n", strerror(errno));
  475. exit(1);
  476. }
  477. if (!WIFSIGNALED(status)) {
  478. printf("FAIL: child wasn't signalled - %d\n", status);
  479. exit(1);
  480. }
  481. if (WTERMSIG(status) != SIGKILL) {
  482. printf("FAIL: child was killed with %d instead of %d\n",
  483. WTERMSIG(status), SIGKILL);
  484. exit(1);
  485. }
  486. }
  487. void test_init_job() {
  488. printf("\nTesting init job\n");
  489. if (seteuid(0) != 0) {
  490. printf("FAIL: seteuid to root failed - %s\n", strerror(errno));
  491. exit(1);
  492. }
  493. FILE* creds = fopen(TEST_ROOT "/creds.txt", "w");
  494. if (creds == NULL) {
  495. printf("FAIL: failed to create credentials file - %s\n", strerror(errno));
  496. exit(1);
  497. }
  498. if (fprintf(creds, "secret key\n") < 0) {
  499. printf("FAIL: fprintf failed - %s\n", strerror(errno));
  500. exit(1);
  501. }
  502. if (fclose(creds) != 0) {
  503. printf("FAIL: fclose failed - %s\n", strerror(errno));
  504. exit(1);
  505. }
  506. FILE* job_xml = fopen(TEST_ROOT "/job.xml", "w");
  507. if (job_xml == NULL) {
  508. printf("FAIL: failed to create job file - %s\n", strerror(errno));
  509. exit(1);
  510. }
  511. if (fprintf(job_xml, "<jobconf/>\n") < 0) {
  512. printf("FAIL: fprintf failed - %s\n", strerror(errno));
  513. exit(1);
  514. }
  515. if (fclose(job_xml) != 0) {
  516. printf("FAIL: fclose failed - %s\n", strerror(errno));
  517. exit(1);
  518. }
  519. if (seteuid(user_detail->pw_uid) != 0) {
  520. printf("FAIL: failed to seteuid back to user - %s\n", strerror(errno));
  521. exit(1);
  522. }
  523. fflush(stdout);
  524. fflush(stderr);
  525. pid_t child = fork();
  526. if (child == -1) {
  527. printf("FAIL: failed to fork process for init_job - %s\n",
  528. strerror(errno));
  529. exit(1);
  530. } else if (child == 0) {
  531. char *final_pgm[] = {"touch", "my-touch-file", 0};
  532. if (initialize_job(username, "job_4", TEST_ROOT "/creds.txt",
  533. TEST_ROOT "/job.xml", final_pgm) != 0) {
  534. printf("FAIL: failed in child\n");
  535. exit(42);
  536. }
  537. // should never return
  538. exit(1);
  539. }
  540. int status = 0;
  541. if (waitpid(child, &status, 0) <= 0) {
  542. printf("FAIL: failed waiting for process %d - %s\n", child,
  543. strerror(errno));
  544. exit(1);
  545. }
  546. if (access(TEST_ROOT "/logs/userlogs/job_4", R_OK) != 0) {
  547. printf("FAIL: failed to create job log directory\n");
  548. exit(1);
  549. }
  550. char* job_dir = get_job_directory(TEST_ROOT "/local-1", username, "job_4");
  551. if (access(job_dir, R_OK) != 0) {
  552. printf("FAIL: failed to create job directory %s\n", job_dir);
  553. exit(1);
  554. }
  555. char buffer[100000];
  556. sprintf(buffer, "%s/jobToken", job_dir);
  557. if (access(buffer, R_OK) != 0) {
  558. printf("FAIL: failed to create credentials %s\n", buffer);
  559. exit(1);
  560. }
  561. sprintf(buffer, "%s/my-touch-file", job_dir);
  562. if (access(buffer, R_OK) != 0) {
  563. printf("FAIL: failed to create touch file %s\n", buffer);
  564. exit(1);
  565. }
  566. free(job_dir);
  567. job_dir = get_job_log_directory("job_4");
  568. if (access(job_dir, R_OK) != 0) {
  569. printf("FAIL: failed to create job log directory %s\n", job_dir);
  570. exit(1);
  571. }
  572. free(job_dir);
  573. }
  574. void test_run_task() {
  575. printf("\nTesting run task\n");
  576. if (seteuid(0) != 0) {
  577. printf("FAIL: seteuid to root failed - %s\n", strerror(errno));
  578. exit(1);
  579. }
  580. const char* script_name = TEST_ROOT "/task-script";
  581. FILE* script = fopen(script_name, "w");
  582. if (script == NULL) {
  583. printf("FAIL: failed to create script file - %s\n", strerror(errno));
  584. exit(1);
  585. }
  586. if (seteuid(user_detail->pw_uid) != 0) {
  587. printf("FAIL: failed to seteuid back to user - %s\n", strerror(errno));
  588. exit(1);
  589. }
  590. if (fprintf(script, "#!/bin/bash\n"
  591. "touch foobar\n"
  592. "exit 0") < 0) {
  593. printf("FAIL: fprintf failed - %s\n", strerror(errno));
  594. exit(1);
  595. }
  596. if (fclose(script) != 0) {
  597. printf("FAIL: fclose failed - %s\n", strerror(errno));
  598. exit(1);
  599. }
  600. fflush(stdout);
  601. fflush(stderr);
  602. char* task_dir = get_attempt_work_directory(TEST_ROOT "/local-1",
  603. username, "job_4", "task_1");
  604. pid_t child = fork();
  605. if (child == -1) {
  606. printf("FAIL: failed to fork process for init_job - %s\n",
  607. strerror(errno));
  608. exit(1);
  609. } else if (child == 0) {
  610. if (run_task_as_user(username, "", "job_4", "task_1",
  611. task_dir, script_name) != 0) {
  612. printf("FAIL: failed in child\n");
  613. exit(42);
  614. }
  615. // should never return
  616. exit(1);
  617. }
  618. int status = 0;
  619. if (waitpid(child, &status, 0) <= 0) {
  620. printf("FAIL: failed waiting for process %d - %s\n", child,
  621. strerror(errno));
  622. exit(1);
  623. }
  624. if (access(TEST_ROOT "/logs/userlogs/job_4/task_1", R_OK) != 0) {
  625. printf("FAIL: failed to create task log directory\n");
  626. exit(1);
  627. }
  628. if (access(task_dir, R_OK) != 0) {
  629. printf("FAIL: failed to create task directory %s\n", task_dir);
  630. exit(1);
  631. }
  632. char buffer[100000];
  633. sprintf(buffer, "%s/foobar", task_dir);
  634. if (access(buffer, R_OK) != 0) {
  635. printf("FAIL: failed to create touch file %s\n", buffer);
  636. exit(1);
  637. }
  638. free(task_dir);
  639. task_dir = get_job_log_directory("job_4/task_1");
  640. if (access(task_dir, R_OK) != 0) {
  641. printf("FAIL: failed to create job log directory %s\n", task_dir);
  642. exit(1);
  643. }
  644. free(task_dir);
  645. }
  646. int main(int argc, char **argv) {
  647. LOGFILE = stdout;
  648. int my_username = 0;
  649. // clean up any junk from previous run
  650. system("chmod -R u=rwx " TEST_ROOT "; rm -fr " TEST_ROOT);
  651. if (mkdirs(TEST_ROOT "/logs/userlogs", 0755) != 0) {
  652. exit(1);
  653. }
  654. if (write_config_file(TEST_ROOT "/test.cfg") != 0) {
  655. exit(1);
  656. }
  657. read_config(TEST_ROOT "/test.cfg");
  658. create_tt_roots();
  659. if (getuid() == 0 && argc == 2) {
  660. username = argv[1];
  661. } else {
  662. username = strdup(getpwuid(getuid())->pw_name);
  663. my_username = 1;
  664. }
  665. set_tasktracker_uid(geteuid(), getegid());
  666. if (set_user(username)) {
  667. exit(1);
  668. }
  669. printf("\nStarting tests\n");
  670. printf("\nTesting get_user_directory()\n");
  671. test_get_user_directory();
  672. printf("\nTesting get_job_directory()\n");
  673. test_get_job_directory();
  674. printf("\nTesting get_attempt_directory()\n");
  675. test_get_attempt_directory();
  676. printf("\nTesting get_task_launcher_file()\n");
  677. test_get_task_launcher_file();
  678. printf("\nTesting get_job_log_dir()\n");
  679. test_get_job_log_dir();
  680. test_check_configuration_permissions();
  681. printf("\nTesting get_task_log_dir()\n");
  682. test_get_task_log_dir();
  683. printf("\nTesting delete_task()\n");
  684. test_delete_task();
  685. printf("\nTesting delete_job()\n");
  686. test_delete_job();
  687. test_delete_user();
  688. test_check_user();
  689. test_delete_log_directory();
  690. // the tests that change user need to be run in a subshell, so that
  691. // when they change user they don't give up our privs
  692. run_test_in_child("test_signal_task", test_signal_task);
  693. run_test_in_child("test_signal_task_group", test_signal_task_group);
  694. // init job and run task can't be run if you aren't testing as root
  695. if (getuid() == 0) {
  696. // these tests do internal forks so that the change_owner and execs
  697. // don't mess up our process.
  698. test_init_job();
  699. test_run_task();
  700. }
  701. seteuid(0);
  702. run("rm -fr " TEST_ROOT);
  703. printf("\nFinished tests\n");
  704. if (my_username) {
  705. free(username);
  706. }
  707. free_configurations();
  708. return 0;
  709. }