loader.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * PM2: Parallel Multithreaded Machine
  3. * Copyright (C) 2001 the PM2 team (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. */
  15. #include <sys/wait.h>
  16. #include <sys/resource.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <signal.h>
  21. #include <string.h>
  22. #define DEFAULT_TIMEOUT 600
  23. #define AUTOTEST_SKIPPED_TEST 77
  24. static pid_t child_pid = 0;
  25. static void test_cleaner(int sig)
  26. {
  27. pid_t child_gid;
  28. // send signal to all loader family members
  29. child_gid = getpgid(child_pid);
  30. kill(-child_gid, SIGKILL);
  31. exit(EXIT_FAILURE);
  32. }
  33. int main(int argc, char *argv[])
  34. {
  35. int timeout;
  36. int child_exit_status;
  37. char *test_name;
  38. int status;
  39. struct sigaction sa;
  40. timeout = 0;
  41. test_name = argv[1];
  42. if (!test_name)
  43. {
  44. fprintf(stderr, "Error. Need name of program to start\n");
  45. exit(-1);
  46. }
  47. /* get user-defined iter_max value */
  48. if (getenv("STARPU_TIMEOUT_ENV"))
  49. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  50. if (timeout <= 0)
  51. timeout = DEFAULT_TIMEOUT;
  52. /* set SIGALARM handler */
  53. sa.sa_flags = 0;
  54. sigemptyset(&sa.sa_mask);
  55. sa.sa_handler = test_cleaner;
  56. if (-1 == sigaction(SIGALRM, &sa, NULL))
  57. perror("sigaction");
  58. child_pid = fork();
  59. if (child_pid == 0)
  60. {
  61. // get a new pgid
  62. if (setpgid(0, 0) == -1)
  63. {
  64. perror("setpgid");
  65. exit(EXIT_FAILURE);
  66. }
  67. execl(test_name, test_name, NULL);
  68. exit(EXIT_FAILURE);
  69. }
  70. if (child_pid == -1)
  71. exit(EXIT_FAILURE);
  72. alarm(timeout);
  73. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  74. {
  75. if (WIFEXITED(child_exit_status))
  76. {
  77. status = WEXITSTATUS(child_exit_status);
  78. if (status == EXIT_SUCCESS)
  79. {
  80. alarm(0);
  81. }
  82. else
  83. {
  84. if (status != AUTOTEST_SKIPPED_TEST)
  85. fprintf(stdout, "Exited with return code %d\n", status);
  86. return status;
  87. }
  88. }
  89. else
  90. {
  91. return EXIT_FAILURE;
  92. }
  93. }
  94. return EXIT_SUCCESS;
  95. }