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.
28 #include "utils-jbus.h"
29 #include "utils-json.h"
33 #include "wgtpkg-install.h"
34 #include "wgtpkg-uninstall.h"
36 static const char appname[] = "afm-system-daemon";
37 static const char *rootdir = NULL;
42 "usage: %s [-q] [-v] [-r rootdir]\n"
44 " -r rootdir set root directory of applications\n"
45 " -d run as a daemon\n"
53 static struct option options[] = {
54 { "root", required_argument, NULL, 'r' },
55 { "daemon", no_argument, NULL, 'd' },
56 { "quiet", no_argument, NULL, 'q' },
57 { "verbose", no_argument, NULL, 'v' },
58 { "help", no_argument, NULL, 'h' },
62 static struct jbus *jbus;
64 const char error_nothing[] = "[]";
65 const char error_bad_request[] = "\"bad request\"";
66 const char error_not_found[] = "\"not found\"";
67 const char error_cant_start[] = "\"can't start\"";
69 static void on_install(struct jreq *jreq, struct json_object *req)
75 struct json_object *resp;
77 /* scan the request */
78 switch (json_object_get_type(req)) {
79 case json_type_string:
80 wgtfile = json_object_get_string(req);
84 case json_type_object:
85 wgtfile = j_string_at(req, "wgt", NULL);
86 if (wgtfile != NULL) {
87 root = j_string_at(req, "root", rootdir);
88 force = j_boolean_at(req, "force", 0);
92 jbus_reply_error_s(jreq, error_bad_request);
96 /* install the widget */
97 ifo = install_widget(wgtfile, root, force);
99 jbus_reply_error_s(jreq, "\"installation failed\"");
101 /* build the response */
102 resp = json_object_new_object();
103 if(!resp || !j_add_string(resp, "added", wgt_info_desc(ifo)->idaver))
104 jbus_reply_error_s(jreq, "\"out of memory but installed!\"");
106 jbus_send_signal_s(jbus, "changed", "true");
107 jbus_reply_j(jreq, resp);
112 json_object_put(resp);
116 static void on_uninstall(struct jreq *jreq, struct json_object *req)
122 /* scan the request */
123 switch (json_object_get_type(req)) {
124 case json_type_string:
125 idaver = json_object_get_string(req);
128 case json_type_object:
129 idaver = j_string_at(req, "id", NULL);
130 if (idaver != NULL) {
131 root = j_string_at(req, "root", rootdir);
135 jbus_reply_error_s(jreq, error_bad_request);
139 /* install the widget */
140 rc = uninstall_widget(idaver, root);
142 jbus_reply_error_s(jreq, "\"uninstallation had error\"");
144 jbus_send_signal_s(jbus, "changed", "true");
145 jbus_reply_s(jreq, "true");
149 static int daemonize()
159 int main(int ac, char **av)
165 /* interpretation of arguments */
166 while ((i = getopt_long(ac, av, "hdqvr:", options, NULL)) >= 0) {
185 ERROR("duplicate definition of rootdir");
190 ERROR("missing argument value");
193 ERROR("unrecognized option");
198 /* check the rootdir */
200 rootdir = FWK_APP_DIR;
202 rootdir = realpath(rootdir, NULL);
203 if (rootdir == NULL) {
204 ERROR("out of memory");
208 if (chdir(rootdir)) {
209 ERROR("can't enter %s", rootdir);
214 if (daemon && daemonize()) {
215 ERROR("daemonization failed");
220 jbus = create_jbus(0, AFM_SYSTEM_DBUS_PATH);
222 ERROR("create_jbus failed");
225 if(jbus_add_service_j(jbus, "install", on_install)
226 || jbus_add_service_j(jbus, "uninstall", on_uninstall)) {
227 ERROR("adding services failed");
232 if (jbus_start_serving(jbus)) {
233 ERROR("can't start server");
236 while (!jbus_read_write_dispatch(jbus, -1));