2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #define TRACE_TAG TRACE_SDB
27 #include "file_sync_service.h"
31 # include <netinet/in.h>
35 //# include <sys/reboot.h> eric
38 #define PROCESS_WORKING_DIRECTORY "/"
40 typedef struct stinfo stinfo;
43 void (*func)(int fd, void *cookie);
49 void *service_bootstrap_func(void *x)
52 sti->func(sti->fd, sti->cookie);
58 SDB_MUTEX_DEFINE( dns_lock );
60 static void dns_service(int fd, void *cookie)
62 char *hostname = cookie;
66 sdb_mutex_lock(&dns_lock);
67 hp = gethostbyname(hostname);
72 writex(fd, hp->h_addr, 4);
74 sdb_mutex_unlock(&dns_lock);
78 extern int recovery_mode;
80 static void recover_service(int s, void *cookie)
82 unsigned char buf[4096];
83 unsigned count = (unsigned) cookie;
86 fd = sdb_creat("/tmp/update", 0644);
93 unsigned xfer = (count > 4096) ? 4096 : count;
94 if(readx(s, buf, xfer)) break;
95 if(writex(fd, buf, xfer)) break;
100 writex(s, "OKAY", 4);
102 writex(s, "FAIL", 4);
107 fd = sdb_creat("/tmp/update.begin", 0644);
111 void restart_root_service(int fd, void *cookie)
114 char value[PROPERTY_VALUE_MAX];
117 snprintf(buf, sizeof(buf), "sdbd is already running as root\n");
118 writex(fd, buf, strlen(buf));
121 property_get("ro.debuggable", value, "");
122 if (strcmp(value, "1") != 0) {
123 snprintf(buf, sizeof(buf), "sdbd cannot run as root in production builds\n");
124 writex(fd, buf, strlen(buf));
129 property_set("service.sdb.root", "1");
130 snprintf(buf, sizeof(buf), "restarting sdbd as root\n");
131 writex(fd, buf, strlen(buf));
134 // quit, and init will restart us as root
140 void restart_tcp_service(int fd, void *cookie)
143 char value[PROPERTY_VALUE_MAX];
144 int port = (int)cookie;
147 snprintf(buf, sizeof(buf), "invalid port\n");
148 writex(fd, buf, strlen(buf));
153 snprintf(value, sizeof(value), "%d", port);
154 property_set("service.sdb.tcp.port", value);
155 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
156 writex(fd, buf, strlen(buf));
159 // quit, and init will restart us in TCP mode
164 void restart_usb_service(int fd, void *cookie)
168 property_set("service.sdb.tcp.port", "0");
169 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
170 writex(fd, buf, strlen(buf));
173 // quit, and init will restart us in USB mode
179 void reboot_service(int fd, void *arg)
186 /* Attempt to unmount the SD card first.
187 * No need to bother checking for errors.
191 /* ask vdc to unmount it */
192 execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount",
193 getenv("EXTERNAL_STORAGE"), "force", NULL);
194 } else if (pid > 0) {
195 /* wait until vdc succeeds or fails */
196 waitpid(pid, &ret, 0);
199 // ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
200 // LINUX_REBOOT_CMD_RESTART2, (char *)arg);
202 snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno));
203 writex(fd, buf, strlen(buf));
212 static void echo_service(int fd, void *cookie)
220 r = read(fd, buf, 4096);
221 if(r == 0) goto done;
223 if(errno == EINTR) continue;
236 if((r < 0) && (errno == EINTR)) continue;
245 static int create_service_thread(void (*func)(int, void *), void *cookie)
251 if(sdb_socketpair(s)) {
252 printf("cannot create service socket pair\n");
256 sti = malloc(sizeof(stinfo));
257 if(sti == 0) fatal("cannot allocate stinfo");
259 sti->cookie = cookie;
262 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
266 printf("cannot create service thread\n");
270 D("service thread started, %d:%d\n",s[0], s[1]);
274 static int create_subprocess(const char *cmd, const char *arg0, const char *arg1)
276 #ifdef HAVE_WIN32_PROC
277 fprintf(stderr, "error: create_subprocess not implemented on Win32 (%s %s %s)\n", cmd, arg0, arg1);
279 #else /* !HAVE_WIN32_PROC */
284 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
286 printf("[ cannot open /dev/ptmx - %s ]\n",strerror(errno));
289 fcntl(ptm, F_SETFD, FD_CLOEXEC);
291 if(grantpt(ptm) || unlockpt(ptm) ||
292 ((devname = (char*) ptsname(ptm)) == 0)){
293 printf("[ trouble with /dev/ptmx - %s ]\n", strerror(errno));
299 printf("- fork failed: %s -\n", strerror(errno));
307 if (chdir(PROCESS_WORKING_DIRECTORY) < 0)
308 D("sdb: unable to change working directory to %s\n", PROCESS_WORKING_DIRECTORY);
310 pts = unix_open(devname, O_RDWR);
311 if(pts < 0) exit(-1);
319 execl(cmd, cmd, arg0, arg1, NULL);
320 fprintf(stderr, "- exec '%s' failed: %s (%d) -\n",
321 cmd, strerror(errno), errno);
325 // set child's OOM adjustment to zero
327 snprintf(text, sizeof text, "/proc/%d/oom_adj", pid);
328 int fd = sdb_open(text, O_WRONLY);
330 sdb_write(fd, "0", 1);
333 D("sdb: unable to open %s\n", text);
338 #endif /* !HAVE_WIN32_PROC */
342 #define SHELL_COMMAND "/bin/sh"
344 //#define SHELL_COMMAND "/system/bin/sh"
347 int service_to_fd(const char *name)
351 if(!strncmp(name, "tcp:", 4)) {
352 int port = atoi(name + 4);
353 name = strchr(name + 4, ':');
355 ret = socket_loopback_client(port, SOCK_STREAM);
357 disable_tcp_nagle(ret);
360 sdb_mutex_lock(&dns_lock);
361 ret = socket_network_client(name + 1, port, SOCK_STREAM);
362 sdb_mutex_unlock(&dns_lock);
367 #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
368 } else if(!strncmp(name, "local:", 6)) {
369 ret = socket_local_client(name + 6,
370 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
371 } else if(!strncmp(name, "localreserved:", 14)) {
372 ret = socket_local_client(name + 14,
373 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
374 } else if(!strncmp(name, "localabstract:", 14)) {
375 ret = socket_local_client(name + 14,
376 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
377 } else if(!strncmp(name, "localfilesystem:", 16)) {
378 ret = socket_local_client(name + 16,
379 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
382 } else if(!strncmp("dns:", name, 4)){
383 char *n = strdup(name + 4);
384 if(n == 0) return -1;
385 ret = create_service_thread(dns_service, n);
386 #else /* !SDB_HOST */
387 } else if(!strncmp("dev:", name, 4)) {
388 ret = unix_open(name + 4, O_RDWR);
389 } else if(!strncmp(name, "framebuffer:", 12)) {
390 ret = create_service_thread(framebuffer_service, 0);
391 } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
392 ret = create_service_thread(recover_service, (void*) atoi(name + 8));
393 } else if (!strncmp(name, "jdwp:", 5)) {
394 ret = create_jdwp_connection_fd(atoi(name+5));
396 } else if (!strncmp(name, "log:", 4)) {
397 ret = create_service_thread(log_service, get_log_file_path(name + 4));
400 } else if(!HOST && !strncmp(name, "shell:", 6)) {
402 ret = create_subprocess(SHELL_COMMAND, "-c", name + 6);
404 ret = create_subprocess(SHELL_COMMAND, "-", 0);
407 } else if(!strncmp(name, "sync:", 5)) {
408 ret = create_service_thread(file_sync_service, NULL);
410 } else if(!strncmp(name, "remount:", 8)) {
411 ret = create_service_thread(remount_service, NULL);
413 } else if(!strncmp(name, "reboot:", 7)) {
414 void* arg = strdup(name + 7);
415 if(arg == 0) return -1;
416 ret = create_service_thread(reboot_service, arg);
418 } else if(!strncmp(name, "root:", 5)) {
419 ret = create_service_thread(restart_root_service, NULL);
420 } else if(!strncmp(name, "tcpip:", 6)) {
422 if (sscanf(name + 6, "%d", &port) == 0) {
425 ret = create_service_thread(restart_tcp_service, (void *)port);
426 } else if(!strncmp(name, "usb:", 4)) {
427 ret = create_service_thread(restart_usb_service, NULL);
431 } else if(!strncmp(name, "echo:", 5)){
432 ret = create_service_thread(echo_service, 0);
443 transport_type transport;
448 static void wait_for_state(int fd, void* cookie)
450 struct state_info* sinfo = cookie;
451 char* err = "unknown error";
453 D("wait_for_state %d\n", sinfo->state);
455 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
457 writex(fd, "OKAY", 4);
459 sendfailmsg(fd, err);
466 D("wait_for_state is done\n");
471 asocket* host_service_to_socket(const char* name, const char *serial)
473 if (!strcmp(name,"track-devices")) {
474 return create_device_tracker();
475 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
476 struct state_info* sinfo = malloc(sizeof(struct state_info));
479 sinfo->serial = strdup(serial);
481 sinfo->serial = NULL;
483 name += strlen("wait-for-");
485 if (!strncmp(name, "local", strlen("local"))) {
486 sinfo->transport = kTransportLocal;
487 sinfo->state = CS_DEVICE;
488 } else if (!strncmp(name, "usb", strlen("usb"))) {
489 sinfo->transport = kTransportUsb;
490 sinfo->state = CS_DEVICE;
491 } else if (!strncmp(name, "any", strlen("any"))) {
492 sinfo->transport = kTransportAny;
493 sinfo->state = CS_DEVICE;
499 int fd = create_service_thread(wait_for_state, sinfo);
500 return create_local_socket(fd);
504 #endif /* SDB_HOST */