2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
26 #define TRACE_TAG TRACE_SERVICES
28 #include "file_sync_service.h"
32 # include <netinet/in.h>
34 # include <sys/ioctl.h>
37 # include <sys/inotify.h>
38 # include "sdktools.h"
43 #include <system_info.h>
44 #include <tzplatform_config.h>
50 #include <sys/ioctl.h>
52 typedef struct stinfo stinfo;
55 void (*func)(int fd, void *cookie);
60 void *service_bootstrap_func(void *x)
63 sti->func(sti->fd, sti->cookie);
69 SDB_MUTEX_DEFINE( dns_lock );
71 static void dns_service(int fd, void *cookie)
73 char *hostname = cookie;
77 sdb_mutex_lock(&dns_lock);
78 hp = gethostbyname(hostname);
83 writex(fd, hp->h_addr, 4);
85 sdb_mutex_unlock(&dns_lock);
90 static int is_support_interactive_shell()
92 return (!strncmp(g_capabilities.intershell_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED)));
96 extern int recovery_mode;
98 static void recover_service(int s, void *cookie)
100 unsigned char buf[4096];
101 unsigned count = (unsigned) cookie;
104 fd = sdb_creat("/tmp/update", 0644);
111 unsigned xfer = (count > 4096) ? 4096 : count;
112 if(readx(s, buf, xfer)) break;
113 if(writex(fd, buf, xfer)) break;
118 writex(s, "OKAY", 4);
120 writex(s, "FAIL", 4);
125 fd = sdb_creat("/tmp/update.begin", 0644);
129 void restart_root_service(int fd, void *cookie)
132 char value[PROPERTY_VALUE_MAX];
135 snprintf(buf, sizeof(buf), "sdbd is already running as root\n");
136 writex(fd, buf, strlen(buf));
139 property_get("ro.debuggable", value, "");
140 if (strcmp(value, "1") != 0) {
141 snprintf(buf, sizeof(buf), "sdbd cannot run as root in production builds\n");
142 writex(fd, buf, strlen(buf));
147 property_set("service.sdb.root", "1");
148 snprintf(buf, sizeof(buf), "restarting sdbd as root\n");
149 writex(fd, buf, strlen(buf));
155 void restart_tcp_service(int fd, void *cookie)
158 char value[PROPERTY_VALUE_MAX];
159 int port = (int)cookie;
162 snprintf(buf, sizeof(buf), "invalid port\n");
163 writex(fd, buf, strlen(buf));
168 snprintf(value, sizeof(value), "%d", port);
169 property_set("service.sdb.tcp.port", value);
170 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
171 writex(fd, buf, strlen(buf));
175 static int is_support_rootonoff()
177 return (!strncmp(g_capabilities.rootonoff_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED)));
180 void rootshell_service(int fd, void *cookie)
183 char *mode = (char*) cookie;
185 if (!strcmp(mode, "on")) {
187 if (rootshell_mode == 1) {
188 //snprintf(buf, sizeof(buf), "Already changed to sdk user mode\n");
189 // do not show message
191 if (is_support_rootonoff()) {
193 //allows a permitted user to execute a command as the superuser
194 snprintf(buf, sizeof(buf), "Switched to 'root' account mode\n");
196 snprintf(buf, sizeof(buf), "Permission denied\n");
198 writex(fd, buf, strlen(buf));
201 D("need root permission for root shell: %d\n", getuid());
203 snprintf(buf, sizeof(buf), "Permission denied\n");
204 writex(fd, buf, strlen(buf));
206 } else if (!strcmp(mode, "off")) {
207 if (rootshell_mode == 1) {
209 snprintf(buf, sizeof(buf), "Switched to 'sdk user' account mode\n");
210 writex(fd, buf, strlen(buf));
213 snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode);
214 writex(fd, buf, strlen(buf));
216 D("set rootshell to %s\n", rootshell_mode == 1 ? "root" : SDK_USER_NAME);
221 enum tzplatform_get_env_error_status {
222 NO_ERROR_TZPLATFORM_ENV = 0,
223 ERROR_TZPLATFORM_ENV_GENERAL = 1,
224 ERROR_TZPLATFORM_ENV_INVALID_VARIABLES = 2,
227 void get_tzplatform_env(int fd, void *cookie) {
228 char buf[PATH_MAX] = { 0, };
229 char *env_name = (char*) cookie;
230 D("environment variable name: %s\n", env_name);
231 enum tzplatform_variable env_id = tzplatform_getid(env_name);
232 if (env_id != _TZPLATFORM_VARIABLES_INVALID_) {
233 const char *env_value = tzplatform_getenv(env_id);
235 D("environment value : %s\n", env_value);
236 snprintf(buf, sizeof(buf), "%d%s", NO_ERROR_TZPLATFORM_ENV, env_value);
238 D("failed to get environment value using tzplatform_getenv");
239 snprintf(buf, sizeof(buf), "%d", ERROR_TZPLATFORM_ENV_GENERAL);
242 D("environment name (%s) is invalid\n", env_name);
243 snprintf(buf, sizeof(buf), "%d", ERROR_TZPLATFORM_ENV_INVALID_VARIABLES);
245 writex(fd, buf, strlen(buf));
250 void restart_usb_service(int fd, void *cookie)
254 property_set("service.sdb.tcp.port", "0");
255 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
256 writex(fd, buf, strlen(buf));
260 void reboot_service(int fd, void *arg)
268 /* Attempt to unmount the SD card first.
269 * No need to bother checking for errors.
273 /* ask vdc to unmount it */
274 // prevent: Use of untrusted string value (TAINTED_STRING)
275 execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount",
276 getenv("EXTERNAL_STORAGE"), "force", NULL);
277 } else if (pid > 0) {
278 /* wait until vdc succeeds or fails */
279 waitpid(pid, &ret, 0);
282 ret = android_reboot(ANDROID_RB_RESTART2, 0, (char *) arg);
284 snprintf(buf, sizeof(buf), "reboot failed: %s errno:%d\n", errno);
285 writex(fd, buf, strlen(buf));
293 #define EVENT_SIZE ( sizeof (struct inotify_event) )
294 #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
295 #define CS_PATH tzplatform_getenv(TZ_SYS_CRASH)
297 void inoti_service(int fd, void *arg)
301 char buffer[BUF_LEN];
303 D( "inoti_service start\n");
304 ifd = inotify_init();
307 D( "inotify_init failed\n");
311 wd = inotify_add_watch( ifd, CS_PATH, IN_CREATE);
313 D("inotify_add_watch failed (errno :%d)\n", errno);
321 length = sdb_read( ifd, buffer, BUF_LEN );
324 D( "inoti read failed\n");
327 while (i >= 0 && i <= (length - EVENT_SIZE)) {
328 struct inotify_event *event = (struct inotify_event *) &buffer[i];
330 if (event->mask & IN_CREATE) {
331 if (!(event->mask & IN_ISDIR)) {
333 int len = asprintf(&cspath, "%s/%s", CS_PATH,
335 D( "The file %s was created.\n", cspath);
336 writex(fd, cspath, len);
337 if (cspath != NULL) {
343 if (i + EVENT_SIZE + event->len < event->len) { // in case of integer overflow
346 i += EVENT_SIZE + event->len;
351 inotify_rm_watch( ifd, wd );
354 D( "inoti_service end\n");
360 static void echo_service(int fd, void *cookie)
368 r = read(fd, buf, 4096);
369 if(r == 0) goto done;
371 if(errno == EINTR) continue;
384 if((r < 0) && (errno == EINTR)) continue;
393 static int create_service_thread(void (*func)(int, void *), void *cookie)
399 if(sdb_socketpair(s)) {
400 D("cannot create service socket pair\n");
404 sti = malloc(sizeof(stinfo));
405 if(sti == 0) fatal("cannot allocate stinfo");
407 sti->cookie = cookie;
410 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
414 D("cannot create service thread\n");
418 D("service thread started, %d:%d\n",s[0], s[1]);
424 static void redirect_and_exec(int pts, const char *cmd, char * const argv[], char * const envp[])
432 execve(cmd, argv, envp);
435 static int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[])
440 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
442 D("[ cannot open /dev/ptmx - errno:%d ]\n",errno);
445 if (fcntl(ptm, F_SETFD, FD_CLOEXEC) < 0) {
446 D("[ cannot set cloexec to /dev/ptmx - errno:%d ]\n",errno);
449 if(grantpt(ptm) || unlockpt(ptm) ||
450 ptsname_r(ptm, devname, sizeof(devname)) != 0 ){
451 D("[ trouble with /dev/ptmx - errno:%d ]\n", errno);
458 D("- fork failed: errno:%d -\n", errno);
468 pts = unix_open(devname, O_RDWR);
470 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
476 // set OOM adjustment to zero
479 //snprintf(text, sizeof text, "/proc/%d/oom_score_adj", getpid());
480 snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
481 int fd = sdb_open(text, O_WRONLY);
483 sdb_write(fd, "0", 1);
486 // FIXME: not supposed to be here
487 D("sdb: unable to open %s due to errno:%d\n", text, errno);
491 if (should_drop_privileges()) {
492 if (argv[2] != NULL && getuid() == 0 && request_plugin_verification(SDBD_CMD_VERIFY_ROOTCMD, argv[2])) {
494 D("sdb: executes root commands!!:%s\n", argv[2]);
496 set_sdk_user_privileges();
499 redirect_and_exec(pts, cmd, argv, envp);
500 fprintf(stderr, "- exec '%s' failed: (errno:%d) -\n",
504 // Don't set child's OOM adjustment to zero.
505 // Let the child do it itself, as sometimes the parent starts
506 // running before the child has a /proc/pid/oom_adj.
507 // """sdb: unable to open /proc/644/oom_adj""" seen in some logs.
511 #endif /* !SDB_HOST */
513 #define SHELL_COMMAND "/bin/sh"
514 #define LOGIN_COMMAND "/bin/login"
515 #define SUPER_USER "root"
516 #define LOGIN_CONFIG "/etc/login.defs"
519 static void subproc_waiter_service(int fd, void *cookie)
521 pid_t pid = (pid_t)cookie;
523 D("entered. fd=%d of pid=%d\n", fd, pid);
526 pid_t p = waitpid(pid, &status, 0);
528 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
529 if (WIFEXITED(status)) {
530 D("*** Exit code %d\n", WEXITSTATUS(status));
532 } else if (WIFSIGNALED(status)) {
533 D("*** Killed by signal %d\n", WTERMSIG(status));
536 D("*** Killed by unknown code %d\n", status);
541 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
542 if (SHELL_EXIT_NOTIFY_FD >=0) {
544 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
545 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
546 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
550 static void get_env(char *key, char **env)
556 fp = fopen (LOGIN_CONFIG, "r");
561 while (fgets(buf, (int) sizeof (buf), fp) != NULL) {
563 e = buf + (strlen(buf) - 1);
566 while(*e == ' ' || *e == '\n' || *e == '\t') {
571 while(*s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n')) {
575 // skip comment or null string
576 if (*s == '#' || *s == '\0') {
579 value = s + strcspn(s, " \t");
582 if(!strcmp(buf, key)) {
583 *env = strdup(value);
591 static int create_subproc_thread(const char *name, int lines, int columns)
598 char *trim_value = NULL;
600 char *envp[MAX_TOKENS];
603 memset(path, 0, sizeof(path));
604 memset(envp, 0, sizeof(envp));
606 envp[envp_cnt++] = strdup("TERM=linux");
607 envp[envp_cnt++] = strdup("DISPLAY=:0");
609 if (should_drop_privileges()) {
610 if (g_sdk_home_dir_env) {
611 envp[envp_cnt++] = strdup(g_sdk_home_dir_env);
613 envp[envp_cnt++] = strdup("HOME=/home/owner");
615 get_env("ENV_PATH", &value);
617 get_env("ENV_SUPATH", &value);
619 get_env("ENV_ROOTPATH", &value);
621 envp[envp_cnt++] = strdup("HOME=/root");
624 trim_value = str_trim(value);
625 if (trim_value != NULL) {
626 // if string is not including 'PATH=', append it.
627 if (strncmp(trim_value, "PATH", 4)) {
628 snprintf(path, sizeof(path), "PATH=%s", trim_value);
630 snprintf(path, sizeof(path), "%s", trim_value);
632 envp[envp_cnt++] = strdup(path);
634 snprintf(path, sizeof(path), "%s", value);
635 envp[envp_cnt++] = strdup(path);
640 /* get environment variables from plugin */
641 char *envp_plugin = NULL;
642 envp_plugin = malloc(SDBD_PLUGIN_OUTBUF_MAX);
643 if (envp_plugin == NULL) {
644 D("Cannot allocate the shell commnad buffer.");
647 memset(envp_plugin, 0, SDBD_PLUGIN_OUTBUF_MAX);
648 if (!request_plugin_cmd(SDBD_CMD_SHELL_ENVVAR, "", envp_plugin, SDBD_PLUGIN_OUTBUF_MAX)) {
649 D("Failed to convert the shell command. (%s)\n", name);
653 if(envp_plugin[0] != '\0') {
654 envp_cnt = tokenize_append(envp_plugin, "\n", envp, MAX_TOKENS, envp_cnt);
659 if(name) { // in case of shell execution directly
660 // Check the shell command validation.
661 if (!request_plugin_verification(SDBD_CMD_VERIFY_SHELLCMD, name)) {
662 D("This shell command is invalid. (%s)\n", name);
666 // Convert the shell command.
667 char *new_cmd = NULL;
668 new_cmd = malloc(SDBD_SHELL_CMD_MAX);
669 if(new_cmd == NULL) {
670 D("Cannot allocate the shell commnad buffer.");
674 memset(new_cmd, 0, SDBD_SHELL_CMD_MAX);
675 if(!request_plugin_cmd(SDBD_CMD_CONV_SHELLCMD, name, new_cmd, SDBD_SHELL_CMD_MAX)) {
676 D("Failed to convert the shell command. (%s)\n", name);
681 D("converted cmd : %s\n", new_cmd);
691 ret_fd = create_subprocess(SHELL_COMMAND, &pid, (char * const*)args, (char * const*)envp);
693 } else { // in case of shell interactively
694 // Check the capability for interactive shell support.
695 if (!is_support_interactive_shell()) {
696 D("This platform dose NOT support the interactive shell\n");
700 char * const args[] = {
705 ret_fd = create_subprocess(SHELL_COMMAND, &pid, (char * const*)args, (char * const*)envp);
706 #if 0 // FIXME: should call login command instead of /bin/sh
707 if (should_drop_privileges()) {
713 ret_fd = create_subprocess(SHELL_COMMAND, &pid, args, envp);
721 ret_fd = create_subprocess(LOGIN_COMMAND, &pid, args, envp);
726 /* free environment variables */
729 for(i = 0; i < envp_cnt; i++) {
731 D("envp[%d] = %s\n", i, envp[i]);
737 D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
740 D("cannot create service thread\n");
744 if (lines > 0 && columns > 0) {
745 D("shell size lines=%d, columns=%d\n", lines, columns);
746 struct winsize win_sz;
747 win_sz.ws_row = lines;
748 win_sz.ws_col = columns;
750 if (ioctl(ret_fd, TIOCSWINSZ, &win_sz) < 0) {
751 D("failed to sync window size.\n");
755 sti = malloc(sizeof(stinfo));
756 if(sti == 0) fatal("cannot allocate stinfo");
757 sti->func = subproc_waiter_service;
758 sti->cookie = (void*)pid;
761 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
764 D("cannot create service thread\n");
768 D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
772 static int create_sync_subprocess(void (*func)(int, void *), void* cookie) {
777 if(sdb_socketpair(s)) {
778 D("cannot create service socket pair\n");
788 } else if (pid > 0) {
790 // FIXME: do not wait child process hear
791 //waitpid(pid, &ret, 0);
794 D("- fork failed: errno:%d -\n", errno);
797 D("cannot create sync service sub process\n");
801 sti = malloc(sizeof(stinfo));
802 if(sti == 0) fatal("cannot allocate stinfo");
803 sti->func = subproc_waiter_service;
804 sti->cookie = (void*)pid;
807 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
810 printf("cannot create service monitor thread\n");
814 D("service process started, fd=%d pid=%d\n",s[0], pid);
818 static int create_syncproc_thread()
822 ret_fd = create_sync_subprocess(file_sync_service, NULL);
823 // FIXME: file missing bug when root on mode
825 if (should_drop_privileges()) {
826 ret_fd = create_sync_subprocess(file_sync_service, NULL);
828 ret_fd = create_service_thread(file_sync_service, NULL);
837 static void get_platforminfo(int fd, void *cookie) {
841 s_strncpy(sysinfo.platform_info_version, INFO_VERSION, strlen(INFO_VERSION));
843 int r = system_info_get_platform_string("http://tizen.org/system/model_name", &value);
844 if (r != SYSTEM_INFO_ERROR_NONE) {
845 s_strncpy(sysinfo.model_name, UNKNOWN, strlen(UNKNOWN));
846 D("fail to get system model:%d\n", errno);
848 s_strncpy(sysinfo.model_name, value, sizeof(sysinfo.model_name));
849 D("returns model_name:%s\n", value);
855 r = system_info_get_platform_string("http://tizen.org/system/platform.name", &value);
856 if (r != SYSTEM_INFO_ERROR_NONE) {
857 s_strncpy(sysinfo.platform_name, UNKNOWN, strlen(UNKNOWN));
858 D("fail to get platform name:%d\n", errno);
860 s_strncpy(sysinfo.platform_name, value, sizeof(sysinfo.platform_name));
861 D("returns platform_name:%s\n", value);
868 // FIXME: the result is different when using SYSTEM_INFO_KEY_TIZEN_VERSION_NAME
869 r = system_info_get_platform_string("tizen.org/feature/platform.version", &value);
870 if (r != SYSTEM_INFO_ERROR_NONE) {
871 s_strncpy(sysinfo.platform_version, UNKNOWN, strlen(UNKNOWN));
872 D("fail to get platform version:%d\n", errno);
874 s_strncpy(sysinfo.platform_version, value, sizeof(sysinfo.platform_version));
875 D("returns platform_version:%s\n", value);
881 r = system_info_get_platform_string("tizen.org/feature/profile", &value);
882 if (r != SYSTEM_INFO_ERROR_NONE) {
883 s_strncpy(sysinfo.profile_name, UNKNOWN, strlen(UNKNOWN));
884 D("fail to get profile name:%d\n", errno);
886 s_strncpy(sysinfo.profile_name, value, sizeof(sysinfo.profile_name));
887 D("returns profile name:%s\n", value);
893 writex(fd, &sysinfo, sizeof(pinfo));
898 static int put_key_value_string(char* buf, int offset, int buf_size, char* key, char* value) {
900 if ((len = snprintf(buf+offset, buf_size-offset, "%s:%s\n", key, value)) > 0) {
906 static void get_capability(int fd, void *cookie) {
907 char cap_buffer[CAPBUF_SIZE] = {0,};
910 // Secure protocol support
911 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
912 "secure_protocol", g_capabilities.secure_protocol);
914 // Interactive shell support
915 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
916 "intershell_support", g_capabilities.intershell_support);
918 // File push/pull support
919 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
920 "filesync_support", g_capabilities.filesync_support);
922 // USB protocol support
923 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
924 "usbproto_support", g_capabilities.usbproto_support);
926 // Socket protocol support
927 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
928 "sockproto_support", g_capabilities.sockproto_support);
930 // Window size synchronization support
931 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
932 "syncwinsz_support", g_capabilities.syncwinsz_support);
934 // Root command support
935 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
936 "rootonoff_support", g_capabilities.rootonoff_support);
939 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
940 "zone_support", g_capabilities.zone_support);
942 // Multi-User support
943 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
944 "multiuser_support", g_capabilities.multiuser_support);
946 // CPU Architecture of model
947 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
948 "cpu_arch", g_capabilities.cpu_arch);
951 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
952 "sdk_toolpath", g_capabilities.sdk_toolpath);
955 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
956 "profile_name", g_capabilities.profile_name);
959 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
960 "vendor_name", g_capabilities.vendor_name);
962 // Target name of the launch possible
963 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
964 "can_launch", g_capabilities.can_launch);
967 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
968 "platform_version", g_capabilities.platform_version);
971 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
972 "product_version", g_capabilities.product_version);
975 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
976 "sdbd_version", g_capabilities.sdbd_version);
978 // Sdbd plugin version
979 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
980 "sdbd_plugin_version", g_capabilities.sdbd_plugin_version);
982 // Capability version
983 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
984 "sdbd_cap_version", g_capabilities.sdbd_cap_version);
987 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
988 "log_enable", g_capabilities.log_enable);
991 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
992 "log_path", g_capabilities.log_path);
994 offset++; // for '\0' character
996 writex(fd, &offset, sizeof(uint16_t));
997 writex(fd, cap_buffer, offset);
1002 static void sync_windowsize(int fd, void *cookie) {
1003 int id, lines, columns;
1004 char *size_info = cookie;
1007 if (sscanf(size_info, "%d:%d:%d", &id, &lines, &columns) == 3) {
1008 D("window size information: id=%d, lines=%d, columns=%d\n", id, lines, columns);
1010 if((s = find_local_socket(id))) {
1011 struct winsize win_sz;
1012 win_sz.ws_row = lines;
1013 win_sz.ws_col = columns;
1015 if (ioctl(s->fd, TIOCSWINSZ, &win_sz) < 0) {
1016 D("failed to sync window size.\n");
1019 D("success to sync window size.\n");
1023 const unsigned COMMAND_TIMEOUT = 10000;
1024 void get_boot(int fd, void *cookie) {
1025 char buf[2] = { 0, };
1027 int interval = 1000;
1028 while (time < COMMAND_TIMEOUT) {
1029 if (booting_done == 1) {
1030 D("get_boot:platform booting is done\n");
1031 snprintf(buf, sizeof(buf), "%s", "1");
1034 D("get_boot:platform booting is in progress\n");
1035 sdb_sleep_ms(interval);
1038 writex(fd, buf, strlen(buf));
1042 int service_to_fd(const char *name)
1046 if(!strncmp(name, "tcp:", 4)) {
1047 int port = atoi(name + 4);
1048 name = strchr(name + 4, ':');
1051 ret = socket_ifr_client(port , SOCK_STREAM, "eth0");
1053 ret = socket_ifr_client(port , SOCK_STREAM, "usb0");
1055 if (ifconfig(SDB_FORWARD_IFNAME, SDB_FORWARD_INTERNAL_IP, SDB_FORWARD_INTERNAL_MASK, 1) == 0) {
1056 ret = socket_ifr_client(port , SOCK_STREAM, SDB_FORWARD_IFNAME);
1061 ret = socket_loopback_client(port, SOCK_STREAM);
1064 disable_tcp_nagle(ret);
1068 sdb_mutex_lock(&dns_lock);
1069 ret = socket_network_client(name + 1, port, SOCK_STREAM);
1070 sdb_mutex_unlock(&dns_lock);
1075 #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
1076 } else if(!strncmp(name, "local:", 6)) {
1077 ret = socket_local_client(name + 6,
1078 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
1079 } else if(!strncmp(name, "localreserved:", 14)) {
1080 ret = socket_local_client(name + 14,
1081 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
1082 } else if(!strncmp(name, "localabstract:", 14)) {
1083 ret = socket_local_client(name + 14,
1084 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
1085 } else if(!strncmp(name, "localfilesystem:", 16)) {
1086 ret = socket_local_client(name + 16,
1087 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
1090 } else if(!strncmp("dns:", name, 4)){
1091 char *n = strdup(name + 4);
1092 if(n == 0) return -1;
1093 ret = create_service_thread(dns_service, n);
1094 #else /* !SDB_HOST */
1095 }/* else if(!strncmp("dev:", name, 4)) {// tizen specific
1096 ret = unix_open(name + 4, O_RDWR);
1097 } else if(!strncmp(name, "framebuffer:", 12)) {
1098 ret = create_service_thread(framebuffer_service, 0);
1099 } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
1100 ret = create_service_thread(recover_service, (void*) atoi(name + 8));
1101 } else if (!strncmp(name, "jdwp:", 5)) {
1102 ret = create_jdwp_connection_fd(atoi(name+5));
1103 } else if (!strncmp(name, "log:", 4)) {
1104 ret = create_service_thread(log_service, get_log_file_path(name + 4));
1105 }*/ else if(!HOST && !strncmp(name, "shell:", 6)) {
1107 ret = create_subproc_thread(name + 6, 0, 0);
1109 ret = create_subproc_thread(NULL, 0, 0);
1111 } else if(!strncmp(name, "eshell:", 7)) {
1113 if (sscanf(name+7, "%d:%d", &lines, &columns) == 2) {
1114 ret = create_subproc_thread(NULL, lines, columns);
1116 } else if(!strncmp(name, "sync:", 5)) {
1117 //ret = create_service_thread(file_sync_service, NULL);
1118 ret = create_syncproc_thread();
1119 }/* else if(!strncmp(name, "remount:", 8)) {
1120 ret = create_service_thread(remount_service, NULL);
1121 } else if(!strncmp(name, "reboot:", 7)) {
1122 void* arg = strdup(name + 7);
1123 if(arg == 0) return -1;
1124 ret = create_service_thread(reboot_service, arg);
1125 } else if(!strncmp(name, "root:", 5)) {
1126 ret = create_service_thread(restart_root_service, NULL);
1127 } else if(!strncmp(name, "backup:", 7)) {
1128 char* arg = strdup(name+7);
1129 if (arg == NULL) return -1;
1130 ret = backup_service(BACKUP, arg);
1131 } else if(!strncmp(name, "restore:", 8)) {
1132 ret = backup_service(RESTORE, NULL);
1133 }*/ else if(!strncmp(name, "root:", 5)) {
1134 char* service_name = NULL;
1136 service_name = strdup(name+5);
1137 ret = create_service_thread(rootshell_service, (void *)(service_name));
1138 } else if(!strncmp(name, "cs:", 5)) {
1139 ret = create_service_thread(inoti_service, NULL);
1141 } else if(!strncmp(name, "sysinfo:", 8)){
1142 ret = create_service_thread(get_platforminfo, 0);
1143 } else if(!strncmp(name, "capability:", 11)){
1144 ret = create_service_thread(get_capability, 0);
1145 } else if(!strncmp(name, "boot:", 5)){
1146 if (is_emulator()) {
1147 ret = create_service_thread(get_boot, 0);
1149 } else if(!strncmp(name, "shellconf:", 10)){
1150 if(!strncmp(name+10, "syncwinsz:", 10)){
1151 ret = create_service_thread(sync_windowsize, (void *)name+20);
1153 } else if(!strncmp(name, "tzplatformenv:", 14)) {
1154 char* env_variable = NULL;
1155 env_variable = strdup(name+14);
1156 ret = create_service_thread(get_tzplatform_env, (void *)(env_variable));
1160 if (close_on_exec(ret) < 0) {
1161 D("failed to close fd exec\n");
1169 transport_type transport;
1174 static void wait_for_state(int fd, void* cookie)
1176 struct state_info* sinfo = cookie;
1177 char* err = "unknown error";
1179 D("wait_for_state %d\n", sinfo->state);
1181 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
1183 writex(fd, "OKAY", 4);
1185 sendfailmsg(fd, err);
1189 free(sinfo->serial);
1192 D("wait_for_state is done\n");
1197 asocket* host_service_to_socket(const char* name, const char *serial)
1199 if (!strcmp(name,"track-devices")) {
1200 return create_device_tracker();
1201 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
1202 struct state_info* sinfo = malloc(sizeof(struct state_info));
1205 sinfo->serial = strdup(serial);
1207 sinfo->serial = NULL;
1209 name += strlen("wait-for-");
1211 if (!strncmp(name, "local", strlen("local"))) {
1212 sinfo->transport = kTransportLocal;
1213 sinfo->state = CS_DEVICE;
1214 } else if (!strncmp(name, "usb", strlen("usb"))) {
1215 sinfo->transport = kTransportUsb;
1216 sinfo->state = CS_DEVICE;
1217 } else if (!strncmp(name, "any", strlen("any"))) {
1218 sinfo->transport = kTransportAny;
1219 sinfo->state = CS_DEVICE;
1225 int fd = create_service_thread(wait_for_state, sinfo);
1226 return create_local_socket(fd);
1230 #endif /* SDB_HOST */