system.c 6.2 KB

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