12 #include <systemd/sd-daemon.h>
18 #include <unordered_map>
25 #include "accesslog.h"
28 #include "input_stats.h"
30 #include "sa_compare.h"
31 #include "serverpool.h"
40 AccessLogThread *access_log = nullptr;
41 ServerPool *servers = nullptr;
42 volatile bool hupped = false;
43 volatile bool stopped = false;
45 typedef pair<string, Input::Encoding> InputKey;
49 struct OrderByConnectionTime {
50 bool operator() (const ClientProto &a, const ClientProto &b) const {
51 if (a.connect_time_sec() != b.connect_time_sec())
52 return a.connect_time_sec() < b.connect_time_sec();
53 return a.connect_time_nsec() < b.connect_time_nsec();
57 // An arbitrary ordering.
58 struct AcceptorConfigCompare {
59 bool operator() (const AcceptorConfig &a, const AcceptorConfig &b) const {
60 int cmp = a.certificate_chain.compare(b.certificate_chain);
65 cmp = a.private_key.compare(b.private_key);
70 return Sockaddr6Compare()(a.addr, b.addr);
76 struct InputWithRefcount {
84 if (signum == SIGINT) {
89 void do_nothing(int signum)
93 CubemapStateProto collect_state(const timespec &serialize_start,
94 const vector<Acceptor *> acceptors,
95 const multimap<InputKey, InputWithRefcount> inputs,
98 CubemapStateProto state = servers->serialize(); // Fills streams() and clients().
99 state.set_serialize_start_sec(serialize_start.tv_sec);
100 state.set_serialize_start_usec(serialize_start.tv_nsec / 1000);
102 for (Acceptor *acceptor : acceptors) {
103 state.add_acceptors()->MergeFrom(acceptor->serialize());
106 for (const auto &key_and_input_with_refcount : inputs) {
107 state.add_inputs()->MergeFrom(key_and_input_with_refcount.second.input->serialize());
113 // Find all port statements in the configuration file, and create acceptors for them.
114 vector<Acceptor *> create_acceptors(
115 const Config &config,
116 map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> *deserialized_acceptors)
118 vector<Acceptor *> acceptors;
119 for (const AcceptorConfig &acceptor_config : config.acceptors) {
120 Acceptor *acceptor = nullptr;
121 const auto deserialized_acceptor_it = deserialized_acceptors->find(acceptor_config);
122 if (deserialized_acceptor_it != deserialized_acceptors->end()) {
123 acceptor = deserialized_acceptor_it->second;
124 deserialized_acceptors->erase(deserialized_acceptor_it);
126 int server_sock = create_server_socket(acceptor_config.addr, TCP_SOCKET);
127 acceptor = new Acceptor(server_sock, acceptor_config.addr,
128 acceptor_config.certificate_chain,
129 acceptor_config.private_key);
132 acceptors.push_back(acceptor);
135 // Close all acceptors that are no longer in the configuration file.
136 for (auto &config_and_acceptor : *deserialized_acceptors) {
137 config_and_acceptor.second->close_socket();
138 delete config_and_acceptor.second;
144 void create_config_input(const string &src, Input::Encoding encoding, multimap<InputKey, InputWithRefcount> *inputs)
149 InputKey key(src, encoding);
150 if (inputs->count(key) != 0) {
154 InputWithRefcount iwr;
155 iwr.input = create_input(src, encoding);
156 if (iwr.input == nullptr) {
157 log(ERROR, "did not understand URL '%s' or source encoding was invalid, clients will not get any data.",
162 inputs->insert(make_pair(key, iwr));
165 // Find all streams in the configuration file, and create inputs for them.
166 void create_config_inputs(const Config &config, multimap<InputKey, InputWithRefcount> *inputs)
168 for (const StreamConfig &stream_config : config.streams) {
169 if (stream_config.src == "delete") {
170 // Ignored for pre-1.4.0 configuration compatibility.
173 create_config_input(stream_config.src, Input::Encoding(stream_config.src_encoding), inputs);
175 for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
176 create_config_input(udpstream_config.src, Input::INPUT_ENCODING_RAW, inputs);
180 void create_streams(const Config &config,
181 const set<string> &deserialized_urls,
182 multimap<InputKey, InputWithRefcount> *inputs)
185 set<string> expecting_urls = deserialized_urls;
186 for (const StreamConfig &stream_config : config.streams) {
189 expecting_urls.erase(stream_config.url);
191 if (stream_config.src == "delete") {
192 // Ignored for pre-1.4.0 configuration compatibility.
196 if (deserialized_urls.count(stream_config.url) == 0) {
197 stream_index = servers->add_stream(stream_config.url,
198 stream_config.hls_url,
199 stream_config.backlog_size,
200 stream_config.prebuffering_bytes,
201 Stream::Encoding(stream_config.encoding),
202 Stream::Encoding(stream_config.src_encoding),
203 stream_config.hls_frag_duration,
204 stream_config.hls_backlog_margin,
205 stream_config.allow_origin);
207 stream_index = servers->lookup_stream_by_url(stream_config.url);
208 assert(stream_index != -1);
209 servers->set_backlog_size(stream_index, stream_config.backlog_size);
210 if (!stream_config.hls_url.empty()) {
211 servers->register_hls_url(stream_index, stream_config.hls_url);
213 servers->set_prebuffering_bytes(stream_index, stream_config.prebuffering_bytes);
214 servers->set_encoding(stream_index,
215 Stream::Encoding(stream_config.encoding));
216 servers->set_src_encoding(stream_index,
217 Stream::Encoding(stream_config.src_encoding));
218 servers->set_hls_frag_duration(stream_index, stream_config.hls_frag_duration);
219 servers->set_hls_backlog_margin(stream_index, stream_config.hls_backlog_margin);
220 servers->set_allow_origin(stream_index, stream_config.allow_origin);
223 servers->set_pacing_rate(stream_index, stream_config.pacing_rate);
225 string src = stream_config.src;
226 Input::Encoding src_encoding = Input::Encoding(stream_config.src_encoding);
228 const auto input_it = inputs->find(make_pair(src, src_encoding));
229 if (input_it != inputs->end()) {
230 input_it->second.input->add_destination(stream_index);
231 ++input_it->second.refcount;
236 // Warn about any streams servers we've lost.
237 for (const string &url : expecting_urls) {
238 log(WARNING, "stream '%s' disappeared from the configuration file. "
239 "It will not be deleted, but clients will not get any new inputs. "
240 "If you really meant to delete it, set src=delete and reload.",
245 for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
246 int stream_index = servers->add_udpstream(
247 udpstream_config.dst,
248 udpstream_config.pacing_rate,
249 udpstream_config.ttl,
250 udpstream_config.multicast_iface_index);
252 string src = udpstream_config.src;
254 auto input_it = inputs->find(make_pair(src, Input::INPUT_ENCODING_RAW));
255 assert(input_it != inputs->end());
256 input_it->second.input->add_destination(stream_index);
257 ++input_it->second.refcount;
261 // HTTP gen204 endpoints.
262 for (const Gen204Config &ping_config : config.pings) {
263 servers->add_gen204(ping_config.url, ping_config.allow_origin);
267 void open_logs(const vector<LogConfig> &log_destinations)
269 for (const LogConfig &log_destination : log_destinations) {
270 if (log_destination.type == LogConfig::LOG_TYPE_FILE) {
271 add_log_destination_file(log_destination.filename);
272 } else if (log_destination.type == LogConfig::LOG_TYPE_CONSOLE) {
273 add_log_destination_console();
274 } else if (log_destination.type == LogConfig::LOG_TYPE_SYSLOG) {
275 add_log_destination_syslog();
283 bool dry_run_config(const string &argv0, const string &config_filename)
285 char *argv0_copy = strdup(argv0.c_str());
286 char *config_filename_copy = strdup(config_filename.c_str());
291 log_perror("fork()");
293 free(config_filename_copy);
297 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, nullptr);
298 log_perror(argv0_copy);
306 free(config_filename_copy);
311 err = waitpid(pid, &status, 0);
312 } while (err == -1 && errno == EINTR);
315 log_perror("waitpid()");
319 return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
322 void find_all_streams(const Config &config, set<string> *all_urls)
324 for (const StreamConfig &stream_config : config.streams) {
325 if (stream_config.src == "delete") {
326 log(WARNING, "stream '%s' has src=delete; ignoring it. Since Cubemap 1.4.0, you do not "
327 "need to set src=delete to delete streams anymore; just delete them from "
328 "the configuration file.",
329 stream_config.url.c_str());
331 all_urls->insert(stream_config.url);
336 int main(int argc, char **argv)
340 signal(SIGUSR1, do_nothing); // Used in internal signalling.
341 signal(SIGPIPE, SIG_IGN);
347 bool test_config = false;
349 static const option long_options[] = {
350 { "state", required_argument, 0, 's' },
351 { "test-config", no_argument, 0, 't' },
354 int option_index = 0;
355 int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
362 state_fd = atoi(optarg);
368 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
373 string config_filename = "cubemap.config";
375 config_filename = argv[optind++];
378 // Canonicalize argv[0] and config_filename.
379 char argv0_canon[PATH_MAX];
380 char config_filename_canon[PATH_MAX];
382 if (realpath("/proc/self/exe", argv0_canon) == nullptr) {
386 if (realpath(config_filename.c_str(), config_filename_canon) == nullptr) {
387 log_perror(config_filename.c_str());
391 // Now parse the configuration file.
393 if (!parse_config(config_filename_canon, &config)) {
400 // Ideally we'd like to daemonize only when we've started up all threads etc.,
401 // but daemon() forks, which is not good in multithreaded software, so we'll
402 // have to do it here.
403 if (config.daemonize) {
404 if (daemon(0, 0) == -1) {
405 log_perror("daemon");
411 // Open logs as soon as possible.
412 open_logs(config.log_destinations);
414 log(INFO, "Cubemap " SERVER_VERSION " starting.");
415 if (config.access_log_file.empty()) {
416 // Create a dummy logger.
417 access_log = new AccessLogThread();
419 access_log = new AccessLogThread(config.access_log_file);
423 servers = new ServerPool(config.num_servers);
425 // Find all the streams that are to be kept.
426 set<string> all_urls;
427 find_all_streams(config, &all_urls);
429 CubemapStateProto loaded_state;
430 timespec serialize_start;
431 set<string> deserialized_urls;
432 map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> deserialized_acceptors;
433 multimap<InputKey, InputWithRefcount> inputs; // multimap due to older versions without deduplication.
434 if (state_fd != -1) {
435 log(INFO, "Deserializing state from previous process...");
437 if (!read_tempfile_and_close(state_fd, &serialized)) {
440 if (!loaded_state.ParseFromString(serialized)) {
441 log(ERROR, "Failed deserialization of state.");
445 serialize_start.tv_sec = loaded_state.serialize_start_sec();
446 serialize_start.tv_nsec = loaded_state.serialize_start_usec() * 1000ull;
448 // Deserialize the streams.
449 unordered_map<string, string> stream_headers_for_url; // See below.
450 for (const StreamProto &stream : loaded_state.streams()) {
451 if (all_urls.count(stream.url()) == 0) {
452 // Delete the stream backlogs.
453 log(INFO, "Deleting stream '%s'.", stream.url().c_str());
454 for (const int fd : stream.data_fds()) {
458 vector<int> data_fds;
459 for (const int fd : stream.data_fds()) {
460 data_fds.push_back(fd);
463 servers->add_stream_from_serialized(stream, data_fds);
464 deserialized_urls.insert(stream.url());
466 stream_headers_for_url.insert(make_pair(stream.url(), stream.stream_header()));
470 // Deserialize the inputs. Note that we don't actually add them to any stream yet.
471 for (const InputProto &serialized_input : loaded_state.inputs()) {
472 InputWithRefcount iwr;
473 iwr.input = create_input(serialized_input);
476 Input::Encoding src_encoding = serialized_input.is_metacube_encoded() ?
477 Input::INPUT_ENCODING_METACUBE :
478 Input::INPUT_ENCODING_RAW;
479 InputKey key(serialized_input.url(), src_encoding);
480 inputs.insert(make_pair(key, iwr));
483 // Deserialize the acceptors.
484 for (const AcceptorProto &serialized_acceptor : loaded_state.acceptors()) {
485 AcceptorConfig config;
486 config.addr = extract_address_from_acceptor_proto(serialized_acceptor);
487 config.certificate_chain = serialized_acceptor.certificate_chain();
488 config.private_key = serialized_acceptor.private_key();
489 deserialized_acceptors.insert(make_pair(
491 new Acceptor(serialized_acceptor)));
494 log(INFO, "Deserialization done.");
497 // Add any new inputs coming from the config.
498 create_config_inputs(config, &inputs);
500 // Find all streams in the configuration file, create them, and connect to the inputs.
501 create_streams(config, deserialized_urls, &inputs);
502 vector<Acceptor *> acceptors = create_acceptors(config, &deserialized_acceptors);
504 // Make all the servers create TLS contexts for every TLS keypair we have.
505 for (Acceptor *acceptor : acceptors) {
506 if (acceptor->is_tls()) {
507 servers->create_tls_context_for_acceptor(acceptor);
511 // Allocate strings for the short responses.
512 vector<shared_ptr<const string>> short_response_pool;
513 for (const ShortResponsePool &str : loaded_state.short_response_pool()) {
514 short_response_pool.emplace_back(new string(str.header_or_short_response()));
517 // Put back the existing clients. It doesn't matter which server we
518 // allocate them to, so just do round-robin. However, we need to sort them
519 // by connection time first, since add_client_serialized() expects that.
520 sort(loaded_state.mutable_clients()->begin(),
521 loaded_state.mutable_clients()->end(),
522 OrderByConnectionTime());
523 for (int i = 0; i < loaded_state.clients_size(); ++i) {
524 if (!loaded_state.clients(i).url().empty() &&
525 all_urls.count(loaded_state.clients(i).url()) == 0) {
526 // Belongs to a dead stream (not keepalive), so we just have to close.
527 safe_close(loaded_state.clients(i).sock());
529 servers->add_client_from_serialized(loaded_state.clients(i), short_response_pool);
533 short_response_pool.clear(); // No longer needed; the clients have their own refcounts now.
535 // Put back the HLS zombies. There's no really good allocation here
536 // except round-robin; it would be marginally more efficient to match it
537 // to the client (since that would have them deleted immediately when
538 // the client requests the next fragment, instead of being later weeded
539 // out during statistics collection), but it's not a big deal.
540 for (const HLSZombieProto &zombie_proto : loaded_state.hls_zombies()) {
541 servers->add_hls_zombie_from_serialized(zombie_proto);
546 // Now delete all inputs that are longer in use, and start the others.
547 for (auto input_it = inputs.begin(); input_it != inputs.end(); ) {
548 if (input_it->second.refcount == 0) {
549 if (input_it->first.second == Input::INPUT_ENCODING_RAW) {
550 log(WARNING, "Raw input '%s' no longer in use, closing.",
551 input_it->first.first.c_str());
553 assert(input_it->first.second == Input::INPUT_ENCODING_METACUBE);
554 log(WARNING, "Metacube input '%s' no longer in use, closing.",
555 input_it->first.first.c_str());
557 input_it->second.input->close_socket();
558 delete input_it->second.input;
559 inputs.erase(input_it++);
561 input_it->second.input->run();
566 // Start writing statistics.
567 unique_ptr<StatsThread> stats_thread;
568 if (!config.stats_file.empty()) {
569 stats_thread.reset(new StatsThread(config.stats_file, config.stats_interval));
573 unique_ptr<InputStatsThread> input_stats_thread;
574 if (!config.input_stats_file.empty()) {
575 vector<Input*> inputs_no_refcount;
576 for (const auto &key_and_input_with_refcount : inputs) {
577 inputs_no_refcount.push_back(key_and_input_with_refcount.second.input);
580 input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount));
581 input_stats_thread->run();
584 timespec server_start;
585 int err = clock_gettime(CLOCK_MONOTONIC, &server_start);
587 if (state_fd != -1) {
588 // Measure time from we started deserializing (below) to now, when basically everything
589 // is up and running. This is, in other words, a conservative estimate of how long our
590 // “glitch” period was, not counting of course reconnects if the configuration changed.
591 double glitch_time = server_start.tv_sec - serialize_start.tv_sec +
592 1e-9 * (server_start.tv_nsec - serialize_start.tv_nsec);
593 log(INFO, "Re-exec happened in approx. %.0f ms.", glitch_time * 1000.0);
596 sd_notify(0, "READY=1");
603 sd_notify(0, "STOPPING=1");
605 sd_notify(0, "RELOADING=1");
608 // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
609 err = clock_gettime(CLOCK_MONOTONIC, &serialize_start);
612 if (input_stats_thread != nullptr) {
613 input_stats_thread->stop();
614 input_stats_thread.reset();
616 if (stats_thread != nullptr) {
617 stats_thread->stop();
618 stats_thread.reset();
620 for (Acceptor *acceptor : acceptors) {
623 for (const auto &key_and_input_with_refcount : inputs) {
624 key_and_input_with_refcount.second.input->stop();
628 CubemapStateProto state;
630 log(INFO, "Shutting down.");
632 log(INFO, "Serializing state and re-execing...");
633 state = collect_state(
634 serialize_start, acceptors, inputs, servers);
636 state.SerializeToString(&serialized);
637 state_fd = make_tempfile(serialized);
638 if (state_fd == -1) {
652 // OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
653 if (!dry_run_config(argv0_canon, config_filename_canon)) {
654 open_logs(config.log_destinations);
655 log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
662 sprintf(buf, "%d", state_fd);
665 execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, nullptr);
666 open_logs(config.log_destinations);
667 log_perror("execlp");
668 log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);