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

Commit a84a42eb authored by Stefan Hilzinger's avatar Stefan Hilzinger Committed by Mike Lockwood
Browse files

Make adb's daemon-port on the host machine configurable.

This is the first CL of a somewhat larger effort which, among other things,
will involve changing the emulator and ddms to talk to adb running on a
configurable port.

The port can be configured using environment variable ANDROID_ADB_SERVER_PORT.

Further CLs will also address the set of ports used for the local transport.

Change-Id: Ib2f431801f0adcd9f2dd290a28005644a36a780a
parent 23e64161
Loading
Loading
Loading
Loading
+23 −9
Original line number Original line Diff line number Diff line
@@ -687,7 +687,7 @@ void start_device_log(void)
#endif
#endif


#if ADB_HOST
#if ADB_HOST
int launch_server()
int launch_server(int server_port)
{
{
#ifdef HAVE_WIN32_PROC
#ifdef HAVE_WIN32_PROC
    /* we need to start the server in the background                    */
    /* we need to start the server in the background                    */
@@ -822,7 +822,17 @@ int launch_server()
}
}
#endif
#endif


int adb_main(int is_daemon)
/* Constructs a local name of form tcp:port.
 * target_str points to the target string, it's content will be overwritten.
 * target_size is the capacity of the target string.
 * server_port is the port number to use for the local name.
 */
void build_local_name(char* target_str, size_t target_size, int server_port)
{
  snprintf(target_str, target_size, "tcp:%d", server_port);
}

int adb_main(int is_daemon, int server_port)
{
{
#if !ADB_HOST
#if !ADB_HOST
    int secure = 0;
    int secure = 0;
@@ -845,9 +855,11 @@ int adb_main(int is_daemon)
    HOST = 1;
    HOST = 1;
    usb_vendors_init();
    usb_vendors_init();
    usb_init();
    usb_init();
    local_init(ADB_LOCAL_TRANSPORT_PORT);
    local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);


    if(install_listener("tcp:5037", "*smartsocket*", NULL)) {
    char local_name[30];
    build_local_name(local_name, sizeof(local_name), server_port);
    if(install_listener(local_name, "*smartsocket*", NULL)) {
        exit(1);
        exit(1);
    }
    }
#else
#else
@@ -873,7 +885,7 @@ int adb_main(int is_daemon)
        }
        }
    }
    }


    /* don't listen on port 5037 if we are running in secure mode */
    /* don't listen on a port (default 5037) if running in secure mode */
    /* don't run as root if we are running in secure mode */
    /* don't run as root if we are running in secure mode */
    if (secure) {
    if (secure) {
        struct __user_cap_header_struct header;
        struct __user_cap_header_struct header;
@@ -905,9 +917,11 @@ int adb_main(int is_daemon)
        cap.inheritable = 0;
        cap.inheritable = 0;
        capset(&header, &cap);
        capset(&header, &cap);


        D("Local port 5037 disabled\n");
        D("Local port disabled\n");
    } else {
    } else {
        if(install_listener("tcp:5037", "*smartsocket*", NULL)) {
        char local_name[30];
        build_local_name(local_name, sizeof(local_name), server_port);
        if(install_listener(local_name, "*smartsocket*", NULL)) {
            exit(1);
            exit(1);
        }
        }
    }
    }
@@ -928,7 +942,7 @@ int adb_main(int is_daemon)
        usb_init();
        usb_init();
    } else {
    } else {
        // listen on default port
        // listen on default port
        local_init(ADB_LOCAL_TRANSPORT_PORT);
        local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT);
    }
    }
    init_jdwp();
    init_jdwp();
#endif
#endif
@@ -1169,6 +1183,6 @@ int main(int argc, char **argv)
    }
    }


    start_device_log();
    start_device_log();
    return adb_main(0);
    return adb_main(0, DEFAULT_ADB_PORT);
#endif
#endif
}
}
+4 −4
Original line number Original line Diff line number Diff line
@@ -237,8 +237,8 @@ void handle_packet(apacket *p, atransport *t);
void send_packet(apacket *p, atransport *t);
void send_packet(apacket *p, atransport *t);


void get_my_path(char *s, size_t maxLen);
void get_my_path(char *s, size_t maxLen);
int launch_server();
int launch_server(int server_port);
int adb_main(int is_daemon);
int adb_main(int is_daemon, int server_port);




