main.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "common.h"
  3. #include "buildinfo.h"
  4. #include "static_threads.h"
  5. int netdata_zero_metrics_enabled;
  6. int netdata_anonymous_statistics_enabled;
  7. struct netdata_static_thread *static_threads;
  8. struct config netdata_config = {
  9. .first_section = NULL,
  10. .last_section = NULL,
  11. .mutex = NETDATA_MUTEX_INITIALIZER,
  12. .index = {
  13. .avl_tree = {
  14. .root = NULL,
  15. .compar = appconfig_section_compare
  16. },
  17. .rwlock = AVL_LOCK_INITIALIZER
  18. }
  19. };
  20. void netdata_cleanup_and_exit(int ret) {
  21. // enabling this, is wrong
  22. // because the threads will be cancelled while cleaning up
  23. // netdata_exit = 1;
  24. error_log_limit_unlimited();
  25. info("EXIT: netdata prepares to exit with code %d...", ret);
  26. send_statistics("EXIT", ret?"ERROR":"OK","-");
  27. char agent_crash_file[FILENAME_MAX + 1];
  28. char agent_incomplete_shutdown_file[FILENAME_MAX + 1];
  29. snprintfz(agent_crash_file, FILENAME_MAX, "%s/.agent_crash", netdata_configured_varlib_dir);
  30. snprintfz(agent_incomplete_shutdown_file, FILENAME_MAX, "%s/.agent_incomplete_shutdown", netdata_configured_varlib_dir);
  31. (void) rename(agent_crash_file, agent_incomplete_shutdown_file);
  32. // cleanup/save the database and exit
  33. info("EXIT: cleaning up the database...");
  34. rrdhost_cleanup_all();
  35. if(!ret) {
  36. // exit cleanly
  37. // stop everything
  38. info("EXIT: stopping static threads...");
  39. #ifdef ENABLE_ACLK
  40. aclk_sync_exit_all();
  41. #endif
  42. cancel_main_threads();
  43. // free the database
  44. info("EXIT: freeing database memory...");
  45. #ifdef ENABLE_DBENGINE
  46. for(int tier = 0; tier < storage_tiers ; tier++)
  47. rrdeng_prepare_exit(multidb_ctx[tier]);
  48. #endif
  49. rrdhost_free_all();
  50. #ifdef ENABLE_DBENGINE
  51. for(int tier = 0; tier < storage_tiers ; tier++)
  52. rrdeng_exit(multidb_ctx[tier]);
  53. #endif
  54. }
  55. sql_close_context_database();
  56. sql_close_database();
  57. // unlink the pid
  58. if(pidfile[0]) {
  59. info("EXIT: removing netdata PID file '%s'...", pidfile);
  60. if(unlink(pidfile) != 0)
  61. error("EXIT: cannot unlink pidfile '%s'.", pidfile);
  62. }
  63. #ifdef ENABLE_HTTPS
  64. security_clean_openssl();
  65. #endif
  66. info("EXIT: all done - netdata is now exiting - bye bye...");
  67. (void) unlink(agent_incomplete_shutdown_file);
  68. exit(ret);
  69. }
  70. void web_server_threading_selection(void) {
  71. web_server_mode = web_server_mode_id(config_get(CONFIG_SECTION_WEB, "mode", web_server_mode_name(web_server_mode)));
  72. int static_threaded = (web_server_mode == WEB_SERVER_MODE_STATIC_THREADED);
  73. int i;
  74. for (i = 0; static_threads[i].name; i++) {
  75. if (static_threads[i].start_routine == socket_listen_main_static_threaded)
  76. static_threads[i].enabled = static_threaded;
  77. }
  78. }
  79. int make_dns_decision(const char *section_name, const char *config_name, const char *default_value, SIMPLE_PATTERN *p)
  80. {
  81. char *value = config_get(section_name,config_name,default_value);
  82. if(!strcmp("yes",value))
  83. return 1;
  84. if(!strcmp("no",value))
  85. return 0;
  86. if(strcmp("heuristic",value))
  87. error("Invalid configuration option '%s' for '%s'/'%s'. Valid options are 'yes', 'no' and 'heuristic'. Proceeding with 'heuristic'",
  88. value, section_name, config_name);
  89. return simple_pattern_is_potential_name(p);
  90. }
  91. void web_server_config_options(void)
  92. {
  93. web_client_timeout =
  94. (int)config_get_number(CONFIG_SECTION_WEB, "disconnect idle clients after seconds", web_client_timeout);
  95. web_client_first_request_timeout =
  96. (int)config_get_number(CONFIG_SECTION_WEB, "timeout for first request", web_client_first_request_timeout);
  97. web_client_streaming_rate_t =
  98. config_get_number(CONFIG_SECTION_WEB, "accept a streaming request every seconds", web_client_streaming_rate_t);
  99. respect_web_browser_do_not_track_policy =
  100. config_get_boolean(CONFIG_SECTION_WEB, "respect do not track policy", respect_web_browser_do_not_track_policy);
  101. web_x_frame_options = config_get(CONFIG_SECTION_WEB, "x-frame-options response header", "");
  102. if(!*web_x_frame_options)
  103. web_x_frame_options = NULL;
  104. web_allow_connections_from =
  105. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow connections from", "localhost *"),
  106. NULL, SIMPLE_PATTERN_EXACT);
  107. web_allow_connections_dns =
  108. make_dns_decision(CONFIG_SECTION_WEB, "allow connections by dns", "heuristic", web_allow_connections_from);
  109. web_allow_dashboard_from =
  110. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow dashboard from", "localhost *"),
  111. NULL, SIMPLE_PATTERN_EXACT);
  112. web_allow_dashboard_dns =
  113. make_dns_decision(CONFIG_SECTION_WEB, "allow dashboard by dns", "heuristic", web_allow_dashboard_from);
  114. web_allow_badges_from =
  115. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow badges from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  116. web_allow_badges_dns =
  117. make_dns_decision(CONFIG_SECTION_WEB, "allow badges by dns", "heuristic", web_allow_badges_from);
  118. web_allow_registry_from =
  119. simple_pattern_create(config_get(CONFIG_SECTION_REGISTRY, "allow from", "*"), NULL, SIMPLE_PATTERN_EXACT);
  120. web_allow_registry_dns = make_dns_decision(CONFIG_SECTION_REGISTRY, "allow by dns", "heuristic",
  121. web_allow_registry_from);
  122. web_allow_streaming_from = simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow streaming from", "*"),
  123. NULL, SIMPLE_PATTERN_EXACT);
  124. web_allow_streaming_dns = make_dns_decision(CONFIG_SECTION_WEB, "allow streaming by dns", "heuristic",
  125. web_allow_streaming_from);
  126. // Note the default is not heuristic, the wildcards could match DNS but the intent is ip-addresses.
  127. web_allow_netdataconf_from = simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow netdata.conf from",
  128. "localhost fd* 10.* 192.168.* 172.16.* 172.17.* 172.18.*"
  129. " 172.19.* 172.20.* 172.21.* 172.22.* 172.23.* 172.24.*"
  130. " 172.25.* 172.26.* 172.27.* 172.28.* 172.29.* 172.30.*"
  131. " 172.31.* UNKNOWN"), NULL, SIMPLE_PATTERN_EXACT);
  132. web_allow_netdataconf_dns =
  133. make_dns_decision(CONFIG_SECTION_WEB, "allow netdata.conf by dns", "no", web_allow_netdataconf_from);
  134. web_allow_mgmt_from =
  135. simple_pattern_create(config_get(CONFIG_SECTION_WEB, "allow management from", "localhost"),
  136. NULL, SIMPLE_PATTERN_EXACT);
  137. web_allow_mgmt_dns =
  138. make_dns_decision(CONFIG_SECTION_WEB, "allow management by dns","heuristic",web_allow_mgmt_from);
  139. #ifdef NETDATA_WITH_ZLIB
  140. web_enable_gzip = config_get_boolean(CONFIG_SECTION_WEB, "enable gzip compression", web_enable_gzip);
  141. char *s = config_get(CONFIG_SECTION_WEB, "gzip compression strategy", "default");
  142. if(!strcmp(s, "default"))
  143. web_gzip_strategy = Z_DEFAULT_STRATEGY;
  144. else if(!strcmp(s, "filtered"))
  145. web_gzip_strategy = Z_FILTERED;
  146. else if(!strcmp(s, "huffman only"))
  147. web_gzip_strategy = Z_HUFFMAN_ONLY;
  148. else if(!strcmp(s, "rle"))
  149. web_gzip_strategy = Z_RLE;
  150. else if(!strcmp(s, "fixed"))
  151. web_gzip_strategy = Z_FIXED;
  152. else {
  153. error("Invalid compression strategy '%s'. Valid strategies are 'default', 'filtered', 'huffman only', 'rle' and 'fixed'. Proceeding with 'default'.", s);
  154. web_gzip_strategy = Z_DEFAULT_STRATEGY;
  155. }
  156. web_gzip_level = (int)config_get_number(CONFIG_SECTION_WEB, "gzip compression level", 3);
  157. if(web_gzip_level < 1) {
  158. error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 1 (fastest compression).", web_gzip_level);
  159. web_gzip_level = 1;
  160. }
  161. else if(web_gzip_level > 9) {
  162. error("Invalid compression level %d. Valid levels are 1 (fastest) to 9 (best ratio). Proceeding with level 9 (best compression).", web_gzip_level);
  163. web_gzip_level = 9;
  164. }
  165. #endif /* NETDATA_WITH_ZLIB */
  166. }
  167. // killpid kills pid with SIGTERM.
  168. int killpid(pid_t pid) {
  169. int ret;
  170. debug(D_EXIT, "Request to kill pid %d", pid);
  171. errno = 0;
  172. ret = kill(pid, SIGTERM);
  173. if (ret == -1) {
  174. switch(errno) {
  175. case ESRCH:
  176. // We wanted the process to exit so just let the caller handle.
  177. return ret;
  178. case EPERM:
  179. error("Cannot kill pid %d, but I do not have enough permissions.", pid);
  180. break;
  181. default:
  182. error("Cannot kill pid %d, but I received an error.", pid);
  183. break;
  184. }
  185. }
  186. return ret;
  187. }
  188. void cancel_main_threads() {
  189. error_log_limit_unlimited();
  190. int i, found = 0;
  191. usec_t max = 5 * USEC_PER_SEC, step = 100000;
  192. for (i = 0; static_threads[i].name != NULL ; i++) {
  193. if(static_threads[i].enabled == NETDATA_MAIN_THREAD_RUNNING) {
  194. info("EXIT: Stopping main thread: %s", static_threads[i].name);
  195. netdata_thread_cancel(*static_threads[i].thread);
  196. found++;
  197. }
  198. }
  199. netdata_exit = 1;
  200. while(found && max > 0) {
  201. max -= step;
  202. info("Waiting %d threads to finish...", found);
  203. sleep_usec(step);
  204. found = 0;
  205. for (i = 0; static_threads[i].name != NULL ; i++) {
  206. if (static_threads[i].enabled != NETDATA_MAIN_THREAD_EXITED)
  207. found++;
  208. }
  209. }
  210. if(found) {
  211. for (i = 0; static_threads[i].name != NULL ; i++) {
  212. if (static_threads[i].enabled != NETDATA_MAIN_THREAD_EXITED)
  213. error("Main thread %s takes too long to exit. Giving up...", static_threads[i].name);
  214. }
  215. }
  216. else
  217. info("All threads finished.");
  218. for (i = 0; static_threads[i].name != NULL ; i++)
  219. freez(static_threads[i].thread);
  220. free(static_threads);
  221. }
  222. struct option_def option_definitions[] = {
  223. // opt description arg name default value
  224. { 'c', "Configuration file to load.", "filename", CONFIG_DIR "/" CONFIG_FILENAME},
  225. { 'D', "Do not fork. Run in the foreground.", NULL, "run in the background"},
  226. { 'd', "Fork. Run in the background.", NULL, "run in the background"},
  227. { 'h', "Display this help message.", NULL, NULL},
  228. { 'P', "File to save a pid while running.", "filename", "do not save pid to a file"},
  229. { 'i', "The IP address to listen to.", "IP", "all IP addresses IPv4 and IPv6"},
  230. { 'p', "API/Web port to use.", "port", "19999"},
  231. { 's', "Prefix for /proc and /sys (for containers).", "path", "no prefix"},
  232. { 't', "The internal clock of netdata.", "seconds", "1"},
  233. { 'u', "Run as user.", "username", "netdata"},
  234. { 'v', "Print netdata version and exit.", NULL, NULL},
  235. { 'V', "Print netdata version and exit.", NULL, NULL},
  236. { 'W', "See Advanced options below.", "options", NULL},
  237. };
  238. int help(int exitcode) {
  239. FILE *stream;
  240. if(exitcode == 0)
  241. stream = stdout;
  242. else
  243. stream = stderr;
  244. int num_opts = sizeof(option_definitions) / sizeof(struct option_def);
  245. int i;
  246. int max_len_arg = 0;
  247. // Compute maximum argument length
  248. for( i = 0; i < num_opts; i++ ) {
  249. if(option_definitions[i].arg_name) {
  250. int len_arg = (int)strlen(option_definitions[i].arg_name);
  251. if(len_arg > max_len_arg) max_len_arg = len_arg;
  252. }
  253. }
  254. if(max_len_arg > 30) max_len_arg = 30;
  255. if(max_len_arg < 20) max_len_arg = 20;
  256. fprintf(stream, "%s", "\n"
  257. " ^\n"
  258. " |.-. .-. .-. .-. . netdata \n"
  259. " | '-' '-' '-' '-' real-time performance monitoring, done right! \n"
  260. " +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+--->\n"
  261. "\n"
  262. " Copyright (C) 2016-2020, Netdata, Inc. <info@netdata.cloud>\n"
  263. " Released under GNU General Public License v3 or later.\n"
  264. " All rights reserved.\n"
  265. "\n"
  266. " Home Page : https://netdata.cloud\n"
  267. " Source Code: https://github.com/netdata/netdata\n"
  268. " Docs : https://learn.netdata.cloud\n"
  269. " Support : https://github.com/netdata/netdata/issues\n"
  270. " License : https://github.com/netdata/netdata/blob/master/LICENSE.md\n"
  271. "\n"
  272. " Twitter : https://twitter.com/linuxnetdata\n"
  273. " Facebook : https://www.facebook.com/linuxnetdata/\n"
  274. "\n"
  275. "\n"
  276. );
  277. fprintf(stream, " SYNOPSIS: netdata [options]\n");
  278. fprintf(stream, "\n");
  279. fprintf(stream, " Options:\n\n");
  280. // Output options description.
  281. for( i = 0; i < num_opts; i++ ) {
  282. fprintf(stream, " -%c %-*s %s", option_definitions[i].val, max_len_arg, option_definitions[i].arg_name ? option_definitions[i].arg_name : "", option_definitions[i].description);
  283. if(option_definitions[i].default_value) {
  284. fprintf(stream, "\n %c %-*s Default: %s\n", ' ', max_len_arg, "", option_definitions[i].default_value);
  285. } else {
  286. fprintf(stream, "\n");
  287. }
  288. fprintf(stream, "\n");
  289. }
  290. fprintf(stream, "\n Advanced options:\n\n"
  291. " -W stacksize=N Set the stacksize (in bytes).\n\n"
  292. " -W debug_flags=N Set runtime tracing to debug.log.\n\n"
  293. " -W unittest Run internal unittests and exit.\n\n"
  294. " -W sqlite-check Check metadata database integrity and exit.\n\n"
  295. " -W sqlite-fix Check metadata database integrity, fix if needed and exit.\n\n"
  296. " -W sqlite-compact Reclaim metadata database unused space and exit.\n\n"
  297. #ifdef ENABLE_DBENGINE
  298. " -W createdataset=N Create a DB engine dataset of N seconds and exit.\n\n"
  299. " -W stresstest=A,B,C,D,E,F,G\n"
  300. " Run a DB engine stress test for A seconds,\n"
  301. " with B writers and C readers, with a ramp up\n"
  302. " time of D seconds for writers, a page cache\n"
  303. " size of E MiB, an optional disk space limit\n"
  304. " of F MiB, G libuv workers (default 16) and exit.\n\n"
  305. #endif
  306. " -W set section option value\n"
  307. " set netdata.conf option from the command line.\n\n"
  308. " -W buildinfo Print the version, the configure options,\n"
  309. " a list of optional features, and whether they\n"
  310. " are enabled or not.\n\n"
  311. " -W buildinfojson Print the version, the configure options,\n"
  312. " a list of optional features, and whether they\n"
  313. " are enabled or not, in JSON format.\n\n"
  314. " -W simple-pattern pattern string\n"
  315. " Check if string matches pattern and exit.\n\n"
  316. " -W \"claim -token=TOKEN -rooms=ROOM1,ROOM2\"\n"
  317. " Claim the agent to the workspace rooms pointed to by TOKEN and ROOM*.\n\n"
  318. );
  319. fprintf(stream, "\n Signals netdata handles:\n\n"
  320. " - HUP Close and reopen log files.\n"
  321. " - USR1 Save internal DB to disk.\n"
  322. " - USR2 Reload health configuration.\n"
  323. "\n"
  324. );
  325. fflush(stream);
  326. return exitcode;
  327. }
  328. #ifdef ENABLE_HTTPS
  329. static void security_init(){
  330. char filename[FILENAME_MAX + 1];
  331. snprintfz(filename, FILENAME_MAX, "%s/ssl/key.pem",netdata_configured_user_config_dir);
  332. security_key = config_get(CONFIG_SECTION_WEB, "ssl key", filename);
  333. snprintfz(filename, FILENAME_MAX, "%s/ssl/cert.pem",netdata_configured_user_config_dir);
  334. security_cert = config_get(CONFIG_SECTION_WEB, "ssl certificate", filename);
  335. tls_version = config_get(CONFIG_SECTION_WEB, "tls version", "1.3");
  336. tls_ciphers = config_get(CONFIG_SECTION_WEB, "tls ciphers", "none");
  337. security_openssl_library();
  338. }
  339. #endif
  340. static void log_init(void) {
  341. char filename[FILENAME_MAX + 1];
  342. snprintfz(filename, FILENAME_MAX, "%s/debug.log", netdata_configured_log_dir);
  343. stdout_filename = config_get(CONFIG_SECTION_LOGS, "debug", filename);
  344. snprintfz(filename, FILENAME_MAX, "%s/error.log", netdata_configured_log_dir);
  345. stderr_filename = config_get(CONFIG_SECTION_LOGS, "error", filename);
  346. snprintfz(filename, FILENAME_MAX, "%s/access.log", netdata_configured_log_dir);
  347. stdaccess_filename = config_get(CONFIG_SECTION_LOGS, "access", filename);
  348. #ifdef ENABLE_ACLK
  349. aclklog_enabled = config_get_boolean(CONFIG_SECTION_CLOUD, "conversation log", CONFIG_BOOLEAN_NO);
  350. if (aclklog_enabled) {
  351. snprintfz(filename, FILENAME_MAX, "%s/aclk.log", netdata_configured_log_dir);
  352. aclklog_filename = config_get(CONFIG_SECTION_CLOUD, "conversation log file", filename);
  353. }
  354. #endif
  355. char deffacility[8];
  356. snprintfz(deffacility,7,"%s","daemon");
  357. facility_log = config_get(CONFIG_SECTION_LOGS, "facility", deffacility);
  358. error_log_throttle_period = config_get_number(CONFIG_SECTION_LOGS, "errors flood protection period", error_log_throttle_period);
  359. error_log_errors_per_period = (unsigned long)config_get_number(CONFIG_SECTION_LOGS, "errors to trigger flood protection", (long long int)error_log_errors_per_period);
  360. error_log_errors_per_period_backup = error_log_errors_per_period;
  361. setenv("NETDATA_ERRORS_THROTTLE_PERIOD", config_get(CONFIG_SECTION_LOGS, "errors flood protection period" , ""), 1);
  362. setenv("NETDATA_ERRORS_PER_PERIOD", config_get(CONFIG_SECTION_LOGS, "errors to trigger flood protection", ""), 1);
  363. }
  364. char *initialize_lock_directory_path(char *prefix)
  365. {
  366. char filename[FILENAME_MAX + 1];
  367. snprintfz(filename, FILENAME_MAX, "%s/lock", prefix);
  368. return config_get(CONFIG_SECTION_DIRECTORIES, "lock", filename);
  369. }
  370. static void backwards_compatible_config() {
  371. // move [global] options to the [web] section
  372. config_move(CONFIG_SECTION_GLOBAL, "http port listen backlog",
  373. CONFIG_SECTION_WEB, "listen backlog");
  374. config_move(CONFIG_SECTION_GLOBAL, "bind socket to IP",
  375. CONFIG_SECTION_WEB, "bind to");
  376. config_move(CONFIG_SECTION_GLOBAL, "bind to",
  377. CONFIG_SECTION_WEB, "bind to");
  378. config_move(CONFIG_SECTION_GLOBAL, "port",
  379. CONFIG_SECTION_WEB, "default port");
  380. config_move(CONFIG_SECTION_GLOBAL, "default port",
  381. CONFIG_SECTION_WEB, "default port");
  382. config_move(CONFIG_SECTION_GLOBAL, "disconnect idle web clients after seconds",
  383. CONFIG_SECTION_WEB, "disconnect idle clients after seconds");
  384. config_move(CONFIG_SECTION_GLOBAL, "respect web browser do not track policy",
  385. CONFIG_SECTION_WEB, "respect do not track policy");
  386. config_move(CONFIG_SECTION_GLOBAL, "web x-frame-options header",
  387. CONFIG_SECTION_WEB, "x-frame-options response header");
  388. config_move(CONFIG_SECTION_GLOBAL, "enable web responses gzip compression",
  389. CONFIG_SECTION_WEB, "enable gzip compression");
  390. config_move(CONFIG_SECTION_GLOBAL, "web compression strategy",
  391. CONFIG_SECTION_WEB, "gzip compression strategy");
  392. config_move(CONFIG_SECTION_GLOBAL, "web compression level",
  393. CONFIG_SECTION_WEB, "gzip compression level");
  394. config_move(CONFIG_SECTION_GLOBAL, "config directory",
  395. CONFIG_SECTION_DIRECTORIES, "config");
  396. config_move(CONFIG_SECTION_GLOBAL, "stock config directory",
  397. CONFIG_SECTION_DIRECTORIES, "stock config");
  398. config_move(CONFIG_SECTION_GLOBAL, "log directory",
  399. CONFIG_SECTION_DIRECTORIES, "log");
  400. config_move(CONFIG_SECTION_GLOBAL, "web files directory",
  401. CONFIG_SECTION_DIRECTORIES, "web");
  402. config_move(CONFIG_SECTION_GLOBAL, "cache directory",
  403. CONFIG_SECTION_DIRECTORIES, "cache");
  404. config_move(CONFIG_SECTION_GLOBAL, "lib directory",
  405. CONFIG_SECTION_DIRECTORIES, "lib");
  406. config_move(CONFIG_SECTION_GLOBAL, "home directory",
  407. CONFIG_SECTION_DIRECTORIES, "home");
  408. config_move(CONFIG_SECTION_GLOBAL, "lock directory",
  409. CONFIG_SECTION_DIRECTORIES, "lock");
  410. config_move(CONFIG_SECTION_GLOBAL, "plugins directory",
  411. CONFIG_SECTION_DIRECTORIES, "plugins");
  412. config_move(CONFIG_SECTION_HEALTH, "health configuration directory",
  413. CONFIG_SECTION_DIRECTORIES, "health config");
  414. config_move(CONFIG_SECTION_HEALTH, "stock health configuration directory",
  415. CONFIG_SECTION_DIRECTORIES, "stock health config");
  416. config_move(CONFIG_SECTION_REGISTRY, "registry db directory",
  417. CONFIG_SECTION_DIRECTORIES, "registry");
  418. config_move(CONFIG_SECTION_GLOBAL, "debug log",
  419. CONFIG_SECTION_LOGS, "debug");
  420. config_move(CONFIG_SECTION_GLOBAL, "error log",
  421. CONFIG_SECTION_LOGS, "error");
  422. config_move(CONFIG_SECTION_GLOBAL, "access log",
  423. CONFIG_SECTION_LOGS, "access");
  424. config_move(CONFIG_SECTION_GLOBAL, "facility log",
  425. CONFIG_SECTION_LOGS, "facility");
  426. config_move(CONFIG_SECTION_GLOBAL, "errors flood protection period",
  427. CONFIG_SECTION_LOGS, "errors flood protection period");
  428. config_move(CONFIG_SECTION_GLOBAL, "errors to trigger flood protection",
  429. CONFIG_SECTION_LOGS, "errors to trigger flood protection");
  430. config_move(CONFIG_SECTION_GLOBAL, "debug flags",
  431. CONFIG_SECTION_LOGS, "debug flags");
  432. config_move(CONFIG_SECTION_GLOBAL, "TZ environment variable",
  433. CONFIG_SECTION_ENV_VARS, "TZ");
  434. config_move(CONFIG_SECTION_PLUGINS, "PATH environment variable",
  435. CONFIG_SECTION_ENV_VARS, "PATH");
  436. config_move(CONFIG_SECTION_PLUGINS, "PYTHONPATH environment variable",
  437. CONFIG_SECTION_ENV_VARS, "PYTHONPATH");
  438. config_move(CONFIG_SECTION_STATSD, "enabled",
  439. CONFIG_SECTION_PLUGINS, "statsd");
  440. config_move(CONFIG_SECTION_GLOBAL, "memory mode",
  441. CONFIG_SECTION_DB, "mode");
  442. config_move(CONFIG_SECTION_GLOBAL, "history",
  443. CONFIG_SECTION_DB, "retention");
  444. config_move(CONFIG_SECTION_GLOBAL, "update every",
  445. CONFIG_SECTION_DB, "update every");
  446. config_move(CONFIG_SECTION_GLOBAL, "page cache size",
  447. CONFIG_SECTION_DB, "dbengine page cache size MB");
  448. config_move(CONFIG_SECTION_DB, "page cache size",
  449. CONFIG_SECTION_DB, "dbengine page cache size MB");
  450. config_move(CONFIG_SECTION_GLOBAL, "page cache uses malloc",
  451. CONFIG_SECTION_DB, "dbengine page cache with malloc");
  452. config_move(CONFIG_SECTION_DB, "page cache with malloc",
  453. CONFIG_SECTION_DB, "dbengine page cache with malloc");
  454. config_move(CONFIG_SECTION_GLOBAL, "dbengine disk space",
  455. CONFIG_SECTION_DB, "dbengine disk space MB");
  456. config_move(CONFIG_SECTION_GLOBAL, "dbengine multihost disk space",
  457. CONFIG_SECTION_DB, "dbengine multihost disk space MB");
  458. config_move(CONFIG_SECTION_GLOBAL, "memory deduplication (ksm)",
  459. CONFIG_SECTION_DB, "memory deduplication (ksm)");
  460. config_move(CONFIG_SECTION_GLOBAL, "dbengine page fetch timeout",
  461. CONFIG_SECTION_DB, "dbengine page fetch timeout secs");
  462. config_move(CONFIG_SECTION_GLOBAL, "dbengine page fetch retries",
  463. CONFIG_SECTION_DB, "dbengine page fetch retries");
  464. config_move(CONFIG_SECTION_GLOBAL, "dbengine extent pages",
  465. CONFIG_SECTION_DB, "dbengine pages per extent");
  466. config_move(CONFIG_SECTION_GLOBAL, "cleanup obsolete charts after seconds",
  467. CONFIG_SECTION_DB, "cleanup obsolete charts after secs");
  468. config_move(CONFIG_SECTION_GLOBAL, "gap when lost iterations above",
  469. CONFIG_SECTION_DB, "gap when lost iterations above");
  470. config_move(CONFIG_SECTION_GLOBAL, "cleanup orphan hosts after seconds",
  471. CONFIG_SECTION_DB, "cleanup orphan hosts after secs");
  472. config_move(CONFIG_SECTION_GLOBAL, "delete obsolete charts files",
  473. CONFIG_SECTION_DB, "delete obsolete charts files");
  474. config_move(CONFIG_SECTION_GLOBAL, "delete orphan hosts files",
  475. CONFIG_SECTION_DB, "delete orphan hosts files");
  476. config_move(CONFIG_SECTION_GLOBAL, "enable zero metrics",
  477. CONFIG_SECTION_DB, "enable zero metrics");
  478. }
  479. static void get_netdata_configured_variables() {
  480. backwards_compatible_config();
  481. // ------------------------------------------------------------------------
  482. // get the hostname
  483. char buf[HOSTNAME_MAX + 1];
  484. if(gethostname(buf, HOSTNAME_MAX) == -1){
  485. error("Cannot get machine hostname.");
  486. }
  487. netdata_configured_hostname = config_get(CONFIG_SECTION_GLOBAL, "hostname", buf);
  488. debug(D_OPTIONS, "hostname set to '%s'", netdata_configured_hostname);
  489. // ------------------------------------------------------------------------
  490. // get default database update frequency
  491. default_rrd_update_every = (int) config_get_number(CONFIG_SECTION_DB, "update every", UPDATE_EVERY);
  492. if(default_rrd_update_every < 1 || default_rrd_update_every > 600) {
  493. error("Invalid data collection frequency (update every) %d given. Defaulting to %d.", default_rrd_update_every, UPDATE_EVERY);
  494. default_rrd_update_every = UPDATE_EVERY;
  495. config_set_number(CONFIG_SECTION_DB, "update every", default_rrd_update_every);
  496. }
  497. // ------------------------------------------------------------------------
  498. // get default memory mode for the database
  499. {
  500. const char *mode = config_get(CONFIG_SECTION_DB, "mode", rrd_memory_mode_name(default_rrd_memory_mode));
  501. default_rrd_memory_mode = rrd_memory_mode_id(mode);
  502. if(strcmp(mode, rrd_memory_mode_name(default_rrd_memory_mode)) != 0) {
  503. error("Invalid memory mode '%s' given. Using '%s'", mode, rrd_memory_mode_name(default_rrd_memory_mode));
  504. config_set(CONFIG_SECTION_DB, "mode", rrd_memory_mode_name(default_rrd_memory_mode));
  505. }
  506. }
  507. // ------------------------------------------------------------------------
  508. // get default database size
  509. if(default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE && default_rrd_memory_mode != RRD_MEMORY_MODE_NONE) {
  510. default_rrd_history_entries = (int)config_get_number(
  511. CONFIG_SECTION_DB, "retention",
  512. align_entries_to_pagesize(default_rrd_memory_mode, RRD_DEFAULT_HISTORY_ENTRIES));
  513. long h = align_entries_to_pagesize(default_rrd_memory_mode, default_rrd_history_entries);
  514. if (h != default_rrd_history_entries) {
  515. config_set_number(CONFIG_SECTION_DB, "retention", h);
  516. default_rrd_history_entries = (int)h;
  517. }
  518. }
  519. // ------------------------------------------------------------------------
  520. // get system paths
  521. netdata_configured_user_config_dir = config_get(CONFIG_SECTION_DIRECTORIES, "config", netdata_configured_user_config_dir);
  522. netdata_configured_stock_config_dir = config_get(CONFIG_SECTION_DIRECTORIES, "stock config", netdata_configured_stock_config_dir);
  523. netdata_configured_log_dir = config_get(CONFIG_SECTION_DIRECTORIES, "log", netdata_configured_log_dir);
  524. netdata_configured_web_dir = config_get(CONFIG_SECTION_DIRECTORIES, "web", netdata_configured_web_dir);
  525. netdata_configured_cache_dir = config_get(CONFIG_SECTION_DIRECTORIES, "cache", netdata_configured_cache_dir);
  526. netdata_configured_varlib_dir = config_get(CONFIG_SECTION_DIRECTORIES, "lib", netdata_configured_varlib_dir);
  527. char *env_home=getenv("HOME");
  528. netdata_configured_home_dir = config_get(CONFIG_SECTION_DIRECTORIES, "home", env_home?env_home:netdata_configured_home_dir);
  529. netdata_configured_lock_dir = initialize_lock_directory_path(netdata_configured_varlib_dir);
  530. {
  531. pluginsd_initialize_plugin_directories();
  532. netdata_configured_primary_plugins_dir = plugin_directories[PLUGINSD_STOCK_PLUGINS_DIRECTORY_PATH];
  533. }
  534. #ifdef ENABLE_DBENGINE
  535. // ------------------------------------------------------------------------
  536. // get default Database Engine page cache size in MiB
  537. db_engine_use_malloc = config_get_boolean(CONFIG_SECTION_DB, "dbengine page cache with malloc", CONFIG_BOOLEAN_NO);
  538. default_rrdeng_page_cache_mb = (int) config_get_number(CONFIG_SECTION_DB, "dbengine page cache size MB", default_rrdeng_page_cache_mb);
  539. if(default_rrdeng_page_cache_mb < RRDENG_MIN_PAGE_CACHE_SIZE_MB) {
  540. error("Invalid page cache size %d given. Defaulting to %d.", default_rrdeng_page_cache_mb, RRDENG_MIN_PAGE_CACHE_SIZE_MB);
  541. default_rrdeng_page_cache_mb = RRDENG_MIN_PAGE_CACHE_SIZE_MB;
  542. config_set_number(CONFIG_SECTION_DB, "dbengine page cache size MB", default_rrdeng_page_cache_mb);
  543. }
  544. // ------------------------------------------------------------------------
  545. // get default Database Engine disk space quota in MiB
  546. default_rrdeng_disk_quota_mb = (int) config_get_number(CONFIG_SECTION_DB, "dbengine disk space MB", default_rrdeng_disk_quota_mb);
  547. if(default_rrdeng_disk_quota_mb < RRDENG_MIN_DISK_SPACE_MB) {
  548. error("Invalid dbengine disk space %d given. Defaulting to %d.", default_rrdeng_disk_quota_mb, RRDENG_MIN_DISK_SPACE_MB);
  549. default_rrdeng_disk_quota_mb = RRDENG_MIN_DISK_SPACE_MB;
  550. config_set_number(CONFIG_SECTION_DB, "dbengine disk space MB", default_rrdeng_disk_quota_mb);
  551. }
  552. default_multidb_disk_quota_mb = (int) config_get_number(CONFIG_SECTION_DB, "dbengine multihost disk space MB", compute_multidb_diskspace());
  553. if(default_multidb_disk_quota_mb < RRDENG_MIN_DISK_SPACE_MB) {
  554. error("Invalid multidb disk space %d given. Defaulting to %d.", default_multidb_disk_quota_mb, default_rrdeng_disk_quota_mb);
  555. default_multidb_disk_quota_mb = default_rrdeng_disk_quota_mb;
  556. config_set_number(CONFIG_SECTION_DB, "dbengine multihost disk space MB", default_multidb_disk_quota_mb);
  557. }
  558. #else
  559. if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) {
  560. error_report("RRD_MEMORY_MODE_DBENGINE is not supported in this platform. The agent will use db mode 'save' instead.");
  561. default_rrd_memory_mode = RRD_MEMORY_MODE_SAVE;
  562. }
  563. #endif
  564. // ------------------------------------------------------------------------
  565. netdata_configured_host_prefix = config_get(CONFIG_SECTION_GLOBAL, "host access prefix", "");
  566. verify_netdata_host_prefix();
  567. // --------------------------------------------------------------------
  568. // get KSM settings
  569. #ifdef MADV_MERGEABLE
  570. enable_ksm = config_get_boolean(CONFIG_SECTION_DB, "memory deduplication (ksm)", enable_ksm);
  571. #endif
  572. // --------------------------------------------------------------------
  573. // metric correlations
  574. enable_metric_correlations = config_get_boolean(CONFIG_SECTION_GLOBAL, "enable metric correlations", enable_metric_correlations);
  575. default_metric_correlations_method = mc_string_to_method(config_get(CONFIG_SECTION_GLOBAL, "metric correlations method", mc_method_to_string(default_metric_correlations_method)));
  576. // --------------------------------------------------------------------
  577. rrdset_free_obsolete_time = config_get_number(CONFIG_SECTION_DB, "cleanup obsolete charts after secs", rrdset_free_obsolete_time);
  578. // Current chart locking and invalidation scheme doesn't prevent Netdata from segmentation faults if a short
  579. // cleanup delay is set. Extensive stress tests showed that 10 seconds is quite a safe delay. Look at
  580. // https://github.com/netdata/netdata/pull/11222#issuecomment-868367920 for more information.
  581. if (rrdset_free_obsolete_time < 10) {
  582. rrdset_free_obsolete_time = 10;
  583. info("The \"cleanup obsolete charts after seconds\" option was set to 10 seconds.");
  584. config_set_number(CONFIG_SECTION_DB, "cleanup obsolete charts after secs", rrdset_free_obsolete_time);
  585. }
  586. gap_when_lost_iterations_above = (int)config_get_number(CONFIG_SECTION_DB, "gap when lost iterations above", gap_when_lost_iterations_above);
  587. if (gap_when_lost_iterations_above < 1) {
  588. gap_when_lost_iterations_above = 1;
  589. config_set_number(CONFIG_SECTION_DB, "gap when lost iterations above", gap_when_lost_iterations_above);
  590. }
  591. // --------------------------------------------------------------------
  592. // rrdcontext
  593. rrdcontext_enabled = config_get_boolean(CONFIG_SECTION_CLOUD, "rrdcontexts", rrdcontext_enabled);
  594. // --------------------------------------------------------------------
  595. // get various system parameters
  596. get_system_HZ();
  597. get_system_cpus();
  598. get_system_pid_max();
  599. }
  600. int load_netdata_conf(char *filename, char overwrite_used) {
  601. errno = 0;
  602. int ret = 0;
  603. if(filename && *filename) {
  604. ret = config_load(filename, overwrite_used, NULL);
  605. if(!ret)
  606. error("CONFIG: cannot load config file '%s'.", filename);
  607. }
  608. else {
  609. filename = strdupz_path_subpath(netdata_configured_user_config_dir, "netdata.conf");
  610. ret = config_load(filename, overwrite_used, NULL);
  611. if(!ret) {
  612. info("CONFIG: cannot load user config '%s'. Will try the stock version.", filename);
  613. freez(filename);
  614. filename = strdupz_path_subpath(netdata_configured_stock_config_dir, "netdata.conf");
  615. ret = config_load(filename, overwrite_used, NULL);
  616. if(!ret)
  617. info("CONFIG: cannot load stock config '%s'. Running with internal defaults.", filename);
  618. }
  619. freez(filename);
  620. }
  621. return ret;
  622. }
  623. // coverity[ +tainted_string_sanitize_content : arg-0 ]
  624. static inline void coverity_remove_taint(char *s)
  625. {
  626. (void)s;
  627. }
  628. int get_system_info(struct rrdhost_system_info *system_info) {
  629. char *script;
  630. script = mallocz(sizeof(char) * (strlen(netdata_configured_primary_plugins_dir) + strlen("system-info.sh") + 2));
  631. sprintf(script, "%s/%s", netdata_configured_primary_plugins_dir, "system-info.sh");
  632. if (unlikely(access(script, R_OK) != 0)) {
  633. info("System info script %s not found.",script);
  634. freez(script);
  635. return 1;
  636. }
  637. pid_t command_pid;
  638. info("Executing %s", script);
  639. FILE *fp = mypopen(script, &command_pid);
  640. if(fp) {
  641. char line[200 + 1];
  642. // Removed the double strlens, if the Coverity tainted string warning reappears I'll revert.
  643. // One time init code, but I'm curious about the warning...
  644. while (fgets(line, 200, fp) != NULL) {
  645. char *value=line;
  646. while (*value && *value != '=') value++;
  647. if (*value=='=') {
  648. *value='\0';
  649. value++;
  650. char *end = value;
  651. while (*end && *end != '\n') end++;
  652. *end = '\0'; // Overwrite newline if present
  653. coverity_remove_taint(line); // I/O is controlled result of system_info.sh - not tainted
  654. coverity_remove_taint(value);
  655. if(unlikely(rrdhost_set_system_info_variable(system_info, line, value))) {
  656. info("Unexpected environment variable %s=%s", line, value);
  657. }
  658. else {
  659. info("%s=%s", line, value);
  660. setenv(line, value, 1);
  661. }
  662. }
  663. }
  664. mypclose(fp, command_pid);
  665. }
  666. freez(script);
  667. return 0;
  668. }
  669. void set_silencers_filename() {
  670. char filename[FILENAME_MAX + 1];
  671. snprintfz(filename, FILENAME_MAX, "%s/health.silencers.json", netdata_configured_varlib_dir);
  672. silencers_filename = config_get(CONFIG_SECTION_HEALTH, "silencers file", filename);
  673. }
  674. /* Any config setting that can be accessed without a default value i.e. configget(...,...,NULL) *MUST*
  675. be set in this procedure to be called in all the relevant code paths.
  676. */
  677. void post_conf_load(char **user)
  678. {
  679. // --------------------------------------------------------------------
  680. // get the user we should run
  681. // IMPORTANT: this is required before web_files_uid()
  682. if(getuid() == 0) {
  683. *user = config_get(CONFIG_SECTION_GLOBAL, "run as user", NETDATA_USER);
  684. }
  685. else {
  686. struct passwd *passwd = getpwuid(getuid());
  687. *user = config_get(CONFIG_SECTION_GLOBAL, "run as user", (passwd && passwd->pw_name)?passwd->pw_name:"");
  688. }
  689. // --------------------------------------------------------------------
  690. // Check if the cloud is enabled
  691. #if defined( DISABLE_CLOUD ) || !defined( ENABLE_ACLK )
  692. netdata_cloud_setting = 0;
  693. #else
  694. netdata_cloud_setting = appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1);
  695. #endif
  696. // This must be set before any point in the code that accesses it. Do not move it from this function.
  697. appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", DEFAULT_CLOUD_BASE_URL);
  698. }
  699. int main(int argc, char **argv) {
  700. int i;
  701. int config_loaded = 0;
  702. int dont_fork = 0;
  703. bool close_open_fds = true;
  704. size_t default_stacksize;
  705. char *user = NULL;
  706. static_threads = static_threads_get();
  707. netdata_ready=0;
  708. // set the name for logging
  709. program_name = "netdata";
  710. if (argc > 1 && strcmp(argv[1], SPAWN_SERVER_COMMAND_LINE_ARGUMENT) == 0) {
  711. // don't run netdata, this is the spawn server
  712. spawn_server();
  713. exit(0);
  714. }
  715. // parse options
  716. {
  717. int num_opts = sizeof(option_definitions) / sizeof(struct option_def);
  718. char optstring[(num_opts * 2) + 1];
  719. int string_i = 0;
  720. for( i = 0; i < num_opts; i++ ) {
  721. optstring[string_i] = option_definitions[i].val;
  722. string_i++;
  723. if(option_definitions[i].arg_name) {
  724. optstring[string_i] = ':';
  725. string_i++;
  726. }
  727. }
  728. // terminate optstring
  729. optstring[string_i] ='\0';
  730. optstring[(num_opts *2)] ='\0';
  731. int opt;
  732. while( (opt = getopt(argc, argv, optstring)) != -1 ) {
  733. switch(opt) {
  734. case 'c':
  735. if(load_netdata_conf(optarg, 1) != 1) {
  736. error("Cannot load configuration file %s.", optarg);
  737. return 1;
  738. }
  739. else {
  740. debug(D_OPTIONS, "Configuration loaded from %s.", optarg);
  741. post_conf_load(&user);
  742. load_cloud_conf(1);
  743. config_loaded = 1;
  744. }
  745. break;
  746. case 'D':
  747. dont_fork = 1;
  748. break;
  749. case 'd':
  750. dont_fork = 0;
  751. break;
  752. case 'h':
  753. return help(0);
  754. case 'i':
  755. config_set(CONFIG_SECTION_WEB, "bind to", optarg);
  756. break;
  757. case 'P':
  758. strncpy(pidfile, optarg, FILENAME_MAX);
  759. pidfile[FILENAME_MAX] = '\0';
  760. break;
  761. case 'p':
  762. config_set(CONFIG_SECTION_GLOBAL, "default port", optarg);
  763. break;
  764. case 's':
  765. config_set(CONFIG_SECTION_GLOBAL, "host access prefix", optarg);
  766. break;
  767. case 't':
  768. config_set(CONFIG_SECTION_GLOBAL, "update every", optarg);
  769. break;
  770. case 'u':
  771. config_set(CONFIG_SECTION_GLOBAL, "run as user", optarg);
  772. break;
  773. case 'v':
  774. case 'V':
  775. printf("%s %s\n", program_name, program_version);
  776. return 0;
  777. case 'W':
  778. {
  779. char* stacksize_string = "stacksize=";
  780. char* debug_flags_string = "debug_flags=";
  781. char* claim_string = "claim";
  782. #ifdef ENABLE_DBENGINE
  783. char* createdataset_string = "createdataset=";
  784. char* stresstest_string = "stresstest=";
  785. #endif
  786. if(strcmp(optarg, "sqlite-check") == 0) {
  787. sql_init_database(DB_CHECK_INTEGRITY, 0);
  788. return 0;
  789. }
  790. if(strcmp(optarg, "sqlite-fix") == 0) {
  791. sql_init_database(DB_CHECK_FIX_DB, 0);
  792. return 0;
  793. }
  794. if(strcmp(optarg, "sqlite-compact") == 0) {
  795. sql_init_database(DB_CHECK_RECLAIM_SPACE, 0);
  796. return 0;
  797. }
  798. if(strcmp(optarg, "unittest") == 0) {
  799. if (unit_test_static_threads())
  800. return 1;
  801. if (unit_test_buffer())
  802. return 1;
  803. if (unit_test_str2ld())
  804. return 1;
  805. if (unit_test_bitmap256())
  806. return 1;
  807. // No call to load the config file on this code-path
  808. post_conf_load(&user);
  809. get_netdata_configured_variables();
  810. default_rrd_update_every = 1;
  811. default_rrd_memory_mode = RRD_MEMORY_MODE_RAM;
  812. default_health_enabled = 0;
  813. storage_tiers = 1;
  814. registry_init();
  815. if(rrd_init("unittest", NULL)) {
  816. fprintf(stderr, "rrd_init failed for unittest\n");
  817. return 1;
  818. }
  819. default_rrdpush_enabled = 0;
  820. if(run_all_mockup_tests()) return 1;
  821. if(unit_test_storage()) return 1;
  822. #ifdef ENABLE_DBENGINE
  823. if(test_dbengine()) return 1;
  824. #endif
  825. if(test_sqlite()) return 1;
  826. if (dictionary_unittest(10000))
  827. return 1;
  828. if (rrdlabels_unittest())
  829. return 1;
  830. fprintf(stderr, "\n\nALL TESTS PASSED\n\n");
  831. return 0;
  832. }
  833. #ifdef ENABLE_ML_TESTS
  834. else if(strcmp(optarg, "mltest") == 0) {
  835. return test_ml(argc, argv);
  836. }
  837. #endif
  838. #ifdef ENABLE_DBENGINE
  839. else if(strcmp(optarg, "mctest") == 0) {
  840. return mc_unittest();
  841. }
  842. else if(strcmp(optarg, "ctxtest") == 0) {
  843. return ctx_unittest();
  844. }
  845. else if(strcmp(optarg, "dicttest") == 0) {
  846. return dictionary_unittest(10000);
  847. }
  848. else if(strcmp(optarg, "rrdlabelstest") == 0) {
  849. return rrdlabels_unittest();
  850. }
  851. else if(strncmp(optarg, createdataset_string, strlen(createdataset_string)) == 0) {
  852. optarg += strlen(createdataset_string);
  853. unsigned history_seconds = strtoul(optarg, NULL, 0);
  854. generate_dbengine_dataset(history_seconds);
  855. return 0;
  856. }
  857. else if(strncmp(optarg, stresstest_string, strlen(stresstest_string)) == 0) {
  858. char *endptr;
  859. unsigned test_duration_sec = 0, dset_charts = 0, query_threads = 0, ramp_up_seconds = 0,
  860. page_cache_mb = 0, disk_space_mb = 0, workers = 16;
  861. optarg += strlen(stresstest_string);
  862. test_duration_sec = (unsigned)strtoul(optarg, &endptr, 0);
  863. if (',' == *endptr)
  864. dset_charts = (unsigned)strtoul(endptr + 1, &endptr, 0);
  865. if (',' == *endptr)
  866. query_threads = (unsigned)strtoul(endptr + 1, &endptr, 0);
  867. if (',' == *endptr)
  868. ramp_up_seconds = (unsigned)strtoul(endptr + 1, &endptr, 0);
  869. if (',' == *endptr)
  870. page_cache_mb = (unsigned)strtoul(endptr + 1, &endptr, 0);
  871. if (',' == *endptr)
  872. disk_space_mb = (unsigned)strtoul(endptr + 1, &endptr, 0);
  873. if (',' == *endptr)
  874. workers = (unsigned)strtoul(endptr + 1, &endptr, 0);
  875. if (workers > 1024)
  876. workers = 1024;
  877. char workers_str[16];
  878. snprintf(workers_str, 15, "%u", workers);
  879. setenv("UV_THREADPOOL_SIZE", workers_str, 1);
  880. dbengine_stress_test(test_duration_sec, dset_charts, query_threads, ramp_up_seconds,
  881. page_cache_mb, disk_space_mb);
  882. return 0;
  883. }
  884. #endif
  885. else if(strcmp(optarg, "simple-pattern") == 0) {
  886. if(optind + 2 > argc) {
  887. fprintf(stderr, "%s", "\nUSAGE: -W simple-pattern 'pattern' 'string'\n\n"
  888. " Checks if 'pattern' matches the given 'string'.\n"
  889. " - 'pattern' can be one or more space separated words.\n"
  890. " - each 'word' can contain one or more asterisks.\n"
  891. " - words starting with '!' give negative matches.\n"
  892. " - words are processed left to right\n"
  893. "\n"
  894. "Examples:\n"
  895. "\n"
  896. " > match all veth interfaces, except veth0:\n"
  897. "\n"
  898. " -W simple-pattern '!veth0 veth*' 'veth12'\n"
  899. "\n"
  900. "\n"
  901. " > match all *.ext files directly in /path/:\n"
  902. " (this will not match *.ext files in a subdir of /path/)\n"
  903. "\n"
  904. " -W simple-pattern '!/path/*/*.ext /path/*.ext' '/path/test.ext'\n"
  905. "\n"
  906. );
  907. return 1;
  908. }
  909. const char *haystack = argv[optind];
  910. const char *needle = argv[optind + 1];
  911. size_t len = strlen(needle) + 1;
  912. char wildcarded[len];
  913. SIMPLE_PATTERN *p = simple_pattern_create(haystack, NULL, SIMPLE_PATTERN_EXACT);
  914. int ret = simple_pattern_matches_extract(p, needle, wildcarded, len);
  915. simple_pattern_free(p);
  916. if(ret) {
  917. fprintf(stdout, "RESULT: MATCHED - pattern '%s' matches '%s', wildcarded '%s'\n", haystack, needle, wildcarded);
  918. return 0;
  919. }
  920. else {
  921. fprintf(stdout, "RESULT: NOT MATCHED - pattern '%s' does not match '%s', wildcarded '%s'\n", haystack, needle, wildcarded);
  922. return 1;
  923. }
  924. }
  925. else if(strncmp(optarg, stacksize_string, strlen(stacksize_string)) == 0) {
  926. optarg += strlen(stacksize_string);
  927. config_set(CONFIG_SECTION_GLOBAL, "pthread stack size", optarg);
  928. }
  929. else if(strncmp(optarg, debug_flags_string, strlen(debug_flags_string)) == 0) {
  930. optarg += strlen(debug_flags_string);
  931. config_set(CONFIG_SECTION_LOGS, "debug flags", optarg);
  932. debug_flags = strtoull(optarg, NULL, 0);
  933. }
  934. else if(strcmp(optarg, "set") == 0) {
  935. if(optind + 3 > argc) {
  936. fprintf(stderr, "%s", "\nUSAGE: -W set 'section' 'key' 'value'\n\n"
  937. " Overwrites settings of netdata.conf.\n"
  938. "\n"
  939. " These options interact with: -c netdata.conf\n"
  940. " If -c netdata.conf is given on the command line,\n"
  941. " before -W set... the user may overwrite command\n"
  942. " line parameters at netdata.conf\n"
  943. " If -c netdata.conf is given after (or missing)\n"
  944. " -W set... the user cannot overwrite the command line\n"
  945. " parameters."
  946. "\n"
  947. );
  948. return 1;
  949. }
  950. const char *section = argv[optind];
  951. const char *key = argv[optind + 1];
  952. const char *value = argv[optind + 2];
  953. optind += 3;
  954. // set this one as the default
  955. // only if it is not already set in the config file
  956. // so the caller can use -c netdata.conf before or
  957. // after this parameter to prevent or allow overwriting
  958. // variables at netdata.conf
  959. config_set_default(section, key, value);
  960. // fprintf(stderr, "SET section '%s', key '%s', value '%s'\n", section, key, value);
  961. }
  962. else if(strcmp(optarg, "set2") == 0) {
  963. if(optind + 4 > argc) {
  964. fprintf(stderr, "%s", "\nUSAGE: -W set 'conf_file' 'section' 'key' 'value'\n\n"
  965. " Overwrites settings of netdata.conf or cloud.conf\n"
  966. "\n"
  967. " These options interact with: -c netdata.conf\n"
  968. " If -c netdata.conf is given on the command line,\n"
  969. " before -W set... the user may overwrite command\n"
  970. " line parameters at netdata.conf\n"
  971. " If -c netdata.conf is given after (or missing)\n"
  972. " -W set... the user cannot overwrite the command line\n"
  973. " parameters."
  974. " conf_file can be \"cloud\" or \"netdata\".\n"
  975. "\n"
  976. );
  977. return 1;
  978. }
  979. const char *conf_file = argv[optind]; /* "cloud" is cloud.conf, otherwise netdata.conf */
  980. struct config *tmp_config = strcmp(conf_file, "cloud") ? &netdata_config : &cloud_config;
  981. const char *section = argv[optind + 1];
  982. const char *key = argv[optind + 2];
  983. const char *value = argv[optind + 3];
  984. optind += 4;
  985. // set this one as the default
  986. // only if it is not already set in the config file
  987. // so the caller can use -c netdata.conf before or
  988. // after this parameter to prevent or allow overwriting
  989. // variables at netdata.conf
  990. appconfig_set_default(tmp_config, section, key, value);
  991. // fprintf(stderr, "SET section '%s', key '%s', value '%s'\n", section, key, value);
  992. }
  993. else if(strcmp(optarg, "get") == 0) {
  994. if(optind + 3 > argc) {
  995. fprintf(stderr, "%s", "\nUSAGE: -W get 'section' 'key' 'value'\n\n"
  996. " Prints settings of netdata.conf.\n"
  997. "\n"
  998. " These options interact with: -c netdata.conf\n"
  999. " -c netdata.conf has to be given before -W get.\n"
  1000. "\n"
  1001. );
  1002. return 1;
  1003. }
  1004. if(!config_loaded) {
  1005. fprintf(stderr, "warning: no configuration file has been loaded. Use -c CONFIG_FILE, before -W get. Using default config.\n");
  1006. load_netdata_conf(NULL, 0);
  1007. post_conf_load(&user);
  1008. }
  1009. get_netdata_configured_variables();
  1010. const char *section = argv[optind];
  1011. const char *key = argv[optind + 1];
  1012. const char *def = argv[optind + 2];
  1013. const char *value = config_get(section, key, def);
  1014. printf("%s\n", value);
  1015. return 0;
  1016. }
  1017. else if(strcmp(optarg, "get2") == 0) {
  1018. if(optind + 4 > argc) {
  1019. fprintf(stderr, "%s", "\nUSAGE: -W get2 'conf_file' 'section' 'key' 'value'\n\n"
  1020. " Prints settings of netdata.conf or cloud.conf\n"
  1021. "\n"
  1022. " These options interact with: -c netdata.conf\n"
  1023. " -c netdata.conf has to be given before -W get2.\n"
  1024. " conf_file can be \"cloud\" or \"netdata\".\n"
  1025. "\n"
  1026. );
  1027. return 1;
  1028. }
  1029. if(!config_loaded) {
  1030. fprintf(stderr, "warning: no configuration file has been loaded. Use -c CONFIG_FILE, before -W get. Using default config.\n");
  1031. load_netdata_conf(NULL, 0);
  1032. post_conf_load(&user);
  1033. load_cloud_conf(1);
  1034. }
  1035. get_netdata_configured_variables();
  1036. const char *conf_file = argv[optind]; /* "cloud" is cloud.conf, otherwise netdata.conf */
  1037. struct config *tmp_config = strcmp(conf_file, "cloud") ? &netdata_config : &cloud_config;
  1038. const char *section = argv[optind + 1];
  1039. const char *key = argv[optind + 2];
  1040. const char *def = argv[optind + 3];
  1041. const char *value = appconfig_get(tmp_config, section, key, def);
  1042. printf("%s\n", value);
  1043. return 0;
  1044. }
  1045. else if(strncmp(optarg, claim_string, strlen(claim_string)) == 0) {
  1046. /* will trigger a claiming attempt when the agent is initialized */
  1047. claiming_pending_arguments = optarg + strlen(claim_string);
  1048. }
  1049. else if(strcmp(optarg, "buildinfo") == 0) {
  1050. printf("Version: %s %s\n", program_name, program_version);
  1051. print_build_info();
  1052. return 0;
  1053. }
  1054. else if(strcmp(optarg, "buildinfojson") == 0) {
  1055. print_build_info_json();
  1056. return 0;
  1057. }
  1058. else if(strcmp(optarg, "keepopenfds") == 0) {
  1059. // Internal dev option to skip closing inherited
  1060. // open FDs. Useful, when we want to run the agent
  1061. // under profiling tools that open/maintain their
  1062. // own FDs.
  1063. close_open_fds = false;
  1064. } else {
  1065. fprintf(stderr, "Unknown -W parameter '%s'\n", optarg);
  1066. return help(1);
  1067. }
  1068. }
  1069. break;
  1070. default: /* ? */
  1071. fprintf(stderr, "Unknown parameter '%c'\n", opt);
  1072. return help(1);
  1073. }
  1074. }
  1075. }
  1076. #ifdef _SC_OPEN_MAX
  1077. if (close_open_fds == true) {
  1078. // close all open file descriptors, except the standard ones
  1079. // the caller may have left open files (lxc-attach has this issue)
  1080. for(int fd = (int) (sysconf(_SC_OPEN_MAX) - 1); fd > 2; fd--)
  1081. if(fd_is_valid(fd))
  1082. close(fd);
  1083. }
  1084. #endif
  1085. if(!config_loaded)
  1086. {
  1087. load_netdata_conf(NULL, 0);
  1088. post_conf_load(&user);
  1089. load_cloud_conf(0);
  1090. }
  1091. // ------------------------------------------------------------------------
  1092. // initialize netdata
  1093. {
  1094. char *pmax = config_get(CONFIG_SECTION_GLOBAL, "glibc malloc arena max for plugins", "1");
  1095. if(pmax && *pmax)
  1096. setenv("MALLOC_ARENA_MAX", pmax, 1);
  1097. #if defined(HAVE_C_MALLOPT)
  1098. i = (int)config_get_number(CONFIG_SECTION_GLOBAL, "glibc malloc arena max for netdata", 1);
  1099. if(i > 0)
  1100. mallopt(M_ARENA_MAX, 1);
  1101. #endif
  1102. // initialize the system clocks
  1103. clocks_init();
  1104. // prepare configuration environment variables for the plugins
  1105. setenv("UV_THREADPOOL_SIZE", config_get(CONFIG_SECTION_GLOBAL, "libuv worker threads", "16"), 1);
  1106. get_netdata_configured_variables();
  1107. set_global_environment();
  1108. // work while we are cd into config_dir
  1109. // to allow the plugins refer to their config
  1110. // files using relative filenames
  1111. if(chdir(netdata_configured_user_config_dir) == -1)
  1112. fatal("Cannot cd to '%s'", netdata_configured_user_config_dir);
  1113. // Get execution path before switching user to avoid permission issues
  1114. get_netdata_execution_path();
  1115. }
  1116. {
  1117. // --------------------------------------------------------------------
  1118. // get the debugging flags from the configuration file
  1119. char *flags = config_get(CONFIG_SECTION_LOGS, "debug flags", "0x0000000000000000");
  1120. setenv("NETDATA_DEBUG_FLAGS", flags, 1);
  1121. debug_flags = strtoull(flags, NULL, 0);
  1122. debug(D_OPTIONS, "Debug flags set to '0x%" PRIX64 "'.", debug_flags);
  1123. if(debug_flags != 0) {
  1124. struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
  1125. if(setrlimit(RLIMIT_CORE, &rl) != 0)
  1126. error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
  1127. #ifdef HAVE_SYS_PRCTL_H
  1128. prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  1129. #endif
  1130. }
  1131. // --------------------------------------------------------------------
  1132. // get log filenames and settings
  1133. log_init();
  1134. error_log_limit_unlimited();
  1135. // initialize the log files
  1136. open_all_log_files();
  1137. get_system_timezone();
  1138. // --------------------------------------------------------------------
  1139. // get the certificate and start security
  1140. #ifdef ENABLE_HTTPS
  1141. security_init();
  1142. #endif
  1143. // --------------------------------------------------------------------
  1144. // This is the safest place to start the SILENCERS structure
  1145. set_silencers_filename();
  1146. health_initialize_global_silencers();
  1147. // --------------------------------------------------------------------
  1148. // Initialize ML configuration
  1149. ml_init();
  1150. // --------------------------------------------------------------------
  1151. // setup process signals
  1152. // block signals while initializing threads.
  1153. // this causes the threads to block signals.
  1154. signals_block();
  1155. // setup the signals we want to use
  1156. signals_init();
  1157. // setup threads configs
  1158. default_stacksize = netdata_threads_init();
  1159. // --------------------------------------------------------------------
  1160. // check which threads are enabled and initialize them
  1161. for (i = 0; static_threads[i].name != NULL ; i++) {
  1162. struct netdata_static_thread *st = &static_threads[i];
  1163. if(st->config_name)
  1164. st->enabled = config_get_boolean(st->config_section, st->config_name, st->enabled);
  1165. if(st->enabled && st->init_routine)
  1166. st->init_routine();
  1167. }
  1168. // --------------------------------------------------------------------
  1169. // create the listening sockets
  1170. web_client_api_v1_init();
  1171. web_server_threading_selection();
  1172. if(web_server_mode != WEB_SERVER_MODE_NONE)
  1173. api_listen_sockets_setup();
  1174. }
  1175. #ifdef NETDATA_INTERNAL_CHECKS
  1176. if(debug_flags != 0) {
  1177. struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
  1178. if(setrlimit(RLIMIT_CORE, &rl) != 0)
  1179. error("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
  1180. #ifdef HAVE_SYS_PRCTL_H
  1181. prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
  1182. #endif
  1183. }
  1184. #endif /* NETDATA_INTERNAL_CHECKS */
  1185. // get the max file limit
  1186. if(getrlimit(RLIMIT_NOFILE, &rlimit_nofile) != 0)
  1187. error("getrlimit(RLIMIT_NOFILE) failed");
  1188. else
  1189. info("resources control: allowed file descriptors: soft = %zu, max = %zu", (size_t)rlimit_nofile.rlim_cur, (size_t)rlimit_nofile.rlim_max);
  1190. // fork, switch user, create pid file, set process priority
  1191. if(become_daemon(dont_fork, user) == -1)
  1192. fatal("Cannot daemonize myself.");
  1193. info("netdata started on pid %d.", getpid());
  1194. netdata_threads_init_after_fork((size_t)config_get_number(CONFIG_SECTION_GLOBAL, "pthread stack size", (long)default_stacksize));
  1195. // initialize internal registry
  1196. registry_init();
  1197. // fork the spawn server
  1198. spawn_init();
  1199. /*
  1200. * Libuv uv_spawn() uses SIGCHLD internally:
  1201. * https://github.com/libuv/libuv/blob/cc51217a317e96510fbb284721d5e6bc2af31e33/src/unix/process.c#L485
  1202. * and inadvertently replaces the netdata signal handler which was setup during initialization.
  1203. * Thusly, we must explicitly restore the signal handler for SIGCHLD.
  1204. * Warning: extreme care is needed when mixing and matching POSIX and libuv.
  1205. */
  1206. signals_restore_SIGCHLD();
  1207. // ------------------------------------------------------------------------
  1208. // initialize rrd, registry, health, rrdpush, etc.
  1209. netdata_anonymous_statistics_enabled=-1;
  1210. struct rrdhost_system_info *system_info = callocz(1, sizeof(struct rrdhost_system_info));
  1211. get_system_info(system_info);
  1212. system_info->hops = 0;
  1213. get_install_type(&system_info->install_type, &system_info->prebuilt_arch, &system_info->prebuilt_dist);
  1214. if(rrd_init(netdata_configured_hostname, system_info))
  1215. fatal("Cannot initialize localhost instance with name '%s'.", netdata_configured_hostname);
  1216. char agent_crash_file[FILENAME_MAX + 1];
  1217. char agent_incomplete_shutdown_file[FILENAME_MAX + 1];
  1218. snprintfz(agent_incomplete_shutdown_file, FILENAME_MAX, "%s/.agent_incomplete_shutdown", netdata_configured_varlib_dir);
  1219. int incomplete_shutdown_detected = (unlink(agent_incomplete_shutdown_file) == 0);
  1220. snprintfz(agent_crash_file, FILENAME_MAX, "%s/.agent_crash", netdata_configured_varlib_dir);
  1221. int crash_detected = (unlink(agent_crash_file) == 0);
  1222. int fd = open(agent_crash_file, O_WRONLY | O_CREAT | O_TRUNC, 444);
  1223. if (fd >= 0)
  1224. close(fd);
  1225. // ------------------------------------------------------------------------
  1226. // Claim netdata agent to a cloud endpoint
  1227. if (claiming_pending_arguments)
  1228. claim_agent(claiming_pending_arguments);
  1229. load_claiming_state();
  1230. // ------------------------------------------------------------------------
  1231. // enable log flood protection
  1232. error_log_limit_reset();
  1233. // Load host labels
  1234. reload_host_labels();
  1235. // ------------------------------------------------------------------------
  1236. // spawn the threads
  1237. web_server_config_options();
  1238. netdata_zero_metrics_enabled = config_get_boolean_ondemand(CONFIG_SECTION_DB, "enable zero metrics", CONFIG_BOOLEAN_NO);
  1239. set_late_global_environment();
  1240. for (i = 0; static_threads[i].name != NULL ; i++) {
  1241. struct netdata_static_thread *st = &static_threads[i];
  1242. if(st->enabled) {
  1243. st->thread = mallocz(sizeof(netdata_thread_t));
  1244. debug(D_SYSTEM, "Starting thread %s.", st->name);
  1245. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, st);
  1246. }
  1247. else debug(D_SYSTEM, "Not starting thread %s.", st->name);
  1248. }
  1249. // ------------------------------------------------------------------------
  1250. // Initialize netdata agent command serving from cli and signals
  1251. commands_init();
  1252. info("netdata initialization completed. Enjoy real-time performance monitoring!");
  1253. netdata_ready = 1;
  1254. send_statistics("START", "-", "-");
  1255. if (crash_detected)
  1256. send_statistics("CRASH", "-", "-");
  1257. if (incomplete_shutdown_detected)
  1258. send_statistics("INCOMPLETE_SHUTDOWN", "-", "-");
  1259. //check if ANALYTICS needs to start
  1260. if (netdata_anonymous_statistics_enabled == 1) {
  1261. for (i = 0; static_threads[i].name != NULL; i++) {
  1262. if (!strncmp(static_threads[i].name, "ANALYTICS", 9)) {
  1263. struct netdata_static_thread *st = &static_threads[i];
  1264. st->thread = mallocz(sizeof(netdata_thread_t));
  1265. st->enabled = 1;
  1266. debug(D_SYSTEM, "Starting thread %s.", st->name);
  1267. netdata_thread_create(st->thread, st->name, NETDATA_THREAD_OPTION_DEFAULT, st->start_routine, st);
  1268. }
  1269. }
  1270. }
  1271. // ------------------------------------------------------------------------
  1272. // Report ACLK build failure
  1273. #ifndef ENABLE_ACLK
  1274. error("This agent doesn't have ACLK.");
  1275. char filename[FILENAME_MAX + 1];
  1276. snprintfz(filename, FILENAME_MAX, "%s/.aclk_report_sent", netdata_configured_varlib_dir);
  1277. if (netdata_anonymous_statistics_enabled > 0 && access(filename, F_OK)) { // -1 -> not initialized
  1278. send_statistics("ACLK_DISABLED", "-", "-");
  1279. int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 444);
  1280. if (fd == -1)
  1281. error("Cannot create file '%s'. Please fix this.", filename);
  1282. else
  1283. close(fd);
  1284. }
  1285. #endif
  1286. // ------------------------------------------------------------------------
  1287. // unblock signals
  1288. signals_unblock();
  1289. // ------------------------------------------------------------------------
  1290. // Handle signals
  1291. signals_handle();
  1292. // should never reach this point
  1293. // but we need it for rpmlint #2752
  1294. return 1;
  1295. }