zygote.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define LOG_TAG "Zygote"
  17. #include <cutils/sockets.h>
  18. #include <cutils/zygote.h>
  19. #include <cutils/log.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <time.h>
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <arpa/inet.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #define ZYGOTE_SOCKET "zygote"
  31. #define ZYGOTE_RETRY_COUNT 1000
  32. #define ZYGOTE_RETRY_MILLIS 500
  33. static void replace_nl(char *str);
  34. /*
  35. * If sendStdio is non-zero, the current process's stdio file descriptors
  36. * will be sent and inherited by the spawned process.
  37. */
  38. static int send_request(int fd, int sendStdio, int argc, const char **argv)
  39. {
  40. #ifndef HAVE_ANDROID_OS
  41. // not supported on simulator targets
  42. //ALOGE("zygote_* not supported on simulator targets");
  43. return -1;
  44. #else /* HAVE_ANDROID_OS */
  45. uint32_t pid;
  46. int i;
  47. struct iovec ivs[2];
  48. struct msghdr msg;
  49. char argc_buffer[12];
  50. const char *newline_string = "\n";
  51. struct cmsghdr *cmsg;
  52. char msgbuf[CMSG_SPACE(sizeof(int) * 3)];
  53. int *cmsg_payload;
  54. ssize_t ret;
  55. memset(&msg, 0, sizeof(msg));
  56. memset(&ivs, 0, sizeof(ivs));
  57. // First line is arg count
  58. snprintf(argc_buffer, sizeof(argc_buffer), "%d\n", argc);
  59. ivs[0].iov_base = argc_buffer;
  60. ivs[0].iov_len = strlen(argc_buffer);
  61. msg.msg_iov = ivs;
  62. msg.msg_iovlen = 1;
  63. if (sendStdio != 0) {
  64. // Pass the file descriptors with the first write
  65. msg.msg_control = msgbuf;
  66. msg.msg_controllen = sizeof msgbuf;
  67. cmsg = CMSG_FIRSTHDR(&msg);
  68. cmsg->cmsg_len = CMSG_LEN(3 * sizeof(int));
  69. cmsg->cmsg_level = SOL_SOCKET;
  70. cmsg->cmsg_type = SCM_RIGHTS;
  71. cmsg_payload = (int *)CMSG_DATA(cmsg);
  72. cmsg_payload[0] = STDIN_FILENO;
  73. cmsg_payload[1] = STDOUT_FILENO;
  74. cmsg_payload[2] = STDERR_FILENO;
  75. }
  76. do {
  77. ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
  78. } while (ret < 0 && errno == EINTR);
  79. if (ret < 0) {
  80. return -1;
  81. }
  82. // Only send the fd's once
  83. msg.msg_control = NULL;
  84. msg.msg_controllen = 0;
  85. // replace any newlines with spaces and send the args
  86. for (i = 0; i < argc; i++) {
  87. char *tofree = NULL;
  88. const char *toprint;
  89. toprint = argv[i];
  90. if (strchr(toprint, '\n') != NULL) {
  91. tofree = strdup(toprint);
  92. toprint = tofree;
  93. replace_nl(tofree);
  94. }
  95. ivs[0].iov_base = (char *)toprint;
  96. ivs[0].iov_len = strlen(toprint);
  97. ivs[1].iov_base = (char *)newline_string;
  98. ivs[1].iov_len = 1;
  99. msg.msg_iovlen = 2;
  100. do {
  101. ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
  102. } while (ret < 0 && errno == EINTR);
  103. if (tofree != NULL) {
  104. free(tofree);
  105. }
  106. if (ret < 0) {
  107. return -1;
  108. }
  109. }
  110. // Read the pid, as a 4-byte network-order integer
  111. ivs[0].iov_base = &pid;
  112. ivs[0].iov_len = sizeof(pid);
  113. msg.msg_iovlen = 1;
  114. do {
  115. do {
  116. ret = recvmsg(fd, &msg, MSG_NOSIGNAL | MSG_WAITALL);
  117. } while (ret < 0 && errno == EINTR);
  118. if (ret < 0) {
  119. return -1;
  120. }
  121. ivs[0].iov_len -= ret;
  122. ivs[0].iov_base += ret;
  123. } while (ivs[0].iov_len > 0);
  124. pid = ntohl(pid);
  125. return pid;
  126. #endif /* HAVE_ANDROID_OS */
  127. }
  128. int zygote_run_wait(int argc, const char **argv, void (*post_run_func)(int))
  129. {
  130. int fd;
  131. int pid;
  132. int err;
  133. const char *newargv[argc + 1];
  134. fd = socket_local_client(ZYGOTE_SOCKET,
  135. ANDROID_SOCKET_NAMESPACE_RESERVED, AF_LOCAL);
  136. if (fd < 0) {
  137. return -1;
  138. }
  139. // The command socket is passed to the peer as close-on-exec
  140. // and will close when the peer dies
  141. newargv[0] = "--peer-wait";
  142. memcpy(newargv + 1, argv, argc * sizeof(*argv));
  143. pid = send_request(fd, 1, argc + 1, newargv);
  144. if (pid > 0 && post_run_func != NULL) {
  145. post_run_func(pid);
  146. }
  147. // Wait for socket to close
  148. do {
  149. int dummy;
  150. err = read(fd, &dummy, sizeof(dummy));
  151. } while ((err < 0 && errno == EINTR) || err != 0);
  152. do {
  153. err = close(fd);
  154. } while (err < 0 && errno == EINTR);
  155. return 0;
  156. }
  157. /**
  158. * Spawns a new dalvik instance via the Zygote process. The non-zygote
  159. * arguments are passed to com.android.internal.os.RuntimeInit(). The
  160. * first non-option argument should be a class name in the system class path.
  161. *
  162. * The arg list may start with zygote params such as --set-uid.
  163. *
  164. * If sendStdio is non-zero, the current process's stdio file descriptors
  165. * will be sent and inherited by the spawned process.
  166. *
  167. * The pid of the child process is returned, or -1 if an error was
  168. * encountered.
  169. *
  170. * zygote_run_oneshot waits up to ZYGOTE_RETRY_COUNT *
  171. * ZYGOTE_RETRY_MILLIS for the zygote socket to be available.
  172. */
  173. int zygote_run_oneshot(int sendStdio, int argc, const char **argv)
  174. {
  175. int fd = -1;
  176. int err;
  177. int i;
  178. int retries;
  179. int pid;
  180. const char **newargv = argv;
  181. const int newargc = argc;
  182. for (retries = 0; (fd < 0) && (retries < ZYGOTE_RETRY_COUNT); retries++) {
  183. if (retries > 0) {
  184. struct timespec ts;
  185. memset(&ts, 0, sizeof(ts));
  186. ts.tv_nsec = ZYGOTE_RETRY_MILLIS * 1000 * 1000;
  187. do {
  188. err = nanosleep (&ts, &ts);
  189. } while (err < 0 && errno == EINTR);
  190. }
  191. fd = socket_local_client(ZYGOTE_SOCKET, AF_LOCAL,
  192. ANDROID_SOCKET_NAMESPACE_RESERVED);
  193. }
  194. if (fd < 0) {
  195. return -1;
  196. }
  197. pid = send_request(fd, 0, newargc, newargv);
  198. do {
  199. err = close(fd);
  200. } while (err < 0 && errno == EINTR);
  201. return pid;
  202. }
  203. /**
  204. * Replaces all occurrances of newline with space.
  205. */
  206. static void replace_nl(char *str)
  207. {
  208. for(; *str; str++) {
  209. if (*str == '\n') {
  210. *str = ' ';
  211. }
  212. }
  213. }