/* transports are ref-counted
/* transports are ref-counted
@@ -358,8 +358,8 @@ typedef enum {
#define print_packet(tag,p) do {} while (0)
#define print_packet(tag,p) do {} while (0)
#endif
#endif


#define ADB_PORT 5037
#define DEFAULT_ADB_PORT 5037
#define ADB_LOCAL_TRANSPORT_PORT 5555
#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555


#define ADB_CLASS              0xff
#define ADB_CLASS              0xff
#define ADB_SUBCLASS           0x42
#define ADB_SUBCLASS           0x42
+11 −5
Original line number Original line Diff line number Diff line
@@ -16,12 +16,19 @@
static transport_type __adb_transport = kTransportAny;
static transport_type __adb_transport = kTransportAny;
static const char* __adb_serial = NULL;
static const char* __adb_serial = NULL;


static int __adb_server_port = DEFAULT_ADB_PORT;

void adb_set_transport(transport_type type, const char* serial)
void adb_set_transport(transport_type type, const char* serial)
{
{
    __adb_transport = type;
    __adb_transport = type;
    __adb_serial = serial;
    __adb_serial = serial;
}
}


void adb_set_tcp_specifics(int server_port)
{
    __adb_server_port = server_port;
}

int  adb_get_emulator_console_port(void)
int  adb_get_emulator_console_port(void)
{
{
    const char*   serial = __adb_serial;
    const char*   serial = __adb_serial;
@@ -174,7 +181,7 @@ int _adb_connect(const char *service)
    }
    }
    snprintf(tmp, sizeof tmp, "%04x", len);
    snprintf(tmp, sizeof tmp, "%04x", len);


    fd = socket_loopback_client(ADB_PORT, SOCK_STREAM);
    fd = socket_loopback_client(__adb_server_port, SOCK_STREAM);
    if(fd < 0) {
    if(fd < 0) {
        strcpy(__adb_error, "cannot connect to daemon");
        strcpy(__adb_error, "cannot connect to daemon");
        return -2;
        return -2;
@@ -204,9 +211,10 @@ int adb_connect(const char *service)
    int fd = _adb_connect("host:version");
    int fd = _adb_connect("host:version");


    if(fd == -2) {
    if(fd == -2) {
        fprintf(stdout,"* daemon not running. starting it now *\n");
        fprintf(stdout,"* daemon not running. starting it now on port %d *\n",
                __adb_server_port);
    start_server:
    start_server:
        if(launch_server(0)) {
        if(launch_server(__adb_server_port)) {
            fprintf(stderr,"* failed to start daemon *\n");
            fprintf(stderr,"* failed to start daemon *\n");
            return -1;
            return -1;
        } else {
        } else {
@@ -314,5 +322,3 @@ oops:
    adb_close(fd);
    adb_close(fd);
    return 0;
    return 0;
}
}

+4 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,10 @@ char *adb_query(const char *service);
*/
*/
void adb_set_transport(transport_type type, const char* serial);
void adb_set_transport(transport_type type, const char* serial);


/* Set TCP specifics of the transport to use
*/
void adb_set_tcp_specifics(int server_port);

/* Return the console port of the currently connected emulator (if any)
/* Return the console port of the currently connected emulator (if any)
 * of -1 if there is no emulator, and -2 if there is more than one.
 * of -1 if there is no emulator, and -2 if there is more than one.
 * assumes adb_set_transport() was alled previously...
 * assumes adb_set_transport() was alled previously...
+21 −6
Original line number Original line Diff line number Diff line
@@ -761,6 +761,7 @@ int adb_commandline(int argc, char **argv)
    int quote;
    int quote;
    transport_type ttype = kTransportAny;
    transport_type ttype = kTransportAny;
    char* serial = NULL;
    char* serial = NULL;
    char* server_port_str = NULL;


        /* If defined, this should be an absolute path to
        /* If defined, this should be an absolute path to
         * the directory containing all of the various system images
         * the directory containing all of the various system images
@@ -776,6 +777,19 @@ int adb_commandline(int argc, char **argv)


    serial = getenv("ANDROID_SERIAL");
    serial = getenv("ANDROID_SERIAL");


    /* Validate and assign the server port */
    server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
    int server_port = DEFAULT_ADB_PORT;
    if (server_port_str && strlen(server_port_str) > 0) {
        server_port = (int) strtol(server_port_str, NULL, 0);
        if (server_port <= 0) {
            fprintf(stderr,
                    "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
                    server_port_str);
            return usage();
        }
    }

    /* modifiers and flags */
    /* modifiers and flags */
    while(argc > 0) {
    while(argc > 0) {
        if(!strcmp(argv[0],"nodaemon")) {
        if(!strcmp(argv[0],"nodaemon")) {
@@ -805,7 +819,7 @@ int adb_commandline(int argc, char **argv)
            if (isdigit(argv[0][2])) {
            if (isdigit(argv[0][2])) {
                serial = argv[0] + 2;
                serial = argv[0] + 2;
            } else {
            } else {
                if(argc < 2) return usage();
                if(argc < 2 || argv[0][2] != '\0') return usage();
                serial = argv[1];
                serial = argv[1];
                argc--;
                argc--;
                argv++;
                argv++;
@@ -823,12 +837,13 @@ int adb_commandline(int argc, char **argv)
    }
    }


    adb_set_transport(ttype, serial);
    adb_set_transport(ttype, serial);
    adb_set_tcp_specifics(server_port);


    if ((argc > 0) && (!strcmp(argv[0],"server"))) {
    if ((argc > 0) && (!strcmp(argv[0],"server"))) {
        if (no_daemon || is_daemon) {
        if (no_daemon || is_daemon) {
            r = adb_main(is_daemon);
            r = adb_main(is_daemon, server_port);
        } else {
        } else {
            r = launch_server();
            r = launch_server(server_port);
        }
        }
        if(r) {
        if(r) {
            fprintf(stderr,"* could not start server *\n");
            fprintf(stderr,"* could not start server *\n");
Loading