]> git.sesse.net Git - cubemap/blob - main.cpp
fc36861f64ab827d22b988f387e1a2b2e9cba75b
[cubemap] / main.cpp
1 #include <assert.h>
2 #include <errno.h>
3 #include <getopt.h>
4 #include <limits.h>
5 #include <signal.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <sys/wait.h>
12 #include <systemd/sd-daemon.h>
13 #include <unistd.h>
14 #include <algorithm>
15 #include <map>
16 #include <set>
17 #include <string>
18 #include <utility>
19 #include <vector>
20
21 #include "tlse.h"
22
23 #include "acceptor.h"
24 #include "accesslog.h"
25 #include "config.h"
26 #include "input.h"
27 #include "input_stats.h"
28 #include "log.h"
29 #include "sa_compare.h"
30 #include "serverpool.h"
31 #include "state.pb.h"
32 #include "stats.h"
33 #include "stream.h"
34 #include "util.h"
35 #include "version.h"
36
37 using namespace std;
38
39 AccessLogThread *access_log = nullptr;
40 ServerPool *servers = nullptr;
41 volatile bool hupped = false;
42 volatile bool stopped = false;
43
44 typedef pair<string, Input::Encoding> InputKey;
45
46 namespace {
47
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();
53         }
54 };
55
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);
60                 if (cmp != 0) {
61                         return cmp < 0;
62                 }
63
64                 cmp = a.private_key.compare(b.private_key);
65                 if (cmp != 0) {
66                         return cmp < 0;
67                 }
68
69                 return Sockaddr6Compare()(a.addr, b.addr);
70         }
71 };
72
73 }  // namespace
74
75 struct InputWithRefcount {
76         Input *input;
77         int refcount;
78 };
79
80 void hup(int signum)
81 {
82         hupped = true;
83         if (signum == SIGINT) {
84                 stopped = true;
85         }
86 }
87
88 void do_nothing(int signum)
89 {
90 }
91
92 CubemapStateProto collect_state(const timespec &serialize_start,
93                                 const vector<Acceptor *> acceptors,
94                                 const multimap<InputKey, InputWithRefcount> inputs,
95                                 ServerPool *servers)
96 {
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);
100
101         for (Acceptor *acceptor : acceptors) {  
102                 state.add_acceptors()->MergeFrom(acceptor->serialize());
103         }
104
105         for (const auto &key_and_input_with_refcount : inputs) {
106                 state.add_inputs()->MergeFrom(key_and_input_with_refcount.second.input->serialize());
107         }
108
109         return state;
110 }
111
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)
116 {
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);
124                 } else {
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);
129                 }
130                 acceptor->run();
131                 acceptors.push_back(acceptor);
132         }
133
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;
138         }
139
140         return acceptors;
141 }
142
143 void create_config_input(const string &src, Input::Encoding encoding, multimap<InputKey, InputWithRefcount> *inputs)
144 {
145         if (src.empty()) {
146                 return;
147         }
148         InputKey key(src, encoding);
149         if (inputs->count(key) != 0) {
150                 return;
151         }
152
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.",
157                         src.c_str());
158                 return;
159         }
160         iwr.refcount = 0;
161         inputs->insert(make_pair(key, iwr));
162 }
163
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)
166 {
167         for (const StreamConfig &stream_config : config.streams) {
168                 if (stream_config.src == "delete") {
169                         // Ignored for pre-1.4.0 configuration compatibility.
170                         continue;
171                 }
172                 create_config_input(stream_config.src, Input::Encoding(stream_config.src_encoding), inputs);
173         }
174         for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
175                 create_config_input(udpstream_config.src, Input::INPUT_ENCODING_RAW, inputs);
176         }
177 }
178
179 void create_streams(const Config &config,
180                     const set<string> &deserialized_urls,
181                     multimap<InputKey, InputWithRefcount> *inputs)
182 {
183         // HTTP streams.
184         set<string> expecting_urls = deserialized_urls;
185         for (const StreamConfig &stream_config : config.streams) {
186                 int stream_index;
187
188                 expecting_urls.erase(stream_config.url);
189
190                 if (stream_config.src == "delete") {
191                         // Ignored for pre-1.4.0 configuration compatibility.
192                         continue;
193                 }
194
195                 if (deserialized_urls.count(stream_config.url) == 0) {
196                         stream_index = servers->add_stream(stream_config.url,
197                                                            stream_config.hls_url,
198                                                            stream_config.backlog_size,
199                                                            stream_config.prebuffering_bytes,
200                                                            Stream::Encoding(stream_config.encoding),
201                                                            Stream::Encoding(stream_config.src_encoding),
202                                                            stream_config.hls_frag_duration,
203                                                            stream_config.hls_backlog_margin,
204                                                            stream_config.allow_origin);
205                 } else {
206                         stream_index = servers->lookup_stream_by_url(stream_config.url);
207                         assert(stream_index != -1);
208                         servers->set_backlog_size(stream_index, stream_config.backlog_size);
209                         if (!stream_config.hls_url.empty()) {
210                                 servers->register_hls_url(stream_index, stream_config.hls_url);
211                         }
212                         servers->set_prebuffering_bytes(stream_index, stream_config.prebuffering_bytes);
213                         servers->set_encoding(stream_index,
214                                               Stream::Encoding(stream_config.encoding));
215                         servers->set_src_encoding(stream_index,
216                                                   Stream::Encoding(stream_config.src_encoding));
217                         servers->set_hls_frag_duration(stream_index, stream_config.hls_frag_duration);
218                         servers->set_hls_backlog_margin(stream_index, stream_config.hls_backlog_margin);
219                         servers->set_allow_origin(stream_index, stream_config.allow_origin);
220                 }
221
222                 servers->set_pacing_rate(stream_index, stream_config.pacing_rate);
223
224                 string src = stream_config.src;
225                 Input::Encoding src_encoding = Input::Encoding(stream_config.src_encoding);
226                 if (!src.empty()) {
227                         const auto input_it = inputs->find(make_pair(src, src_encoding));
228                         if (input_it != inputs->end()) {
229                                 input_it->second.input->add_destination(stream_index);
230                                 ++input_it->second.refcount;
231                         }
232                 }
233         }
234
235         // Warn about any streams servers we've lost.
236         for (const string &url : expecting_urls) {
237                 log(WARNING, "stream '%s' disappeared from the configuration file. "
238                              "It will not be deleted, but clients will not get any new inputs. "
239                              "If you really meant to delete it, set src=delete and reload.",
240                              url.c_str());
241         }
242
243         // UDP streams.
244         for (const UDPStreamConfig &udpstream_config : config.udpstreams) {
245                 int stream_index = servers->add_udpstream(
246                         udpstream_config.dst,
247                         udpstream_config.pacing_rate,
248                         udpstream_config.ttl,
249                         udpstream_config.multicast_iface_index);
250
251                 string src = udpstream_config.src;
252                 if (!src.empty()) {
253                         auto input_it = inputs->find(make_pair(src, Input::INPUT_ENCODING_RAW));
254                         assert(input_it != inputs->end());
255                         input_it->second.input->add_destination(stream_index);
256                         ++input_it->second.refcount;
257                 }
258         }
259
260         // HTTP gen204 endpoints.
261         for (const Gen204Config &ping_config : config.pings) {
262                 servers->add_gen204(ping_config.url, ping_config.allow_origin);
263         }
264 }
265         
266 void open_logs(const vector<LogConfig> &log_destinations)
267 {
268         for (const LogConfig &log_destination : log_destinations) {
269                 if (log_destination.type == LogConfig::LOG_TYPE_FILE) {
270                         add_log_destination_file(log_destination.filename);
271                 } else if (log_destination.type == LogConfig::LOG_TYPE_CONSOLE) {
272                         add_log_destination_console();
273                 } else if (log_destination.type == LogConfig::LOG_TYPE_SYSLOG) {
274                         add_log_destination_syslog();
275                 } else {
276                         assert(false);
277                 }
278         }
279         start_logging();
280 }
281         
282 bool dry_run_config(const string &argv0, const string &config_filename)
283 {
284         char *argv0_copy = strdup(argv0.c_str());
285         char *config_filename_copy = strdup(config_filename.c_str());
286
287         pid_t pid = fork();
288         switch (pid) {
289         case -1:
290                 log_perror("fork()");
291                 free(argv0_copy);
292                 free(config_filename_copy);
293                 return false;
294         case 0:
295                 // Child.
296                 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, nullptr);
297                 log_perror(argv0_copy);
298                 _exit(1);
299         default:
300                 // Parent.
301                 break;
302         }
303                 
304         free(argv0_copy);
305         free(config_filename_copy);
306
307         int status;
308         pid_t err;
309         do {
310                 err = waitpid(pid, &status, 0);
311         } while (err == -1 && errno == EINTR);
312
313         if (err == -1) {
314                 log_perror("waitpid()");
315                 return false;
316         }       
317
318         return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
319 }
320
321 void find_all_streams(const Config &config, set<string> *all_urls)
322 {
323         for (const StreamConfig &stream_config : config.streams) {
324                 if (stream_config.src == "delete") {
325                         log(WARNING, "stream '%s' has src=delete; ignoring it. Since Cubemap 1.4.0, you do not "
326                                      "need to set src=delete to delete streams anymore; just delete them from "
327                                      "the configuration file.",
328                                      stream_config.url.c_str());
329                 } else {
330                         all_urls->insert(stream_config.url);
331                 }
332         }
333 }
334
335 int main(int argc, char **argv)
336 {
337         signal(SIGHUP, hup);
338         signal(SIGINT, hup);
339         signal(SIGUSR1, do_nothing);  // Used in internal signalling.
340         signal(SIGPIPE, SIG_IGN);
341
342         tls_init();
343         
344         // Parse options.
345         int state_fd = -1;
346         bool test_config = false;
347         for ( ;; ) {
348                 static const option long_options[] = {
349                         { "state", required_argument, 0, 's' },
350                         { "test-config", no_argument, 0, 't' },
351                         { 0, 0, 0, 0 }
352                 };
353                 int option_index = 0;
354                 int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
355      
356                 if (c == -1) {
357                         break;
358                 }
359                 switch (c) {
360                 case 's':
361                         state_fd = atoi(optarg);
362                         break;
363                 case 't':
364                         test_config = true;
365                         break;
366                 default:
367                         fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
368                         exit(1);
369                 }
370         }
371
372         string config_filename = "cubemap.config";
373         if (optind < argc) {
374                 config_filename = argv[optind++];
375         }
376
377         // Canonicalize argv[0] and config_filename.
378         char argv0_canon[PATH_MAX];
379         char config_filename_canon[PATH_MAX];
380
381         if (realpath("/proc/self/exe", argv0_canon) == nullptr) {
382                 log_perror(argv[0]);
383                 exit(1);
384         }
385         if (realpath(config_filename.c_str(), config_filename_canon) == nullptr) {
386                 log_perror(config_filename.c_str());
387                 exit(1);
388         }
389
390         // Now parse the configuration file.
391         Config config;
392         if (!parse_config(config_filename_canon, &config)) {
393                 exit(1);
394         }
395         if (test_config) {
396                 exit(0);
397         }
398         
399         // Ideally we'd like to daemonize only when we've started up all threads etc.,
400         // but daemon() forks, which is not good in multithreaded software, so we'll
401         // have to do it here.
402         if (config.daemonize) {
403                 if (daemon(0, 0) == -1) {
404                         log_perror("daemon");
405                         exit(1);
406                 }
407         }
408
409 start:
410         // Open logs as soon as possible.
411         open_logs(config.log_destinations);
412
413         log(INFO, "Cubemap " SERVER_VERSION " starting.");
414         if (config.access_log_file.empty()) {
415                 // Create a dummy logger.
416                 access_log = new AccessLogThread();
417         } else {
418                 access_log = new AccessLogThread(config.access_log_file);
419         }
420         access_log->run();
421
422         servers = new ServerPool(config.num_servers);
423
424         // Find all the streams that are to be kept.
425         set<string> all_urls;
426         find_all_streams(config, &all_urls);
427
428         CubemapStateProto loaded_state;
429         timespec serialize_start;
430         set<string> deserialized_urls;
431         map<AcceptorConfig, Acceptor *, AcceptorConfigCompare> deserialized_acceptors;
432         multimap<InputKey, InputWithRefcount> inputs;  // multimap due to older versions without deduplication.
433         if (state_fd != -1) {
434                 log(INFO, "Deserializing state from previous process...");
435                 string serialized;
436                 if (!read_tempfile_and_close(state_fd, &serialized)) {
437                         exit(1);
438                 }
439                 if (!loaded_state.ParseFromString(serialized)) {
440                         log(ERROR, "Failed deserialization of state.");
441                         exit(1);
442                 }
443
444                 serialize_start.tv_sec = loaded_state.serialize_start_sec();
445                 serialize_start.tv_nsec = loaded_state.serialize_start_usec() * 1000ull;
446
447                 // Deserialize the streams.
448                 map<string, string> stream_headers_for_url;  // See below.
449                 for (const StreamProto &stream : loaded_state.streams()) {
450                         if (all_urls.count(stream.url()) == 0) {
451                                 // Delete the stream backlogs.
452                                 log(INFO, "Deleting stream '%s'.", stream.url().c_str());
453                                 for (const int fd : stream.data_fds()) {
454                                         safe_close(fd);
455                                 }
456                         } else {
457                                 vector<int> data_fds;
458                                 for (const int fd : stream.data_fds()) {
459                                         data_fds.push_back(fd);
460                                 }
461
462                                 servers->add_stream_from_serialized(stream, data_fds);
463                                 deserialized_urls.insert(stream.url());
464
465                                 stream_headers_for_url.insert(make_pair(stream.url(), stream.stream_header()));
466                         }
467                 }
468
469                 // Deserialize the inputs. Note that we don't actually add them to any stream yet.
470                 for (const InputProto &serialized_input : loaded_state.inputs()) {
471                         InputWithRefcount iwr;
472                         iwr.input = create_input(serialized_input);
473                         iwr.refcount = 0;
474
475                         Input::Encoding src_encoding = serialized_input.is_metacube_encoded() ?
476                                 Input::INPUT_ENCODING_METACUBE :
477                                 Input::INPUT_ENCODING_RAW;
478                         InputKey key(serialized_input.url(), src_encoding);
479                         inputs.insert(make_pair(key, iwr));
480                 } 
481
482                 // Deserialize the acceptors.
483                 for (const AcceptorProto &serialized_acceptor : loaded_state.acceptors()) {
484                         AcceptorConfig config;
485                         config.addr = extract_address_from_acceptor_proto(serialized_acceptor);
486                         config.certificate_chain = serialized_acceptor.certificate_chain();
487                         config.private_key = serialized_acceptor.private_key();
488                         deserialized_acceptors.insert(make_pair(
489                                 config,
490                                 new Acceptor(serialized_acceptor)));
491                 }
492
493                 log(INFO, "Deserialization done.");
494         }
495
496         // Add any new inputs coming from the config.
497         create_config_inputs(config, &inputs);
498         
499         // Find all streams in the configuration file, create them, and connect to the inputs.
500         create_streams(config, deserialized_urls, &inputs);
501         vector<Acceptor *> acceptors = create_acceptors(config, &deserialized_acceptors);
502
503         // Make all the servers create TLS contexts for every TLS keypair we have.
504         for (Acceptor *acceptor : acceptors) {
505                 if (acceptor->is_tls()) {
506                         servers->create_tls_context_for_acceptor(acceptor);
507                 }
508         }
509
510         // Allocate strings for the short responses.
511         vector<shared_ptr<const string>> short_response_pool;
512         for (const ShortResponsePool &str : loaded_state.short_response_pool()) {
513                 short_response_pool.emplace_back(new string(str.header_or_short_response()));
514         }
515         
516         // Put back the existing clients. It doesn't matter which server we
517         // allocate them to, so just do round-robin. However, we need to sort them
518         // by connection time first, since add_client_serialized() expects that.
519         sort(loaded_state.mutable_clients()->begin(),
520              loaded_state.mutable_clients()->end(),
521              OrderByConnectionTime());
522         for (int i = 0; i < loaded_state.clients_size(); ++i) {
523                 if (all_urls.count(loaded_state.clients(i).url()) == 0) {
524                         safe_close(loaded_state.clients(i).sock());
525                 } else {
526                         servers->add_client_from_serialized(loaded_state.clients(i), short_response_pool);
527                 }
528         }
529         
530         short_response_pool.clear();  // No longer needed; the clients have their own refcounts now.
531
532         servers->run();
533
534         // Now delete all inputs that are longer in use, and start the others.
535         for (auto input_it = inputs.begin(); input_it != inputs.end(); ) {
536                 if (input_it->second.refcount == 0) {
537                         if (input_it->first.second == Input::INPUT_ENCODING_RAW) {
538                                 log(WARNING, "Raw input '%s' no longer in use, closing.",
539                                     input_it->first.first.c_str());
540                         } else {
541                                 assert(input_it->first.second == Input::INPUT_ENCODING_METACUBE);
542                                 log(WARNING, "Metacube input '%s' no longer in use, closing.",
543                                     input_it->first.first.c_str());
544                         }
545                         input_it->second.input->close_socket();
546                         delete input_it->second.input;
547                         inputs.erase(input_it++);
548                 } else {
549                         input_it->second.input->run();
550                         ++input_it;
551                 }
552         }
553
554         // Start writing statistics.
555         unique_ptr<StatsThread> stats_thread;
556         if (!config.stats_file.empty()) {
557                 stats_thread.reset(new StatsThread(config.stats_file, config.stats_interval));
558                 stats_thread->run();
559         }
560
561         unique_ptr<InputStatsThread> input_stats_thread;
562         if (!config.input_stats_file.empty()) {
563                 vector<Input*> inputs_no_refcount;
564                 for (const auto &key_and_input_with_refcount : inputs) {
565                         inputs_no_refcount.push_back(key_and_input_with_refcount.second.input);
566                 }
567
568                 input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount));
569                 input_stats_thread->run();
570         }
571
572         timespec server_start;
573         int err = clock_gettime(CLOCK_MONOTONIC, &server_start);
574         assert(err != -1);
575         if (state_fd != -1) {
576                 // Measure time from we started deserializing (below) to now, when basically everything
577                 // is up and running. This is, in other words, a conservative estimate of how long our
578                 // “glitch” period was, not counting of course reconnects if the configuration changed.
579                 double glitch_time = server_start.tv_sec - serialize_start.tv_sec +
580                         1e-9 * (server_start.tv_nsec - serialize_start.tv_nsec);
581                 log(INFO, "Re-exec happened in approx. %.0f ms.", glitch_time * 1000.0);
582         }
583
584         sd_notify(0, "READY=1");
585
586         while (!hupped) {
587                 usleep(100000);
588         }
589
590         if (stopped) {
591                 sd_notify(0, "STOPPING=1");
592         } else {
593                 sd_notify(0, "RELOADING=1");
594         }
595
596         // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
597         err = clock_gettime(CLOCK_MONOTONIC, &serialize_start);
598         assert(err != -1);
599
600         if (input_stats_thread != nullptr) {
601                 input_stats_thread->stop();
602                 input_stats_thread.reset();
603         }
604         if (stats_thread != nullptr) {
605                 stats_thread->stop();
606                 stats_thread.reset();
607         }
608         for (Acceptor *acceptor : acceptors) {
609                 acceptor->stop();
610         }
611         for (const auto &key_and_input_with_refcount : inputs) {
612                 key_and_input_with_refcount.second.input->stop();
613         }
614         servers->stop();
615
616         CubemapStateProto state;
617         if (stopped) {
618                 log(INFO, "Shutting down.");
619         } else {
620                 log(INFO, "Serializing state and re-execing...");
621                 state = collect_state(
622                         serialize_start, acceptors, inputs, servers);
623                 string serialized;
624                 state.SerializeToString(&serialized);
625                 state_fd = make_tempfile(serialized);
626                 if (state_fd == -1) {
627                         exit(1);
628                 }
629         }
630         delete servers;
631
632         access_log->stop();
633         delete access_log;
634         shut_down_logging();
635
636         if (stopped) {
637                 exit(0);
638         }
639
640         // OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
641         if (!dry_run_config(argv0_canon, config_filename_canon)) {
642                 open_logs(config.log_destinations);
643                 log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
644                 hupped = false;
645                 shut_down_logging();
646                 goto start;
647         }
648          
649         char buf[16];
650         sprintf(buf, "%d", state_fd);
651
652         for ( ;; ) {
653                 execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, nullptr);
654                 open_logs(config.log_destinations);
655                 log_perror("execlp");
656                 log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);
657                 shut_down_logging();
658                 usleep(200000);
659         }
660 }