2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Zygote"
19 #include <cutils/sockets.h>
20 #include <cutils/zygote.h>
21 #include <cutils/log.h>
30 #include <arpa/inet.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #define ZYGOTE_SOCKET "zygote"
36 #define ZYGOTE_RETRY_COUNT 1000
37 #define ZYGOTE_RETRY_MILLIS 500
39 static void replace_nl(char *str);
42 * If sendStdio is non-zero, the current process's stdio file descriptors
43 * will be sent and inherited by the spawned process.
45 static int send_request(int fd, int sendStdio, int argc, const char **argv)
47 #ifndef HAVE_ANDROID_OS
48 // not supported on simulator targets
49 //LOGE("zygote_* not supported on simulator targets");
51 #else /* HAVE_ANDROID_OS */
57 const char *newline_string = "\n";
59 char msgbuf[CMSG_SPACE(sizeof(int) * 3)];
63 memset(&msg, 0, sizeof(msg));
64 memset(&ivs, 0, sizeof(ivs));
66 // First line is arg count
67 snprintf(argc_buffer, sizeof(argc_buffer), "%d\n", argc);
69 ivs[0].iov_base = argc_buffer;
70 ivs[0].iov_len = strlen(argc_buffer);
76 // Pass the file descriptors with the first write
77 msg.msg_control = msgbuf;
78 msg.msg_controllen = sizeof msgbuf;
80 cmsg = CMSG_FIRSTHDR(&msg);
82 cmsg->cmsg_len = CMSG_LEN(3 * sizeof(int));
83 cmsg->cmsg_level = SOL_SOCKET;
84 cmsg->cmsg_type = SCM_RIGHTS;
86 cmsg_payload = (int *)CMSG_DATA(cmsg);
87 cmsg_payload[0] = STDIN_FILENO;
88 cmsg_payload[1] = STDOUT_FILENO;
89 cmsg_payload[2] = STDERR_FILENO;
93 ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
94 } while (ret < 0 && errno == EINTR);
100 // Only send the fd's once
101 msg.msg_control = NULL;
102 msg.msg_controllen = 0;
104 // replace any newlines with spaces and send the args
105 for (i = 0; i < argc; i++) {
111 if (strchr(toprint, '\n') != NULL) {
112 tofree = strdup(toprint);
117 ivs[0].iov_base = (char *)toprint;
118 ivs[0].iov_len = strlen(toprint);
119 ivs[1].iov_base = (char *)newline_string;
125 ret = sendmsg(fd, &msg, MSG_NOSIGNAL);
126 } while (ret < 0 && errno == EINTR);
128 if (tofree != NULL) {
137 // Read the pid, as a 4-byte network-order integer
139 ivs[0].iov_base = &pid;
140 ivs[0].iov_len = sizeof(pid);
145 ret = recvmsg(fd, &msg, MSG_NOSIGNAL | MSG_WAITALL);
146 } while (ret < 0 && errno == EINTR);
152 ivs[0].iov_len -= ret;
153 ivs[0].iov_base += ret;
154 } while (ivs[0].iov_len > 0);
159 #endif /* HAVE_ANDROID_OS */
162 int zygote_run_wait(int argc, const char **argv, void (*post_run_func)(int))
167 const char *newargv[argc + 1];
169 fd = socket_local_client(ZYGOTE_SOCKET,
170 ANDROID_SOCKET_NAMESPACE_RESERVED, AF_LOCAL);
176 // The command socket is passed to the peer as close-on-exec
177 // and will close when the peer dies
178 newargv[0] = "--peer-wait";
179 memcpy(newargv + 1, argv, argc * sizeof(*argv));
181 pid = send_request(fd, 1, argc + 1, newargv);
183 if (pid > 0 && post_run_func != NULL) {
187 // Wait for socket to close
190 err = read(fd, &dummy, sizeof(dummy));
191 } while ((err < 0 && errno == EINTR) || err != 0);
195 } while (err < 0 && errno == EINTR);
201 * Spawns a new dalvik instance via the Zygote process. The non-zygote
202 * arguments are passed to com.android.internal.os.RuntimeInit(). The
203 * first non-option argument should be a class name in the system class path.
205 * The arg list may start with zygote params such as --set-uid.
207 * If sendStdio is non-zero, the current process's stdio file descriptors
208 * will be sent and inherited by the spawned process.
210 * The pid of the child process is returned, or -1 if an error was
213 * zygote_run_oneshot waits up to ZYGOTE_RETRY_COUNT *
214 * ZYGOTE_RETRY_MILLIS for the zygote socket to be available.
216 int zygote_run_oneshot(int sendStdio, int argc, const char **argv)
223 const char **newargv = argv;
224 const int newargc = argc;
226 for (retries = 0; (fd < 0) && (retries < ZYGOTE_RETRY_COUNT); retries++) {
230 memset(&ts, 0, sizeof(ts));
231 ts.tv_nsec = ZYGOTE_RETRY_MILLIS * 1000 * 1000;
234 err = nanosleep (&ts, &ts);
235 } while (err < 0 && errno == EINTR);
237 fd = socket_local_client(ZYGOTE_SOCKET, AF_LOCAL,
238 ANDROID_SOCKET_NAMESPACE_RESERVED);
245 pid = send_request(fd, 0, newargc, newargv);
249 } while (err < 0 && errno == EINTR);
255 * Replaces all occurrances of newline with space.
257 static void replace_nl(char *str)