12 #include <systemd/sd-daemon.h>
24 #include "accesslog.h"
27 #include "input_stats.h"
29 #include "sa_compare.h"
30 #include "serverpool.h"
39 AccessLogThread *access_log = nullptr;
40 ServerPool *servers = nullptr;
41 volatile bool hupped = false;
42 volatile bool stopped = false;
44 typedef pair<string, Input::Encoding> InputKey;
48 struct OrderByConnectionTime {
49 bool operator() (const ClientProto &a, const ClientProto &b) const {
50 if (a.connect_time_sec() != b.connect_time_sec())
51 return a.connect_time_sec() < b.connect_time_sec();
52 return a.connect_time_nsec() < b.connect_time_nsec();
56 // An arbitrary ordering.
57 struct AcceptorConfigCompare {
58 bool operator() (const AcceptorConfig &a, const AcceptorConfig &b) const {
59 int cmp = a.certificate_chain.compare(b.certificate_chain);
64 cmp = a.private_key.compare(b.private_key);
69 return Sockaddr6Compare()(a.addr, b.addr);
75 struct InputWithRefcount {
83 if (signum == SIGINT) {
88 void do_nothing(int signum)
92 CubemapStateProto collect_state(const timespec &serialize_start,
93 const vector<Acceptor *> acceptors,
94 const multimap<InputKey, InputWithRefcount> inputs,
97 CubemapStateProto state = servers->serialize(); // Fills streams() and clients().
98 state.set_serialize_start_sec(serialize_start.tv_sec);
99 state.set_serialize_start_usec(serialize_start.tv_nsec / 1000);
101 for (Acceptor *acceptor : acceptors) {
102 state.add_acceptors()->MergeFrom(acceptor->serialize());
105 for (const auto &key_and_input_with_refcount : inputs) {
106 state.add_inputs()->MergeFrom(key_and_input_with_refcount.second.input->serialize());
112 // Find all port statements in the configuration file, and create acceptors for them.
113 vector<Acceptor *> create_acceptors(
114 const Config &config,
115 map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> *deserialized_acceptors)
117 vector<Acceptor *> acceptors;
118 for (const AcceptorConfig &acceptor_config : config.acceptors) {
119 Acceptor *acceptor = nullptr;
120 const auto deserialized_acceptor_it = deserialized_acceptors->find(acceptor_config);
121 if (deserialized_acceptor_it != deserialized_acceptors->end()) {
122 acceptor = deserialized_acceptor_it->second;
123 deserialized_acceptors->erase(deserialized_acceptor_it);
125 int server_sock = create_server_socket(acceptor_config.addr, TCP_SOCKET);
126 acceptor = new Acceptor(server_sock, acceptor_config.addr,
127 acceptor_config.certificate_chain,
128 acceptor_config.private_key);
131 acceptors.push_back(acceptor);
134 // Close all acceptors that are no longer in the configuration file.
135 for (auto &config_and_acceptor : *deserialized_acceptors) {
136 config_and_acceptor.second->close_socket();
137 delete config_and_acceptor.second;
143 void create_config_input(const string &src, Input::Encoding encoding, multimap<InputKey, InputWithRefcount> *inputs)
148 InputKey key(src, encoding);
149 if (inputs->count(key) != 0) {
153 InputWithRefcount iwr;
154 iwr.input = create_input(src, encoding);
155 if (iwr.input == nullptr) {
156 log(ERROR, "did not understand URL '%s' or source encoding was invalid, clients will not get any data.",
161 inputs->insert(make_pair(key, iwr));
164 // Find all streams in the configuration file, and create inputs for them.
165 void create_config_inputs(const Config &config, multimap<InputKey, InputWithRefcount> *inputs)
167 for (const StreamConfig &stream_config : config.streams) {
168 if (stream_config.src == "delete") {
169 // Ignored for pre-1.4.0 configuration compatibility.
172 create_config_input(stream_config.src, Input::Encoding(stream_config.src_encoding), inputs);
174 for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
175 create_config_input(udpstream_config.src, Input::INPUT_ENCODING_RAW, inputs);
179 void create_streams(const Config &config,
180 const set<string> &deserialized_urls,
181 multimap<InputKey, InputWithRefcount> *inputs)
184 set<string> expecting_urls = deserialized_urls;
185 for (const StreamConfig &stream_config : config.streams) {
188 expecting_urls.erase(stream_config.url);
190 if (stream_config.src == "delete") {
191 // Ignored for pre-1.4.0 configuration compatibility.
195 if (deserialized_urls.count(stream_config.url) == 0) {
196 stream_index = servers->add_stream(stream_config.url,
197 stream_config.backlog_size,
198 stream_config.prebuffering_bytes,
199 Stream::Encoding(stream_config.encoding),
200 Stream::Encoding(stream_config.src_encoding));
202 stream_index = servers->lookup_stream_by_url(stream_config.url);
203 assert(stream_index != -1);
204 servers->set_backlog_size(stream_index, stream_config.backlog_size);
205 servers->set_prebuffering_bytes(stream_index, stream_config.prebuffering_bytes);
206 servers->set_encoding(stream_index,
207 Stream::Encoding(stream_config.encoding));
208 servers->set_src_encoding(stream_index,
209 Stream::Encoding(stream_config.src_encoding));
212 servers->set_pacing_rate(stream_index, stream_config.pacing_rate);
214 string src = stream_config.src;
215 Input::Encoding src_encoding = Input::Encoding(stream_config.src_encoding);
217 const auto input_it = inputs->find(make_pair(src, src_encoding));
218 if (input_it != inputs->end()) {
219 input_it->second.input->add_destination(stream_index);
220 ++input_it->second.refcount;
225 // Warn about any streams servers we've lost.
226 for (const string &url : expecting_urls) {
227 log(WARNING, "stream '%s' disappeared from the configuration file. "
228 "It will not be deleted, but clients will not get any new inputs. "
229 "If you really meant to delete it, set src=delete and reload.",
234 for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
235 int stream_index = servers->add_udpstream(
236 udpstream_config.dst,
237 udpstream_config.pacing_rate,
238 udpstream_config.ttl,
239 udpstream_config.multicast_iface_index);
241 string src = udpstream_config.src;
243 auto input_it = inputs->find(make_pair(src, Input::INPUT_ENCODING_RAW));
244 assert(input_it != inputs->end());
245 input_it->second.input->add_destination(stream_index);
246 ++input_it->second.refcount;
250 // HTTP gen204 endpoints.
251 for (const Gen204Config &ping_config : config.pings) {
252 servers->add_gen204(ping_config.url, ping_config.allow_origin);
256 void open_logs(const vector<LogConfig> &log_destinations)
258 for (const LogConfig &log_destination : log_destinations) {
259 if (log_destination.type == LogConfig::LOG_TYPE_FILE) {
260 add_log_destination_file(log_destination.filename);
261 } else if (log_destination.type == LogConfig::LOG_TYPE_CONSOLE) {
262 add_log_destination_console();
263 } else if (log_destination.type == LogConfig::LOG_TYPE_SYSLOG) {
264 add_log_destination_syslog();
272 bool dry_run_config(const string &argv0, const string &config_filename)
274 char *argv0_copy = strdup(argv0.c_str());
275 char *config_filename_copy = strdup(config_filename.c_str());
280 log_perror("fork()");
282 free(config_filename_copy);
286 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, nullptr);
287 log_perror(argv0_copy);
295 free(config_filename_copy);
300 err = waitpid(pid, &status, 0);
301 } while (err == -1 && errno == EINTR);
304 log_perror("waitpid()");
308 return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
311 void find_all_streams(const Config &config, set<string> *all_urls)
313 for (const StreamConfig &stream_config : config.streams) {
314 if (stream_config.src == "delete") {
315 log(WARNING, "stream '%s' has src=delete; ignoring it. Since Cubemap 1.4.0, you do not "
316 "need to set src=delete to delete streams anymore; just delete them from "
317 "the configuration file.",
318 stream_config.url.c_str());
320 all_urls->insert(stream_config.url);
325 int main(int argc, char **argv)
329 signal(SIGUSR1, do_nothing); // Used in internal signalling.
330 signal(SIGPIPE, SIG_IGN);
336 bool test_config = false;
338 static const option long_options[] = {
339 { "state", required_argument, 0, 's' },
340 { "test-config", no_argument, 0, 't' },
343 int option_index = 0;
344 int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
351 state_fd = atoi(optarg);
357 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
362 string config_filename = "cubemap.config";
364 config_filename = argv[optind++];
367 // Canonicalize argv[0] and config_filename.
368 char argv0_canon[PATH_MAX];
369 char config_filename_canon[PATH_MAX];
371 if (realpath("/proc/self/exe", argv0_canon) == nullptr) {
375 if (realpath(config_filename.c_str(), config_filename_canon) == nullptr) {
376 log_perror(config_filename.c_str());
380 // Now parse the configuration file.
382 if (!parse_config(config_filename_canon, &config)) {
389 // Ideally we'd like to daemonize only when we've started up all threads etc.,
390 // but daemon() forks, which is not good in multithreaded software, so we'll
391 // have to do it here.
392 if (config.daemonize) {
393 if (daemon(0, 0) == -1) {
394 log_perror("daemon");
400 // Open logs as soon as possible.
401 open_logs(config.log_destinations);
403 log(INFO, "Cubemap " SERVER_VERSION " starting.");
404 if (config.access_log_file.empty()) {
405 // Create a dummy logger.
406 access_log = new AccessLogThread();
408 access_log = new AccessLogThread(config.access_log_file);
412 servers = new ServerPool(config.num_servers);
414 // Find all the streams that are to be kept.
415 set<string> all_urls;
416 find_all_streams(config, &all_urls);
418 CubemapStateProto loaded_state;
419 timespec serialize_start;
420 set<string> deserialized_urls;
421 map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> deserialized_acceptors;
422 multimap<InputKey, InputWithRefcount> inputs; // multimap due to older versions without deduplication.
423 if (state_fd != -1) {
424 log(INFO, "Deserializing state from previous process...");
426 if (!read_tempfile_and_close(state_fd, &serialized)) {
429 if (!loaded_state.ParseFromString(serialized)) {
430 log(ERROR, "Failed deserialization of state.");
434 serialize_start.tv_sec = loaded_state.serialize_start_sec();
435 serialize_start.tv_nsec = loaded_state.serialize_start_usec() * 1000ull;
437 // Deserialize the streams.
438 map<string, string> stream_headers_for_url; // See below.
439 for (const StreamProto &stream : loaded_state.streams()) {
440 if (all_urls.count(stream.url()) == 0) {
441 // Delete the stream backlogs.
442 log(INFO, "Deleting stream '%s'.", stream.url().c_str());
443 for (const int fd : stream.data_fds()) {
447 vector<int> data_fds;
448 for (const int fd : stream.data_fds()) {
449 data_fds.push_back(fd);
452 servers->add_stream_from_serialized(stream, data_fds);
453 deserialized_urls.insert(stream.url());
455 stream_headers_for_url.insert(make_pair(stream.url(), stream.stream_header()));
459 // Deserialize the inputs. Note that we don't actually add them to any stream yet.
460 for (const InputProto &serialized_input : loaded_state.inputs()) {
461 InputWithRefcount iwr;
462 iwr.input = create_input(serialized_input);
465 Input::Encoding src_encoding = serialized_input.is_metacube_encoded() ?
466 Input::INPUT_ENCODING_METACUBE :
467 Input::INPUT_ENCODING_RAW;
468 InputKey key(serialized_input.url(), src_encoding);
469 inputs.insert(make_pair(key, iwr));
472 // Deserialize the acceptors.
473 for (const AcceptorProto &serialized_acceptor : loaded_state.acceptors()) {
474 AcceptorConfig config;
475 config.addr = extract_address_from_acceptor_proto(serialized_acceptor);
476 config.certificate_chain = serialized_acceptor.certificate_chain();
477 config.private_key = serialized_acceptor.private_key();
478 deserialized_acceptors.insert(make_pair(
480 new Acceptor(serialized_acceptor)));
483 log(INFO, "Deserialization done.");
486 // Add any new inputs coming from the config.
487 create_config_inputs(config, &inputs);
489 // Find all streams in the configuration file, create them, and connect to the inputs.
490 create_streams(config, deserialized_urls, &inputs);
491 vector<Acceptor *> acceptors = create_acceptors(config, &deserialized_acceptors);
493 // Make all the servers create TLS contexts for every TLS keypair we have.
494 for (Acceptor *acceptor : acceptors) {
495 if (acceptor->is_tls()) {
496 servers->create_tls_context_for_acceptor(acceptor);
500 // Put back the existing clients. It doesn't matter which server we
501 // allocate them to, so just do round-robin. However, we need to sort them
502 // by connection time first, since add_client_serialized() expects that.
503 sort(loaded_state.mutable_clients()->begin(),
504 loaded_state.mutable_clients()->end(),
505 OrderByConnectionTime());
506 for (int i = 0; i < loaded_state.clients_size(); ++i) {
507 if (all_urls.count(loaded_state.clients(i).url()) == 0) {
508 safe_close(loaded_state.clients(i).sock());
510 servers->add_client_from_serialized(loaded_state.clients(i));
516 // Now delete all inputs that are longer in use, and start the others.
517 for (auto input_it = inputs.begin(); input_it != inputs.end(); ) {
518 if (input_it->second.refcount == 0) {
519 if (input_it->first.second == Input::INPUT_ENCODING_RAW) {
520 log(WARNING, "Raw input '%s' no longer in use, closing.",
521 input_it->first.first.c_str());
523 assert(input_it->first.second == Input::INPUT_ENCODING_METACUBE);
524 log(WARNING, "Metacube input '%s' no longer in use, closing.",
525 input_it->first.first.c_str());
527 input_it->second.input->close_socket();
528 delete input_it->second.input;
529 inputs.erase(input_it++);
531 input_it->second.input->run();
536 // Start writing statistics.
537 unique_ptr<StatsThread> stats_thread;
538 if (!config.stats_file.empty()) {
539 stats_thread.reset(new StatsThread(config.stats_file, config.stats_interval));
543 unique_ptr<InputStatsThread> input_stats_thread;
544 if (!config.input_stats_file.empty()) {
545 vector<Input*> inputs_no_refcount;
546 for (const auto &key_and_input_with_refcount : inputs) {
547 inputs_no_refcount.push_back(key_and_input_with_refcount.second.input);
550 input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount));
551 input_stats_thread->run();
554 timespec server_start;
555 int err = clock_gettime(CLOCK_MONOTONIC, &server_start);
557 if (state_fd != -1) {
558 // Measure time from we started deserializing (below) to now, when basically everything
559 // is up and running. This is, in other words, a conservative estimate of how long our
560 // “glitch” period was, not counting of course reconnects if the configuration changed.
561 double glitch_time = server_start.tv_sec - serialize_start.tv_sec +
562 1e-9 * (server_start.tv_nsec - serialize_start.tv_nsec);
563 log(INFO, "Re-exec happened in approx. %.0f ms.", glitch_time * 1000.0);
566 sd_notify(0, "READY=1");
573 sd_notify(0, "STOPPING=1");
575 sd_notify(0, "RELOADING=1");
578 // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
579 err = clock_gettime(CLOCK_MONOTONIC, &serialize_start);
582 if (input_stats_thread != nullptr) {
583 input_stats_thread->stop();
584 input_stats_thread.reset();
586 if (stats_thread != nullptr) {
587 stats_thread->stop();
588 stats_thread.reset();
590 for (Acceptor *acceptor : acceptors) {
593 for (const auto &key_and_input_with_refcount : inputs) {
594 key_and_input_with_refcount.second.input->stop();
598 CubemapStateProto state;
600 log(INFO, "Shutting down.");
602 log(INFO, "Serializing state and re-execing...");
603 state = collect_state(
604 serialize_start, acceptors, inputs, servers);
606 state.SerializeToString(&serialized);
607 state_fd = make_tempfile(serialized);
608 if (state_fd == -1) {
622 // OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
623 if (!dry_run_config(argv0_canon, config_filename_canon)) {
624 open_logs(config.log_destinations);
625 log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
632 sprintf(buf, "%d", state_fd);
635 execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, nullptr);
636 open_logs(config.log_destinations);
637 log_perror("execlp");
638 log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);