20 #include "accesslog.h"
25 #include "serverpool.h"
34 AccessLogThread *access_log = NULL;
35 ServerPool *servers = NULL;
36 vector<MarkPool *> mark_pools;
37 volatile bool hupped = false;
38 volatile bool stopped = false;
40 struct InputWithRefcount {
48 if (signum == SIGINT) {
53 void do_nothing(int signum)
57 CubemapStateProto collect_state(const timeval &serialize_start,
58 const vector<Acceptor *> acceptors,
59 const multimap<string, InputWithRefcount> inputs,
62 CubemapStateProto state = servers->serialize(); // Fills streams() and clients().
63 state.set_serialize_start_sec(serialize_start.tv_sec);
64 state.set_serialize_start_usec(serialize_start.tv_usec);
66 for (size_t i = 0; i < acceptors.size(); ++i) {
67 state.add_acceptors()->MergeFrom(acceptors[i]->serialize());
70 for (multimap<string, InputWithRefcount>::const_iterator input_it = inputs.begin();
71 input_it != inputs.end();
73 state.add_inputs()->MergeFrom(input_it->second.input->serialize());
79 // Find all port statements in the configuration file, and create acceptors for htem.
80 vector<Acceptor *> create_acceptors(
82 map<int, Acceptor *> *deserialized_acceptors)
84 vector<Acceptor *> acceptors;
85 for (unsigned i = 0; i < config.acceptors.size(); ++i) {
86 const AcceptorConfig &acceptor_config = config.acceptors[i];
87 Acceptor *acceptor = NULL;
88 map<int, Acceptor *>::iterator deserialized_acceptor_it =
89 deserialized_acceptors->find(acceptor_config.port);
90 if (deserialized_acceptor_it != deserialized_acceptors->end()) {
91 acceptor = deserialized_acceptor_it->second;
92 deserialized_acceptors->erase(deserialized_acceptor_it);
94 int server_sock = create_server_socket(acceptor_config.port, TCP_SOCKET);
95 acceptor = new Acceptor(server_sock, acceptor_config.port);
98 acceptors.push_back(acceptor);
101 // Close all acceptors that are no longer in the configuration file.
102 for (map<int, Acceptor *>::iterator acceptor_it = deserialized_acceptors->begin();
103 acceptor_it != deserialized_acceptors->end();
105 acceptor_it->second->close_socket();
106 delete acceptor_it->second;
112 // Find all streams in the configuration file, and create inputs for them.
113 void create_config_inputs(const Config &config, multimap<string, InputWithRefcount> *inputs)
115 for (unsigned i = 0; i < config.streams.size(); ++i) {
116 const StreamConfig &stream_config = config.streams[i];
117 if (stream_config.src.empty()) {
121 string src = stream_config.src;
122 if (inputs->count(src) != 0) {
126 InputWithRefcount iwr;
127 iwr.input = create_input(src);
128 if (iwr.input == NULL) {
129 log(ERROR, "did not understand URL '%s', clients will not get any data.",
134 inputs->insert(make_pair(src, iwr));
138 void create_streams(const Config &config,
139 const set<string> &deserialized_stream_ids,
140 multimap<string, InputWithRefcount> *inputs)
142 for (unsigned i = 0; i < config.mark_pools.size(); ++i) {
143 const MarkPoolConfig &mp_config = config.mark_pools[i];
144 mark_pools.push_back(new MarkPool(mp_config.from, mp_config.to));
147 set<string> expecting_stream_ids = deserialized_stream_ids;
148 for (unsigned i = 0; i < config.streams.size(); ++i) {
149 const StreamConfig &stream_config = config.streams[i];
150 if (deserialized_stream_ids.count(stream_config.stream_id) == 0) {
151 servers->add_stream(stream_config.stream_id,
152 stream_config.backlog_size,
153 Stream::Encoding(stream_config.encoding));
155 servers->set_backlog_size(stream_config.stream_id, stream_config.backlog_size);
156 servers->set_encoding(stream_config.stream_id,
157 Stream::Encoding(stream_config.encoding));
159 expecting_stream_ids.erase(stream_config.stream_id);
161 if (stream_config.mark_pool != -1) {
162 servers->set_mark_pool(stream_config.stream_id,
163 mark_pools[stream_config.mark_pool]);
166 string src = stream_config.src;
168 multimap<string, InputWithRefcount>::iterator input_it = inputs->find(src);
169 assert(input_it != inputs->end());
170 input_it->second.input->add_destination(stream_config.stream_id);
171 ++input_it->second.refcount;
175 // Warn about any servers we've lost.
176 // TODO: Make an option (delete=yes?) to actually shut down streams.
177 for (set<string>::const_iterator stream_it = expecting_stream_ids.begin();
178 stream_it != expecting_stream_ids.end();
180 string stream_id = *stream_it;
181 log(WARNING, "stream '%s' disappeared from the configuration file. "
182 "It will not be deleted, but clients will not get any new inputs.",
187 void open_logs(const vector<LogConfig> &log_destinations)
189 for (size_t i = 0; i < log_destinations.size(); ++i) {
190 if (log_destinations[i].type == LogConfig::LOG_TYPE_FILE) {
191 add_log_destination_file(log_destinations[i].filename);
192 } else if (log_destinations[i].type == LogConfig::LOG_TYPE_CONSOLE) {
193 add_log_destination_console();
194 } else if (log_destinations[i].type == LogConfig::LOG_TYPE_SYSLOG) {
195 add_log_destination_syslog();
203 bool dry_run_config(const std::string &argv0, const std::string &config_filename)
205 char *argv0_copy = strdup(argv0.c_str());
206 char *config_filename_copy = strdup(config_filename.c_str());
211 log_perror("fork()");
213 free(config_filename_copy);
217 execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, NULL);
218 log_perror(argv0_copy);
226 free(config_filename_copy);
231 err = waitpid(pid, &status, 0);
232 } while (err == -1 && errno == EINTR);
235 log_perror("waitpid()");
239 return (WIFEXITED(status) && WEXITSTATUS(status) == 0);
242 int main(int argc, char **argv)
246 signal(SIGUSR1, do_nothing); // Used in internal signalling.
247 signal(SIGPIPE, SIG_IGN);
251 bool test_config = false;
253 static const option long_options[] = {
254 { "state", required_argument, 0, 's' },
255 { "test-config", no_argument, 0, 't' },
258 int option_index = 0;
259 int c = getopt_long(argc, argv, "s:t", long_options, &option_index);
266 state_fd = atoi(optarg);
272 fprintf(stderr, "Unknown option '%s'\n", argv[option_index]);
277 string config_filename = "cubemap.config";
279 config_filename = argv[optind++];
282 // Canonicalize argv[0] and config_filename.
283 char argv0_canon[PATH_MAX];
284 char config_filename_canon[PATH_MAX];
286 if (realpath(argv[0], argv0_canon) == NULL) {
290 if (realpath(config_filename.c_str(), config_filename_canon) == NULL) {
291 log_perror(config_filename.c_str());
295 // Now parse the configuration file.
297 if (!parse_config(config_filename_canon, &config)) {
304 // Ideally we'd like to daemonize only when we've started up all threads etc.,
305 // but daemon() forks, which is not good in multithreaded software, so we'll
306 // have to do it here.
307 if (config.daemonize) {
308 if (daemon(0, 0) == -1) {
309 log_perror("daemon");
315 // Open logs as soon as possible.
316 open_logs(config.log_destinations);
318 log(INFO, "Cubemap " SERVER_VERSION " starting.");
319 if (config.access_log_file.empty()) {
320 // Create a dummy logger.
321 access_log = new AccessLogThread();
323 access_log = new AccessLogThread(config.access_log_file);
327 servers = new ServerPool(config.num_servers);
329 CubemapStateProto loaded_state;
330 struct timeval serialize_start;
331 set<string> deserialized_stream_ids;
332 map<int, Acceptor *> deserialized_acceptors;
333 multimap<string, InputWithRefcount> inputs; // multimap due to older versions without deduplication.
334 if (state_fd != -1) {
335 log(INFO, "Deserializing state from previous process...");
337 if (!read_tempfile(state_fd, &serialized)) {
340 if (!loaded_state.ParseFromString(serialized)) {
341 log(ERROR, "Failed deserialization of state.");
345 serialize_start.tv_sec = loaded_state.serialize_start_sec();
346 serialize_start.tv_usec = loaded_state.serialize_start_usec();
348 // Deserialize the streams.
349 for (int i = 0; i < loaded_state.streams_size(); ++i) {
350 const StreamProto &stream = loaded_state.streams(i);
352 vector<int> data_fds;
353 for (int j = 0; j < stream.data_fds_size(); ++j) {
354 data_fds.push_back(stream.data_fds(j));
357 // Older versions stored the data once in the protobuf instead of
358 // sending around file descriptors.
359 if (data_fds.empty() && stream.has_data()) {
360 data_fds.push_back(make_tempfile(stream.data()));
363 servers->add_stream_from_serialized(stream, data_fds);
364 deserialized_stream_ids.insert(stream.stream_id());
367 // Deserialize the inputs. Note that we don't actually add them to any stream yet.
368 for (int i = 0; i < loaded_state.inputs_size(); ++i) {
369 InputWithRefcount iwr;
370 iwr.input = create_input(loaded_state.inputs(i));
372 inputs.insert(make_pair(loaded_state.inputs(i).url(), iwr));
375 // Deserialize the acceptors.
376 for (int i = 0; i < loaded_state.acceptors_size(); ++i) {
377 deserialized_acceptors.insert(make_pair(
378 loaded_state.acceptors(i).port(),
379 new Acceptor(loaded_state.acceptors(i))));
382 log(INFO, "Deserialization done.");
385 // Add any new inputs coming from the config.
386 create_config_inputs(config, &inputs);
388 // Find all streams in the configuration file, create them, and connect to the inputs.
389 create_streams(config, deserialized_stream_ids, &inputs);
390 vector<Acceptor *> acceptors = create_acceptors(config, &deserialized_acceptors);
392 // Put back the existing clients. It doesn't matter which server we
393 // allocate them to, so just do round-robin. However, we need to add
394 // them after the mark pools have been set up.
395 for (int i = 0; i < loaded_state.clients_size(); ++i) {
396 servers->add_client_from_serialized(loaded_state.clients(i));
401 // Now delete all inputs that are longer in use, and start the others.
402 for (multimap<string, InputWithRefcount>::iterator input_it = inputs.begin();
403 input_it != inputs.end(); ) {
404 if (input_it->second.refcount == 0) {
405 log(WARNING, "Input '%s' no longer in use, closing.",
406 input_it->first.c_str());
407 input_it->second.input->close_socket();
408 delete input_it->second.input;
409 inputs.erase(input_it++);
411 input_it->second.input->run();
416 // Start writing statistics.
417 StatsThread *stats_thread = NULL;
418 if (!config.stats_file.empty()) {
419 stats_thread = new StatsThread(config.stats_file, config.stats_interval);
423 struct timeval server_start;
424 gettimeofday(&server_start, NULL);
425 if (state_fd != -1) {
426 // Measure time from we started deserializing (below) to now, when basically everything
427 // is up and running. This is, in other words, a conservative estimate of how long our
428 // “glitch” period was, not counting of course reconnects if the configuration changed.
429 double glitch_time = server_start.tv_sec - serialize_start.tv_sec +
430 1e-6 * (server_start.tv_usec - serialize_start.tv_usec);
431 log(INFO, "Re-exec happened in approx. %.0f ms.", glitch_time * 1000.0);
438 // OK, we've been HUPed. Time to shut down everything, serialize, and re-exec.
439 gettimeofday(&serialize_start, NULL);
441 if (stats_thread != NULL) {
442 stats_thread->stop();
445 for (size_t i = 0; i < acceptors.size(); ++i) {
446 acceptors[i]->stop();
448 for (multimap<string, InputWithRefcount>::iterator input_it = inputs.begin();
449 input_it != inputs.end();
451 input_it->second.input->stop();
455 CubemapStateProto state;
457 log(INFO, "Shutting down.");
459 log(INFO, "Serializing state and re-execing...");
460 state = collect_state(
461 serialize_start, acceptors, inputs, servers);
463 state.SerializeToString(&serialized);
464 state_fd = make_tempfile(serialized);
465 if (state_fd == -1) {
471 for (unsigned i = 0; i < mark_pools.size(); ++i) {
472 delete mark_pools[i];
484 // OK, so the signal was SIGHUP. Check that the new config is okay, then exec the new binary.
485 if (!dry_run_config(argv0_canon, config_filename_canon)) {
486 open_logs(config.log_destinations);
487 log(ERROR, "%s --test-config failed. Restarting old version instead of new.", argv[0]);
494 sprintf(buf, "%d", state_fd);
497 execlp(argv0_canon, argv0_canon, config_filename_canon, "--state", buf, NULL);
498 open_logs(config.log_destinations);
499 log_perror("execlp");
500 log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv0_canon);