loader.c 2.3 KB

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