sigexec.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * sigexec.c
  3. *
  4. * Programm to execute processes with unblocked signals.
  5. *
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif /* HAVE_CONFIG_H */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #if WITH_INTERNAL_GETOPT
  17. #include "libc/getopt.h"
  18. #else
  19. #ifdef HAVE_GETOPT_H
  20. #include <getopt.h>
  21. #endif
  22. #endif
  23. #ifndef VERSION
  24. #define VERSION 1.5.6
  25. #endif
  26. #define QUOTE(t) #t
  27. #define version(v) printf("%s\n", v)
  28. static void help(int status, const char *argv0);
  29. int main(int argc, char *argv[])
  30. {
  31. char **child_argv;
  32. sigset_t sigs;
  33. static const char *shortopts = "+hv";
  34. #ifdef HAVE_GETOPT_LONG
  35. static const struct option longopts[] = {
  36. {"help", 0, NULL, 'h'},
  37. {"version", 0, NULL, 'v'},
  38. {NULL, 0, NULL, 0}
  39. };
  40. #endif
  41. while(1) {
  42. int opt;
  43. #ifdef HAVE_GETOPT_LONG
  44. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  45. #else
  46. opt = getopt(argc, argv, shortopts);
  47. #endif
  48. if (-1 == opt)
  49. break;
  50. switch (opt) {
  51. case 0:
  52. break;
  53. case 'h':
  54. help(0, argv[0]);
  55. exit(0);
  56. break;
  57. case 'v':
  58. version(VERSION);
  59. exit(0);
  60. break;
  61. default:
  62. help(1, argv[0]);
  63. break;
  64. }
  65. }
  66. child_argv = &argv[optind];
  67. /* Check user command */
  68. if (! child_argv[0]) {
  69. fprintf(stderr, "Error: Nothing to execute.\n");
  70. return 1;
  71. }
  72. /* Unblock signals */
  73. sigemptyset(&sigs);
  74. sigprocmask(SIG_SETMASK, &sigs, NULL);
  75. /* Execute user command */
  76. /* fprintf(stderr, "%s %s %s\n", child_argv[0], child_argv[1], child_argv[2]); */
  77. if (execvp(child_argv[0], child_argv) < 0) {
  78. fprintf(stderr, "Error: Cannot execute %s: %s\n",
  79. child_argv[0], strerror(errno));
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. /*--------------------------------------------------------- */
  85. /* Print help message */
  86. static void help(int status, const char *argv0)
  87. {
  88. const char *name = NULL;
  89. if (!argv0)
  90. return;
  91. /* Find the basename */
  92. name = strrchr(argv0, '/');
  93. if (name)
  94. name++;
  95. else
  96. name = argv0;
  97. if (status != 0) {
  98. fprintf(stderr, "Try `%s -h' for more information.\n",
  99. name);
  100. } else {
  101. printf("Usage: %s [options] -- <command to execute>\n", name);
  102. printf("Utility to execute process with unblocked signals.\n");
  103. printf("Options:\n");
  104. printf("\t-v, --version\tPrint utility version.\n");
  105. printf("\t-h, --help\tPrint this help.\n");
  106. }
  107. }