system.c 6.4 KB

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