]> git.sesse.net Git - cubemap/blob - main.cpp
Create $(libdir) on make install.
[cubemap] / main.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <getopt.h>
5 #include <limits.h>
6 #include <signal.h>
7 #include <stddef.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <sys/wait.h>
13 #include <systemd/sd-daemon.h>
14 #include <unistd.h>
15 #include <algorithm>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <unordered_map>
20 #include <utility>
21 #include <vector>
22
23 #include "tlse.h"
24
25 #include "acceptor.h"
26 #include "accesslog.h"
27 #include "config.h"
28 #include "input.h"
29 #include "input_stats.h"
30 #include "log.h"
31 #include "sa_compare.h"
32 #include "serverpool.h"
33 #include "state.pb.h"
34 #include "stats.h"
35 #include "stream.h"
36 #include "util.h"
37 #include "version.h"
38
39 using namespace std;
40
41 AccessLogThread *access_log = nullptr;
42 ServerPool *servers = nullptr;
43 volatile bool hupped = false;
44 volatile bool stopped = false;
45
46 typedef pair<string, Input::Encoding> InputKey;
47
48 namespace {
49
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();
55         }
56 };
57
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);
62                 if (cmp != 0) {
63                         return cmp < 0;
64                 }
65
66                 cmp = a.private_key.compare(b.private_key);
67                 if (cmp != 0) {
68                         return cmp < 0;
69                 }
70
71                 return Sockaddr6Compare()(a.addr, b.addr);
72         }
73 };
74
75 }  // namespace
76
77 struct InputWithRefcount {
78         Input *input;
79         int refcount;
80 };
81
82 void hup(int signum)
83 {
84         hupped = true;
85         if (signum == SIGINT) {
86                 stopped = true;
87         }
88 }
89
90 void do_nothing(int signum)
91 {
92 }
93
94 CubemapStateProto collect_state(const timespec &serialize_start,
95                                 const vector<Acceptor *> acceptors,
96                                 const multimap<InputKey, InputWithRefcount> inputs,
97                                 ServerPool *servers)
98 {
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);
102
103         for (Acceptor *acceptor : acceptors) {  
104                 state.add_acceptors()->MergeFrom(acceptor->serialize());
105         }
106
107         for (const auto &key_and_input_with_refcount : inputs) {
108                 state.add_inputs()->MergeFrom(key_and_input_with_refcount.second.input->serialize());
109         }
110
111         return state;
112 }
113
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)
118 {
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);
126                 } else {
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);
131                 }
132                 acceptor->run();
133                 acceptors.push_back(acceptor);
134         }
135
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;
140         }
141
142         return acceptors;
143 }
144
145 void create_config_input(const string &src, Input::Encoding encoding, multimap<InputKey, InputWithRefcount> *inputs)
146 {
147         if (src.empty()) {
148                 return;
149         }
150         InputKey key(src, encoding);
151         if (inputs->count(key) != 0) {
152                 return;
153         }
154
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.",
159                         src.c_str());
160                 return;
161         }
162         iwr.refcount = 0;
163         inputs->insert(make_pair(key, iwr));
164 }
165
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)
168 {
169         for (const StreamConfig &stream_config : config.streams) {
170                 if (stream_config.src == "delete") {
171                         // Ignored for pre-1.4.0 configuration compatibility.
172                         continue;
173                 }
174                 create_config_input(stream_config.src, Input::Encoding(stream_config.src_encoding), inputs);
175         }
176         for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
177                 create_config_input(udpstream_config.src, Input::INPUT_ENCODING_RAW, inputs);
178         }
179 }
180
181 void create_streams(const Config &config,
182                     const set<string> &deserialized_urls,
183                     multimap<InputKey, InputWithRefcount> *inputs)
184 {
185         // HTTP streams.
186         set<string> expecting_urls = deserialized_urls;
187         for (const StreamConfig &stream_config : config.streams) {
188                 int stream_index;
189
190                 expecting_urls.erase(stream_config.url);
191
192                 if (stream_config.src == "delete") {
193                         // Ignored for pre-1.4.0 configuration compatibility.
194                         continue;
195                 }
196
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);
207                 } else {
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);
213                         }
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);
222                 }
223
224                 servers->set_pacing_rate(stream_index, stream_config.pacing_rate);
225
226                 string src = stream_config.src;
227                 Input::Encoding src_encoding = Input::Encoding(stream_config.src_encoding);
228                 if (!src.empty()) {
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;
233                         }
234                 }
235         }
236
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.",
242                              url.c_str());
243         }
244
245         // UDP streams.
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);
252
253                 string src = udpstream_config.src;
254                 if (!src.empty()) {
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;
259                 }
260         }
261
262         // HTTP gen204 endpoints.
263         for (const Gen204Config &ping_config : config.pings) {
264                 servers->add_gen204(ping_config.url, ping_config.allow_origin);
265         }
266 }
267         
268 void open_logs(const vector<LogConfig> &log_destinations)
269 {
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();
277                 } else {
278                         assert(false);
279                 }
280         }
281         start_logging();
282 }
283         
284 bool dry_run_config(const string &argv0, const string &config_filename)
285 {
286         char *argv0_copy = strdup(argv0.c_str());
287         char *config_filename_copy = strdup(config_filename.c_str());
288
289         pid_t pid = fork();
290         switch (pid) {
291         case -1:
292                 log_perror("fork()");
293                 free(argv0_copy);
294                 free(config_filename_copy);
295                 return false;
296         case 0:
297                 // Child.
298                 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, nullptr);
299                 log_perror(argv0_copy);
300                 _exit(1);
301         default:
302                 // Parent.
303                 break;
304         }
305                 
306         free(argv0_copy);
307         free(config_filename_copy);
308
309         int status;
310         pid_t err;
311         do {
312                 err = waitpid(pid, &status, 0);
313         } while (err == -1 && errno == EINTR);
314
315         if (err == -1) {
316                 log_perror("waitpid()");
317                 return false;
318         }       
319
320         return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
321 }
322
323 void find_all_streams(const Config &config, set<string> *all_urls)
324 {
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());
331                 } else {
332                         all_urls->insert(stream_config.url);
333                 }
334         }
335 }
336
337 int main(int argc, char **argv)
338 {
339         signal(SIGHUP, hup);
340         signal(SIGINT, hup);
341         signal(SIGUSR1, do_nothing);  // Used in internal signalling.
342         signal(SIGPIPE, SIG_IGN);
343
344         tls_init();
345         
346         // Parse options.
347         int state_fd = -1;
348         bool test_config = false;
349         for ( ;; ) {
350                 static const option long_options[] = {
351                         { "state", required_argument, 0, 's' },
352                         { "test-config", no_argument, 0, 't' },
353                         { 0, 0, 0, 0 }
354                 };
355                 int option_index = 0;
356                 int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
357      
358                 if (c == -1) {
359                         break;
360                 }
361                 switch (c) {
362                 case 's':
363                         state_fd = atoi(optarg);
364                         break;
365                 case 't':
366                         test_config = true;
367                         break;
368                 default:
369                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
370                         exit(1);
371                 }
372         }
373
374         string config_filename = "cubemap.config";
375         if (optind < argc) {
376                 config_filename = argv[optind++];
377         }
378
379         // Canonicalize argv[0] and config_filename.
380         char argv0_canon[PATH_MAX];
381         char config_filename_canon[PATH_MAX];
382
383         if (realpath("/proc/self/exe", argv0_canon) == nullptr) {
384                 log_perror(argv[0]);
385                 exit(1);
386         }
387         if (realpath(config_filename.c_str(), config_filename_canon) == nullptr) {
388                 log_perror(config_filename.c_str());
389                 exit(1);
390         }
391
392         // Now parse the configuration file.
393         Config config;
394         if (!parse_config(config_filename_canon, &config)) {
395                 exit(1);
396         }
397         if (test_config) {
398                 exit(0);
399         }
400         
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");
407                         exit(1);
408                 }
409         }
410
411 start:
412         // Open logs as soon as possible.
413         open_logs(config.log_destinations);
414
415         log(INFO, "Cubemap " SERVER_VERSION " starting.");
416         if (config.access_log_file.empty()) {
417                 // Create a dummy logger.
418                 access_log = new AccessLogThread();
419         } else {
420                 access_log = new AccessLogThread(config.access_log_file);
421         }
422         access_log->run();
423
424         servers = new ServerPool(config.num_servers);
425
426         // Find all the streams that are to be kept.
427         set<string> all_urls;
428         find_all_streams(config, &all_urls);
429
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...");
437                 string serialized;
438                 if (!read_tempfile_and_close(state_fd, &serialized)) {
439                         exit(1);
440                 }
441                 if (!loaded_state.ParseFromString(serialized)) {
442                         log(ERROR, "Failed deserialization of state.");
443                         exit(1);
444                 }
445
446                 serialize_start.tv_sec = loaded_state.serialize_start_sec();
447                 serialize_start.tv_nsec = loaded_state.serialize_start_usec() * 1000ull;
448
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()) {
456                                         safe_close(fd);
457                                 }
458                         } else {
459                                 vector<int> data_fds;
460                                 for (const int fd : stream.data_fds()) {
461                                         data_fds.push_back(fd);
462                                 }
463
464                                 servers->add_stream_from_serialized(stream, data_fds);
465                                 deserialized_urls.insert(stream.url());
466
467                                 stream_headers_for_url.insert(make_pair(stream.url(), stream.stream_header()));
468                         }
469                 }
470
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);
475                         iwr.refcount = 0;
476
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));
482                 } 
483
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(
491                                 config,
492                                 new Acceptor(serialized_acceptor)));
493                 }
494
495                 log(INFO, "Deserialization done.");
496         }
497
498         // Add any new inputs coming from the config.
499         create_config_inputs(config, &inputs);
500         
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);
504
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);
509                 }
510         }
511
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()));
516         }
517         
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());
529                 } else {
530                         servers->add_client_from_serialized(loaded_state.clients(i), short_response_pool);
531                 }
532         }
533         
534         short_response_pool.clear();  // No longer needed; the clients have their own refcounts now.
535
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);
543         }
544
545         servers->run();
546
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());
553                         } else {
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());
557                         }
558                         input_it->second.input->close_socket();
559                         delete input_it->second.input;
560                         inputs.erase(input_it++);
561                 } else {
562                         input_it->second.input->run();
563                         ++input_it;
564                 }
565         }
566
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));
571                 stats_thread->run();
572         }
573
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);
579                 }
580
581                 input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount));
582                 input_stats_thread->run();
583         }
584
585         timespec server_start;
586         int err = clock_gettime(CLOCK_MONOTONIC, &server_start);
587         assert(err != -1);
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);
595         }
596
597         sd_notify(0, "READY=1");
598
599         while (!hupped) {
600                 usleep(100000);
601         }
602
603         if (stopped) {
604                 sd_notify(0, "STOPPING=1");
605         } else {
606                 sd_notify(0, "RELOADING=1");
607         }
608
609         // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
610         err = clock_gettime(CLOCK_MONOTONIC, &serialize_start);
611         assert(err != -1);
612
613         if (input_stats_thread != nullptr) {
614                 input_stats_thread->stop();
615                 input_stats_thread.reset();
616         }
617         if (stats_thread != nullptr) {
618                 stats_thread->stop();
619                 stats_thread.reset();
620         }
621         for (Acceptor *acceptor : acceptors) {
622                 acceptor->stop();
623         }
624         for (const auto &key_and_input_with_refcount : inputs) {
625                 key_and_input_with_refcount.second.input->stop();
626                 if (stopped) {
627                         key_and_input_with_refcount.second.input->close_socket();
628                 }
629         }
630         servers->stop();
631
632         CubemapStateProto state;
633         if (stopped) {
634                 log(INFO, "Shutting down.");
635         } else {
636                 log(INFO, "Serializing state and re-execing...");
637                 state = collect_state(
638                         serialize_start, acceptors, inputs, servers);
639                 string serialized;
640                 state.SerializeToString(&serialized);
641                 state_fd = make_tempfile(serialized);
642                 if (state_fd == -1) {
643                         exit(1);
644                 }
645         }
646         delete servers;
647
648         access_log->stop();
649         delete access_log;
650         shut_down_logging();
651
652         if (stopped) {
653                 exit(0);
654         }
655
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]);
660                 hupped = false;
661                 shut_down_logging();
662                 goto start;
663         }
664          
665         char buf[16];
666         sprintf(buf, "%d", state_fd);
667
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);
671
672         for ( ;; ) {
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);
677                 shut_down_logging();
678                 usleep(200000);
679         }
680 }