1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "dispatcher_stub.h" 20#include "../frontend/hash_request.h" 21#include "../frontend/user_interface.h" 22#include "../statements/statement_dump.h" 23#include "../../expat/escape_json.h" 28#include <sys/resource.h> 29#include <sys/select.h> 44std::string de_escape(const std::string& input) 47795673 q std::string::size_type pos = 0; 48795673 q while (pos < input.length()) 500 if (input[pos] != '\\') 510 result += input[pos]; 550 if (pos >= input.length()) 570 if (input[pos] == 'n') 590 else if (input[pos] == 't') 620 result += input[pos]; 70void set_limits(uint32 time, uint64 space) 74673673 q int result = getrlimit(RLIMIT_CPU, &limit); 75673673 q if (result == 0 && time < limit.rlim_cur && time < limit.rlim_max) 79673673 q result = setrlimit(RLIMIT_CPU, &limit); 82673673 q result = getrlimit(RLIMIT_AS, &limit); 83673673 q if (result == 0 && space < limit.rlim_cur && space < limit.rlim_max) 87673673 q result = setrlimit(RLIMIT_AS, &limit); 94 uint64_t decode_hex(const std::string& representation) 970 std::string::size_type pos = 0; 990 while (pos < representation.size()) 1010 if (representation[pos] >= '0' && representation[pos] <= '9') 1020 result = (result<<4) | (representation[pos] - '0'); 1030 else if (representation[pos] >= 'a' && representation[pos] <= 'f') 1040 result = (result<<4) | (representation[pos] - 'a' + 10); 1050 else if (representation[pos] >= 'A' && representation[pos] <= 'F') 1060 result = (result<<4) | (representation[pos] - 'A' + 10); 114bool hash_is_blacklisted(uint64_t hash, const std::string& db_dir) 1180 std::ifstream blacklist_f((db_dir + "anon_hash_blacklist").c_str()); 1190 for (std::string buf; std::getline(blacklist_f, buf); ) 1210 if (hash == decode_hex(buf)) 131Dispatcher_Stub::Dispatcher_Stub 132 (std::string db_dir_, Error_Output* error_output_, const std::string& xml_raw, 133 int area_level, uint32 max_allowed_time, uint64 max_allowed_space, Parsed_Query& global_settings) 134673673 q : db_dir(db_dir_), error_output(error_output_), 135673673 q dispatcher_client(0), area_dispatcher_client(0), 136673673 q transaction(0), area_transaction(0), rman(0), 137673673 q full_hash(hash(sanitize_string(xml_raw, false))), 138673673 q anon_hash(hash(sanitize_string(xml_raw, true))), client_token(0) 140673673 q const uint32_t MAX_LOG_SIZE = 8192; 143673673 q set_limits(2*max_allowed_time + 60, 2*max_allowed_space + 1024*1024*1024); 1470 client_token = probe_client_token(); 1480 client_identifier = probe_client_identifier(); 1490 if (client_identifier.empty()) 1500 client_identifier = "::"; 1510 dispatcher_client = new Dispatcher_Client(osm_base_settings().shared_name); 1520 Logger db_logger(dispatcher_client->get_db_dir()); 1530 Logger client_logger(dispatcher_client->get_db_dir(), basic_settings().client_logfile_name); 1540 if (xml_raw.size() <= MAX_LOG_SIZE) 1550 client_logger.annotated_log("requesting " + escape_cstr(xml_raw)); 1570 client_logger.annotated_log("requesting " + escape_cstr( 1580 xml_raw.substr(0, MAX_LOG_SIZE/2) + " ... " + xml_raw.substr(xml_raw.size() - MAX_LOG_SIZE/2))); 1600 if (hash_is_blacklisted(anon_hash, dispatcher_client->get_db_dir())) 1620 std::ostringstream out; 1630 out<<"hash_limited "<<std::hex<<anon_hash<<' '<<full_hash<<' '<<std::dec<<client_token<<' '<<client_identifier; 1640 client_logger.annotated_log(out.str()); 1650 delete dispatcher_client; 1660 dispatcher_client = 0; 1670 throw File_Error(0, "-", "Dispatcher_Client::request_read_and_idx::timeout"); 1720 db_logger.annotated_log("request_read_and_idx() start"); 1730 dispatcher_client->request_read_and_idx(max_allowed_time, max_allowed_space, client_token, full_hash); 1740 db_logger.annotated_log("request_read_and_idx() end"); 1760 catch (const File_Error& e) 1780 std::ostringstream out; 1790 if (e.origin == "Dispatcher_Client::request_read_and_idx::rate_limited" 1800 || e.origin == "Dispatcher_Client::request_read_and_idx::timeout" 1810 || e.origin == "Dispatcher_Client::request_read_and_idx::duplicate_query") 1820 out<<e.origin.substr(41)<<' ' 1830 <<std::hex<<anon_hash<<' '<<full_hash<<' '<<std::dec<<client_token<<' '<<client_identifier; 1850 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 1860 client_logger.annotated_log(out.str()); 1890 transaction = new Nonsynced_Transaction(Access_Mode::readonly, false, dispatcher_client->get_db_dir(), ""); 1910 for (auto i : osm_base_settings().bin_idxs()) 1920 transaction->data_index(i); 1930 for (auto i : osm_base_settings().map_idxs()) 1940 transaction->random_index(i); 1950 for (auto i : meta_settings().bin_idxs()) 1960 transaction->data_index(i); 197 // meta_settings().map_idxs() is always empty 1980 for (auto i : attic_settings().bin_idxs()) 1990 transaction->data_index(i); 2000 for (auto i : attic_settings().map_idxs()) 2010 transaction->random_index(i); 2040 std::ifstream version((dispatcher_client->get_db_dir() + "osm_base_version").c_str()); 2050 getline(version, timestamp); 2060 timestamp = de_escape(timestamp); 2100 db_logger.annotated_log("read_idx_finished() start"); 2110 dispatcher_client->read_idx_finished(); 2120 db_logger.annotated_log("read_idx_finished() end"); 2140 catch (const File_Error& e) 2160 std::ostringstream out; 2170 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 2180 db_logger.annotated_log(out.str()); 2240 area_dispatcher_client = new Dispatcher_Client(area_settings().shared_name); 2250 Logger logger(area_dispatcher_client->get_db_dir()); 2270 if (area_level == 1) 2310 db_logger.annotated_log("request_read_and_idx() area start"); 2320 area_dispatcher_client->request_read_and_idx(max_allowed_time, max_allowed_space, client_token, full_hash); 2330 db_logger.annotated_log("request_read_and_idx() area end"); 2350 catch (const File_Error& e) 2370 std::ostringstream out; 2380 if (e.origin == "Dispatcher_Client::request_read_and_idx::rate_limited" 2390 || e.origin == "Dispatcher_Client::request_read_and_idx::timeout" 2400 || e.origin == "Dispatcher_Client::request_read_and_idx::duplicate_query") 2410 out<<"Area::"<<e.origin.substr(41)<<' ' 2420 <<std::hex<<anon_hash<<' '<<full_hash<<' '<<std::dec<<client_token<<' '<<client_identifier; 2440 out<<"Area::"<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 2450 client_logger.annotated_log(out.str()); 2480 area_transaction = new Nonsynced_Transaction( 2490 Access_Mode::readonly, false, area_dispatcher_client->get_db_dir(), ""); 2510 std::ifstream version((area_dispatcher_client->get_db_dir() + 2520 "area_version").c_str()); 2530 getline(version, area_timestamp); 2540 area_timestamp = de_escape(area_timestamp); 2570 else if (area_level == 2) 2610 db_logger.annotated_log("write_start() area start"); 2620 area_dispatcher_client->write_start(); 2630 db_logger.annotated_log("write_start() area end"); 2650 catch (const File_Error& e) 2670 std::ostringstream out; 2680 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 2690 db_logger.annotated_log(out.str()); 2720 area_transaction = new Nonsynced_Transaction( 2730 Access_Mode::writeable, true, area_dispatcher_client->get_db_dir(), ""); 2750 std::ofstream area_version((area_dispatcher_client->get_db_dir() 2760 + "area_version.shadow").c_str()); 2770 area_version<<timestamp<<'\n'; 2780 area_timestamp = de_escape(timestamp); 2820 area_transaction->data_index(area_settings().AREAS); 2830 area_transaction->data_index(area_settings().AREA_BLOCKS); 2840 area_transaction->data_index(area_settings().AREA_TAGS_LOCAL); 2850 area_transaction->data_index(area_settings().AREA_TAGS_GLOBAL); 2870 if (area_level == 1) 2910 db_logger.annotated_log("read_idx_finished() area start"); 2920 area_dispatcher_client->read_idx_finished(); 2930 db_logger.annotated_log("read_idx_finished() area end"); 2950 catch (const File_Error& e) 2970 std::ostringstream out; 2980 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 2990 db_logger.annotated_log(out.str()); 3040 rman = new Resource_Manager(*transaction, global_settings, area_level == 2 ? error_output : 0, 3050 *area_transaction, this, area_level == 2 ? new Area_Updater(*area_transaction) : 0); 3080 rman = new Resource_Manager(*transaction, &global_settings, this, error_output); 312673673 q if (file_present(db_dir + osm_base_settings().shared_name)) 3130 throw Context_Error("File " + db_dir + osm_base_settings().shared_name + " present, " 3140 "which indicates a running dispatcher. Delete file if no dispatcher is running."); 316673673 q transaction = new Nonsynced_Transaction(Access_Mode::readonly, false, db_dir, ""); 319122122 q area_transaction = new Nonsynced_Transaction( 320122122 q area_level == 2 ? Access_Mode::writeable : Access_Mode::readonly, false, db_dir, ""); 321122122 q rman = new Resource_Manager(*transaction, global_settings, area_level == 2 ? error_output : 0, 322122122 q *area_transaction, this, area_level == 2 ? new Area_Updater(*area_transaction) : 0); 325551551 q rman = new Resource_Manager(*transaction, &global_settings, this, error_output); 328673673 q std::ifstream version((db_dir + "osm_base_version").c_str()); 329673673 q getline(version, timestamp); 330673673 q timestamp = de_escape(timestamp); 334122122 q std::ifstream version((db_dir + "area_version").c_str()); 335122122 q getline(version, area_timestamp); 336122122 q area_timestamp = de_escape(timestamp); 3400 std::ofstream area_version((db_dir + "area_version").c_str()); 3410 area_version<<timestamp<<'\n'; 3420 area_timestamp = de_escape(timestamp); 348void Dispatcher_Stub::ping() const 3510 dispatcher_client->ping(); 3526360 q if (area_dispatcher_client) 3530 area_dispatcher_client->ping(); 357bool Dispatcher_Stub::all_meta_empty() const 3590 for (auto i : meta_settings().bin_idxs()) 3610 if (transaction->data_index(i) && !transaction->data_index(i)->empty()) 368std::string basename(const std::string& filename) 3700 uint start = filename.size(); 3710 while (start > 0 && filename[--start] != '/') 3730 if (start < filename.size() && filename[start] == '/') 3760 while (end < filename.size() && filename[end] != '.') 3780 return filename.substr(start, end-start); 382bool Dispatcher_Stub::is_meta_file(const std::string& filename) const 3840 std::string trunk = basename(filename); 3850 for (auto i : meta_settings().bin_idxs()) 3870 if (i && i->get_file_name_trunk() == trunk) 394bool Dispatcher_Stub::all_attic_empty() const 3960 for (auto i : attic_settings().bin_idxs()) 3980 if (transaction->data_index(i) && !transaction->data_index(i)->empty()) 405bool Dispatcher_Stub::is_attic_file(const std::string& filename) const 4070 std::string trunk = basename(filename); 4080 for (auto i : attic_settings().bin_idxs()) 4100 if (i && i->get_file_name_trunk() == trunk) 417Dispatcher_Stub::~Dispatcher_Stub() 419673673 q bool areas_written = (rman->area_updater() != 0); 420673673 q std::vector< uint64 > cpu_runtime = rman ? rman->cpu_time() : std::vector< uint64 >(); 4280 Logger db_logger(dispatcher_client->get_db_dir()); 4290 Logger client_logger(dispatcher_client->get_db_dir(), basic_settings().client_logfile_name); 4320 std::ostringstream out; 4330 out<<"read_finished "<<std::hex<<anon_hash<<' '<<full_hash<<' ' 4340 <<std::dec<<client_token<<' '<<client_identifier<<' '<<global_read_counter(); 4350 for (std::vector< uint64 >::const_iterator it = cpu_runtime.begin(); it != cpu_runtime.end(); ++it) 4370 client_logger.annotated_log(out.str()); 4380 db_logger.annotated_log("read_finished() start"); 4390 dispatcher_client->read_finished(); 4400 db_logger.annotated_log("read_finished() end"); 4420 catch (const File_Error& e) 4440 std::ostringstream out; 4450 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 4460 db_logger.annotated_log(out.str()); 4480 delete dispatcher_client; 4540 Logger logger(area_dispatcher_client->get_db_dir()); 4570 logger.annotated_log("write_commit() area start"); 4580 area_dispatcher_client->write_commit(); 4590 rename((area_dispatcher_client->get_db_dir() + "area_version.shadow").c_str(), 4600 (area_dispatcher_client->get_db_dir() + "area_version").c_str()); 4610 logger.annotated_log("write_commit() area end"); 4630 catch (const File_Error& e) 4650 std::ostringstream out; 4660 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 4670 logger.annotated_log(out.str()); 4720 Logger logger(area_dispatcher_client->get_db_dir()); 4750 logger.annotated_log("read_finished() area start"); 4760 area_dispatcher_client->read_finished(); 4770 logger.annotated_log("read_finished() area end"); 4790 catch (const File_Error& e) 4810 std::ostringstream out; 4820 out<<e.origin<<' '<<e.filename<<' '<<e.error_number<<' '<<strerror(e.error_number); 4830 logger.annotated_log(out.str()); 4860 delete area_dispatcher_client;