system.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
  22. #include <sys/syscall.h>
  23. #ifndef __NR_vfork
  24. # define vfork fork
  25. #endif
  26. int __libc_system(const char *command)
  27. {
  28. int wait_val, pid;
  29. struct sigaction sa, save_quit, save_int;
  30. sigset_t save_mask;
  31. if (command == 0)
  32. return 1;
  33. memset(&sa, 0, sizeof(sa));
  34. sa.sa_handler = SIG_IGN;
  35. /* __sigemptyset(&sa.sa_mask); - done by memset() */
  36. /* sa.sa_flags = 0; - done by memset() */
  37. sigaction(SIGQUIT, &sa, &save_quit);
  38. sigaction(SIGINT, &sa, &save_int);
  39. __sigaddset(&sa.sa_mask, SIGCHLD);
  40. sigprocmask(SIG_BLOCK, &sa.sa_mask, &save_mask);
  41. if ((pid = vfork()) < 0) {
  42. wait_val = -1;
  43. goto out;
  44. }
  45. if (pid == 0) {
  46. sigaction(SIGQUIT, &save_quit, NULL);
  47. sigaction(SIGINT, &save_int, NULL);
  48. sigprocmask(SIG_SETMASK, &save_mask, NULL);
  49. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  50. _exit(127);
  51. }
  52. #if 0
  53. __printf("Waiting for child %d\n", pid);
  54. #endif
  55. if (__wait4_nocancel(pid, &wait_val, 0, 0) == -1)
  56. wait_val = -1;
  57. out:
  58. sigaction(SIGQUIT, &save_quit, NULL);
  59. sigaction(SIGINT, &save_int, NULL);
  60. sigprocmask(SIG_SETMASK, &save_mask, NULL);
  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. memset(&sa, 0, sizeof(sa));
  110. sa.sa_handler = SIG_IGN;
  111. /*sa.sa_flags = 0; - done by memset */
  112. /*__sigemptyset (&sa.sa_mask); - done by memset */
  113. DO_LOCK ();
  114. if (ADD_REF () == 0)
  115. {
  116. if (sigaction (SIGINT, &sa, &intr) < 0)
  117. {
  118. SUB_REF ();
  119. goto out;
  120. }
  121. if (sigaction (SIGQUIT, &sa, &quit) < 0)
  122. {
  123. save = errno;
  124. SUB_REF ();
  125. goto out_restore_sigint;
  126. }
  127. }
  128. DO_UNLOCK ();
  129. /* We reuse the bitmap in the 'sa' structure. */
  130. __sigaddset (&sa.sa_mask, SIGCHLD);
  131. save = errno;
  132. if (sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0)
  133. {
  134. {
  135. DO_LOCK ();
  136. if (SUB_REF () == 0)
  137. {
  138. save = errno;
  139. (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
  140. out_restore_sigint:
  141. (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
  142. __set_errno (save);
  143. }
  144. out:
  145. DO_UNLOCK ();
  146. return -1;
  147. }
  148. }
  149. CLEANUP_HANDLER;
  150. pid = FORK ();
  151. if (pid == (pid_t) 0)
  152. {
  153. /* Child side. */
  154. const char *new_argv[4];
  155. new_argv[0] = "/bin/sh";
  156. new_argv[1] = "-c";
  157. new_argv[2] = line;
  158. new_argv[3] = NULL;
  159. /* Restore the signals. */
  160. (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
  161. (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
  162. (void) sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);
  163. INIT_LOCK ();
  164. /* Exec the shell. */
  165. (void) execve ("/bin/sh", (char *const *) new_argv, __environ);
  166. _exit (127);
  167. }
  168. else if (pid < (pid_t) 0)
  169. /* The fork failed. */
  170. status = -1;
  171. else
  172. /* Parent side. */
  173. {
  174. /* Note the system() is a cancellation point. But since we call
  175. waitpid() which itself is a cancellation point we do not
  176. have to do anything here. */
  177. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  178. status = -1;
  179. }
  180. CLEANUP_RESET;
  181. save = errno;
  182. DO_LOCK ();
  183. if ((SUB_REF () == 0
  184. && (sigaction (SIGINT, &intr, (struct sigaction *) NULL)
  185. | sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)
  186. || sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0)
  187. {
  188. status = -1;
  189. }
  190. DO_UNLOCK ();
  191. return status;
  192. }
  193. int
  194. __libc_system (const char *line)
  195. {
  196. if (line == NULL)
  197. /* Check that we have a command processor available. It might
  198. not be available after a chroot(), for example. */
  199. return do_system ("exit 0") == 0;
  200. if (SINGLE_THREAD_P)
  201. return do_system (line);
  202. int oldtype = LIBC_CANCEL_ASYNC ();
  203. int result = do_system (line);
  204. LIBC_CANCEL_RESET (oldtype);
  205. return result;
  206. }
  207. /* The cancellation handler. */
  208. static void
  209. cancel_handler (void *arg)
  210. {
  211. pid_t child = *(pid_t *) arg;
  212. INTERNAL_SYSCALL_DECL (err);
  213. INTERNAL_SYSCALL (kill, err, 2, child, SIGKILL);
  214. TEMP_FAILURE_RETRY (waitpid (child, NULL, 0));
  215. DO_LOCK ();
  216. if (SUB_REF () == 0)
  217. {
  218. (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
  219. (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
  220. }
  221. DO_UNLOCK ();
  222. }
  223. #endif
  224. #ifdef IS_IN_libc
  225. weak_alias(__libc_system,system)
  226. #endif