system.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stddef.h>
  9. #include <signal.h>
  10. #include <unistd.h>
  11. #include <sys/wait.h>
  12. #include <stdlib.h>
  13. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  14. #include <sched.h>
  15. #include <errno.h>
  16. #include <bits/libc-lock.h>
  17. #include <sysdep-cancel.h>
  18. #endif
  19. extern __typeof(system) __libc_system;
  20. #if !defined __UCLIBC_HAS_THREADS_NATIVE__
  21. #include <sys/syscall.h>
  22. int __libc_system(const char *command)
  23. {
  24. int wait_val, pid;
  25. struct sigaction sa, save_quit, save_int;
  26. sigset_t save_mask;
  27. if (command == 0)
  28. return 1;
  29. memset(&sa, 0, sizeof(sa));
  30. sa.sa_handler = SIG_IGN;
  31. /* __sigemptyset(&sa.sa_mask); - done by memset() */
  32. /* sa.sa_flags = 0; - done by memset() */
  33. sigaction(SIGQUIT, &sa, &save_quit);
  34. sigaction(SIGINT, &sa, &save_int);
  35. __sigaddset(&sa.sa_mask, SIGCHLD);
  36. sigprocmask(SIG_BLOCK, &sa.sa_mask, &save_mask);
  37. if ((pid = vfork()) < 0) {
  38. wait_val = -1;
  39. goto out;
  40. }
  41. if (pid == 0) {
  42. sigaction(SIGQUIT, &save_quit, NULL);
  43. sigaction(SIGINT, &save_int, NULL);
  44. sigprocmask(SIG_SETMASK, &save_mask, NULL);
  45. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  46. _exit(127);
  47. }
  48. #if 0
  49. __printf("Waiting for child %d\n", pid);
  50. #endif
  51. if (__wait4_nocancel(pid, &wait_val, 0, 0) == -1)
  52. wait_val = -1;
  53. out:
  54. sigaction(SIGQUIT, &save_quit, NULL);
  55. sigaction(SIGINT, &save_int, NULL);
  56. sigprocmask(SIG_SETMASK, &save_mask, NULL);
  57. return wait_val;
  58. }
  59. #else
  60. /* We have to and actually can handle cancelable system(). The big
  61. problem: we have to kill the child process if necessary. To do
  62. this a cleanup handler has to be registered and is has to be able
  63. to find the PID of the child. The main problem is to reliable have
  64. the PID when needed. It is not necessary for the parent thread to
  65. return. It might still be in the kernel when the cancellation
  66. request comes. Therefore we have to use the clone() calls ability
  67. to have the kernel write the PID into the user-level variable. */
  68. libc_hidden_proto(sigaction)
  69. libc_hidden_proto(waitpid)
  70. #if defined __ia64__
  71. # define FORK() \
  72. INLINE_SYSCALL (clone2, 6, CLONE_PARENT_SETTID | SIGCHLD, NULL, 0, \
  73. &pid, NULL, NULL)
  74. #elif defined __sparc__
  75. # define FORK() \
  76. INLINE_CLONE_SYSCALL (CLONE_PARENT_SETTID | SIGCHLD, 0, &pid, NULL, NULL)
  77. #else
  78. # define FORK() \
  79. INLINE_SYSCALL (clone, 3, CLONE_PARENT_SETTID | SIGCHLD, 0, &pid)
  80. #endif
  81. static void cancel_handler (void *arg);
  82. # define CLEANUP_HANDLER \
  83. __libc_cleanup_region_start (1, cancel_handler, &pid)
  84. # define CLEANUP_RESET \
  85. __libc_cleanup_region_end (0)
  86. static struct sigaction intr, quit;
  87. static int sa_refcntr;
  88. __libc_lock_define_initialized (static, lock);
  89. # define DO_LOCK() __libc_lock_lock (lock)
  90. # define DO_UNLOCK() __libc_lock_unlock (lock)
  91. # define INIT_LOCK() ({ __libc_lock_init (lock); sa_refcntr = 0; })
  92. # define ADD_REF() sa_refcntr++
  93. # define SUB_REF() --sa_refcntr
  94. /* Execute LINE as a shell command, returning its status. */
  95. static int
  96. do_system (const char *line)
  97. {
  98. int status, save;
  99. pid_t pid;
  100. struct sigaction sa;
  101. sigset_t omask;
  102. memset(&sa, 0, sizeof(sa));
  103. sa.sa_handler = SIG_IGN;
  104. /*sa.sa_flags = 0; - done by memset */
  105. /*__sigemptyset (&sa.sa_mask); - done by memset */
  106. DO_LOCK ();
  107. if (ADD_REF () == 0)
  108. {
  109. if (sigaction (SIGINT, &sa, &intr) < 0)
  110. {
  111. SUB_REF ();
  112. goto out;
  113. }
  114. if (sigaction (SIGQUIT, &sa, &quit) < 0)
  115. {
  116. save = errno;
  117. SUB_REF ();
  118. goto out_restore_sigint;
  119. }
  120. }
  121. DO_UNLOCK ();
  122. /* We reuse the bitmap in the 'sa' structure. */
  123. __sigaddset (&sa.sa_mask, SIGCHLD);
  124. save = errno;
  125. if (sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0)
  126. {
  127. {
  128. DO_LOCK ();
  129. if (SUB_REF () == 0)
  130. {
  131. save = errno;
  132. (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
  133. out_restore_sigint:
  134. (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
  135. __set_errno (save);
  136. }
  137. out:
  138. DO_UNLOCK ();
  139. return -1;
  140. }
  141. }
  142. CLEANUP_HANDLER;
  143. pid = FORK ();
  144. if (pid == (pid_t) 0)
  145. {
  146. /* Child side. */
  147. const char *new_argv[4];
  148. new_argv[0] = "/bin/sh";
  149. new_argv[1] = "-c";
  150. new_argv[2] = line;
  151. new_argv[3] = NULL;
  152. /* Restore the signals. */
  153. (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
  154. (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
  155. (void) sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
  156. INIT_LOCK ();
  157. /* Exec the shell. */
  158. (void) execve ("/bin/sh", (char *const *) new_argv, __environ);
  159. _exit (127);
  160. }
  161. else if (pid < (pid_t) 0)
  162. /* The fork failed. */
  163. status = -1;
  164. else
  165. /* Parent side. */
  166. {
  167. /* Note the system() is a cancellation point. But since we call
  168. waitpid() which itself is a cancellation point we do not
  169. have to do anything here. */
  170. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  171. status = -1;
  172. }
  173. CLEANUP_RESET;
  174. save = errno;
  175. DO_LOCK ();
  176. if ((SUB_REF () == 0
  177. && (sigaction (SIGINT, &intr, (struct sigaction *) NULL)
  178. | sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)
  179. || sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0)
  180. {
  181. status = -1;
  182. }
  183. DO_UNLOCK ();
  184. return status;
  185. }
  186. int
  187. __libc_system (const char *line)
  188. {
  189. if (line == NULL)
  190. /* Check that we have a command processor available. It might
  191. not be available after a chroot(), for example. */
  192. return do_system ("exit 0") == 0;
  193. if (SINGLE_THREAD_P)
  194. return do_system (line);
  195. int oldtype = LIBC_CANCEL_ASYNC ();
  196. int result = do_system (line);
  197. LIBC_CANCEL_RESET (oldtype);
  198. return result;
  199. }
  200. /* The cancellation handler. */
  201. static void
  202. cancel_handler (void *arg)
  203. {
  204. pid_t child = *(pid_t *) arg;
  205. INTERNAL_SYSCALL_DECL (err);
  206. INTERNAL_SYSCALL (kill, err, 2, child, SIGKILL);
  207. TEMP_FAILURE_RETRY (waitpid (child, NULL, 0));
  208. DO_LOCK ();
  209. if (SUB_REF () == 0)
  210. {
  211. (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
  212. (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
  213. }
  214. DO_UNLOCK ();
  215. }
  216. #endif
  217. #ifdef IS_IN_libc
  218. weak_alias(__libc_system,system)
  219. #endif