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
30 #include "file_sync_service.h"
34 # include <netinet/in.h>
36 # include <sys/ioctl.h>
39 # include <sys/inotify.h>
40 # include "sdktools.h"
45 #include <system_info.h>
46 #include <tzplatform_config.h>
52 #include <sys/ioctl.h>
54 #include "sdbd_plugin.h"
57 typedef struct stinfo stinfo;
60 void (*func)(int fd, void *cookie);
65 void *service_bootstrap_func(void *x)
68 sti->func(sti->fd, sti->cookie);
74 SDB_MUTEX_DEFINE( dns_lock );
76 static void dns_service(int fd, void *cookie)
78 char *hostname = cookie;
82 sdb_mutex_lock(&dns_lock);
83 hp = gethostbyname(hostname);
88 writex(fd, hp->h_addr, 4);
90 sdb_mutex_unlock(&dns_lock);
95 static int is_support_interactive_shell()
97 return (!strncmp(g_capabilities.intershell_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED)));
101 extern int recovery_mode;
103 static void recover_service(int s, void *cookie)
105 unsigned char buf[4096];
106 unsigned count = (unsigned) cookie;
109 fd = sdb_creat("/tmp/update", 0644);
116 unsigned xfer = (count > 4096) ? 4096 : count;
117 if(readx(s, buf, xfer)) break;
118 if(writex(fd, buf, xfer)) break;
123 writex(s, "OKAY", 4);
125 writex(s, "FAIL", 4);
130 fd = sdb_creat("/tmp/update.begin", 0644);
134 void restart_root_service(int fd, void *cookie)
137 char value[PROPERTY_VALUE_MAX];
140 snprintf(buf, sizeof(buf), "sdbd is already running as root\n");
141 writex(fd, buf, strlen(buf));
144 property_get("ro.debuggable", value, "");
145 if (strcmp(value, "1") != 0) {
146 snprintf(buf, sizeof(buf), "sdbd cannot run as root in production builds\n");
147 writex(fd, buf, strlen(buf));
152 property_set("service.sdb.root", "1");
153 snprintf(buf, sizeof(buf), "restarting sdbd as root\n");
154 writex(fd, buf, strlen(buf));
160 void restart_tcp_service(int fd, void *cookie)
163 char value[PROPERTY_VALUE_MAX];
164 int port = (int)cookie;
167 snprintf(buf, sizeof(buf), "invalid port\n");
168 writex(fd, buf, strlen(buf));
173 snprintf(value, sizeof(value), "%d", port);
174 property_set("service.sdb.tcp.port", value);
175 snprintf(buf, sizeof(buf), "restarting in TCP mode port: %d\n", port);
176 writex(fd, buf, strlen(buf));
180 static int is_support_rootonoff()
182 return (!strncmp(g_capabilities.rootonoff_support, PLUGIN_RET_ENABLED, strlen(PLUGIN_RET_ENABLED)));
185 void rootshell_service(int fd, void *cookie)
188 char *mode = (char*) cookie;
190 if (!strcmp(mode, "on")) {
192 if (rootshell_mode == 1) {
193 //snprintf(buf, sizeof(buf), "Already changed to sdk user mode\n");
194 // do not show message
196 if (is_support_rootonoff()) {
198 //allows a permitted user to execute a command as the superuser
199 snprintf(buf, sizeof(buf), "Switched to 'root' account mode\n");
201 snprintf(buf, sizeof(buf), "Permission denied\n");
203 writex(fd, buf, strlen(buf));
206 D("need root permission for root shell: %d\n", getuid());
208 snprintf(buf, sizeof(buf), "Permission denied\n");
209 writex(fd, buf, strlen(buf));
211 } else if (!strcmp(mode, "off")) {
212 if (rootshell_mode == 1) {
214 snprintf(buf, sizeof(buf), "Switched to 'sdk user' account mode\n");
215 writex(fd, buf, strlen(buf));
218 snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode);
219 writex(fd, buf, strlen(buf));
221 D("set rootshell to %s\n", rootshell_mode == 1 ? "root" : SDK_USER_NAME);
226 enum tzplatform_get_env_error_status {
227 NO_ERROR_TZPLATFORM_ENV = 0,
228 ERROR_TZPLATFORM_ENV_GENERAL = 1,
229 ERROR_TZPLATFORM_ENV_INVALID_VARIABLES = 2,
232 void get_tzplatform_env(int fd, void *cookie) {
233 char buf[PATH_MAX] = { 0, };
234 char *env_name = (char*) cookie;
235 D("environment variable name: %s\n", env_name);
236 enum tzplatform_variable env_id = tzplatform_getid(env_name);
237 if (env_id != _TZPLATFORM_VARIABLES_INVALID_) {
238 const char *env_value = tzplatform_getenv(env_id);
240 D("environment value : %s\n", env_value);
241 snprintf(buf, sizeof(buf), "%d%s", NO_ERROR_TZPLATFORM_ENV, env_value);
243 D("failed to get environment value using tzplatform_getenv");
244 snprintf(buf, sizeof(buf), "%d", ERROR_TZPLATFORM_ENV_GENERAL);
247 D("environment name (%s) is invalid\n", env_name);
248 snprintf(buf, sizeof(buf), "%d", ERROR_TZPLATFORM_ENV_INVALID_VARIABLES);
250 writex(fd, buf, strlen(buf));
255 void restart_usb_service(int fd, void *cookie)
259 property_set("service.sdb.tcp.port", "0");
260 snprintf(buf, sizeof(buf), "restarting in USB mode\n");
261 writex(fd, buf, strlen(buf));
265 void reboot_service(int fd, void *arg)
273 /* Attempt to unmount the SD card first.
274 * No need to bother checking for errors.
278 /* ask vdc to unmount it */
279 // prevent: Use of untrusted string value (TAINTED_STRING)
280 execl("/system/bin/vdc", "/system/bin/vdc", "volume", "unmount",
281 getenv("EXTERNAL_STORAGE"), "force", NULL);
282 } else if (pid > 0) {
283 /* wait until vdc succeeds or fails */
284 waitpid(pid, &ret, 0);
287 ret = android_reboot(ANDROID_RB_RESTART2, 0, (char *) arg);
289 snprintf(buf, sizeof(buf), "reboot failed: %s errno:%d\n", errno);
290 writex(fd, buf, strlen(buf));
298 #define EVENT_SIZE ( sizeof (struct inotify_event) )
299 #define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
300 #define CS_PATH tzplatform_getenv(TZ_SYS_CRASH)
302 void inoti_service(int fd, void *arg)
306 char buffer[BUF_LEN];
308 D( "inoti_service start\n");
309 ifd = inotify_init();
312 D( "inotify_init failed\n");
317 wd = inotify_add_watch( ifd, CS_PATH, IN_CREATE);
319 D("inotify_add_watch failed (errno :%d)\n", errno);
327 length = sdb_read( ifd, buffer, BUF_LEN );
330 D( "inoti read failed\n");
333 while (i >= 0 && i <= (length - EVENT_SIZE)) {
334 struct inotify_event *event = (struct inotify_event *) &buffer[i];
336 if (event->mask & IN_CREATE) {
337 if (!(event->mask & IN_ISDIR)) {
339 int len = asprintf(&cspath, "%s/%s", CS_PATH,
341 D( "The file %s was created.\n", cspath);
342 writex(fd, cspath, len);
343 if (cspath != NULL) {
349 if (i + EVENT_SIZE + event->len < event->len) { // in case of integer overflow
352 i += EVENT_SIZE + event->len;
357 inotify_rm_watch( ifd, wd );
360 D( "inoti_service end\n");
366 static void echo_service(int fd, void *cookie)
374 r = read(fd, buf, 4096);
375 if(r == 0) goto done;
377 if(errno == EINTR) continue;
390 if((r < 0) && (errno == EINTR)) continue;
399 static int create_service_thread(void (*func)(int, void *), void *cookie)
405 if(sdb_socketpair(s)) {
406 D("cannot create service socket pair\n");
410 sti = malloc(sizeof(stinfo));
411 if(sti == 0) fatal("cannot allocate stinfo");
413 sti->cookie = cookie;
416 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
420 D("cannot create service thread\n");
424 D("service thread started, %d:%d\n",s[0], s[1]);
430 static void redirect_and_exec(int pts, const char *cmd, char * const argv[], char * const envp[])
438 execve(cmd, argv, envp);
441 int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[])
446 ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
448 D("[ cannot open /dev/ptmx - errno:%d ]\n",errno);
451 if (fcntl(ptm, F_SETFD, FD_CLOEXEC) < 0) {
452 D("[ cannot set cloexec to /dev/ptmx - errno:%d ]\n",errno);
455 if(grantpt(ptm) || unlockpt(ptm) ||
456 ptsname_r(ptm, devname, sizeof(devname)) != 0 ){
457 D("[ trouble with /dev/ptmx - errno:%d ]\n", errno);
464 D("- fork failed: errno:%d -\n", errno);
474 pts = unix_open(devname, O_RDWR);
476 fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
482 // set OOM adjustment to zero
485 //snprintf(text, sizeof text, "/proc/%d/oom_score_adj", getpid());
486 snprintf(text, sizeof text, "/proc/%d/oom_adj", getpid());
487 int fd = sdb_open(text, O_WRONLY);
489 sdb_write(fd, "0", 1);
492 // FIXME: not supposed to be here
493 D("sdb: unable to open %s due to errno:%d\n", text, errno);
497 if (should_drop_privileges()) {
498 if (argv[2] != NULL && getuid() == 0 && request_validity_to_plugin(PLUGIN_SYNC_CMD_VERIFY_ROOTCMD, argv[2])) {
500 D("sdb: executes root commands!!:%s\n", argv[2]);
502 if (getuid() != g_sdk_user_id && set_sdk_user_privileges() < 0) {
503 fprintf(stderr, "failed to set SDK user privileges\n");
508 redirect_and_exec(pts, cmd, argv, envp);
509 fprintf(stderr, "- exec '%s' failed: (errno:%d) -\n",
513 // Don't set child's OOM adjustment to zero.
514 // Let the child do it itself, as sometimes the parent starts
515 // running before the child has a /proc/pid/oom_adj.
516 // """sdb: unable to open /proc/644/oom_adj""" seen in some logs.
520 #endif /* !SDB_HOST */
522 #define LOGIN_COMMAND "/bin/login"
523 #define SUPER_USER "root"
524 #define LOGIN_CONFIG "/etc/login.defs"
527 static void subproc_waiter_service(int fd, void *cookie)
529 pid_t pid = (pid_t)cookie;
531 D("entered. fd=%d of pid=%d\n", fd, pid);
534 pid_t p = waitpid(pid, &status, 0);
536 D("fd=%d, post waitpid(pid=%d) status=%04x\n", fd, p, status);
537 if (WIFEXITED(status)) {
538 D("*** Exit code %d\n", WEXITSTATUS(status));
540 } else if (WIFSIGNALED(status)) {
541 D("*** Killed by signal %d\n", WTERMSIG(status));
544 D("*** Killed by unknown code %d\n", status);
549 D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
550 if (SHELL_EXIT_NOTIFY_FD >=0) {
552 res = writex(SHELL_EXIT_NOTIFY_FD, &fd, sizeof(fd));
553 D("notified shell exit via fd=%d for pid=%d res=%d errno=%d\n",
554 SHELL_EXIT_NOTIFY_FD, pid, res, errno);
558 void get_env(char *key, char **env)
564 fp = fopen (LOGIN_CONFIG, "r");
569 while (fgets(buf, (int) sizeof (buf), fp) != NULL) {
571 e = buf + (strlen(buf) - 1);
574 while( (e > s) && (*e == ' ' || *e == '\n' || *e == '\t')) {
579 while(*s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n')) {
583 // skip comment or null string
584 if (*s == '#' || *s == '\0') {
587 value = s + strcspn(s, " \t");
590 if(!strcmp(buf, key)) {
591 *env = strdup(value);
599 static int create_subproc_thread(const char *name, int lines, int columns)
606 char *trim_value = NULL;
608 char *envp[MAX_TOKENS];
611 memset(path, 0, sizeof(path));
612 memset(envp, 0, sizeof(envp));
614 envp[envp_cnt++] = strdup("TERM=linux");
615 envp[envp_cnt++] = strdup("DISPLAY=:0");
617 if (should_drop_privileges()) {
618 if (g_sdk_home_dir_env) {
619 envp[envp_cnt++] = strdup(g_sdk_home_dir_env);
621 envp[envp_cnt++] = strdup("HOME=/home/owner");
623 get_env("ENV_PATH", &value);
625 get_env("ENV_SUPATH", &value);
627 get_env("ENV_ROOTPATH", &value);
629 envp[envp_cnt++] = strdup("HOME=/root");
632 trim_value = str_trim(value);
633 if (trim_value != NULL) {
634 // if string is not including 'PATH=', append it.
635 if (strncmp(trim_value, "PATH", 4)) {
636 snprintf(path, sizeof(path), "PATH=%s", trim_value);
638 snprintf(path, sizeof(path), "%s", trim_value);
640 envp[envp_cnt++] = strdup(path);
642 snprintf(path, sizeof(path), "%s", value);
643 envp[envp_cnt++] = strdup(path);
648 /* get environment variables from plugin */
649 char *envp_plugin = NULL;
650 envp_plugin = malloc(SDBD_PLUGIN_OUTBUF_MAX);
651 if (envp_plugin == NULL) {
652 D("Cannot allocate the shell commnad buffer.");
655 memset(envp_plugin, 0, SDBD_PLUGIN_OUTBUF_MAX);
656 if (!request_plugin_cmd(SDBD_CMD_SHELL_ENVVAR, "", envp_plugin, SDBD_PLUGIN_OUTBUF_MAX)) {
657 D("Failed to convert the shell command. (%s)\n", name);
661 if(envp_plugin[0] != '\0') {
662 envp_cnt = tokenize_append(envp_plugin, "\n", envp, MAX_TOKENS, envp_cnt);
667 /* Last element of envp must be the NULL-terminator to prevent execvp fail */
668 envp[envp_cnt] = NULL;
670 if(name) { // in case of shell execution directly
671 // Check the shell command validation.
672 if (!request_validity_to_plugin(PLUGIN_SYNC_CMD_VERIFY_SHELLCMD, name)) {
673 D("This shell command is invalid. (%s)\n", name);
677 // Convert the shell command.
678 char *new_cmd = NULL;
679 new_cmd = malloc(SDBD_SHELL_CMD_MAX);
680 if(new_cmd == NULL) {
681 D("Cannot allocate the shell commnad buffer.");
685 memset(new_cmd, 0, SDBD_SHELL_CMD_MAX);
686 if(!request_conversion_to_plugin(PLUGIN_SYNC_CMD_CONVERT_SHELLCMD, name, new_cmd, SDBD_SHELL_CMD_MAX)) {
687 D("Failed to convert the shell command. (%s)\n", name);
692 D("converted cmd : %s\n", new_cmd);
702 ret_fd = create_subprocess(SHELL_COMMAND, &pid, (char * const*)args, (char * const*)envp);
704 } else { // in case of shell interactively
705 // Check the capability for interactive shell support.
706 if (!is_support_interactive_shell()) {
707 D("This platform dose NOT support the interactive shell\n");
711 char * const args[] = {
716 ret_fd = create_subprocess(SHELL_COMMAND, &pid, (char * const*)args, (char * const*)envp);
717 #if 0 // FIXME: should call login command instead of /bin/sh
718 if (should_drop_privileges()) {
724 ret_fd = create_subprocess(SHELL_COMMAND, &pid, args, envp);
732 ret_fd = create_subprocess(LOGIN_COMMAND, &pid, args, envp);
737 /* free environment variables */
740 for(i = 0; i < envp_cnt; i++) {
742 D("envp[%d] = %s\n", i, envp[i]);
748 D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
751 D("cannot create service thread\n");
755 if (lines > 0 && columns > 0) {
756 D("shell size lines=%d, columns=%d\n", lines, columns);
757 struct winsize win_sz;
758 win_sz.ws_row = lines;
759 win_sz.ws_col = columns;
761 if (ioctl(ret_fd, TIOCSWINSZ, &win_sz) < 0) {
762 D("failed to sync window size.\n");
766 sti = malloc(sizeof(stinfo));
767 if(sti == 0) fatal("cannot allocate stinfo");
768 sti->func = subproc_waiter_service;
769 sti->cookie = (void*)pid;
772 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
775 D("cannot create service thread\n");
779 D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
783 static int create_sync_subprocess(void (*func)(int, void *), void* cookie) {
788 if(sdb_socketpair(s)) {
789 D("cannot create service socket pair\n");
799 } else if (pid > 0) {
801 // FIXME: do not wait child process hear
802 //waitpid(pid, &ret, 0);
805 D("- fork failed: errno:%d -\n", errno);
808 D("cannot create sync service sub process\n");
812 sti = malloc(sizeof(stinfo));
813 if(sti == 0) fatal("cannot allocate stinfo");
814 sti->func = subproc_waiter_service;
815 sti->cookie = (void*)pid;
818 if(sdb_thread_create( &t, service_bootstrap_func, sti)){
821 printf("cannot create service monitor thread\n");
825 D("service process started, fd=%d pid=%d\n",s[0], pid);
829 static int create_syncproc_thread()
833 ret_fd = create_sync_subprocess(file_sync_service, NULL);
834 // FIXME: file missing bug when root on mode
836 if (should_drop_privileges()) {
837 ret_fd = create_sync_subprocess(file_sync_service, NULL);
839 ret_fd = create_service_thread(file_sync_service, NULL);
847 static void get_platforminfo(int fd, void *cookie) {
851 s_strncpy(sysinfo.platform_info_version, INFO_VERSION, strlen(INFO_VERSION));
853 int r = system_info_get_platform_string("http://tizen.org/system/model_name", &value);
854 if (r != SYSTEM_INFO_ERROR_NONE) {
855 s_strncpy(sysinfo.model_name, UNKNOWN, strlen(UNKNOWN));
856 D("fail to get system model:%d\n", errno);
858 s_strncpy(sysinfo.model_name, value, sizeof(sysinfo.model_name));
859 D("returns model_name:%s\n", value);
865 r = system_info_get_platform_string("http://tizen.org/system/platform.name", &value);
866 if (r != SYSTEM_INFO_ERROR_NONE) {
867 s_strncpy(sysinfo.platform_name, UNKNOWN, strlen(UNKNOWN));
868 D("fail to get platform name:%d\n", errno);
870 s_strncpy(sysinfo.platform_name, value, sizeof(sysinfo.platform_name));
871 D("returns platform_name:%s\n", value);
878 // FIXME: the result is different when using SYSTEM_INFO_KEY_TIZEN_VERSION_NAME
879 r = system_info_get_platform_string("tizen.org/feature/platform.version", &value);
880 if (r != SYSTEM_INFO_ERROR_NONE) {
881 s_strncpy(sysinfo.platform_version, UNKNOWN, strlen(UNKNOWN));
882 D("fail to get platform version:%d\n", errno);
884 s_strncpy(sysinfo.platform_version, value, sizeof(sysinfo.platform_version));
885 D("returns platform_version:%s\n", value);
891 r = system_info_get_platform_string("tizen.org/feature/profile", &value);
892 if (r != SYSTEM_INFO_ERROR_NONE) {
893 s_strncpy(sysinfo.profile_name, UNKNOWN, strlen(UNKNOWN));
894 D("fail to get profile name:%d\n", errno);
896 s_strncpy(sysinfo.profile_name, value, sizeof(sysinfo.profile_name));
897 D("returns profile name:%s\n", value);
903 writex(fd, &sysinfo, sizeof(pinfo));
908 static int put_key_value_string(char* buf, int offset, int buf_size, char* key, char* value) {
910 if ((len = snprintf(buf+offset, buf_size-offset, "%s:%s\n", key, value)) > 0) {
916 static void get_capability(int fd, void *cookie) {
917 char cap_buffer[CAPBUF_SIZE] = {0,};
920 // Secure protocol support
921 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
922 "secure_protocol", g_capabilities.secure_protocol);
924 // Interactive shell support
925 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
926 "intershell_support", g_capabilities.intershell_support);
928 // File push/pull support
929 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
930 "filesync_support", g_capabilities.filesync_support);
932 // USB protocol support
933 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
934 "usbproto_support", g_capabilities.usbproto_support);
936 // Socket protocol support
937 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
938 "sockproto_support", g_capabilities.sockproto_support);
940 // Window size synchronization support
941 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
942 "syncwinsz_support", g_capabilities.syncwinsz_support);
944 // Root command support
945 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
946 "rootonoff_support", g_capabilities.rootonoff_support);
949 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
950 "zone_support", g_capabilities.zone_support);
952 // Multi-User support
953 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
954 "multiuser_support", g_capabilities.multiuser_support);
956 // CPU Architecture of model
957 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
958 "cpu_arch", g_capabilities.cpu_arch);
961 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
962 "sdk_toolpath", g_capabilities.sdk_toolpath);
965 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
966 "profile_name", g_capabilities.profile_name);
969 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
970 "vendor_name", g_capabilities.vendor_name);
972 // Target name of the launch possible
973 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
974 "can_launch", g_capabilities.can_launch);
977 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
978 "platform_version", g_capabilities.platform_version);
981 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
982 "product_version", g_capabilities.product_version);
985 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
986 "sdbd_version", g_capabilities.sdbd_version);
988 // Sdbd plugin version
989 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
990 "sdbd_plugin_version", g_capabilities.sdbd_plugin_version);
992 // Capability version
993 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
994 "sdbd_cap_version", g_capabilities.sdbd_cap_version);
997 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
998 "log_enable", g_capabilities.log_enable);
1001 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
1002 "log_path", g_capabilities.log_path);
1004 // Application command support
1005 offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
1006 "appcmd_support", g_capabilities.appcmd_support);
1008 offset++; // for '\0' character
1010 writex(fd, &offset, sizeof(uint16_t));
1011 writex(fd, cap_buffer, offset);
1016 static void sync_windowsize(int fd, void *cookie) {
1017 int id, lines, columns;
1018 char *size_info = cookie;
1021 if (sscanf(size_info, "%d:%d:%d", &id, &lines, &columns) == 3) {
1022 D("window size information: id=%d, lines=%d, columns=%d\n", id, lines, columns);
1024 if((s = find_local_socket(id))) {
1025 struct winsize win_sz;
1026 win_sz.ws_row = lines;
1027 win_sz.ws_col = columns;
1029 if (ioctl(s->fd, TIOCSWINSZ, &win_sz) < 0) {
1030 D("failed to sync window size.\n");
1033 D("success to sync window size.\n");
1037 const unsigned COMMAND_TIMEOUT = 10000;
1038 void get_boot(int fd, void *cookie) {
1039 char buf[2] = { 0, };
1041 int interval = 1000;
1042 while (time < COMMAND_TIMEOUT) {
1043 if (booting_done == 1) {
1044 D("get_boot:platform booting is done\n");
1045 snprintf(buf, sizeof(buf), "%s", "1");
1048 D("get_boot:platform booting is in progress\n");
1049 sdb_sleep_ms(interval);
1052 writex(fd, buf, strlen(buf));
1056 int service_to_fd(const char *name)
1060 if(!strncmp(name, "tcp:", 4)) {
1061 int port = atoi(name + 4);
1062 name = strchr(name + 4, ':');
1065 ret = socket_ifr_client(port , SOCK_STREAM, "eth0");
1067 ret = socket_ifr_client(port , SOCK_STREAM, "usb0");
1069 if (ifconfig(SDB_FORWARD_IFNAME, SDB_FORWARD_INTERNAL_IP, SDB_FORWARD_INTERNAL_MASK, 1) == 0) {
1070 ret = socket_ifr_client(port , SOCK_STREAM, SDB_FORWARD_IFNAME);
1075 ret = socket_loopback_client(port, SOCK_STREAM);
1078 disable_tcp_nagle(ret);
1082 sdb_mutex_lock(&dns_lock);
1083 ret = socket_network_client(name + 1, port, SOCK_STREAM);
1084 sdb_mutex_unlock(&dns_lock);
1089 #ifndef HAVE_WINSOCK /* winsock doesn't implement unix domain sockets */
1090 } else if(!strncmp(name, "local:", 6)) {
1091 ret = socket_local_client(name + 6,
1092 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
1093 } else if(!strncmp(name, "localreserved:", 14)) {
1094 ret = socket_local_client(name + 14,
1095 ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
1096 } else if(!strncmp(name, "localabstract:", 14)) {
1097 ret = socket_local_client(name + 14,
1098 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
1099 } else if(!strncmp(name, "localfilesystem:", 16)) {
1100 ret = socket_local_client(name + 16,
1101 ANDROID_SOCKET_NAMESPACE_FILESYSTEM, SOCK_STREAM);
1104 } else if(!strncmp("dns:", name, 4)){
1105 char *n = strdup(name + 4);
1106 if(n == 0) return -1;
1107 ret = create_service_thread(dns_service, n);
1108 #else /* !SDB_HOST */
1109 }/* else if(!strncmp("dev:", name, 4)) {// tizen specific
1110 ret = unix_open(name + 4, O_RDWR);
1111 } else if(!strncmp(name, "framebuffer:", 12)) {
1112 ret = create_service_thread(framebuffer_service, 0);
1113 } else if(recovery_mode && !strncmp(name, "recover:", 8)) {
1114 ret = create_service_thread(recover_service, (void*) atoi(name + 8));
1115 } else if (!strncmp(name, "jdwp:", 5)) {
1116 ret = create_jdwp_connection_fd(atoi(name+5));
1117 } else if (!strncmp(name, "log:", 4)) {
1118 ret = create_service_thread(log_service, get_log_file_path(name + 4));
1119 }*/ else if(!HOST && !strncmp(name, "shell:", 6)) {
1121 ret = create_subproc_thread(name + 6, 0, 0);
1123 ret = create_subproc_thread(NULL, 0, 0);
1125 } else if(!strncmp(name, "eshell:", 7)) {
1127 if (sscanf(name+7, "%d:%d", &lines, &columns) == 2) {
1128 ret = create_subproc_thread(NULL, lines, columns);
1130 } else if(!strncmp(name, "sync:", 5)) {
1131 //ret = create_service_thread(file_sync_service, NULL);
1132 ret = create_syncproc_thread();
1133 }/* else if(!strncmp(name, "remount:", 8)) {
1134 ret = create_service_thread(remount_service, NULL);
1135 } else if(!strncmp(name, "reboot:", 7)) {
1136 void* arg = strdup(name + 7);
1137 if(arg == 0) return -1;
1138 ret = create_service_thread(reboot_service, arg);
1139 } else if(!strncmp(name, "root:", 5)) {
1140 ret = create_service_thread(restart_root_service, NULL);
1141 } else if(!strncmp(name, "backup:", 7)) {
1142 char* arg = strdup(name+7);
1143 if (arg == NULL) return -1;
1144 ret = backup_service(BACKUP, arg);
1145 } else if(!strncmp(name, "restore:", 8)) {
1146 ret = backup_service(RESTORE, NULL);
1147 }*/ else if(!strncmp(name, "root:", 5)) {
1148 char* service_name = NULL;
1150 service_name = strdup(name+5);
1151 ret = create_service_thread(rootshell_service, (void *)(service_name));
1152 } else if(!strncmp(name, "cs:", 5)) {
1153 ret = create_service_thread(inoti_service, NULL);
1155 } else if(!strncmp(name, "sysinfo:", 8)){
1156 ret = create_service_thread(get_platforminfo, 0);
1157 } else if(!strncmp(name, "capability:", 11)){
1158 ret = create_service_thread(get_capability, 0);
1159 } else if(!strncmp(name, "boot:", 5)){
1160 if (is_emulator()) {
1161 ret = create_service_thread(get_boot, 0);
1163 } else if(!strncmp(name, "shellconf:", 10)){
1164 if(!strncmp(name+10, "syncwinsz:", 10)){
1165 ret = create_service_thread(sync_windowsize, (void *)name+20);
1167 } else if(!strncmp(name, "tzplatformenv:", 14)) {
1168 char* env_variable = NULL;
1169 env_variable = strdup(name+14);
1170 ret = create_service_thread(get_tzplatform_env, (void *)(env_variable));
1171 } else if(!strncmp(name, "appcmd:", 7)){
1172 ret = request_appcmd_to_plugin(name+7);
1176 if (close_on_exec(ret) < 0) {
1177 D("failed to close fd exec\n");
1185 transport_type transport;
1190 static void wait_for_state(int fd, void* cookie)
1192 struct state_info* sinfo = cookie;
1193 char* err = "unknown error";
1195 D("wait_for_state %d\n", sinfo->state);
1197 atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
1199 writex(fd, "OKAY", 4);
1201 sendfailmsg(fd, err);
1205 free(sinfo->serial);
1208 D("wait_for_state is done\n");
1213 asocket* host_service_to_socket(const char* name, const char *serial)
1215 if (!strcmp(name,"track-devices")) {
1216 return create_device_tracker();
1217 } else if (!strncmp(name, "wait-for-", strlen("wait-for-"))) {
1218 struct state_info* sinfo = malloc(sizeof(struct state_info));
1221 sinfo->serial = strdup(serial);
1223 sinfo->serial = NULL;
1225 name += strlen("wait-for-");
1227 if (!strncmp(name, "local", strlen("local"))) {
1228 sinfo->transport = kTransportLocal;
1229 sinfo->state = CS_DEVICE;
1230 } else if (!strncmp(name, "usb", strlen("usb"))) {
1231 sinfo->transport = kTransportUsb;
1232 sinfo->state = CS_DEVICE;
1233 } else if (!strncmp(name, "any", strlen("any"))) {
1234 sinfo->transport = kTransportAny;
1235 sinfo->state = CS_DEVICE;
1241 int fd = create_service_thread(wait_for_state, sinfo);
1242 return create_local_socket(fd);
1246 #endif /* SDB_HOST */