loader.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 int timeout;
  27. static void test_cleaner(int sig)
  28. {
  29. pid_t child_gid;
  30. // send signal to all loader family members
  31. fprintf(stderr, "Test is blocked since %d seconds ... mark it as failed\n", timeout);
  32. child_gid = getpgid(child_pid);
  33. kill(-child_gid, SIGKILL);
  34. exit(EXIT_FAILURE);
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. int child_exit_status;
  39. char *test_name;
  40. int status;
  41. struct sigaction sa;
  42. timeout = 0;
  43. test_name = argv[1];
  44. if (!test_name)
  45. {
  46. fprintf(stderr, "Error. Need name of program to start\n");
  47. exit(-1);
  48. }
  49. /* get user-defined iter_max value */
  50. if (getenv("STARPU_TIMEOUT_ENV"))
  51. timeout = strtol(getenv("STARPU_TIMEOUT_ENV"), NULL, 10);
  52. if (timeout <= 0)
  53. timeout = DEFAULT_TIMEOUT;
  54. /* set SIGALARM handler */
  55. sa.sa_flags = 0;
  56. sigemptyset(&sa.sa_mask);
  57. sa.sa_handler = test_cleaner;
  58. if (-1 == sigaction(SIGALRM, &sa, NULL))
  59. perror("sigaction");
  60. child_pid = fork();
  61. if (child_pid == 0)
  62. {
  63. // get a new pgid
  64. if (setpgid(0, 0) == -1)
  65. {
  66. perror("setpgid");
  67. exit(EXIT_FAILURE);
  68. }
  69. execl(test_name, test_name, NULL);
  70. exit(EXIT_FAILURE);
  71. }
  72. if (child_pid == -1)
  73. exit(EXIT_FAILURE);
  74. alarm(timeout);
  75. if (child_pid == waitpid(child_pid, &child_exit_status, 0))
  76. {
  77. if (WIFEXITED(child_exit_status))
  78. {
  79. status = WEXITSTATUS(child_exit_status);
  80. if (status == EXIT_SUCCESS)
  81. {
  82. alarm(0);
  83. }
  84. else
  85. {
  86. if (status != AUTOTEST_SKIPPED_TEST)
  87. fprintf(stdout, "Exited with return code %d\n", status);
  88. return status;
  89. }
  90. }
  91. else
  92. {
  93. return EXIT_FAILURE;
  94. }
  95. }
  96. return EXIT_SUCCESS;
  97. }