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