Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 865f6f98 authored by JP Abgrall's avatar JP Abgrall Committed by android-merger
Browse files

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 69c5c4c4.

Change-Id: I630bf2e04d2ecf0223bd2af4e87136754ff880d3
parent 1e0582eb
Loading
Loading
Loading
Loading
+9 −13
Original line number Diff line number Diff line
@@ -36,9 +36,6 @@
#include "usb_vendors.h"
#endif

#if ADB_TRACE
ADB_MUTEX_DEFINE( D_lock );
#endif

int HOST = 0;

@@ -594,6 +591,14 @@ nomem:
    return 0;
}

#ifdef HAVE_FORKEXEC
static void sigchld_handler(int n)
{
    int status;
    while(waitpid(-1, &status, WNOHANG) > 0) ;
}
#endif

#ifdef HAVE_WIN32_PROC
static BOOL WINAPI ctrlc_handler(DWORD type)
{
@@ -636,7 +641,6 @@ void start_logging(void)

    fd = unix_open("/dev/null", O_RDONLY);
    dup2(fd, 0);
    adb_close(fd);

    fd = unix_open("/tmp/adb.log", O_WRONLY | O_CREAT | O_APPEND, 0640);
    if(fd < 0) {
@@ -644,8 +648,6 @@ void start_logging(void)
    }
    dup2(fd, 1);
    dup2(fd, 2);
    adb_close(fd);

    fprintf(stderr,"--- adb starting (pid %d) ---\n", getpid());
#endif
}
@@ -790,7 +792,6 @@ int launch_server(int server_port)
        // we use stderr instead of stdout due to stdout's buffering behavior.
        adb_close(fd[0]);
        dup2(fd[1], STDERR_FILENO);
        dup2(fd[1], STDOUT_FILENO);
        adb_close(fd[1]);

        // child process
@@ -847,7 +848,7 @@ int adb_main(int is_daemon, int server_port)
#ifdef HAVE_WIN32_PROC
    SetConsoleCtrlHandler( ctrlc_handler, TRUE );
#elif defined(HAVE_FORKEXEC)
    // Let the service subproc creator handle its children.
    signal(SIGCHLD, sigchld_handler);
    signal(SIGPIPE, SIG_IGN);
#endif

@@ -956,9 +957,7 @@ int adb_main(int is_daemon, int server_port)
        // listen on default port
        local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
    }
    D("adb_main(): pre init_jdwp()\n");
    init_jdwp();
    D("adb_main(): post init_jdwp()\n");
#endif

    if (is_daemon)
@@ -972,7 +971,6 @@ int adb_main(int is_daemon, int server_port)
#endif
        start_logging();
    }
    D("Event loop starting\n");

    fdevent_loop();

@@ -1273,7 +1271,6 @@ int main(int argc, char **argv)
#if ADB_HOST
    adb_trace_init();
    adb_sysdeps_init();
    D("Handling commandline()\n");
    return adb_commandline(argc - 1, argv + 1);
#else
    if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
@@ -1282,7 +1279,6 @@ int main(int argc, char **argv)
    }

    start_device_log();
    D("Handling main()\n");
    return adb_main(0, DEFAULT_ADB_PORT);
#endif
}
+4 −34
Original line number Diff line number Diff line
@@ -344,22 +344,8 @@ typedef enum {

#if ADB_TRACE

// TODO(jpa): remove this after fixing fprintf() multithreading issue {
// Can't include sysdeps.h, because framebuffer_service.c wants a different
// close() than what sysdeps overrides when linking adbd.

// Assume Linux.
#ifndef _ADB_SYSDEPS_H
#include <pthread.h>
typedef  pthread_mutex_t          adb_mutex_t;
#define  ADB_MUTEX(x)   extern adb_mutex_t  x;
#include "mutex_list.h"
#endif

// }
  int     adb_trace_mask;

  extern int     adb_trace_mask;
  extern unsigned char    adb_trace_output_count;
  void    adb_trace_init(void);

#  define ADB_TRACING  ((adb_trace_mask & (1 << TRACE_TAG)) != 0)
@@ -367,27 +353,11 @@ typedef pthread_mutex_t adb_mutex_t;
  /* you must define TRACE_TAG before using this macro */
  #define  D(...)                                      \
        do {                                           \
            if (ADB_TRACING) {                         \
                adb_mutex_lock(&D_lock);               \
                fprintf(stderr, "%s::%s():",           \
                    __FILE__, __FUNCTION__);           \
                fprintf(stderr, __VA_ARGS__ );         \
                fflush(stderr);                        \
                adb_mutex_unlock(&D_lock);             \
           }                                           \
        } while (0)
#  define  DR(...)                                     \
        do {                                           \
            if (ADB_TRACING) {                         \
                adb_mutex_lock(&D_lock);               \
            if (ADB_TRACING)                           \
                fprintf(stderr, __VA_ARGS__ );         \
                fflush(stderr);                        \
                adb_mutex_unlock(&D_lock);             \
           }                                           \
        } while (0)
#else
#  define  D(...)          ((void)0)
#  define  DR(...)         ((void)0)
#  define  ADB_TRACING     0
#endif

+0 −3
Original line number Diff line number Diff line
@@ -202,7 +202,6 @@ int _adb_connect(const char *service)
        return -1;
    }

    D("_adb_connect: return fd %d\n", fd);
    return fd;
}

