aboutsummaryrefslogtreecommitdiffstats
path: root/src/afs-main.c
blob: 99689e9001d923b08534903571b2542de8303b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * Copyright (C) 2016, 2017 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#include <systemd/sd-daemon.h>

#include "afb-common.h"
#include "afb-hsrv.h"
#include "afb-hswitch.h"
#include "afb-hreq.h"
#include "afb-apiset.h"
#include "afb-session.h"

#include "afs-supervisor.h"
#include "afs-config.h"

#include "verbose.h"
#include "jobs.h"
#include "process-name.h"

/* the main config */
struct afs_config *main_config;

/* the main apiset */
struct afb_apiset *main_apiset;

/*************************************************************************************/

static int init_http_server(struct afb_hsrv *hsrv)
{
	if (!afb_hsrv_add_handler
	    (hsrv, main_config->rootapi, afb_hswitch_websocket_switch, main_apiset, 20))
		return 0;

	if (!afb_hsrv_add_handler
	    (hsrv, main_config->rootapi, afb_hswitch_apis, main_apiset, 10))
		return 0;

	if (main_config->roothttp != NULL) {
		if (!afb_hsrv_add_alias
		    (hsrv, "", afb_common_rootdir_get_fd(), main_config->roothttp,
		     -10, 1))
			return 0;
	}

	if (!afb_hsrv_add_handler
	    (hsrv, main_config->rootbase, afb_hswitch_one_page_api_redirect, NULL,
	     -20))
		return 0;

	return 1;
}

static struct afb_hsrv *start_http_server()
{
	int rc;
	struct afb_hsrv *hsrv;

	if (afb_hreq_init_download_path(main_config->uploaddir)) {
		ERROR("unable to set the upload directory %s", main_config->uploaddir);
		return NULL;
	}

	hsrv = afb_hsrv_create();
	if (hsrv == NULL) {
		ERROR("memory allocation failure");
		return NULL;
	}

	if (!afb_hsrv_set_cache_timeout(hsrv, main_config->cacheTimeout)
	    || !init_http_server(hsrv)) {
		ERROR("initialisation of httpd failed");
		afb_hsrv_put(hsrv);
		return NULL;
	}

	NOTICE("Waiting port=%d rootdir=%s", main_config->httpdPort, main_config->rootdir);
	NOTICE("Browser URL= http://localhost:%d", main_config->httpdPort);

	rc = afb_hsrv_start(hsrv, (uint16_t) main_config->httpdPort, 15);
	if (!rc) {
		ERROR("starting of httpd failed");
		afb_hsrv_put(hsrv);
		return NULL;
	}

	return hsrv;
}

static void start(int signum, void *arg)
{
	struct afb_hsrv *hsrv;
	int rc;

	/* check illness */
	if (signum) {
		ERROR("start aborted: received signal %s", strsignal(signum));
		exit(1);
	}

	/* set the directories */
	mkdir(main_config->workdir, S_IRWXU | S_IRGRP | S_IXGRP);
	if (chdir(main_config->workdir) < 0) {
		ERROR("Can't enter working dir %s", main_config->workdir);
		goto error;
	}
	if (afb_common_rootdir_set(main_config->rootdir) < 0) {
		ERROR("failed to set common root directory");
		goto error;
	}

	/* configure the daemon */
	if (afb_session_init(main_config->nbSessionMax, main_config->cntxTimeout, main_config->token)) {
		ERROR("initialisation of session manager failed");
		goto error;
	}

	main_apiset = afb_apiset_create("main", main_config->apiTimeout);
	if (!main_apiset) {
		ERROR("can't create main apiset");
		goto error;
	}

	/* init the main apiset */
	rc = afs_supervisor_add(main_apiset);
	if (rc < 0) {
		ERROR("Can't create supervision's apiset: %m");
		goto error;
	}

	/* start the services */
	if (afb_apiset_start_all_services(main_apiset, 1) < 0)
		goto error;

	/* start the HTTP server */
	if (main_config->httpdPort <= 0) {
		ERROR("no port is defined");
		goto error;
	}

	if (!afb_hreq_init_cookie(main_config->httpdPort, main_config->rootapi, main_config->cntxTimeout)) {
		ERROR("initialisation of HTTP cookies failed");
		goto error;
	}

	hsrv = start_http_server();
	if (hsrv == NULL)
		goto error;

	/* ready */
	sd_notify(1, "READY=1");
	afs_supervisor_discover();
	return;
error:
	exit(1);
}

/**
 * initalize the supervision
 */
int main(int ac, char **av)
{
	/* scan arguments */
	main_config = afs_config_parse_arguments(ac, av);
	if (main_config->name) {
		verbose_set_name(main_config->name, 0);
		process_name_set_name(main_config->name);
		process_name_replace_cmdline(av, main_config->name);
	}
	/* enter job processing */
	jobs_start(3, 0, 10, start, av[1]);
	WARNING("hoops returned from jobs_enter! [report bug]");
	return 1;
}