system.c 6.3 KB

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