X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafm-user-daemon.c;h=f9d74e9f65931f6c3675493894e5aa29312a16f5;hb=3a6e947bef1b2942e24d2fdee1a76dbf3305b508;hp=e051c97583f891ce72850de7776cc54e0adaca29;hpb=4d72f7da11f6c3ff3e5885ee51c415c119f0ace3;p=src%2Fapp-framework-main.git diff --git a/src/afm-user-daemon.c b/src/afm-user-daemon.c index e051c97..f9d74e9 100644 --- a/src/afm-user-daemon.c +++ b/src/afm-user-daemon.c @@ -1,5 +1,5 @@ /* - Copyright 2015 IoT.bzh + Copyright 2015, 2016, 2017 IoT.bzh author: José Bollo @@ -49,6 +49,8 @@ static const char usagestr[] = " -r rootdir adds a root directory of applications\n" " -m mode set default launch mode (local or remote)\n" " -d run as a daemon\n" + " -u addr address of user D-Bus to use\n" + " -s addr address of system D-Bus to use\n" " -q quiet\n" " -v verbose\n" "\n"; @@ -61,6 +63,8 @@ static struct option options_l[] = { { "root", required_argument, NULL, 'r' }, { "application", required_argument, NULL, 'a' }, { "mode", required_argument, NULL, 'm' }, + { "user-dbus", required_argument, NULL, 'u' }, + { "system-dbus", required_argument, NULL, 's' }, { "daemon", no_argument, NULL, 'd' }, { "quiet", no_argument, NULL, 'q' }, { "verbose", no_argument, NULL, 'v' }, @@ -181,7 +185,6 @@ static void on_detail(struct sd_bus_message *smsg, struct json_object *obj, void json_object_put(resp); } - /* * On query "start" from 'smsg' with parameters of 'obj'. */ @@ -242,7 +245,7 @@ static void on_start(struct sd_bus_message *smsg, struct json_object *obj, void && j_add_string(resp, "uri", uri)) jbus_reply_j(smsg, resp); else { - afm_run_stop(runid); + afm_run_terminate(runid); jbus_reply_error_s(smsg, error_system); } json_object_put(resp); @@ -250,29 +253,83 @@ static void on_start(struct sd_bus_message *smsg, struct json_object *obj, void } /* - * On query "stop" from 'smsg' with parameters of 'obj'. + * On query "once" from 'smsg' with parameters of 'obj'. */ -static void on_stop(struct sd_bus_message *smsg, struct json_object *obj, void *unused) +static void on_once(struct sd_bus_message *smsg, struct json_object *obj, void *unused) +{ + const char *appid; + struct json_object *appli, *resp; + int runid; + + /* get the parameters */ + if (!j_read_string(obj, &appid) && !j_read_string_at(obj, "id", &appid)) { + jbus_reply_error_s(smsg, error_bad_request); + return; + } + + /* get the application */ + INFO("method once called for %s", appid); + appli = afm_db_get_application(afdb, appid); + if (appli == NULL) { + jbus_reply_error_s(smsg, error_not_found); + return; + } + + /* launch the application */ + runid = afm_run_once(appli); + if (runid <= 0) { + jbus_reply_error_s(smsg, error_cant_start); + return; + } + + /* returns the state */ + resp = afm_run_state(runid); + reply(smsg, resp, error_not_found); + json_object_put(resp); +} + +/* + * On query "pause" from 'smsg' with parameters of 'obj'. + */ +static void on_pause(struct sd_bus_message *smsg, struct json_object *obj, void *unused) { int runid, status; - if (onrunid(smsg, obj, "stop", &runid)) { - status = afm_run_stop(runid); + if (onrunid(smsg, obj, "pause", &runid)) { + status = afm_run_pause(runid); reply_status(smsg, status, error_not_found); } } /* - * On query "continue" from 'smsg' with parameters of 'obj'. + * On query "resume" from 'smsg' with parameters of 'obj'. */ -static void on_continue(struct sd_bus_message *smsg, struct json_object *obj, void *unused) +static void on_resume(struct sd_bus_message *smsg, struct json_object *obj, void *unused) { int runid, status; - if (onrunid(smsg, obj, "continue", &runid)) { - status = afm_run_continue(runid); + if (onrunid(smsg, obj, "resume", &runid)) { + status = afm_run_resume(runid); reply_status(smsg, status, error_not_found); } } +/* + * On query "stop" from 'smsg' with parameters of 'obj'. + */ +static void on_stop(struct sd_bus_message *smsg, struct json_object *obj, void *unused) +{ + NOTICE("call to obsolete 'stop'"); + on_pause(smsg, obj, unused); +} + +/* + * On query "continue" from 'smsg' with parameters of 'obj'. + */ +static void on_continue(struct sd_bus_message *smsg, struct json_object *obj, void *unused) +{ + NOTICE("call to obsolete 'continue'"); + on_resume(smsg, obj, unused); +} + /* * On query "terminate" from 'smsg' with parameters of 'obj'. */ @@ -379,6 +436,45 @@ static int daemonize() return 0; } +/* + * Opens a sd-bus connection and returns it in 'ret'. + * The sd-bus connexion is intended to be for user if 'isuser' + * is not null. The adress is the default address when 'address' + * is NULL or, otherwise, the given address. + * It might be necessary to pass the address as an argument because + * library systemd uses secure_getenv to retrieves the default + * addresses and secure_getenv might return NULL in some cases. + */ +static int open_bus(sd_bus **ret, int isuser, const char *address) +{ + sd_bus *b; + int rc; + + if (address == NULL) + return (isuser ? sd_bus_open_user : sd_bus_open_system)(ret); + + rc = sd_bus_new(&b); + if (rc < 0) + return rc; + + rc = sd_bus_set_address(b, address); + if (rc < 0) + goto fail; + + sd_bus_set_bus_client(b, 1); + + rc = sd_bus_start(b); + if (rc < 0) + goto fail; + + *ret = b; + return 0; + +fail: + sd_bus_unref(b); + return rc; +} + /* * ENTRY POINT OF AFM-USER-DAEMON */ @@ -388,11 +484,13 @@ int main(int ac, char **av) enum afm_launch_mode mode; struct sd_event *evloop; struct sd_bus *sysbus, *usrbus; - + const char *sys_bus_addr, *usr_bus_addr; LOGAUTH(appname); /* first interpretation of arguments */ + sys_bus_addr = NULL; + usr_bus_addr = NULL; while ((i = getopt_long(ac, av, options_s, options_l, NULL)) >= 0) { switch (i) { case 'h': @@ -420,6 +518,12 @@ int main(int ac, char **av) } set_default_launch_mode(mode); break; + case 'u': + usr_bus_addr = optarg; + break; + case 's': + sys_bus_addr = optarg; + break; case ':': ERROR("missing argument value"); return 1; @@ -486,7 +590,7 @@ int main(int ac, char **av) ERROR("can't create event loop"); return 1; } - rc = sd_bus_open_system(&sysbus); + rc = open_bus(&sysbus, 0, sys_bus_addr); if (rc < 0) { ERROR("can't create system bus"); return 1; @@ -496,7 +600,7 @@ int main(int ac, char **av) ERROR("can't attach system bus to event loop"); return 1; } - rc = sd_bus_open_user(&usrbus); + rc = open_bus(&usrbus, 1, usr_bus_addr); if (rc < 0) { ERROR("can't create user bus"); return 1; @@ -531,18 +635,22 @@ int main(int ac, char **av) if (jbus_add_service_j(user_bus, "runnables", on_runnables, NULL) || jbus_add_service_j(user_bus, "detail", on_detail, NULL) || jbus_add_service_j(user_bus, "start", on_start, NULL) + || jbus_add_service_j(user_bus, "once", on_once, NULL) || jbus_add_service_j(user_bus, "terminate", on_terminate, NULL) + || jbus_add_service_j(user_bus, "pause", on_pause, NULL) + || jbus_add_service_j(user_bus, "resume", on_resume, NULL) || jbus_add_service_j(user_bus, "stop", on_stop, NULL) || jbus_add_service_j(user_bus, "continue", on_continue, NULL) || jbus_add_service_j(user_bus, "runners", on_runners, NULL) || jbus_add_service_j(user_bus, "state", on_state, NULL) #if defined(EXPLICIT_CALL) || jbus_add_service_s(user_bus, "install", on_install, NULL) - || jbus_add_service_s(user_bus, "uninstall", on_uninstall, NULL)) { + || jbus_add_service_s(user_bus, "uninstall", on_uninstall, NULL) #else || jbus_add_service_s(user_bus, "install", (void (*)(struct sd_bus_message *, const char *, void *))propagate, "install") - || jbus_add_service_s(user_bus, "uninstall", (void (*)(struct sd_bus_message *, const char *, void *))propagate, "uninstall")) { + || jbus_add_service_s(user_bus, "uninstall", (void (*)(struct sd_bus_message *, const char *, void *))propagate, "uninstall") #endif + ) { ERROR("adding services failed"); return 1; } @@ -559,3 +667,17 @@ int main(int ac, char **av) return 0; } + + + + + + + + + + + + + +