@@ -211,7 +210,6 @@ int adb_connect(const char *service)
    // first query the adb server's version
    int fd = _adb_connect("host:version");

    D("adb_connect: service %s\n", service);
    if(fd == -2) {
        fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
                __adb_server_port);
@@ -268,7 +266,6 @@ int adb_connect(const char *service)
    if(fd == -2) {
        fprintf(stderr,"** daemon still not running");
    }
    D("adb_connect: return fd %d\n", fd);

    return fd;
error:
+0 −9
Original line number Diff line number Diff line
@@ -218,9 +218,7 @@ static void read_and_dump(int fd)
    int len;

    while(fd >= 0) {
        D("read_and_dump(): pre adb_read(fd=%d)\n", fd);
        len = adb_read(fd, buf, 4096);
        D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len);
        if(len == 0) {
            break;
        }
@@ -248,9 +246,7 @@ static void *stdin_read_thread(void *x)

    for(;;) {
        /* fdi is really the client's stdin, so use read, not adb_read here */
        D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi);
        r = unix_read(fdi, buf, 1024);
        D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi);
        if(r == 0) break;
        if(r < 0) {
            if(errno == EINTR) continue;
@@ -857,7 +853,6 @@ top:
        }

        if(argc < 2) {
            D("starting interactive shell\n");
            r = interactive_shell();
            if (h) {
                printf("\x1b[0m");
@@ -882,12 +877,9 @@ top:
        }

        for(;;) {
            D("interactive shell loop. buff=%s\n", buf);
            fd = adb_connect(buf);
            if(fd >= 0) {
                D("about to read_and_dump(fd=%d)\n", fd);
                read_and_dump(fd);
                D("read_and_dump() done.\n");
                adb_close(fd);
                r = 0;
            } else {
@@ -904,7 +896,6 @@ top:
                    printf("\x1b[0m");
                    fflush(stdout);
                }
                D("interactive shell loop. return r=%d\n", r);
                return r;
            }
        }
+27 −116
Original line number Diff line number Diff line
@@ -28,12 +28,9 @@

#include "fdevent.h"

#define TRACE(x...) fprintf(stderr,x)

/* !!! Do not enable DEBUG for the adb that will run as the server:
** both stdout and stderr are used to communicate between the client
** and server. Any extra output will cause failures.
*/
#define DEBUG 0   /* non-0 will break adb server */
#define DEBUG 0

static void fatal(const char *fn, const char *fmt, ...)
{
@@ -48,11 +45,6 @@ static void fatal(const char *fn, const char *fmt, ...)
#define FATAL(x...) fatal(__FUNCTION__, x)

#if DEBUG
#define D(...) \
    do { \
        fprintf(stderr, "%s::%s():", __FILE__, __FUNCTION__);  \
        fprintf(stderr, __VA_ARGS__);                          \
    } while(0)
static void dump_fde(fdevent *fde, const char *info)
{
    fprintf(stderr,"FDE #%03d %c%c%c %s\n", fde->fd,
@@ -62,7 +54,6 @@ static void dump_fde(fdevent *fde, const char *info)
            info);
}
#else
#define D(...) ((void)0)
#define dump_fde(fde, info) do { } while(0)
#endif

@@ -282,121 +273,39 @@ static void fdevent_update(fdevent *fde, unsigned events)
    fde->state = (fde->state & FDE_STATEMASK) | events;    
}

