daemon.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "common.h"
  3. #include <sched.h>
  4. char pidfile[FILENAME_MAX + 1] = "";
  5. char claimingdirectory[FILENAME_MAX + 1];
  6. char exepath[FILENAME_MAX + 1];
  7. void get_netdata_execution_path(void)
  8. {
  9. int ret;
  10. size_t exepath_size = 0;
  11. struct passwd *passwd = NULL;
  12. char *user = NULL;
  13. passwd = getpwuid(getuid());
  14. user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
  15. exepath_size = sizeof(exepath) - 1;
  16. ret = uv_exepath(exepath, &exepath_size);
  17. if (0 != ret) {
  18. error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
  19. uv_strerror(ret));
  20. fatal("Cannot start netdata without getting execution path.");
  21. }
  22. exepath[exepath_size] = '\0';
  23. }
  24. static void chown_open_file(int fd, uid_t uid, gid_t gid) {
  25. if(fd == -1) return;
  26. struct stat buf;
  27. if(fstat(fd, &buf) == -1) {
  28. error("Cannot fstat() fd %d", fd);
  29. return;
  30. }
  31. if((buf.st_uid != uid || buf.st_gid != gid) && S_ISREG(buf.st_mode)) {
  32. if(fchown(fd, uid, gid) == -1)
  33. error("Cannot fchown() fd %d.", fd);
  34. }
  35. }
  36. void create_needed_dir(const char *dir, uid_t uid, gid_t gid)
  37. {
  38. // attempt to create the directory
  39. if(mkdir(dir, 0755) == 0) {
  40. // we created it
  41. // chown it to match the required user
  42. if(chown(dir, uid, gid) == -1)
  43. error("Cannot chown directory '%s' to %u:%u", dir, (unsigned int)uid, (unsigned int)gid);
  44. }
  45. else if(errno != EEXIST)
  46. // log an error only if the directory does not exist
  47. error("Cannot create directory '%s'", dir);
  48. }
  49. void clean_directory(char *dirname)
  50. {
  51. DIR *dir = opendir(dirname);
  52. if(!dir) return;
  53. int dir_fd = dirfd(dir);
  54. struct dirent *de = NULL;
  55. while((de = readdir(dir)))
  56. if(de->d_type == DT_REG)
  57. if (unlinkat(dir_fd, de->d_name, 0))
  58. error("Cannot delete %s/%s", dirname, de->d_name);
  59. closedir(dir);
  60. }
  61. int become_user(const char *username, int pid_fd) {
  62. int am_i_root = (getuid() == 0)?1:0;
  63. struct passwd *pw = getpwnam(username);
  64. if(!pw) {
  65. error("User %s is not present.", username);
  66. return -1;
  67. }
  68. uid_t uid = pw->pw_uid;
  69. gid_t gid = pw->pw_gid;
  70. create_needed_dir(netdata_configured_cache_dir, uid, gid);
  71. create_needed_dir(netdata_configured_varlib_dir, uid, gid);
  72. create_needed_dir(netdata_configured_lock_dir, uid, gid);
  73. create_needed_dir(claimingdirectory, uid, gid);
  74. clean_directory(netdata_configured_lock_dir);
  75. if(pidfile[0]) {
  76. if(chown(pidfile, uid, gid) == -1)
  77. error("Cannot chown '%s' to %u:%u", pidfile, (unsigned int)uid, (unsigned int)gid);
  78. }
  79. int ngroups = (int)sysconf(_SC_NGROUPS_MAX);
  80. gid_t *supplementary_groups = NULL;
  81. if(ngroups > 0) {
  82. supplementary_groups = mallocz(sizeof(gid_t) * ngroups);
  83. if(getgrouplist(username, gid, supplementary_groups, &ngroups) == -1) {
  84. if(am_i_root)
  85. error("Cannot get supplementary groups of user '%s'.", username);
  86. ngroups = 0;
  87. }
  88. }
  89. chown_open_file(STDOUT_FILENO, uid, gid);
  90. chown_open_file(STDERR_FILENO, uid, gid);
  91. chown_open_file(stdaccess_fd, uid, gid);
  92. chown_open_file(pid_fd, uid, gid);
  93. if(supplementary_groups && ngroups > 0) {
  94. if(setgroups((size_t)ngroups, supplementary_groups) == -1) {
  95. if(am_i_root)
  96. error("Cannot set supplementary groups for user '%s'", username);
  97. }
  98. ngroups = 0;
  99. }
  100. if(supplementary_groups)
  101. freez(supplementary_groups);
  102. #ifdef __APPLE__
  103. if(setregid(gid, gid) != 0) {
  104. #else
  105. if(setresgid(gid, gid, gid) != 0) {
  106. #endif /* __APPLE__ */
  107. error("Cannot switch to user's %s group (gid: %u).", username, gid);
  108. return -1;
  109. }
  110. #ifdef __APPLE__
  111. if(setreuid(uid, uid) != 0) {
  112. #else
  113. if(setresuid(uid, uid, uid) != 0) {
  114. #endif /* __APPLE__ */
  115. error("Cannot switch to user %s (uid: %u).", username, uid);
  116. return -1;
  117. }
  118. if(setgid(gid) != 0) {
  119. error("Cannot switch to user's %s group (gid: %u).", username, gid);
  120. return -1;
  121. }
  122. if(setegid(gid) != 0) {
  123. error("Cannot effectively switch to user's %s group (gid: %u).", username, gid);
  124. return -1;
  125. }
  126. if(setuid(uid) != 0) {
  127. error("Cannot switch to user %s (uid: %u).", username, uid);
  128. return -1;
  129. }
  130. if(seteuid(uid) != 0) {
  131. error("Cannot effectively switch to user %s (uid: %u).", username, uid);
  132. return -1;
  133. }
  134. return(0);
  135. }
  136. #ifndef OOM_SCORE_ADJ_MAX
  137. #define OOM_SCORE_ADJ_MAX (1000)
  138. #endif
  139. #ifndef OOM_SCORE_ADJ_MIN
  140. #define OOM_SCORE_ADJ_MIN (-1000)
  141. #endif
  142. static void oom_score_adj(void) {
  143. char buf[30 + 1];
  144. long long int old_score, wanted_score = OOM_SCORE_ADJ_MAX, final_score = 0;
  145. // read the existing score
  146. if(read_single_signed_number_file("/proc/self/oom_score_adj", &old_score)) {
  147. error("Out-Of-Memory (OOM) score setting is not supported on this system.");
  148. return;
  149. }
  150. if (old_score != 0) {
  151. wanted_score = old_score;
  152. analytics_report_oom_score(old_score);
  153. }
  154. // check the environment
  155. char *s = getenv("OOMScoreAdjust");
  156. if(!s || !*s) {
  157. snprintfz(buf, 30, "%d", (int)wanted_score);
  158. s = buf;
  159. }
  160. // check netdata.conf configuration
  161. s = config_get(CONFIG_SECTION_GLOBAL, "OOM score", s);
  162. if(s && *s && (isdigit(*s) || *s == '-' || *s == '+'))
  163. wanted_score = atoll(s);
  164. else if(s && !strcmp(s, "keep")) {
  165. info("Out-Of-Memory (OOM) kept as-is (running with %d)", (int) old_score);
  166. return;
  167. }
  168. else {
  169. info("Out-Of-Memory (OOM) score not changed due to non-numeric setting: '%s' (running with %d)", s, (int)old_score);
  170. return;
  171. }
  172. if(wanted_score < OOM_SCORE_ADJ_MIN) {
  173. error("Wanted Out-Of-Memory (OOM) score %d is too small. Using %d", (int)wanted_score, (int)OOM_SCORE_ADJ_MIN);
  174. wanted_score = OOM_SCORE_ADJ_MIN;
  175. }
  176. if(wanted_score > OOM_SCORE_ADJ_MAX) {
  177. error("Wanted Out-Of-Memory (OOM) score %d is too big. Using %d", (int)wanted_score, (int)OOM_SCORE_ADJ_MAX);
  178. wanted_score = OOM_SCORE_ADJ_MAX;
  179. }
  180. if(old_score == wanted_score) {
  181. info("Out-Of-Memory (OOM) score is already set to the wanted value %d", (int)old_score);
  182. return;
  183. }
  184. int written = 0;
  185. int fd = open("/proc/self/oom_score_adj", O_WRONLY);
  186. if(fd != -1) {
  187. snprintfz(buf, 30, "%d", (int)wanted_score);
  188. ssize_t len = strlen(buf);
  189. if(len > 0 && write(fd, buf, (size_t)len) == len) written = 1;
  190. close(fd);
  191. if(written) {
  192. if(read_single_signed_number_file("/proc/self/oom_score_adj", &final_score))
  193. error("Adjusted my Out-Of-Memory (OOM) score to %d, but cannot verify it.", (int)wanted_score);
  194. else if(final_score == wanted_score)
  195. info("Adjusted my Out-Of-Memory (OOM) score from %d to %d.", (int)old_score, (int)final_score);
  196. else
  197. error("Adjusted my Out-Of-Memory (OOM) score from %d to %d, but it has been set to %d.", (int)old_score, (int)wanted_score, (int)final_score);
  198. analytics_report_oom_score(final_score);
  199. }
  200. else
  201. error("Failed to adjust my Out-Of-Memory (OOM) score to %d. Running with %d. (systemd systems may change it via netdata.service)", (int)wanted_score, (int)old_score);
  202. }
  203. else
  204. error("Failed to adjust my Out-Of-Memory (OOM) score. Cannot open /proc/self/oom_score_adj for writing.");
  205. }
  206. static void process_nice_level(void) {
  207. #ifdef HAVE_NICE
  208. int nice_level = (int)config_get_number(CONFIG_SECTION_GLOBAL, "process nice level", 19);
  209. if(nice(nice_level) == -1) error("Cannot set netdata CPU nice level to %d.", nice_level);
  210. else debug(D_SYSTEM, "Set netdata nice level to %d.", nice_level);
  211. #endif // HAVE_NICE
  212. };
  213. #define SCHED_FLAG_NONE 0x00
  214. #define SCHED_FLAG_PRIORITY_CONFIGURABLE 0x01 // the priority is user configurable
  215. #define SCHED_FLAG_KEEP_AS_IS 0x04 // do not attempt to set policy, priority or nice()
  216. #define SCHED_FLAG_USE_NICE 0x08 // use nice() after setting this policy
  217. struct sched_def {
  218. char *name;
  219. int policy;
  220. int priority;
  221. uint8_t flags;
  222. } scheduler_defaults[] = {
  223. // the order of array members is important!
  224. // the first defined is the default used by netdata
  225. // the available members are important too!
  226. // these are all the possible scheduling policies supported by netdata
  227. #ifdef SCHED_IDLE
  228. { "idle", SCHED_IDLE, 0, SCHED_FLAG_NONE },
  229. #endif
  230. #ifdef SCHED_OTHER
  231. { "other", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
  232. { "nice", SCHED_OTHER, 0, SCHED_FLAG_USE_NICE },
  233. #endif
  234. #ifdef SCHED_RR
  235. { "rr", SCHED_RR, 0, SCHED_FLAG_PRIORITY_CONFIGURABLE },
  236. #endif
  237. #ifdef SCHED_FIFO
  238. { "fifo", SCHED_FIFO, 0, SCHED_FLAG_PRIORITY_CONFIGURABLE },
  239. #endif
  240. #ifdef SCHED_BATCH
  241. { "batch", SCHED_BATCH, 0, SCHED_FLAG_USE_NICE },
  242. #endif
  243. // do not change the scheduling priority
  244. { "keep", 0, 0, SCHED_FLAG_KEEP_AS_IS },
  245. { "none", 0, 0, SCHED_FLAG_KEEP_AS_IS },
  246. // array termination
  247. { NULL, 0, 0, 0 }
  248. };
  249. #ifdef HAVE_SCHED_GETSCHEDULER
  250. static void sched_getscheduler_report(void) {
  251. int sched = sched_getscheduler(0);
  252. if(sched == -1) {
  253. error("Cannot get my current process scheduling policy.");
  254. return;
  255. }
  256. else {
  257. int i;
  258. for(i = 0 ; scheduler_defaults[i].name ; i++) {
  259. if(scheduler_defaults[i].policy == sched) {
  260. if(scheduler_defaults[i].flags & SCHED_FLAG_PRIORITY_CONFIGURABLE) {
  261. struct sched_param param;
  262. if(sched_getparam(0, &param) == -1) {
  263. error("Cannot get the process scheduling priority for my policy '%s'", scheduler_defaults[i].name);
  264. return;
  265. }
  266. else {
  267. info("Running with process scheduling policy '%s', priority %d", scheduler_defaults[i].name, param.sched_priority);
  268. }
  269. }
  270. else if(scheduler_defaults[i].flags & SCHED_FLAG_USE_NICE) {
  271. #ifdef HAVE_GETPRIORITY
  272. int n = getpriority(PRIO_PROCESS, 0);
  273. info("Running with process scheduling policy '%s', nice level %d", scheduler_defaults[i].name, n);
  274. #else // !HAVE_GETPRIORITY
  275. info("Running with process scheduling policy '%s'", scheduler_defaults[i].name);
  276. #endif // !HAVE_GETPRIORITY
  277. }
  278. else {
  279. info("Running with process scheduling policy '%s'", scheduler_defaults[i].name);
  280. }
  281. return;
  282. }
  283. }
  284. }
  285. }
  286. #else // !HAVE_SCHED_GETSCHEDULER
  287. static void sched_getscheduler_report(void) {
  288. #ifdef HAVE_GETPRIORITY
  289. info("Running with priority %d", getpriority(PRIO_PROCESS, 0));
  290. #endif // HAVE_GETPRIORITY
  291. }
  292. #endif // !HAVE_SCHED_GETSCHEDULER
  293. #ifdef HAVE_SCHED_SETSCHEDULER
  294. static void sched_setscheduler_set(void) {
  295. if(scheduler_defaults[0].name) {
  296. const char *name = scheduler_defaults[0].name;
  297. int policy = scheduler_defaults[0].policy, priority = scheduler_defaults[0].priority;
  298. uint8_t flags = scheduler_defaults[0].flags;
  299. int found = 0;
  300. // read the configuration
  301. name = config_get(CONFIG_SECTION_GLOBAL, "process scheduling policy", name);
  302. int i;
  303. for(i = 0 ; scheduler_defaults[i].name ; i++) {
  304. if(!strcmp(name, scheduler_defaults[i].name)) {
  305. found = 1;
  306. policy = scheduler_defaults[i].policy;
  307. priority = scheduler_defaults[i].priority;
  308. flags = scheduler_defaults[i].flags;
  309. if(flags & SCHED_FLAG_KEEP_AS_IS)
  310. goto report;
  311. if(flags & SCHED_FLAG_PRIORITY_CONFIGURABLE)
  312. priority = (int)config_get_number(CONFIG_SECTION_GLOBAL, "process scheduling priority", priority);
  313. #ifdef HAVE_SCHED_GET_PRIORITY_MIN
  314. errno = 0;
  315. if(priority < sched_get_priority_min(policy)) {
  316. error("scheduler %s (%d) priority %d is below the minimum %d. Using the minimum.", name, policy, priority, sched_get_priority_min(policy));
  317. priority = sched_get_priority_min(policy);
  318. }
  319. #endif
  320. #ifdef HAVE_SCHED_GET_PRIORITY_MAX
  321. errno = 0;
  322. if(priority > sched_get_priority_max(policy)) {
  323. error("scheduler %s (%d) priority %d is above the maximum %d. Using the maximum.", name, policy, priority, sched_get_priority_max(policy));
  324. priority = sched_get_priority_max(policy);
  325. }
  326. #endif
  327. break;
  328. }
  329. }
  330. if(!found) {
  331. error("Unknown scheduling policy '%s' - falling back to nice", name);
  332. goto fallback;
  333. }
  334. const struct sched_param param = {
  335. .sched_priority = priority
  336. };
  337. errno = 0;
  338. i = sched_setscheduler(0, policy, &param);
  339. if(i != 0) {
  340. error("Cannot adjust netdata scheduling policy to %s (%d), with priority %d. Falling back to nice.", name, policy, priority);
  341. }
  342. else {
  343. info("Adjusted netdata scheduling policy to %s (%d), with priority %d.", name, policy, priority);
  344. if(!(flags & SCHED_FLAG_USE_NICE))
  345. goto report;
  346. }
  347. }
  348. fallback:
  349. process_nice_level();
  350. report:
  351. sched_getscheduler_report();
  352. }
  353. #else // !HAVE_SCHED_SETSCHEDULER
  354. static void sched_setscheduler_set(void) {
  355. process_nice_level();
  356. }
  357. #endif // !HAVE_SCHED_SETSCHEDULER
  358. int become_daemon(int dont_fork, const char *user)
  359. {
  360. if(!dont_fork) {
  361. int i = fork();
  362. if(i == -1) {
  363. perror("cannot fork");
  364. exit(1);
  365. }
  366. if(i != 0) {
  367. exit(0); // the parent
  368. }
  369. // become session leader
  370. if (setsid() < 0) {
  371. perror("Cannot become session leader.");
  372. exit(2);
  373. }
  374. // fork() again
  375. i = fork();
  376. if(i == -1) {
  377. perror("cannot fork");
  378. exit(1);
  379. }
  380. if(i != 0) {
  381. exit(0); // the parent
  382. }
  383. }
  384. // generate our pid file
  385. int pidfd = -1;
  386. if(pidfile[0]) {
  387. pidfd = open(pidfile, O_WRONLY | O_CREAT, 0644);
  388. if(pidfd >= 0) {
  389. if(ftruncate(pidfd, 0) != 0)
  390. error("Cannot truncate pidfile '%s'.", pidfile);
  391. char b[100];
  392. sprintf(b, "%d\n", getpid());
  393. ssize_t i = write(pidfd, b, strlen(b));
  394. if(i <= 0)
  395. error("Cannot write pidfile '%s'.", pidfile);
  396. }
  397. else error("Failed to open pidfile '%s'.", pidfile);
  398. }
  399. // Set new file permissions
  400. umask(0007);
  401. // adjust my Out-Of-Memory score
  402. oom_score_adj();
  403. // never become a problem
  404. sched_setscheduler_set();
  405. // Set claiming directory based on user config directory with correct ownership
  406. snprintfz(claimingdirectory, FILENAME_MAX, "%s/cloud.d", netdata_configured_varlib_dir);
  407. if(user && *user) {
  408. if(become_user(user, pidfd) != 0) {
  409. error("Cannot become user '%s'. Continuing as we are.", user);
  410. }
  411. else debug(D_SYSTEM, "Successfully became user '%s'.", user);
  412. }
  413. else {
  414. create_needed_dir(netdata_configured_cache_dir, getuid(), getgid());
  415. create_needed_dir(netdata_configured_varlib_dir, getuid(), getgid());
  416. create_needed_dir(netdata_configured_lock_dir, getuid(), getgid());
  417. create_needed_dir(claimingdirectory, getuid(), getgid());
  418. clean_directory(netdata_configured_lock_dir);
  419. }
  420. if(pidfd != -1)
  421. close(pidfd);
  422. return(0);
  423. }