system/corennnnn
リビジョン | 5f64296995844981fdfebebdd4b5d84044df3c28 (tree) |
---|---|
日時 | 2009-08-26 08:32:35 |
作者 | Mike Lockwood <lockwood@andr...> |
コミッター | Android Git Automerger |
am 968c8f07: Merge change 22503 into eclair
Merge commit '968c8f07031c881dbb2e27b308d88f76e2917121' into eclair-plus-aosp
* commit '968c8f07031c881dbb2e27b308d88f76e2917121':
@@ -832,6 +832,7 @@ int adb_main(int is_daemon) | ||
832 | 832 | { |
833 | 833 | #if !ADB_HOST |
834 | 834 | int secure = 0; |
835 | + int port; | |
835 | 836 | char value[PROPERTY_VALUE_MAX]; |
836 | 837 | #endif |
837 | 838 |
@@ -918,14 +919,17 @@ int adb_main(int is_daemon) | ||
918 | 919 | } |
919 | 920 | |
920 | 921 | /* for the device, start the usb transport if the |
921 | - ** android usb device exists, otherwise start the | |
922 | - ** network transport. | |
922 | + ** android usb device exists and "service.adb.tcp" | |
923 | + ** is not set, otherwise start the network transport. | |
923 | 924 | */ |
924 | - if(access("/dev/android_adb", F_OK) == 0 || | |
925 | - access("/dev/android", F_OK) == 0) { | |
925 | + property_get("service.adb.tcp.port", value, "0"); | |
926 | + if (sscanf(value, "%d", &port) == 0) { | |
927 | + port = 0; | |
928 | + } | |
929 | + if (port == 0 && access("/dev/android_adb", F_OK) == 0) { | |
926 | 930 | usb_init(); |
927 | 931 | } else { |
928 | - local_init(); | |
932 | + local_init(port); | |
929 | 933 | } |
930 | 934 | init_jdwp(); |
931 | 935 | #endif |
@@ -1006,6 +1010,43 @@ int handle_host_request(char *service, transport_type ttype, char* serial, int r | ||
1006 | 1010 | return 0; |
1007 | 1011 | } |
1008 | 1012 | |
1013 | + // add a new TCP transport | |
1014 | + if (!strncmp(service, "connect:", 8)) { | |
1015 | + char buffer[4096]; | |
1016 | + int port, fd; | |
1017 | + char* host = service + 8; | |
1018 | + char* portstr = strchr(host, ':'); | |
1019 | + | |
1020 | + if (!portstr) { | |
1021 | + snprintf(buffer, sizeof(buffer), "unable to parse %s as <host>:<port>\n", host); | |
1022 | + goto done; | |
1023 | + } | |
1024 | + // zero terminate host by overwriting the ':' | |
1025 | + *portstr++ = 0; | |
1026 | + if (sscanf(portstr, "%d", &port) == 0) { | |
1027 | + snprintf(buffer, sizeof(buffer), "bad port number %s\n", portstr); | |
1028 | + goto done; | |
1029 | + } | |
1030 | + | |
1031 | + fd = socket_network_client(host, port, SOCK_STREAM); | |
1032 | + if (fd < 0) { | |
1033 | + snprintf(buffer, sizeof(buffer), "unable to connect to %s:%d\n", host, port); | |
1034 | + goto done; | |
1035 | + } | |
1036 | + | |
1037 | + D("client: connected on remote on fd %d\n", fd); | |
1038 | + close_on_exec(fd); | |
1039 | + disable_tcp_nagle(fd); | |
1040 | + snprintf(buf, sizeof buf, "%s:%d", host, port); | |
1041 | + register_socket_transport(fd, buf, port, 0); | |
1042 | + snprintf(buffer, sizeof(buffer), "connected to %s:%d\n", host, port); | |
1043 | + | |
1044 | +done: | |
1045 | + snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer); | |
1046 | + writex(reply_fd, buf, strlen(buf)); | |
1047 | + return 0; | |
1048 | + } | |
1049 | + | |
1009 | 1050 | // returns our value for ADB_SERVER_VERSION |
1010 | 1051 | if (!strcmp(service, "version")) { |
1011 | 1052 | char version[12]; |
@@ -33,7 +33,7 @@ | ||
33 | 33 | #define ADB_VERSION_MAJOR 1 // Used for help/version information |
34 | 34 | #define ADB_VERSION_MINOR 0 // Used for help/version information |
35 | 35 | |
36 | -#define ADB_SERVER_VERSION 23 // Increment this when we want to force users to start a new adb server | |
36 | +#define ADB_SERVER_VERSION 24 // Increment this when we want to force users to start a new adb server | |
37 | 37 | |
38 | 38 | typedef struct amessage amessage; |
39 | 39 | typedef struct apacket apacket; |
@@ -262,14 +262,14 @@ void run_transport_disconnects( atransport* t ); | ||
262 | 262 | void kick_transport( atransport* t ); |
263 | 263 | |
264 | 264 | /* initialize a transport object's func pointers and state */ |
265 | -int init_socket_transport(atransport *t, int s, int port); | |
265 | +int init_socket_transport(atransport *t, int s, int port, int local); | |
266 | 266 | void init_usb_transport(atransport *t, usb_handle *usb, int state); |
267 | 267 | |
268 | 268 | /* for MacOS X cleanup */ |
269 | 269 | void close_usb_devices(); |
270 | 270 | |
271 | 271 | /* cause new transports to be init'd and added to the list */ |
272 | -void register_socket_transport(int s, const char *serial, int port); | |
272 | +void register_socket_transport(int s, const char *serial, int port, int local); | |
273 | 273 | void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable); |
274 | 274 | |
275 | 275 | /* this should only be used for transports with connection_state == CS_NOPERM */ |
@@ -105,6 +105,7 @@ void help() | ||
105 | 105 | " environment variable is used, which must\n" |
106 | 106 | " be an absolute path.\n" |
107 | 107 | " devices - list all connected devices\n" |
108 | + " connect <host>:<port> - connect to a device via TCP/IP" | |
108 | 109 | "\n" |
109 | 110 | "device commands:\n" |
110 | 111 | " adb push <local> <remote> - copy file/dir to device\n" |
@@ -149,7 +150,9 @@ void help() | ||
149 | 150 | " adb status-window - continuously print device status for a specified device\n" |
150 | 151 | " adb remount - remounts the /system partition on the device read-write\n" |
151 | 152 | " adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n" |
152 | - " adb root - restarts adb with root permissions\n" | |
153 | + " adb root - restarts the adbd daemon with root permissions\n" | |
154 | + " adb usb - restarts the adbd daemon listening on USB" | |
155 | + " adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port" | |
153 | 156 | "\n" |
154 | 157 | "networking:\n" |
155 | 158 | " adb ppp <tty> [parameters] - Run PPP over USB.\n" |
@@ -850,6 +853,22 @@ top: | ||
850 | 853 | } |
851 | 854 | } |
852 | 855 | |
856 | + if(!strcmp(argv[0], "connect")) { | |
857 | + char *tmp; | |
858 | + if (argc != 2) { | |
859 | + fprintf(stderr, "Usage: adb connect <host>:<port>\n"); | |
860 | + return 1; | |
861 | + } | |
862 | + snprintf(buf, sizeof buf, "host:%s:%s", argv[0], argv[1]); | |
863 | + tmp = adb_query(buf); | |
864 | + if(tmp) { | |
865 | + printf("%s\n", tmp); | |
866 | + return 0; | |
867 | + } else { | |
868 | + return 1; | |
869 | + } | |
870 | + } | |
871 | + | |
853 | 872 | if (!strcmp(argv[0], "emu")) { |
854 | 873 | return adb_send_emulator_command(argc, argv); |
855 | 874 | } |
@@ -908,35 +927,15 @@ top: | ||
908 | 927 | return 0; |
909 | 928 | } |
910 | 929 | |
911 | - if(!strcmp(argv[0], "remount")) { | |
912 | - int fd = adb_connect("remount:"); | |
913 | - if(fd >= 0) { | |
914 | - read_and_dump(fd); | |
915 | - adb_close(fd); | |
916 | - return 0; | |
917 | - } | |
918 | - fprintf(stderr,"error: %s\n", adb_error()); | |
919 | - return 1; | |
920 | - } | |
921 | - | |
922 | - if(!strcmp(argv[0], "reboot")) { | |
923 | - int fd; | |
930 | + if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot") | |
931 | + || !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb") | |
932 | + || !strcmp(argv[0], "reboot")) { | |
933 | + char command[100]; | |
924 | 934 | if (argc > 1) |
925 | - snprintf(buf, sizeof(buf), "reboot:%s", argv[1]); | |
935 | + snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]); | |
926 | 936 | else |
927 | - snprintf(buf, sizeof(buf), "reboot:"); | |
928 | - fd = adb_connect(buf); | |
929 | - if(fd >= 0) { | |
930 | - read_and_dump(fd); | |
931 | - adb_close(fd); | |
932 | - return 0; | |
933 | - } | |
934 | - fprintf(stderr,"error: %s\n", adb_error()); | |
935 | - return 1; | |
936 | - } | |
937 | - | |
938 | - if(!strcmp(argv[0], "root")) { | |
939 | - int fd = adb_connect("root:"); | |
937 | + snprintf(command, sizeof(command), "%s:", argv[0]); | |
938 | + int fd = adb_connect(command); | |
940 | 939 | if(fd >= 0) { |
941 | 940 | read_and_dump(fd); |
942 | 941 | adb_close(fd); |
@@ -120,6 +120,7 @@ void restart_root_service(int fd, void *cookie) | ||
120 | 120 | if (strcmp(value, "1") != 0) { |
121 | 121 | snprintf(buf, sizeof(buf), "adbd cannot run as root in production builds\n"); |
122 | 122 | writex(fd, buf, strlen(buf)); |
123 | + adb_close(fd); | |
123 | 124 | return; |
124 | 125 | } |
125 | 126 |
@@ -134,13 +135,52 @@ void restart_root_service(int fd, void *cookie) | ||
134 | 135 | } |
135 | 136 | } |
136 | 137 | |
137 | -void reboot_service(int fd, char *arg) | |
138 | +void restart_tcp_service(int fd, void *cookie) | |
139 | +{ | |
140 | + char buf[100]; | |
141 | + char value[PROPERTY_VALUE_MAX]; | |
142 | + int port = (int)cookie; | |
143 | + | |
144 | + if (port <= 0) { | |
145 | + snprintf(buf, sizeof(buf), "invalid port\n"); | |
146 | + writex(fd, buf, strlen(buf)); | |
147 | + adb_close(fd); | |
148 | + return; | |
149 | + } | |
150 | + | |
151 | + snprintf(value, sizeof(value), "%d", port); | |
152 | + property_set("service.adb.tcp.port", value); | |
153 | + snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port); | |
154 | + writex(fd, buf, strlen(buf)); | |
155 | + adb_close(fd); | |
156 | + | |
157 | + // quit, and init will restart us in TCP mode | |
158 | + sleep(1); | |
159 | + exit(1); | |
160 | +} | |
161 | + | |
162 | +void restart_usb_service(int fd, void *cookie) | |
163 | +{ | |
164 | + char buf[100]; | |
165 | + | |
166 | + property_set("service.adb.tcp.port", "0"); | |
167 | + snprintf(buf, sizeof(buf), "restarting in USB mode\n"); | |
168 | + writex(fd, buf, strlen(buf)); | |
169 | + adb_close(fd); | |
170 | + | |
171 | + // quit, and init will restart us in USB mode | |
172 | + sleep(1); | |
173 | + exit(1); | |
174 | +} | |
175 | + | |
176 | +void reboot_service(int fd, void *arg) | |
138 | 177 | { |
139 | 178 | char buf[100]; |
140 | 179 | int ret; |
141 | 180 | |
142 | 181 | sync(); |
143 | - ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, arg); | |
182 | + ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | |
183 | + LINUX_REBOOT_CMD_RESTART2, (char *)arg); | |
144 | 184 | if (ret < 0) { |
145 | 185 | snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno)); |
146 | 186 | writex(fd, buf, strlen(buf)); |
@@ -415,12 +455,20 @@ int service_to_fd(const char *name) | ||
415 | 455 | } else if(!strncmp(name, "remount:", 8)) { |
416 | 456 | ret = create_service_thread(remount_service, NULL); |
417 | 457 | } else if(!strncmp(name, "reboot:", 7)) { |
418 | - char* arg = name + 7; | |
458 | + const char* arg = name + 7; | |
419 | 459 | if (*name == 0) |
420 | 460 | arg = NULL; |
421 | - ret = create_service_thread(reboot_service, arg); | |
461 | + ret = create_service_thread(reboot_service, (void *)arg); | |
422 | 462 | } else if(!strncmp(name, "root:", 5)) { |
423 | 463 | ret = create_service_thread(restart_root_service, NULL); |
464 | + } else if(!strncmp(name, "tcpip:", 6)) { | |
465 | + int port; | |
466 | + if (sscanf(name + 6, "%d", &port) == 0) { | |
467 | + port = 0; | |
468 | + } | |
469 | + ret = create_service_thread(restart_tcp_service, (void *)port); | |
470 | + } else if(!strncmp(name, "usb:", 4)) { | |
471 | + ret = create_service_thread(restart_usb_service, NULL); | |
424 | 472 | #endif |
425 | 473 | #if 0 |
426 | 474 | } else if(!strncmp(name, "echo:", 5)){ |
@@ -849,11 +849,11 @@ void close_usb_devices() | ||
849 | 849 | } |
850 | 850 | #endif // ADB_HOST |
851 | 851 | |
852 | -void register_socket_transport(int s, const char *serial, int port) | |
852 | +void register_socket_transport(int s, const char *serial, int port, int local) | |
853 | 853 | { |
854 | 854 | atransport *t = calloc(1, sizeof(atransport)); |
855 | 855 | D("transport: %p init'ing for socket %d, on port %d\n", t, s, port); |
856 | - if ( init_socket_transport(t, s, port) < 0 ) { | |
856 | + if ( init_socket_transport(t, s, port, local) < 0 ) { | |
857 | 857 | adb_close(s); |
858 | 858 | free(t); |
859 | 859 | return; |
@@ -122,7 +122,7 @@ int local_connect(int port) | ||
122 | 122 | close_on_exec(fd); |
123 | 123 | disable_tcp_nagle(fd); |
124 | 124 | snprintf(buf, sizeof buf, "%s%d", LOCAL_CLIENT_PREFIX, port - 1); |
125 | - register_socket_transport(fd, buf, port); | |
125 | + register_socket_transport(fd, buf, port, 1); | |
126 | 126 | return 0; |
127 | 127 | } |
128 | 128 | return -1; |
@@ -147,17 +147,18 @@ static void *client_socket_thread(void *x) | ||
147 | 147 | return 0; |
148 | 148 | } |
149 | 149 | |
150 | -static void *server_socket_thread(void *x) | |
150 | +static void *server_socket_thread(void * arg) | |
151 | 151 | { |
152 | 152 | int serverfd, fd; |
153 | 153 | struct sockaddr addr; |
154 | 154 | socklen_t alen; |
155 | + int port = (int)arg; | |
155 | 156 | |
156 | 157 | D("transport: server_socket_thread() starting\n"); |
157 | 158 | serverfd = -1; |
158 | 159 | for(;;) { |
159 | 160 | if(serverfd == -1) { |
160 | - serverfd = socket_inaddr_any_server(ADB_LOCAL_TRANSPORT_PORT, SOCK_STREAM); | |
161 | + serverfd = socket_inaddr_any_server(port, SOCK_STREAM); | |
161 | 162 | if(serverfd < 0) { |
162 | 163 | D("server: cannot bind socket yet\n"); |
163 | 164 | adb_sleep_ms(1000); |
@@ -167,20 +168,20 @@ static void *server_socket_thread(void *x) | ||
167 | 168 | } |
168 | 169 | |
169 | 170 | alen = sizeof(addr); |
170 | - D("server: trying to get new connection from %d\n", ADB_LOCAL_TRANSPORT_PORT); | |
171 | + D("server: trying to get new connection from %d\n", port); | |
171 | 172 | fd = adb_socket_accept(serverfd, &addr, &alen); |
172 | 173 | if(fd >= 0) { |
173 | 174 | D("server: new connection on fd %d\n", fd); |
174 | 175 | close_on_exec(fd); |
175 | 176 | disable_tcp_nagle(fd); |
176 | - register_socket_transport(fd,"host",ADB_LOCAL_TRANSPORT_PORT); | |
177 | + register_socket_transport(fd, "host", port, 1); | |
177 | 178 | } |
178 | 179 | } |
179 | 180 | D("transport: server_socket_thread() exiting\n"); |
180 | 181 | return 0; |
181 | 182 | } |
182 | 183 | |
183 | -void local_init(void) | |
184 | +void local_init(int port) | |
184 | 185 | { |
185 | 186 | adb_thread_t thr; |
186 | 187 | void* (*func)(void *); |
@@ -193,7 +194,7 @@ void local_init(void) | ||
193 | 194 | |
194 | 195 | D("transport: local %s init\n", HOST ? "client" : "server"); |
195 | 196 | |
196 | - if(adb_thread_create(&thr, func, 0)) { | |
197 | + if(adb_thread_create(&thr, func, (void *)port)) { | |
197 | 198 | fatal_errno("cannot create local socket %s thread", |
198 | 199 | HOST ? "client" : "server"); |
199 | 200 | } |
@@ -225,7 +226,7 @@ static void remote_close(atransport *t) | ||
225 | 226 | adb_close(t->fd); |
226 | 227 | } |
227 | 228 | |
228 | -int init_socket_transport(atransport *t, int s, int port) | |
229 | +int init_socket_transport(atransport *t, int s, int port, int local) | |
229 | 230 | { |
230 | 231 | int fail = 0; |
231 | 232 |
@@ -239,7 +240,7 @@ int init_socket_transport(atransport *t, int s, int port) | ||
239 | 240 | t->type = kTransportLocal; |
240 | 241 | |
241 | 242 | #if ADB_HOST |
242 | - if (HOST) { | |
243 | + if (HOST && local) { | |
243 | 244 | adb_mutex_lock( &local_transports_lock ); |
244 | 245 | { |
245 | 246 | int index = (port - ADB_LOCAL_TRANSPORT_PORT)/2; |