13 #include <systemd/sd-daemon.h>
19 #include <unordered_map>
26 #include "accesslog.h"
29 #include "input_stats.h"
31 #include "sa_compare.h"
32 #include "serverpool.h"
41 AccessLogThread *access_log = nullptr;
42 ServerPool *servers = nullptr;
43 volatile bool hupped = false;
44 volatile bool stopped = false;
46 typedef pair<string, Input::Encoding> InputKey;
50 struct OrderByConnectionTime {
51 bool operator() (const ClientProto &a, const ClientProto &b) const {
52 if (a.connect_time_sec() != b.connect_time_sec())
53 return a.connect_time_sec() < b.connect_time_sec();
54 return a.connect_time_nsec() < b.connect_time_nsec();
58 // An arbitrary ordering.
59 struct AcceptorConfigCompare {
60 bool operator() (const AcceptorConfig &a, const AcceptorConfig &b) const {
61 int cmp = a.certificate_chain.compare(b.certificate_chain);
66 cmp = a.private_key.compare(b.private_key);
71 return Sockaddr6Compare()(a.addr, b.addr);
77 struct InputWithRefcount {
85 if (signum == SIGINT) {
90 void do_nothing(int signum)
94 CubemapStateProto collect_state(const timespec &serialize_start,
95 const vector<Acceptor *> acceptors,
96 const multimap<InputKey, InputWithRefcount> inputs,
99 CubemapStateProto state = servers->serialize(); // Fills streams() and clients().
100 state.set_serialize_start_sec(serialize_start.tv_sec);
101 state.set_serialize_start_usec(serialize_start.tv_nsec / 1000);
103 for (Acceptor *acceptor : acceptors) {
104 state.add_acceptors()->MergeFrom(acceptor->serialize());
107 for (const auto &key_and_input_with_refcount : inputs) {
108 state.add_inputs()->MergeFrom(key_and_input_with_refcount.second.input->serialize());
114 // Find all port statements in the configuration file, and create acceptors for them.
115 vector<Acceptor *> create_acceptors(
116 const Config &config,
117 map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> *deserialized_acceptors)
119 vector<Acceptor *> acceptors;
120 for (const AcceptorConfig &acceptor_config : config.acceptors) {
121 Acceptor *acceptor = nullptr;
122 const auto deserialized_acceptor_it = deserialized_acceptors->find(acceptor_config);
123 if (deserialized_acceptor_it != deserialized_acceptors->end()) {
124 acceptor = deserialized_acceptor_it->second;
125 deserialized_acceptors->erase(deserialized_acceptor_it);
127 int server_sock = create_server_socket(acceptor_config.addr, TCP_SOCKET);
128 acceptor = new Acceptor(server_sock, acceptor_config.addr,
129 acceptor_config.certificate_chain,
130 acceptor_config.private_key);
133 acceptors.push_back(acceptor);
136 // Close all acceptors that are no longer in the configuration file.
137 for (auto &config_and_acceptor : *deserialized_acceptors) {
138 config_and_acceptor.second->close_socket();
139 delete config_and_acceptor.second;
145 void create_config_input(const string &src, Input::Encoding encoding, multimap<InputKey, InputWithRefcount> *inputs)
150 InputKey key(src, encoding);
151 if (inputs->count(key) != 0) {
155 InputWithRefcount iwr;
156 iwr.input = create_input(src, encoding);
157 if (iwr.input == nullptr) {
158 log(ERROR, "did not understand URL '%s' or source encoding was invalid, clients will not get any data.",
163 inputs->insert(make_pair(key, iwr));
166 // Find all streams in the configuration file, and create inputs for them.
167 void create_config_inputs(const Config &config, multimap<InputKey, InputWithRefcount> *inputs)
169 for (const StreamConfig &stream_config : config.streams) {
170 if (stream_config.src == "delete") {
171 // Ignored for pre-1.4.0 configuration compatibility.
174 create_config_input(stream_config.src, Input::Encoding(stream_config.src_encoding), inputs);
176 for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
177 create_config_input(udpstream_config.src, Input::INPUT_ENCODING_RAW, inputs);
181 void create_streams(const Config &config,
182 const set<string> &deserialized_urls,
183 multimap<InputKey, InputWithRefcount> *inputs)
186 set<string> expecting_urls = deserialized_urls;
187 for (const StreamConfig &stream_config : config.streams) {
190 expecting_urls.erase(stream_config.url);
192 if (stream_config.src == "delete") {
193 // Ignored for pre-1.4.0 configuration compatibility.
197 if (deserialized_urls.count(stream_config.url) == 0) {
198 stream_index = servers->add_stream(stream_config.url,
199 stream_config.hls_url,
200 stream_config.backlog_size,
201 stream_config.prebuffering_bytes,
202 Stream::Encoding(stream_config.encoding),
203 Stream::Encoding(stream_config.src_encoding),
204 stream_config.hls_frag_duration,
205 stream_config.hls_backlog_margin,
206 stream_config.allow_origin);
208 stream_index = servers->lookup_stream_by_url(stream_config.url);
209 assert(stream_index != -1);
210 servers->set_backlog_size(stream_index, stream_config.backlog_size);
211 if (!stream_config.hls_url.empty()) {
212 servers->register_hls_url(stream_index, stream_config.hls_url);
214 servers->set_prebuffering_bytes(stream_index, stream_config.prebuffering_bytes);
215 servers->set_encoding(stream_index,
216 Stream::Encoding(stream_config.encoding));
217 servers->set_src_encoding(stream_index,
218 Stream::Encoding(stream_config.src_encoding));
219 servers->set_hls_frag_duration(stream_index, stream_config.hls_frag_duration);
220 servers->set_hls_backlog_margin(stream_index, stream_config.hls_backlog_margin);
221 servers->set_allow_origin(stream_index, stream_config.allow_origin);
224 servers->set_pacing_rate(stream_index, stream_config.pacing_rate);
226 string src = stream_config.src;
227 Input::Encoding src_encoding = Input::Encoding(stream_config.src_encoding);
229 const auto input_it = inputs->find(make_pair(src, src_encoding));
230 if (input_it != inputs->end()) {
231 input_it->second.input->add_destination(stream_index);
232 ++input_it->second.refcount;
237 // Warn about any streams servers we've lost.
238 for (const string &url : expecting_urls) {
239 log(WARNING, "stream '%s' disappeared from the configuration file. "
240 "It will not be deleted, but clients will not get any new inputs. "
241 "If you really meant to delete it, set src=delete and reload.",
246 for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
247 int stream_index = servers->add_udpstream(
248 udpstream_config.dst,
249 udpstream_config.pacing_rate,
250 udpstream_config.ttl,
251 udpstream_config.multicast_iface_index);
253 string src = udpstream_config.src;
255 auto input_it = inputs->find(make_pair(src, Input::INPUT_ENCODING_RAW));
256 assert(input_it != inputs->end());
257 input_it->second.input->add_destination(stream_index);
258 ++input_it->second.refcount;
262 // HTTP gen204 endpoints.
263 for (const Gen204Config &ping_config : config.pings) {
264 servers->add_gen204(ping_config.url, ping_config.allow_origin);
268 void open_logs(const vector<LogConfig> &log_destinations)
270 for (const LogConfig &log_destination : log_destinations) {
271 if (log_destination.type == LogConfig::LOG_TYPE_FILE) {
272 add_log_destination_file(log_destination.filename);
273 } else if (log_destination.type == LogConfig::LOG_TYPE_CONSOLE) {
274 add_log_destination_console();
275 } else if (log_destination.type == LogConfig::LOG_TYPE_SYSLOG) {
276 add_log_destination_syslog();
284 bool dry_run_config(const string &argv0, const string &config_filename)
286 char *argv0_copy = strdup(argv0.c_str());
287 char *config_filename_copy = strdup(config_filename.c_str());
292 log_perror("fork()");
294 free(config_filename_copy);
298 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, nullptr);
299 log_perror(argv0_copy);
307 free(config_filename_copy);
312 err = waitpid(pid, &status, 0);
313 } while (err == -1 && errno == EINTR);
316 log_perror("waitpid()");
320 return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
323 void find_all_streams(const Config &config, set<string> *all_urls)
325 for (const StreamConfig &stream_config : config.streams) {
326 if (stream_config.src == "delete") {
327 log(WARNING, "stream '%s' has src=delete; ignoring it. Since Cubemap 1.4.0, you do not "
328 "need to set src=delete to delete streams anymore; just delete them from "
329 "the configuration file.",
330 stream_config.url.c_str());
332 all_urls->insert(stream_config.url);
337 int main(int argc, char **argv)
341 signal(SIGUSR1, do_nothing); // Used in internal signalling.
342 signal(SIGPIPE, SIG_IGN);
348 bool test_config = false;
350 static const option long_options[] = {
351 { "state", required_argument, 0, 's' },
352 { "test-config", no_argument, 0, 't' },
355 int option_index = 0;
356 int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
363 state_fd = atoi(optarg);
369 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
374 string config_filename = "cubemap.config";
376 config_filename = argv[optind++];
379 // Canonicalize argv[0] and config_filename.
380 char argv0_canon[PATH_MAX];
381 char config_filename_canon[PATH_MAX];
383 if (realpath("/proc/self/exe", argv0_canon) == nullptr) {
387 if (realpath(config_filename.c_str(), config_filename_canon) == nullptr) {
388 log_perror(config_filename.c_str());
392 // Now parse the configuration file.
394 if (!parse_config(config_filename_canon, &config)) {
401 // Ideally we'd like to daemonize only when we've started up all threads etc.,
402 // but daemon() forks, which is not good in multithreaded software, so we'll
403 // have to do it here.
404 if (config.daemonize) {
405 if (daemon(0, 0) == -1) {
406 log_perror("daemon");
412 // Open logs as soon as possible.
413 open_logs(config.log_destinations);
415 log(INFO, "Cubemap " SERVER_VERSION " starting.");
416 if (config.access_log_file.empty()) {
417 // Create a dummy logger.
418 access_log = new AccessLogThread();
420 access_log = new AccessLogThread(config.access_log_file);
424 servers = new ServerPool(config.num_servers);
426 // Find all the streams that are to be kept.
427 set<string> all_urls;
428 find_all_streams(config, &all_urls);
430 CubemapStateProto loaded_state;
431 timespec serialize_start;
432 set<string> deserialized_urls;
433 map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> deserialized_acceptors;
434 multimap<InputKey, InputWithRefcount> inputs; // multimap due to older versions without deduplication.
435 if (state_fd != -1) {
436 log(INFO, "Deserializing state from previous process...");
438 if (!read_tempfile_and_close(state_fd, &serialized)) {
441 if (!loaded_state.ParseFromString(serialized)) {
442 log(ERROR, "Failed deserialization of state.");
446 serialize_start.tv_sec = loaded_state.serialize_start_sec();
447 serialize_start.tv_nsec = loaded_state.serialize_start_usec() * 1000ull;
449 // Deserialize the streams.
450 unordered_map<string, string> stream_headers_for_url; // See below.
451 for (const StreamProto &stream : loaded_state.streams()) {
452 if (all_urls.count(stream.url()) == 0) {
453 // Delete the stream backlogs.
454 log(INFO, "Deleting stream '%s'.", stream.url().c_str());
455 for (const int fd : stream.data_fds()) {
459 vector<int> data_fds;
460 for (const int fd : stream.data_fds()) {
461 data_fds.push_back(fd);
464 servers->add_stream_from_serialized(stream, data_fds);
465 deserialized_urls.insert(stream.url());
467 stream_headers_for_url.insert(make_pair(stream.url(), stream.stream_header()));
471 // Deserialize the inputs. Note that we don't actually add them to any stream yet.
472 for (const InputProto &serialized_input : loaded_state.inputs()) {
473 InputWithRefcount iwr;
474 iwr.input = create_input(serialized_input);
477 Input::Encoding src_encoding = serialized_input.is_metacube_encoded() ?
478 Input::INPUT_ENCODING_METACUBE :
479 Input::INPUT_ENCODING_RAW;
480 InputKey key(serialized_input.url(), src_encoding);
481 inputs.insert(make_pair(key, iwr));
484 // Deserialize the acceptors.
485 for (const AcceptorProto &serialized_acceptor : loaded_state.acceptors()) {
486 AcceptorConfig config;
487 config.addr = extract_address_from_acceptor_proto(serialized_acceptor);
488 config.certificate_chain = serialized_acceptor.certificate_chain();
489 config.private_key = serialized_acceptor.private_key();
490 deserialized_acceptors.insert(make_pair(
492 new Acceptor(serialized_acceptor)));
495 log(INFO, "Deserialization done.");
498 // Add any new inputs coming from the config.
499 create_config_inputs(config, &inputs);
501 // Find all streams in the configuration file, create them, and connect to the inputs.
502 create_streams(config, deserialized_urls, &inputs);
503 vector<Acceptor *> acceptors = create_acceptors(config, &deserialized_acceptors);
505 // Make all the servers create TLS contexts for every TLS keypair we have.
506 for (Acceptor *acceptor : acceptors) {
507 if (acceptor->is_tls()) {
508 servers->create_tls_context_for_acceptor(acceptor);
512 // Allocate strings for the short responses.
513 vector<shared_ptr<const string>> short_response_pool;
514 for (const ShortResponsePool &str : loaded_state.short_response_pool()) {
515 short_response_pool.emplace_back(new string(str.header_or_short_response()));
518 // Put back the existing clients. It doesn't matter which server we
519 // allocate them to, so just do round-robin. However, we need to sort them
520 // by connection time first, since add_client_serialized() expects that.
521 sort(loaded_state.mutable_clients()->begin(),
522 loaded_state.mutable_clients()->end(),
523 OrderByConnectionTime());
524 for (int i = 0; i < loaded_state.clients_size(); ++i) {
525 if (!loaded_state.clients(i).url().empty() &&
526 all_urls.count(loaded_state.clients(i).url()) == 0) {
527 // Belongs to a dead stream (not keepalive), so we just have to close.
528 safe_close(loaded_state.clients(i).sock());
530 servers->add_client_from_serialized(loaded_state.clients(i), short_response_pool);
534 short_response_pool.clear(); // No longer needed; the clients have their own refcounts now.
536 // Put back the HLS zombies. There's no really good allocation here
537 // except round-robin; it would be marginally more efficient to match it
538 // to the client (since that would have them deleted immediately when
539 // the client requests the next fragment, instead of being later weeded
540 // out during statistics collection), but it's not a big deal.
541 for (const HLSZombieProto &zombie_proto : loaded_state.hls_zombies()) {
542 servers->add_hls_zombie_from_serialized(zombie_proto);
547 // Now delete all inputs that are longer in use, and start the others.
548 for (auto input_it = inputs.begin(); input_it != inputs.end(); ) {
549 if (input_it->second.refcount == 0) {
550 if (input_it->first.second == Input::INPUT_ENCODING_RAW) {
551 log(WARNING, "Raw input '%s' no longer in use, closing.",
552 input_it->first.first.c_str());
554 assert(input_it->first.second == Input::INPUT_ENCODING_METACUBE);
555 log(WARNING, "Metacube input '%s' no longer in use, closing.",
556 input_it->first.first.c_str());
558 input_it->second.input->close_socket();
559 delete input_it->second.input;
560 inputs.erase(input_it++);
562 input_it->second.input->run();
567 // Start writing statistics.
568 unique_ptr<StatsThread> stats_thread;
569 if (!config.stats_file.empty()) {
570 stats_thread.reset(new StatsThread(config.stats_file, config.stats_interval));
574 unique_ptr<InputStatsThread> input_stats_thread;
575 if (!config.input_stats_file.empty()) {
576 vector<Input*> inputs_no_refcount;
577 for (const auto &key_and_input_with_refcount : inputs) {
578 inputs_no_refcount.push_back(key_and_input_with_refcount.second.input);
581 input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount));
582 input_stats_thread->run();
585 timespec server_start;
586 int err = clock_gettime(CLOCK_MONOTONIC, &server_start);
588 if (state_fd != -1) {
589 // Measure time from we started deserializing (below) to now, when basically everything
590 // is up and running. This is, in other words, a conservative estimate of how long our
591 // “glitch” period was, not counting of course reconnects if the configuration changed.
592 double glitch_time = server_start.tv_sec - serialize_start.tv_sec +
593 1e-9 * (server_start.tv_nsec - serialize_start.tv_nsec);
594 log(INFO, "Re-exec happened in approx. %.0f ms.", glitch_time * 1000.0);
597 sd_notify(0, "READY=1");
604 sd_notify(0, "STOPPING=1");
606 sd_notify(0, "RELOADING=1");
609 // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
610 err = clock_gettime(CLOCK_MONOTONIC, &serialize_start);
613 if (input_stats_thread != nullptr) {
614 input_stats_thread->stop();
615 input_stats_thread.reset();
617 if (stats_thread != nullptr) {
618 stats_thread->stop();
619 stats_thread.reset();
621 for (Acceptor *acceptor : acceptors) {
624 for (const auto &key_and_input_with_refcount : inputs) {
625 key_and_input_with_refcount.second.input->stop();
627 key_and_input_with_refcount.second.input->close_socket();
632 CubemapStateProto state;
634 log(INFO, "Shutting down.");
636 log(INFO, "Serializing state and re-execing...");
637 state = collect_state(
638 serialize_start, acceptors, inputs, servers);
640 state.SerializeToString(&serialized);
641 state_fd = make_tempfile(serialized);
642 if (state_fd == -1) {
656 // OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
657 if (!dry_run_config(argv0_canon, config_filename_canon)) {
658 open_logs(config.log_destinations);
659 log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
666 sprintf(buf, "%d", state_fd);
668 // Unset the close-on-exec flag for the state fd.
669 // (This can't leak into a child, since there's only one thread left.)
670 fcntl(state_fd, F_SETFD, 0);
673 execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, nullptr);
674 open_logs(config.log_destinations);
675 log_perror("execlp");
676 log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);