sigexec.c 2.2 KB

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