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 <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. extern __typeof(system) __libc_system;
  19. /* TODO: the cancellable version breaks on sparc currently,
  20. * need to figure out why still
  21. */
  22. #if !defined __UCLIBC_HAS_THREADS_NATIVE__ || defined __sparc__
  23. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  24. #include <sys/syscall.h>
  25. #ifndef __NR_vfork
  26. # define vfork fork
  27. #endif
  28. int __libc_system(const char *command)
  29. {
  30. int wait_val, pid;
  31. __sighandler_t save_quit, save_int, save_chld;
  32. if (command == 0)
  33. return 1;
  34. save_quit = signal(SIGQUIT, SIG_IGN);
  35. save_int = signal(SIGINT, SIG_IGN);
  36. save_chld = signal(SIGCHLD, SIG_DFL);
  37. if ((pid = vfork()) < 0) {
  38. signal(SIGQUIT, save_quit);
  39. signal(SIGINT, save_int);
  40. signal(SIGCHLD, save_chld);
  41. return -1;
  42. }
  43. if (pid == 0) {
  44. signal(SIGQUIT, SIG_DFL);
  45. signal(SIGINT, SIG_DFL);
  46. signal(SIGCHLD, SIG_DFL);
  47. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  48. _exit(127);
  49. }
  50. /* Signals are not absolutly guarenteed with vfork */
  51. signal(SIGQUIT, SIG_IGN);
  52. signal(SIGINT, SIG_IGN);
  53. #if 0
  54. __printf("Waiting for child %d\n", pid);
  55. #endif
  56. if (wait4(pid, &wait_val, 0, 0) == -1)
  57. wait_val = -1;
  58. signal(SIGQUIT, save_quit);
  59. signal(SIGINT, save_int);
  60. signal(SIGCHLD, save_chld);
  61. return wait_val;
  62. }
  63. #else
  64. /* We have to and actually can handle cancelable system(). The big
  65. problem: we have to kill the child process if necessary. To do
  66. this a cleanup handler has to be registered and is has to be able
  67. to find the PID of the child. The main problem is to reliable have
  68. the PID when needed. It is not necessary for the parent thread to
  69. return. It might still be in the kernel when the cancellation
  70. request comes. Therefore we have to use the clone() calls ability
  71. to have the kernel write the PID into the user-level variable. */
  72. libc_hidden_proto(sigaction)
  73. libc_hidden_proto(waitpid)
  74. #if defined __ia64__
  75. # define FORK() \
  76. INLINE_SYSCALL (clone2, 6, CLONE_PARENT_SETTID | SIGCHLD, NULL, 0, \
  77. &pid, NULL, NULL)
  78. #elif defined __sparc__
  79. # define FORK() \
  80. INLINE_CLONE_SYSCALL (CLONE_PARENT_SETTID | SIGCHLD, 0, &pid, NULL, NULL)
  81. #elif defined __s390__
  82. # define FORK() \
  83. INLINE_SYSCALL (clone, 3, 0, CLONE_PARENT_SETTID | SIGCHLD, &pid)
  84. #else
  85. # define FORK() \
  86. INLINE_SYSCALL (clone, 3, CLONE_PARENT_SETTID | SIGCHLD, 0, &pid)
  87. #endif
  88. static void cancel_handler (void *arg);
  89. # define CLEANUP_HANDLER \
  90. __libc_cleanup_region_start (1, cancel_handler, &pid)
  91. # define CLEANUP_RESET \
  92. __libc_cleanup_region_end (0)
  93. static struct sigaction intr, quit;
  94. static int sa_refcntr;
  95. __libc_lock_define_initialized (static, lock);
  96. # define DO_LOCK() __libc_lock_lock (lock)
  97. # define DO_UNLOCK() __libc_lock_unlock (lock)
  98. # define INIT_LOCK() ({ __libc_lock_init (lock); sa_refcntr = 0; })
  99. # define ADD_REF() sa_refcntr++
  100. # define SUB_REF() --sa_refcntr
  101. /* Execute LINE as a shell command, returning its status. */
  102. static int
  103. do_system (const char *line)
  104. {
  105. int status, save;
  106. pid_t pid;
  107. struct sigaction sa;
  108. sigset_t omask;
  109. sa.sa_handler = SIG_IGN;
  110. sa.sa_flags = 0;
  111. __sigemptyset (&sa.sa_mask);
  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] = "/bin/sh";
  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 ("/bin/sh", (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