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
17#include <cutils/zygote.h>
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25#include <signal.h>
26
27#ifndef NELEM
28# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
29#endif
30
31// pid of child process
32static pid_t g_pid = -1;
33
34static void signal_forwarder (int signal, siginfo_t *si, void *context)
35{
36    if (g_pid >= 0) {
37        kill(g_pid, signal);
38    }
39}
40
41static void post_run_func (int pid) {
42    int my_pgid;
43    int spawned_pgid;
44    int i;
45    int err;
46
47    g_pid = pid;
48
49    my_pgid = getpgid(0);
50    if (my_pgid < 0) {
51        perror ("error with getpgid()");
52        exit (-1);
53    }
54
55    spawned_pgid = getpgid(pid);
56    if (spawned_pgid < 0) {
57        perror ("error with getpgid()");
58        exit (-1);
59    }
60
61    if (my_pgid != spawned_pgid) {
62        // The zygote was unable to move this process into our pgid
63        // We have to forward signals
64
65        int forward_signals[]
66            = {SIGHUP, SIGINT, SIGTERM, SIGWINCH,
67            SIGTSTP, SIGTTIN, SIGTTOU, SIGCONT};
68
69        struct sigaction sa;
70        memset(&sa, 0, sizeof(sa));
71
72        sa.sa_sigaction = signal_forwarder;
73        sa.sa_flags = SA_SIGINFO;
74
75        for (i = 0; i < NELEM(forward_signals); i++) {
76            err = sigaction(forward_signals[i], &sa, NULL);
77            if (err < 0) {
78                perror ("unexpected error");
79                exit (-1);
80            }
81        }
82    }
83}
84
85static void usage(const char *argv0) {
86    fprintf(stderr,"Usage: %s [--help] [-classpath <classpath>] \n"
87    "\t[additional zygote args] fully.qualified.java.ClassName [args]\n", argv0);
88    fprintf(stderr, "\nRequests a new Dalvik VM instance to be spawned from the zygote\n"
89    "process. stdin, stdout, and stderr are hooked up. This process remains\n"
90    "while the spawned VM instance is alive and forwards some signals.\n"
91    "The exit code of the spawned VM instance is dropped.\n");
92}
93
94int main (int argc, const char **argv) {
95    int err;
96
97    if (argc > 1 && 0 == strcmp(argv[1], "--help")) {
98        usage(argv[0]);
99        exit(0);
100    }
101
102    err = zygote_run_wait(argc - 1, argv + 1, post_run_func);
103
104    if (err < 0) {
105        fprintf(stderr, "%s error: no zygote process found\n", argv[0]);
106        exit(-1);
107    }
108    exit(0);
109}
110