461b0a0f09d0cb5c24878cd02cc7b728509e71cc
[src/app-framework-binder.git] / src / main-afs-supervisor.c
1 /*
2  * Copyright (C) 2016, 2017, 2018 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25
26 #include <systemd/sd-daemon.h>
27
28 #include "afb-common.h"
29 #include "afb-hsrv.h"
30 #include "afb-hswitch.h"
31 #include "afb-hreq.h"
32 #include "afb-apiset.h"
33 #include "afb-api-ws.h"
34 #include "afb-session.h"
35
36 #include "afs-supervisor.h"
37 #include "afs-config.h"
38
39 #include "verbose.h"
40 #include "jobs.h"
41 #include "process-name.h"
42
43 /* the main config */
44 struct afs_config *main_config;
45
46 /* the main apiset */
47 struct afb_apiset *main_apiset;
48
49 /*************************************************************************************/
50
51 static int init_http_server(struct afb_hsrv *hsrv)
52 {
53         if (!afb_hsrv_add_handler
54             (hsrv, main_config->rootapi, afb_hswitch_websocket_switch, main_apiset, 20))
55                 return 0;
56
57         if (!afb_hsrv_add_handler
58             (hsrv, main_config->rootapi, afb_hswitch_apis, main_apiset, 10))
59                 return 0;
60
61         if (main_config->roothttp != NULL) {
62                 if (!afb_hsrv_add_alias
63                     (hsrv, "", afb_common_rootdir_get_fd(), main_config->roothttp,
64                      -10, 1))
65                         return 0;
66         }
67
68         if (!afb_hsrv_add_handler
69             (hsrv, main_config->rootbase, afb_hswitch_one_page_api_redirect, NULL,
70              -20))
71                 return 0;
72
73         return 1;
74 }
75
76 static struct afb_hsrv *start_http_server()
77 {
78         int rc;
79         struct afb_hsrv *hsrv;
80
81         if (afb_hreq_init_download_path(main_config->uploaddir)) {
82                 ERROR("unable to set the upload directory %s", main_config->uploaddir);
83                 return NULL;
84         }
85
86         hsrv = afb_hsrv_create();
87         if (hsrv == NULL) {
88                 ERROR("memory allocation failure");
89                 return NULL;
90         }
91
92         if (!afb_hsrv_set_cache_timeout(hsrv, main_config->cacheTimeout)
93             || !init_http_server(hsrv)) {
94                 ERROR("initialisation of httpd failed");
95                 afb_hsrv_put(hsrv);
96                 return NULL;
97         }
98
99         NOTICE("Waiting port=%d rootdir=%s", main_config->httpdPort, main_config->rootdir);
100         NOTICE("Browser URL= http://localhost:%d", main_config->httpdPort);
101
102         rc = afb_hsrv_start(hsrv, (uint16_t) main_config->httpdPort, 15);
103         if (!rc) {
104                 ERROR("starting of httpd failed");
105                 afb_hsrv_put(hsrv);
106                 return NULL;
107         }
108
109         return hsrv;
110 }
111
112 static void start(int signum, void *arg)
113 {
114         struct afb_hsrv *hsrv;
115         int rc;
116
117         /* check illness */
118         if (signum) {
119                 ERROR("start aborted: received signal %s", strsignal(signum));
120                 exit(1);
121         }
122
123         /* set the directories */
124         mkdir(main_config->workdir, S_IRWXU | S_IRGRP | S_IXGRP);
125         if (chdir(main_config->workdir) < 0) {
126                 ERROR("Can't enter working dir %s", main_config->workdir);
127                 goto error;
128         }
129         if (afb_common_rootdir_set(main_config->rootdir) < 0) {
130                 ERROR("failed to set common root directory");
131                 goto error;
132         }
133
134         /* configure the daemon */
135         if (afb_session_init(main_config->nbSessionMax, main_config->cntxTimeout, main_config->token)) {
136                 ERROR("initialisation of session manager failed");
137                 goto error;
138         }
139
140         main_apiset = afb_apiset_create("main", main_config->apiTimeout);
141         if (!main_apiset) {
142                 ERROR("can't create main apiset");
143                 goto error;
144         }
145
146         /* init the main apiset */
147         rc = afs_supervisor_add(main_apiset, main_apiset);
148         if (rc < 0) {
149                 ERROR("Can't create supervision's apiset: %m");
150                 goto error;
151         }
152
153         /* export the service if required */
154         if (main_config->ws_server) {
155                 rc = afb_api_ws_add_server(main_config->ws_server, main_apiset, main_apiset);
156                 if (rc < 0) {
157                         ERROR("Can't export (ws-server) api %s: %m", main_config->ws_server);
158                         goto error;
159                 }
160         }
161
162         /* start the services */
163         if (afb_apiset_start_all_services(main_apiset) < 0)
164                 goto error;
165
166         /* start the HTTP server */
167         if (main_config->httpdPort <= 0) {
168                 ERROR("no port is defined");
169                 goto error;
170         }
171
172         if (!afb_hreq_init_cookie(main_config->httpdPort, main_config->rootapi, main_config->cntxTimeout)) {
173                 ERROR("initialisation of HTTP cookies failed");
174                 goto error;
175         }
176
177         hsrv = start_http_server();
178         if (hsrv == NULL)
179                 goto error;
180
181         /* ready */
182         sd_notify(1, "READY=1");
183         afs_supervisor_discover();
184         return;
185 error:
186         exit(1);
187 }
188
189 /**
190  * initialize the supervision
191  */
192 int main(int ac, char **av)
193 {
194         /* scan arguments */
195         main_config = afs_config_parse_arguments(ac, av);
196         if (main_config->name) {
197                 verbose_set_name(main_config->name, 0);
198                 process_name_set_name(main_config->name);
199                 process_name_replace_cmdline(av, main_config->name);
200         }
201         /* enter job processing */
202         jobs_start(3, 0, 10, start, av[1]);
203         WARNING("hoops returned from jobs_enter! [report bug]");
204         return 1;
205 }
206