Revert "Fix adb hang when subprocess dies early."
This is for http://b/3482112 "adb interactions with device causing test harness failures".
This reverts commit 5066a9b05b90c6440df235427afa07522023121d.
Change-Id: I630bf2e04d2ecf0223bd2af4e87136754ff880d3
diff --git a/services.c b/services.c
index e2b58fe..c22ce17 100644
--- a/services.c
+++ b/services.c
@@ -268,15 +268,15 @@
return s[0];
}
-static int create_subprocess(const char *cmd, const char *arg0, const char *arg1, pid_t *pid)
+static int create_subprocess(const char *cmd, const char *arg0, const char *arg1)
{
- D("create_subprocess(cmd=%s, arg0=%s, arg1=%s)\n", cmd, arg0, arg1);
#ifdef HAVE_WIN32_PROC
- fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
- return -1;
+ fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
+ return -1;
#else /* !HAVE_WIN32_PROC */
char *devname;
int ptm;
+ pid_t pid;
ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
if(ptm < 0){
@@ -291,22 +291,19 @@
return -1;
}
- *pid = fork();
- if(*pid < 0) {
+ pid = fork();
+ if(pid < 0) {
printf("- fork failed: %s -\n", strerror(errno));
return -1;
}
- if(*pid == 0){
+ if(pid == 0){
int pts;
setsid();
pts = unix_open(devname, O_RDWR);
- if(pts < 0) {
- fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
- exit(-1);
- }
+ if(pts < 0) exit(-1);
dup2(pts, 0);
dup2(pts, 1);
@@ -315,10 +312,15 @@
adb_close(pts);
adb_close(ptm);
+ execl(cmd, cmd, arg0, arg1, NULL);
+ fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
+ cmd, strerror(errno), errno);
+ exit(-1);
+ } else {
#if !ADB_HOST
- // set OOM adjustment to zero
+ // set child's OOM adjustment to zero
char text[64];
- snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
+ snprintf(text, sizeof text, "/proc/%d/oom_adj", pid);
int fd = adb_open(text, O_WRONLY);
if (fd >= 0) {
adb_write(fd, "0", 1);
@@ -327,17 +329,6 @@
D("adb: unable to open %s\n", text);
}
#endif
- execl(cmd, cmd, arg0, arg1, NULL);
- fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
- cmd, strerror(errno), errno);
- exit(-1);
- } else {
-#if !ADB_HOST
- // Don't set child's OOM adjustment to zero.
- // Let the child do it itself, as sometimes the parent starts
- // running before the child has a /proc/pid/oom_adj.
- // """adb: unable to open /proc/644/oom_adj""" seen in some logs.
-#endif
return ptm;
}
#endif /* !HAVE_WIN32_PROC */
@@ -349,67 +340,6 @@
#define SHELL_COMMAND "/system/bin/sh"
#endif
-#if !ADB_HOST
-static void subproc_waiter_service(int fd, void *cookie)
-{
- pid_t pid = (pid_t)cookie;
-
- D("entered. fd=%d of pid=%d\n", fd, pid);
- for (;;) {
- int status;
- pid_t p = waitpid(pid, &status, 0);
- if (p == pid) {
- D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
- if (WIFSIGNALED(status)) {
- D("*** Killed by signal %d\n", WTERMSIG(status));
- break;
- } else if (!WIFEXITED(status)) {
- D("*** Didn't exit!!. status %d\n", status);
- break;
- } else if (WEXITSTATUS(status) >= 0) {
- D("*** Exit code %d\n", WEXITSTATUS(status));
- break;
- }
- }
- usleep(100000); // poll every 0.1 sec
- }
- D("closing fd=%d of pid=%d\n", fd, pid);
- sleep(5); // Give a chance for the exiting proc to flush.
- adb_close(fd);
-}
-
-static int create_subproc_thread(const char *name)
-{
- stinfo *sti;
- adb_thread_t t;
- int ret_fd;
- pid_t pid;
-
- if(name) {
- ret_fd = create_subprocess(SHELL_COMMAND, "-c", name, &pid);
- } else {
- ret_fd = create_subprocess(SHELL_COMMAND, "-", 0, &pid);
- }
- D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
-
- sti = malloc(sizeof(stinfo));
- if(sti == 0) fatal("cannot allocate stinfo");
- sti->func = subproc_waiter_service;
- sti->cookie = (void*)pid;
- sti->fd = ret_fd;
-
- if(adb_thread_create( &t, service_bootstrap_func, sti)){
- free(sti);
- adb_close(ret_fd);
- printf("cannot create service thread\n");
- return -1;
- }
-
- D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
- return ret_fd;
-}
-#endif
-
int service_to_fd(const char *name)
{
int ret = -1;
@@ -460,12 +390,14 @@
ret = create_jdwp_connection_fd(atoi(name+5));
} else if (!strncmp(name, "log:", 4)) {
ret = create_service_thread(log_service, get_log_file_path(name + 4));
+#endif
} else if(!HOST && !strncmp(name, "shell:", 6)) {
if(name[6]) {
- ret = create_subproc_thread(name + 6);
+ ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
} else {
- ret = create_subproc_thread(0);
+ ret = create_subprocess(SHELL_COMMAND, "-", 0);
}
+#if !ADB_HOST
} else if(!strncmp(name, "sync:", 5)) {
ret = create_service_thread(file_sync_service, NULL);
} else if(!strncmp(name, "remount:", 8)) {