2 Copyright 2015, 2016 IoT.bzh
4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
25 #include <systemd/sd-bus.h>
26 #include <systemd/sd-event.h>
27 #include <json-c/json.h>
30 #include "utils-jbus.h"
31 #include "utils-json.h"
35 #include "wgtpkg-install.h"
36 #include "wgtpkg-uninstall.h"
38 static const char appname[] = "afm-system-daemon";
39 static const char *rootdir = NULL;
44 "usage: %s [-q] [-v] [-r rootdir]\n"
46 " -r rootdir set root directory of applications\n"
47 " -d run as a daemon\n"
55 static struct option options[] = {
56 { "root", required_argument, NULL, 'r' },
57 { "daemon", no_argument, NULL, 'd' },
58 { "quiet", no_argument, NULL, 'q' },
59 { "verbose", no_argument, NULL, 'v' },
60 { "help", no_argument, NULL, 'h' },
64 static struct jbus *jbus;
66 const char error_nothing[] = "[]";
67 const char error_bad_request[] = "\"bad request\"";
68 const char error_not_found[] = "\"not found\"";
69 const char error_cant_start[] = "\"can't start\"";
71 static void on_install(struct sd_bus_message *smsg, struct json_object *req, void *unused)
77 struct json_object *resp;
79 /* scan the request */
80 switch (json_object_get_type(req)) {
81 case json_type_string:
82 wgtfile = json_object_get_string(req);
86 case json_type_object:
87 wgtfile = j_string_at(req, "wgt", NULL);
88 if (wgtfile != NULL) {
89 root = j_string_at(req, "root", rootdir);
90 force = j_boolean_at(req, "force", 0);
94 jbus_reply_error_s(smsg, error_bad_request);
98 /* install the widget */
99 ifo = install_widget(wgtfile, root, force);
101 jbus_reply_error_s(smsg, "\"installation failed\"");
103 /* build the response */
104 resp = json_object_new_object();
105 if(!resp || !j_add_string(resp, "added", wgt_info_desc(ifo)->idaver))
106 jbus_reply_error_s(smsg, "\"out of memory but installed!\"");
108 jbus_send_signal_s(jbus, "changed", "true");
109 jbus_reply_j(smsg, resp);
114 json_object_put(resp);
118 static void on_uninstall(struct sd_bus_message *smsg, struct json_object *req, void *unused)
124 /* scan the request */
125 switch (json_object_get_type(req)) {
126 case json_type_string:
127 idaver = json_object_get_string(req);
130 case json_type_object:
131 idaver = j_string_at(req, "id", NULL);
132 if (idaver != NULL) {
133 root = j_string_at(req, "root", rootdir);
137 jbus_reply_error_s(smsg, error_bad_request);
141 /* install the widget */
142 rc = uninstall_widget(idaver, root);
144 jbus_reply_error_s(smsg, "\"uninstallation had error\"");
146 jbus_send_signal_s(jbus, "changed", "true");
147 jbus_reply_s(smsg, "true");
151 static int daemonize()
161 int main(int ac, char **av)
163 int i, daemon = 0, rc;
164 struct sd_event *evloop;
165 struct sd_bus *sysbus;
169 /* interpretation of arguments */
170 while ((i = getopt_long(ac, av, "hdqvr:", options, NULL)) >= 0) {
189 ERROR("duplicate definition of rootdir");
194 ERROR("missing argument value");
197 ERROR("unrecognized option");
202 /* check the rootdir */
204 rootdir = FWK_APP_DIR;
206 rootdir = realpath(rootdir, NULL);
207 if (rootdir == NULL) {
208 ERROR("out of memory");
212 if (chdir(rootdir)) {
213 ERROR("can't enter %s", rootdir);
218 if (daemon && daemonize()) {
219 ERROR("daemonization failed");
223 /* get systemd objects */
224 rc = sd_event_new(&evloop);
226 ERROR("can't create event loop");
229 rc = sd_bus_open_system(&sysbus);
231 ERROR("can't create system bus");
234 rc = sd_bus_attach_event(sysbus, evloop, 0);
236 ERROR("can't attach system bus to event loop");
241 jbus = create_jbus(sysbus, AFM_SYSTEM_DBUS_PATH);
243 ERROR("create_jbus failed");
246 if(jbus_add_service_j(jbus, "install", on_install, NULL)
247 || jbus_add_service_j(jbus, "uninstall", on_uninstall, NULL)) {
248 ERROR("adding services failed");
253 if (jbus_start_serving(jbus) < 0) {
254 ERROR("can't start server");
258 sd_event_run(evloop, (uint64_t)-1);