X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=4ae8af5e7d7a296319ddefe89d13454421435cf9;hb=7cd20a135f6d2df212fb6aefdc3ee0a6949a740d;hp=9fbbb072d7ae17f23afb4524813fbf1ac1e52530;hpb=84e049cf2312286ad1895cbebc82cabd5c30b9bb;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index 9fbbb072..4ae8af5e 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #include #include +#include #include "afb-config.h" #include "afb-hswitch.h" @@ -47,6 +48,12 @@ #include +/* + if SELF_PGROUP == 0 the launched command is the group leader + if SELF_PGROUP != 0 afb-daemon is the group leader +*/ +#define SELF_PGROUP 1 + static struct afb_config *config; static pid_t childpid; @@ -92,9 +99,11 @@ static void start_list(struct afb_config_list *list, +--------------------------------------------------------- */ static void exit_handler() { - if (childpid > 0) + /* TODO: check whether using SIGHUP isn't better */ + if (SELF_PGROUP) + killpg(0, SIGKILL); + else if (childpid > 0) killpg(childpid, SIGKILL); - /* TODO: check whether using SIGHUP isn't better */ } /*---------------------------------------------------------- @@ -194,8 +203,8 @@ static struct afb_hsrv *start_http_server() int rc; struct afb_hsrv *hsrv; - if (afb_hreq_init_download_path("/tmp")) { /* TODO: sessiondir? */ - ERROR("unable to set the tmp directory"); + if (afb_hreq_init_download_path(config->uploaddir)) { + ERROR("unable to set the upload directory %s", config->uploaddir); return NULL; } @@ -212,8 +221,7 @@ static struct afb_hsrv *start_http_server() return NULL; } - NOTICE("Waiting port=%d rootdir=%s", config->httpdPort, - config->rootdir); + NOTICE("Waiting port=%d rootdir=%s", config->httpdPort, config->rootdir); NOTICE("Browser URL= http:/*localhost:%d", config->httpdPort); rc = afb_hsrv_start(hsrv, (uint16_t) config->httpdPort, 15); @@ -239,7 +247,8 @@ static void on_sigchld(int signum, siginfo_t *info, void *uctx) case CLD_KILLED: case CLD_DUMPED: childpid = 0; - killpg(info->si_pid, SIGKILL); + if (!SELF_PGROUP) + killpg(info->si_pid, SIGKILL); waitpid(info->si_pid, NULL, 0); exit(0); } @@ -313,6 +322,9 @@ static int execute_command() if (!config->exec || !config->exec[0]) return 0; + if (SELF_PGROUP) + setpgid(0, 0); + /* install signal handler */ memset(&siga, 0, sizeof siga); siga.sa_sigaction = on_sigchld; @@ -326,7 +338,8 @@ static int execute_command() /* makes arguments */ if (instanciate_command_args() >= 0) { - setpgid(0, 0); + if (!SELF_PGROUP) + setpgid(0, 0); execv(config->exec[0], config->exec); ERROR("can't launch %s: %m", config->exec[0]); } @@ -356,13 +369,20 @@ int main(int argc, char *argv[]) exit(1); } - afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token, afb_apis_count()); + mkdir(config->workdir, S_IRWXU | S_IRGRP | S_IXGRP); + if (chdir(config->workdir) < 0) { + ERROR("Can't enter working dir %s", config->workdir); + exit(1); + } afb_api_so_set_timeout(config->apiTimeout); start_list(config->dbus_clients, afb_api_dbus_add_client, "the afb-dbus client"); start_list(config->ws_clients, afb_api_ws_add_client, "the afb-websocket client"); start_list(config->ldpaths, afb_api_so_add_pathset, "the binding path set"); start_list(config->so_bindings, afb_api_so_add_binding, "the binding"); + + afb_session_init(config->nbSessionMax, config->cntxTimeout, config->token, afb_apis_count()); + start_list(config->dbus_servers, afb_api_dbus_add_server, "the afb-dbus service"); start_list(config->ws_servers, afb_api_ws_add_server, "the afb-websocket service"); @@ -375,8 +395,8 @@ int main(int argc, char *argv[]) ERROR("failed to initialise signal handlers"); return 1; } - // if directory does not exist createit - mkdir(config->rootdir, O_RDWR | S_IRWXU | S_IRGRP); + + // set the root dir if (afb_common_rootdir_set(config->rootdir) < 0) { ERROR("failed to set common root directory"); return 1; @@ -436,6 +456,7 @@ int main(int argc, char *argv[]) // infinite loop eventloop = afb_common_get_event_loop(); + sd_notify(1, "READY=1"); for (;;) sd_event_run(eventloop, 30000000);