Fixed execve EFAULT error on the asan environment.
[sdk/target/sdbd.git] / src / services.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <grp.h>
23
24 #include "sysdeps.h"
25
26 #define  TRACE_TAG  TRACE_SERVICES
27 #include "sdb.h"
28 #include "file_sync_service.h"
29
30 #if SDB_HOST
31 #  ifndef HAVE_WINSOCK
32 #    include <netinet/in.h>
33 #    include <netdb.h>
34 #    include <sys/ioctl.h>
35 #  endif
36 #else
37 #   include <sys/inotify.h>
38 #   include "sdktools.h"
39 #endif
40
41 #include "strutils.h"
42 #include "utils.h"
43 #include <system_info.h>
44 #include <tzplatform_config.h>
45
46 #include <vconf.h>
47 #include <limits.h>
48
49 #include <termios.h>
50 #include <sys/ioctl.h>
51
52 typedef struct stinfo stinfo;
53
54 struct stinfo {
55     void (*func)(int fd, void *cookie);
56     int fd;
57     void *cookie;
58 };
59
60 void *service_bootstrap_func(void *x)
61 {
62     stinfo *sti = x;
63     sti->func(sti->fd, sti->cookie);
64     free(sti);
65     return 0;
66 }
67
68 #if SDB_HOST
69 SDB_MUTEX_DEFINE( dns_lock );
70
71 static void dns_service(int fd, void *cookie)
72 {
73     char *hostname = cookie;
74     struct hostent *hp;
75     unsigned zero = 0;
76
77     sdb_mutex_lock(&dns_lock);
78     hp = gethostbyname(hostname);
79     free(cookie);
80     if(hp == 0) {
81         writex(fd, &zero, 4);
82     } else {
83         writex(fd, hp->h_addr, 4);
84     }
85     sdb_mutex_unlock(&dns_lock);
86     sdb_close(fd);
87 }
88 #else
89
90 static int is_support_interactive_shell()
91 {
92     return (!strncmp(g_capabilities.intershell_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED)));
93 }
94
95 #if 0
96 extern int recovery_mode;
97
98 static void recover_service(int s, void *cookie)
99 {
100     unsigned char buf[4096];
101     unsigned count = (unsigned) cookie;
102     int fd;
103
104     fd = sdb_creat("/tmp/update", 0644);
105     if(fd < 0) {
106         sdb_close(s);
107         return;
108     }
109
110     while(count > 0) {
111         unsigned xfer = (count > 4096) ? 4096 : count;
112         if(readx(s, buf, xfer)) break;
113         if(writex(fd, buf, xfer)) break;
114         count -= xfer;
115     }
116
117     if(count == 0) {
118         writex(s, "OKAY", 4);
119     } else {
120         writex(s, "FAIL", 4);
121     }
122     sdb_close(fd);
123     sdb_close(s);
124
125     fd = sdb_creat("/tmp/update.begin", 0644);
126     sdb_close(fd);
127 }
128
129 void restart_root_service(int fd, void *cookie)
130 {
131     char buf[100];
132     char value[PROPERTY_VALUE_MAX];
133
134     if (getuid() == 0) {
135         snprintf(buf, sizeof(buf), "sdbd is already running as root\n");
136         writex(fd, buf, strlen(buf));
137         sdb_close(fd);
138     } else {
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));
143             sdb_close(fd);
144             return;
145         }
146
147         property_set("service.sdb.root", "1");
148         snprintf(buf, sizeof(buf), "restarting sdbd as root\n");
149         writex(fd, buf, strlen(buf));
150         sdb_close(fd);
151     }
152 }
153 #endif
154
155 void restart_tcp_service(int fd, void *cookie)
156 {
157     char buf[100];
158     char value[PROPERTY_VALUE_MAX];
159     int port = (int)cookie;
160
161     if (port <= 0) {
162         snprintf(buf, sizeof(buf), "invalid port\n");
163         writex(fd, buf, strlen(buf));
164         sdb_close(fd);
165         return;
166     }
167
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));
172     sdb_close(fd);
173 }
174
175 static int is_support_rootonoff()
176 {
177     return (!strncmp(g_capabilities.rootonoff_support, SDBD_CAP_RET_ENABLED, strlen(SDBD_CAP_RET_ENABLED)));
178 }
179
180 void rootshell_service(int fd, void *cookie)
181 {
182     char buf[100];
183     char *mode = (char*) cookie;
184
185     if (!strcmp(mode, "on")) {
186         if (getuid() == 0) {
187             if (rootshell_mode == 1) {
188                 //snprintf(buf, sizeof(buf), "Already changed to sdk user mode\n");
189                 // do not show message
190             } else {
191                 if (is_support_rootonoff()) {
192                     rootshell_mode = 1;
193                     //allows a permitted user to execute a command as the superuser
194                     snprintf(buf, sizeof(buf), "Switched to 'root' account mode\n");
195                 } else {
196                     snprintf(buf, sizeof(buf), "Permission denied\n");
197                 }
198                 writex(fd, buf, strlen(buf));
199             }
200         } else {
201             D("need root permission for root shell: %d\n", getuid());
202             rootshell_mode = 0;
203             snprintf(buf, sizeof(buf), "Permission denied\n");
204             writex(fd, buf, strlen(buf));
205         }
206     } else if (!strcmp(mode, "off")) {
207         if (rootshell_mode == 1) {
208             rootshell_mode = 0;
209             snprintf(buf, sizeof(buf), "Switched to 'sdk user' account mode\n");
210             writex(fd, buf, strlen(buf));
211         }
212     } else {
213         snprintf(buf, sizeof(buf), "Unknown command option : %s\n", mode);
214         writex(fd, buf, strlen(buf));
215     }
216     D("set rootshell to %s\n", rootshell_mode == 1 ? "root" : SDK_USER_NAME);
217     free(mode);
218     sdb_close(fd);
219 }
220
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,
225 };
226
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);
234         if (env_value) {
235             D("environment value : %s\n", env_value);
236             snprintf(buf, sizeof(buf), "%d%s", NO_ERROR_TZPLATFORM_ENV, env_value);
237         } else {
238             D("failed to get environment value using tzplatform_getenv");
239             snprintf(buf, sizeof(buf), "%d", ERROR_TZPLATFORM_ENV_GENERAL);
240         }
241     } else {
242         D("environment name (%s) is invalid\n", env_name);
243         snprintf(buf, sizeof(buf), "%d", ERROR_TZPLATFORM_ENV_INVALID_VARIABLES);
244     }
245     writex(fd, buf, strlen(buf));
246     free(env_name);
247     sdb_close(fd);
248 }
249
250 void restart_usb_service(int fd, void *cookie)
251 {
252     char buf[100];
253
254     property_set("service.sdb.tcp.port", "0");
255     snprintf(buf, sizeof(buf), "restarting in USB mode\n");
256     writex(fd, buf, strlen(buf));
257     sdb_close(fd);
258 }
259
260 void reboot_service(int fd, void *arg)
261 {
262 #if 0
263     char buf[100];
264     int pid, ret;
265
266     sync();
267
268     /* Attempt to unmount the SD card first.
269      * No need to bother checking for errors.
270      */
271     pid = fork();
272     if (pid == 0) {
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);
280     }
281
282     ret = android_reboot(ANDROID_RB_RESTART2, 0, (char *) arg);
283     if (ret < 0) {
284         snprintf(buf, sizeof(buf), "reboot failed: %s errno:%d\n", errno);
285         writex(fd, buf, strlen(buf));
286     }
287     free(arg);
288     sdb_close(fd);
289 #endif
290 }
291
292 #if !SDB_HOST
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)
296
297 void inoti_service(int fd, void *arg)
298 {
299     int wd;
300     int ifd;
301     char buffer[BUF_LEN];
302
303     D( "inoti_service start\n");
304     ifd = inotify_init();
305
306     if ( ifd < 0 ) {
307         D( "inotify_init failed\n");
308         return;
309     }
310
311     wd = inotify_add_watch( ifd, CS_PATH, IN_CREATE);
312     if ( wd < 0 ) {
313         D("inotify_add_watch failed (errno :%d)\n", errno);
314         sdb_close(ifd);
315         sdb_close(fd);
316         return;
317     }
318
319     for ( ; ; ) {
320         int length, i = 0;
321         length = sdb_read( ifd, buffer, BUF_LEN );
322
323         if ( length < 0 ) {
324             D( "inoti read failed\n");
325             goto done;
326         }
327         while (i >= 0 && i <= (length - EVENT_SIZE)) {
328             struct inotify_event *event = (struct inotify_event *) &buffer[i];
329             if (event->len) {
330                 if (event->mask & IN_CREATE) {
331                     if (!(event->mask & IN_ISDIR)) {
332                         char *cspath = NULL;
333                         int len = asprintf(&cspath, "%s/%s", CS_PATH,
334                                 event->name);
335                         D( "The file %s was created.\n", cspath);
336                         writex(fd, cspath, len);
337                         if (cspath != NULL) {
338                             free(cspath);
339                         }
340                     }
341                 }
342             }
343             if (i + EVENT_SIZE + event->len < event->len) { // in case of integer overflow
344                 break;
345             }
346             i += EVENT_SIZE + event->len;
347         }
348     }
349
350 done:
351     inotify_rm_watch( ifd, wd );
352     sdb_close(ifd);
353     sdb_close(fd);
354     D( "inoti_service end\n");
355 }
356 #endif
357 #endif
358
359 #if 0
360 static void echo_service(int fd, void *cookie)
361 {
362     char buf[4096];
363     int r;
364     char *p;
365     int c;
366
367     for(;;) {
368         r = read(fd, buf, 4096);
369         if(r == 0) goto done;
370         if(r < 0) {
371             if(errno == EINTR) continue;
372             else goto done;
373         }
374
375         c = r;
376         p = buf;
377         while(c > 0) {
378             r = write(fd, p, c);
379             if(r > 0) {
380                 c -= r;
381                 p += r;
382                 continue;
383             }
384             if((r < 0) && (errno == EINTR)) continue;
385             goto done;
386         }
387     }
388 done:
389     close(fd);
390 }
391 #endif
392
393 static int create_service_thread(void (*func)(int, void *), void *cookie)
394 {
395     stinfo *sti;
396     sdb_thread_t t;
397     int s[2];
398
399     if(sdb_socketpair(s)) {
400         D("cannot create service socket pair\n");
401         return -1;
402     }
403
404     sti = malloc(sizeof(stinfo));
405     if(sti == 0) fatal("cannot allocate stinfo");
406     sti->func = func;
407     sti->cookie = cookie;
408     sti->fd = s[1];
409
410     if(sdb_thread_create( &t, service_bootstrap_func, sti)){
411         free(sti);
412         sdb_close(s[0]);
413         sdb_close(s[1]);
414         D("cannot create service thread\n");
415         return -1;
416     }
417
418     D("service thread started, %d:%d\n",s[0], s[1]);
419     return s[0];
420 }
421
422 #if !SDB_HOST
423
424 static void redirect_and_exec(int pts, const char *cmd, char * const argv[], char * const envp[])
425 {
426     dup2(pts, 0);
427     dup2(pts, 1);
428     dup2(pts, 2);
429
430     sdb_close(pts);
431
432     execve(cmd, argv, envp);
433 }
434
435 static int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[])
436 {
437     char devname[64];
438     int ptm;
439
440     ptm = unix_open("/dev/ptmx", O_RDWR); // | O_NOCTTY);
441     if(ptm < 0){
442         D("[ cannot open /dev/ptmx - errno:%d ]\n",errno);
443         return -1;
444     }
445     if (fcntl(ptm, F_SETFD, FD_CLOEXEC) < 0) {
446         D("[ cannot set cloexec to /dev/ptmx - errno:%d ]\n",errno);
447     }
448
449     if(grantpt(ptm) || unlockpt(ptm) ||
450         ptsname_r(ptm, devname, sizeof(devname)) != 0 ){
451         D("[ trouble with /dev/ptmx - errno:%d ]\n", errno);
452         sdb_close(ptm);
453         return -1;
454     }
455
456     *pid = fork();
457     if(*pid < 0) {
458         D("- fork failed: errno:%d -\n", errno);
459         sdb_close(ptm);
460         return -1;
461     }
462
463     if(*pid == 0){
464         int pts;
465
466         setsid();
467
468         pts = unix_open(devname, O_RDWR);
469         if(pts < 0) {
470             fprintf(stderr, "child failed to open pseudo-term slave: %s\n", devname);
471             exit(-1);
472         }
473
474         sdb_close(ptm);
475
476         // set OOM adjustment to zero
477         {
478             char text[64];
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);
482             if (fd >= 0) {
483                 sdb_write(fd, "0", 1);
484                 sdb_close(fd);
485             } else {
486                // FIXME: not supposed to be here
487                D("sdb: unable to open %s due to errno:%d\n", text, errno);
488             }
489         }
490
491         if (should_drop_privileges()) {
492             if (argv[2] != NULL && getuid() == 0 && request_plugin_verification(SDBD_CMD_VERIFY_ROOTCMD, argv[2])) {
493                 // do nothing
494                 D("sdb: executes root commands!!:%s\n", argv[2]);
495             } else {
496                 set_sdk_user_privileges();
497             }
498         }
499         redirect_and_exec(pts, cmd, argv, envp);
500         fprintf(stderr, "- exec '%s' failed: (errno:%d) -\n",
501                 cmd, errno);
502         exit(-1);
503     } else {
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.
508         return ptm;
509     }
510 }
511 #endif  /* !SDB_HOST */
512
513 #define SHELL_COMMAND "/bin/sh"
514 #define LOGIN_COMMAND "/bin/login"
515 #define SUPER_USER    "root"
516 #define LOGIN_CONFIG  "/etc/login.defs"
517
518 #if !SDB_HOST
519 static void subproc_waiter_service(int fd, void *cookie)
520 {
521     pid_t pid = (pid_t)cookie;
522
523     D("entered. fd=%d of pid=%d\n", fd, pid);
524     for (;;) {
525         int status;
526         pid_t p = waitpid(pid, &status, 0);
527         if (p == pid) {
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));
531                 break;
532             } else if (WIFSIGNALED(status)) {
533                 D("*** Killed by signal %d\n", WTERMSIG(status));
534                 break;
535             } else {
536                 D("*** Killed by unknown code %d\n", status);
537                 break;
538             }
539          }
540     }
541     D("shell exited fd=%d of pid=%d err=%d\n", fd, pid, errno);
542     if (SHELL_EXIT_NOTIFY_FD >=0) {
543       int res;
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);
547     }
548 }
549
550 static void get_env(char *key, char **env)
551 {
552     FILE *fp;
553     char buf[1024];
554     char *s, *e, *value;
555
556     fp = fopen (LOGIN_CONFIG, "r");
557     if (NULL == fp) {
558         return;
559     }
560
561     while (fgets(buf, (int) sizeof (buf), fp) != NULL) {
562         s = buf;
563         e = buf + (strlen(buf) - 1);
564
565         // trim string
566         while(*e == ' ' ||  *e == '\n' || *e == '\t') {
567             e--;
568         }
569         *(e+1) ='\0';
570
571         while(*s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n')) {
572             s++;
573         }
574
575         // skip comment or null string
576         if (*s == '#' || *s == '\0') {
577             continue;
578         }
579         value = s + strcspn(s, " \t");
580         *value++ = '\0';
581
582         if(!strcmp(buf, key)) {
583             *env = strdup(value);
584             break;
585         }
586     }
587
588     fclose(fp);
589 }
590
591 static int create_subproc_thread(const char *name, int lines, int columns)
592 {
593     stinfo *sti;
594     sdb_thread_t t;
595     int ret_fd;
596     pid_t pid;
597     char *value = NULL;
598     char *trim_value = NULL;
599     char path[PATH_MAX];
600     char *envp[MAX_TOKENS];
601     int envp_cnt = 0;
602
603     memset(path, 0, sizeof(path));
604     memset(envp, 0, sizeof(envp));
605
606     envp[envp_cnt++] = strdup("TERM=linux");
607     envp[envp_cnt++] = strdup("DISPLAY=:0");
608
609     if (should_drop_privileges()) {
610         if (g_sdk_home_dir_env) {
611             envp[envp_cnt++] = strdup(g_sdk_home_dir_env);
612         } else {
613             envp[envp_cnt++] = strdup("HOME=/home/owner");
614         }
615         get_env("ENV_PATH", &value);
616     } else {
617         get_env("ENV_SUPATH", &value);
618         if(value == NULL) {
619             get_env("ENV_ROOTPATH", &value);
620         }
621         envp[envp_cnt++] = strdup("HOME=/root");
622     }
623     if (value != NULL) {
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);
629             } else {
630                 snprintf(path, sizeof(path), "%s", trim_value);
631             }
632             envp[envp_cnt++] = strdup(path);
633         } else {
634             snprintf(path, sizeof(path), "%s", value);
635             envp[envp_cnt++] = strdup(path);
636         }
637         free(value);
638     }
639
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.");
645         return -1;
646     }
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);
650         free(envp_plugin);
651         return -1;
652     } else {
653        if(envp_plugin[0] != '\0') {
654             envp_cnt = tokenize_append(envp_plugin, "\n", envp, MAX_TOKENS, envp_cnt);
655         }
656     }
657     free(envp_plugin);
658
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);
663             return -1;
664         }
665
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.");
671             return -1;
672         }
673
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);
677             free(new_cmd);
678             return -1;
679         }
680
681         D("converted cmd : %s\n", new_cmd);
682
683         char *args[] = {
684             SHELL_COMMAND,
685             "-c",
686             NULL,
687             NULL,
688         };
689         args[2] = new_cmd;
690
691         ret_fd = create_subprocess(SHELL_COMMAND, &pid, (char * const*)args, (char * const*)envp);
692         free(new_cmd);
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");
697             return -1;
698         }
699
700         char * const args[] = {
701                 SHELL_COMMAND,
702                 "-",
703                 NULL,
704         };
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()) {
708             char *args[] = {
709                 SHELL_COMMAND,
710                 "-",
711                 NULL,
712             };
713             ret_fd = create_subprocess(SHELL_COMMAND, &pid, args, envp);
714         } else {
715             char *args[] = {
716                 LOGIN_COMMAND,
717                 "-f",
718                 SUPER_USER,
719                 NULL,
720             };
721             ret_fd = create_subprocess(LOGIN_COMMAND, &pid, args, envp);
722         }
723 #endif
724     }
725
726     /* free environment variables */
727     int i = 0;
728     if(envp_cnt > 0) {
729         for(i = 0; i < envp_cnt; i++) {
730             if(envp[i]) {
731                 D("envp[%d] = %s\n", i, envp[i]);
732                 free(envp[i]);
733             }
734         }
735     }
736
737     D("create_subprocess() ret_fd=%d pid=%d\n", ret_fd, pid);
738
739     if (ret_fd < 0) {
740         D("cannot create service thread\n");
741         return -1;
742     }
743
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;
749
750         if (ioctl(ret_fd, TIOCSWINSZ, &win_sz) < 0) {
751             D("failed to sync window size.\n");
752         }
753     }
754
755     sti = malloc(sizeof(stinfo));
756     if(sti == 0) fatal("cannot allocate stinfo");
757     sti->func = subproc_waiter_service;
758     sti->cookie = (void*)pid;
759     sti->fd = ret_fd;
760
761     if(sdb_thread_create( &t, service_bootstrap_func, sti)){
762         free(sti);
763         sdb_close(ret_fd);
764         D("cannot create service thread\n");
765         return -1;
766     }
767
768     D("service thread started, fd=%d pid=%d\n",ret_fd, pid);
769     return ret_fd;
770 }
771
772 static int create_sync_subprocess(void (*func)(int, void *), void* cookie) {
773     stinfo *sti;
774     sdb_thread_t t;
775     int s[2];
776
777     if(sdb_socketpair(s)) {
778         D("cannot create service socket pair\n");
779         return -1;
780     }
781
782     pid_t pid = fork();
783
784     if (pid == 0) {
785         sdb_close(s[0]);
786         func(s[1], cookie);
787         exit(-1);
788     } else if (pid > 0) {
789         sdb_close(s[1]);
790         // FIXME: do not wait child process hear
791         //waitpid(pid, &ret, 0);
792     }
793     if (pid < 0) {
794         D("- fork failed: errno:%d -\n", errno);
795         sdb_close(s[0]);
796         sdb_close(s[1]);
797         D("cannot create sync service sub process\n");
798         return -1;
799     }
800
801     sti = malloc(sizeof(stinfo));
802     if(sti == 0) fatal("cannot allocate stinfo");
803     sti->func = subproc_waiter_service;
804     sti->cookie = (void*)pid;
805     sti->fd = s[0];
806
807     if(sdb_thread_create( &t, service_bootstrap_func, sti)){
808         free(sti);
809         sdb_close(s[0]);
810         printf("cannot create service monitor thread\n");
811         return -1;
812     }
813
814     D("service process started, fd=%d pid=%d\n",s[0], pid);
815     return s[0];
816 }
817
818 static int create_syncproc_thread()
819 {
820     int ret_fd;
821
822     ret_fd = create_sync_subprocess(file_sync_service, NULL);
823     // FIXME: file missing bug when root on mode
824     /*
825     if (should_drop_privileges()) {
826         ret_fd = create_sync_subprocess(file_sync_service, NULL);
827     } else {
828         ret_fd = create_service_thread(file_sync_service, NULL);
829     }
830     */
831
832     return ret_fd;
833 }
834
835 #endif
836
837 static void get_platforminfo(int fd, void *cookie) {
838     pinfo sysinfo;
839
840     char *value = NULL;
841     s_strncpy(sysinfo.platform_info_version, INFO_VERSION, strlen(INFO_VERSION));
842
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);
847     } else {
848         s_strncpy(sysinfo.model_name, value, sizeof(sysinfo.model_name));
849         D("returns model_name:%s\n", value);
850         if (value != NULL) {
851             free(value);
852         }
853     }
854
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);
859     } else {
860         s_strncpy(sysinfo.platform_name, value, sizeof(sysinfo.platform_name));
861         D("returns platform_name:%s\n", value);
862         if (value != NULL) {
863             free(value);
864         }
865
866     }
867
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);
873     } else {
874         s_strncpy(sysinfo.platform_version, value, sizeof(sysinfo.platform_version));
875         D("returns platform_version:%s\n", value);
876         if (value != NULL) {
877             free(value);
878         }
879     }
880
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);
885     } else {
886         s_strncpy(sysinfo.profile_name, value, sizeof(sysinfo.profile_name));
887         D("returns profile name:%s\n", value);
888         if (value != NULL) {
889             free(value);
890         }
891     }
892
893     writex(fd, &sysinfo, sizeof(pinfo));
894
895     sdb_close(fd);
896 }
897
898 static int put_key_value_string(char* buf, int offset, int buf_size, char* key, char* value) {
899     int len = 0;
900     if ((len = snprintf(buf+offset, buf_size-offset, "%s:%s\n", key, value)) > 0) {
901         return len;
902     }
903     return 0;
904 }
905
906 static void get_capability(int fd, void *cookie) {
907     char cap_buffer[CAPBUF_SIZE] = {0,};
908     uint16_t offset = 0;
909
910     // Secure protocol support
911     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
912                                 "secure_protocol", g_capabilities.secure_protocol);
913
914     // Interactive shell support
915     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
916                                 "intershell_support", g_capabilities.intershell_support);
917
918     // File push/pull support
919     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
920                                 "filesync_support", g_capabilities.filesync_support);
921
922     // USB protocol support
923     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
924                                 "usbproto_support", g_capabilities.usbproto_support);
925
926     // Socket protocol support
927     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
928                                 "sockproto_support", g_capabilities.sockproto_support);
929
930     // Window size synchronization support
931     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
932                                 "syncwinsz_support", g_capabilities.syncwinsz_support);
933
934     // Root command support
935     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
936                                 "rootonoff_support", g_capabilities.rootonoff_support);
937
938     // Zone support
939     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
940                                 "zone_support", g_capabilities.zone_support);
941
942     // Multi-User support
943     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
944                                 "multiuser_support", g_capabilities.multiuser_support);
945
946     // CPU Architecture of model
947     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
948                                 "cpu_arch", g_capabilities.cpu_arch);
949
950     // SDK Tool path
951     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
952                                 "sdk_toolpath", g_capabilities.sdk_toolpath);
953
954     // Profile name
955     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
956                                 "profile_name", g_capabilities.profile_name);
957
958     // Vendor name
959     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
960                                 "vendor_name", g_capabilities.vendor_name);
961
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);
965
966     // Platform version
967     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
968                                 "platform_version", g_capabilities.platform_version);
969
970     // Product version
971     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
972                                 "product_version", g_capabilities.product_version);
973
974     // Sdbd version
975     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
976                                 "sdbd_version", g_capabilities.sdbd_version);
977
978     // Sdbd plugin version
979     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
980                                 "sdbd_plugin_version", g_capabilities.sdbd_plugin_version);
981
982     // Capability version
983     offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
984                                 "sdbd_cap_version", g_capabilities.sdbd_cap_version);
985
986     // Sdbd log enable
987    offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
988                                "log_enable", g_capabilities.log_enable);
989
990     // Sdbd log path
991    offset += put_key_value_string(cap_buffer, offset, CAPBUF_SIZE,
992                                "log_path", g_capabilities.log_path);
993
994     offset++; // for '\0' character
995
996     writex(fd, &offset, sizeof(uint16_t));
997     writex(fd, cap_buffer, offset);
998
999     sdb_close(fd);
1000 }
1001
1002 static void sync_windowsize(int fd, void *cookie) {
1003     int id, lines, columns;
1004     char *size_info = cookie;
1005     asocket *s = NULL;
1006
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);
1009     }
1010     if((s = find_local_socket(id))) {
1011         struct winsize win_sz;
1012         win_sz.ws_row = lines;
1013         win_sz.ws_col = columns;
1014
1015         if (ioctl(s->fd, TIOCSWINSZ, &win_sz) < 0) {
1016             D("failed to sync window size.\n");
1017             return;
1018         }
1019         D("success to sync window size.\n");
1020     }
1021 }
1022
1023 const unsigned COMMAND_TIMEOUT = 10000;
1024 void get_boot(int fd, void *cookie) {
1025     char buf[2] = { 0, };
1026     int time = 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");
1032             break;
1033         }
1034         D("get_boot:platform booting is in progress\n");
1035         sdb_sleep_ms(interval);
1036         time += interval;
1037     }
1038     writex(fd, buf, strlen(buf));
1039     sdb_close(fd);
1040 }
1041
1042 int service_to_fd(const char *name)
1043 {
1044     int ret = -1;
1045
1046     if(!strncmp(name, "tcp:", 4)) {
1047         int port = atoi(name + 4);
1048         name = strchr(name + 4, ':');
1049         if(name == 0) {
1050             if (is_emulator()){
1051                 ret = socket_ifr_client(port , SOCK_STREAM, "eth0");
1052             } else {
1053                 ret = socket_ifr_client(port , SOCK_STREAM, "usb0");
1054                 if (ret < 0) {
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);
1057                     }
1058                 }
1059             }
1060             if (ret < 0) {
1061                 ret = socket_loopback_client(port, SOCK_STREAM);
1062             }
1063             if (ret >= 0) {
1064                 disable_tcp_nagle(ret);
1065             }
1066         } else {
1067 #if SDB_HOST
1068             sdb_mutex_lock(&dns_lock);
1069             ret = socket_network_client(name + 1, port, SOCK_STREAM);
1070             sdb_mutex_unlock(&dns_lock);
1071 #else
1072             return -1;
1073 #endif
1074         }
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);
1088 #endif
1089 #if SDB_HOST
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)) {
1106         if(name[6]) {
1107             ret = create_subproc_thread(name + 6, 0, 0);
1108         } else {
1109             ret = create_subproc_thread(NULL, 0, 0);
1110         }
1111     } else if(!strncmp(name, "eshell:", 7)) {
1112         int lines, columns;
1113         if (sscanf(name+7, "%d:%d", &lines, &columns) == 2) {
1114             ret = create_subproc_thread(NULL, lines, columns);
1115         }
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;
1135
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);
1140 #endif
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);
1148         }
1149     } else if(!strncmp(name, "shellconf:", 10)){
1150         if(!strncmp(name+10, "syncwinsz:", 10)){
1151             ret = create_service_thread(sync_windowsize, (void *)name+20);
1152         }
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));
1157     }
1158
1159     if (ret >= 0) {
1160         if (close_on_exec(ret) < 0) {
1161             D("failed to close fd exec\n");
1162         }
1163     }
1164     return ret;
1165 }
1166
1167 #if SDB_HOST
1168 struct state_info {
1169     transport_type transport;
1170     char* serial;
1171     int state;
1172 };
1173
1174 static void wait_for_state(int fd, void* cookie)
1175 {
1176     struct state_info* sinfo = cookie;
1177     char* err = "unknown error";
1178
1179     D("wait_for_state %d\n", sinfo->state);
1180
1181     atransport *t = acquire_one_transport(sinfo->state, sinfo->transport, sinfo->serial, &err);
1182     if(t != 0) {
1183         writex(fd, "OKAY", 4);
1184     } else {
1185         sendfailmsg(fd, err);
1186     }
1187
1188     if (sinfo->serial)
1189         free(sinfo->serial);
1190     free(sinfo);
1191     sdb_close(fd);
1192     D("wait_for_state is done\n");
1193 }
1194 #endif
1195
1196 #if SDB_HOST
1197 asocket*  host_service_to_socket(const char*  name, const char *serial)
1198 {
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));
1203
1204         if (serial)
1205             sinfo->serial = strdup(serial);
1206         else
1207             sinfo->serial = NULL;
1208
1209         name += strlen("wait-for-");
1210
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;
1220         } else {
1221             free(sinfo);
1222             return NULL;
1223         }
1224
1225         int fd = create_service_thread(wait_for_state, sinfo);
1226         return create_local_socket(fd);
1227     }
1228     return NULL;
1229 }
1230 #endif /* SDB_HOST */