#if DEBUG
static void dump_all_fds(const char *extra_msg)
{
int i;
    fdevent *fde;
    // per fd: 4 digits (but really: log10(FD_SETSIZE)), 1 staus, 1 blank
    char msg_buff[FD_SETSIZE*6 + 1], *pb=msg_buff;
    size_t max_chars = FD_SETSIZE * 6 + 1;
    int printed_out;
#define SAFE_SPRINTF(...)                                                    \
    do {                                                                     \
        printed_out = snprintf(pb, max_chars, __VA_ARGS__);                  \
        if (printed_out <= 0) {                                              \
            D("... snprintf failed.\n");                                     \
            return;                                                          \
        }                                                                    \
        if (max_chars < (unsigned int)printed_out) {                         \
            D("... snprintf out of space.\n");                               \
            return;                                                          \
        }                                                                    \
        pb += printed_out;                                                   \
        max_chars -= printed_out;                                            \
    } while(0)

    for(i = 0; i < select_n; i++) {
        fde = fd_table[i];
        SAFE_SPRINTF("%d", i);
        if(fde == 0) {
            SAFE_SPRINTF("? ");
            continue;
        }
        if(fcntl(i, F_GETFL, NULL) < 0) {
            SAFE_SPRINTF("b");
        }
        SAFE_SPRINTF(" ");
    }
    D("%s fd_table[]->fd = {%s}\n", extra_msg, msg_buff);
}
#endif

/* Looks at fd_table[] for bad FDs and sets bit in fds.
** Returns the number of bad FDs.
*/
static int fdevent_fd_check(fd_set *fds)
{
    int i, n = 0;
    fdevent *fde;

    for(i = 0; i < select_n; i++) {
        fde = fd_table[i];
        if(fde == 0) continue;
        if(fcntl(i, F_GETFL, NULL) < 0) {
            FD_SET(i, fds);
            n++;
            fde->state |= FDE_DONT_CLOSE;

        }
    }
    return n;
}

static void fdevent_process()
{
    int i, n;
    fdevent *fde;
    unsigned events;
    fd_set rfd, wfd, efd;
    struct timeval tv = { /*tv_sec=*/5, /*tv_usec=*/0 };

    memcpy(&rfd, &read_fds, sizeof(fd_set));
    memcpy(&wfd, &write_fds, sizeof(fd_set));
    memcpy(&efd, &error_fds, sizeof(fd_set));

    n = select(select_n, &rfd, &wfd, &efd, &tv);

    D("select() returned n=%d, errno=%d\n", n, n<0?errno:0);

#if DEBUG
    dump_all_fds("post select()");
#endif
    n = select(select_n, &rfd, &wfd, &efd, 0);

    if(n < 0) {
        switch(errno) {
        case EINTR: return;
        case EBADF:
            // Can't trust the FD sets after an error.
            FD_ZERO(&wfd);
            FD_ZERO(&efd);
            FD_ZERO(&rfd);
            break;
        default:
            D("Unexpected select() error=%d\n", errno);
        if(errno == EINTR) return;
        perror("select");
        return;
    }
    }
    if(n <= 0) {
        // We fake a read, as the rest of the code assumes
        // that errors will be detected at that point.
        n = fdevent_fd_check(&rfd);
    }

    for(i = 0; (i < select_n) && (n > 0); i++) {
        events = 0;
        if(FD_ISSET(i, &rfd)) { events |= FDE_READ; n--; }
        if(FD_ISSET(i, &wfd)) { events |= FDE_WRITE; n--; }
        if(FD_ISSET(i, &efd)) { events |= FDE_ERROR; n--; }
        if(FD_ISSET(i, &rfd)) events |= FDE_READ;
        if(FD_ISSET(i, &wfd)) events |= FDE_WRITE;
        if(FD_ISSET(i, &efd)) events |= FDE_ERROR;

        if(events) {
            n--;

            fde = fd_table[i];
            if(fde == 0) FATAL("missing fde for fd %d\n", i);

            fde->events |= events;

            D("got events fde->fd=%d events=%04x, state=%04x\n",
                fde->fd, fde->events, fde->state);
            if(fde->state & FDE_PENDING) continue;
            fde->state |= FDE_PENDING;
            fdevent_plist_enqueue(fde);
@@ -441,7 +350,7 @@ static void fdevent_unregister(fdevent *fde)
    }

    if(fd_table[fde->fd] != fde) {
        FATAL("fd_table out of sync [%d]\n", fde->fd);
        FATAL("fd_table out of sync");
    }

    fd_table[fde->fd] = 0;
@@ -580,8 +489,9 @@ void fdevent_loop()
    fdevent *fde;

    for(;;) {
        D("--- ---- waiting for events\n");

#if DEBUG
        fprintf(stderr,"--- ---- waiting for events\n");
#endif
        fdevent_process();

        while((fde = fdevent_plist_dequeue())) {
@@ -593,3 +503,4 @@ void fdevent_loop()
        }
    }
}
Loading