summaryrefslogtreecommitdiffstats
path: root/src/daemon/daemon.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-11-25 17:33:56 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-11-25 17:34:10 +0000
commit83ba6762cc43d9db581b979bb5e3445669e46cc2 (patch)
tree2e69833b43f791ed253a7a20318b767ebe56cdb8 /src/daemon/daemon.c
parentReleasing debian version 1.47.5-1. (diff)
downloadnetdata-83ba6762cc43d9db581b979bb5e3445669e46cc2.tar.xz
netdata-83ba6762cc43d9db581b979bb5e3445669e46cc2.zip
Merging upstream version 2.0.3+dfsg (Closes: #923993, #1042533, #1045145).
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/daemon/daemon.c')
-rw-r--r--src/daemon/daemon.c61
1 files changed, 23 insertions, 38 deletions
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 2392d4cc1..d3ddf027d 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -3,34 +3,24 @@
#include "common.h"
#include <sched.h>
-char pidfile[FILENAME_MAX + 1] = "";
-char claiming_directory[FILENAME_MAX + 1];
-char netdata_exe_path[FILENAME_MAX + 1];
-char netdata_exe_file[FILENAME_MAX + 1];
+char *pidfile = NULL;
+char *netdata_exe_path = NULL;
void get_netdata_execution_path(void) {
- int ret;
- size_t exepath_size = 0;
- struct passwd *passwd = NULL;
- char *user = NULL;
-
- passwd = getpwuid(getuid());
- user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
-
- exepath_size = sizeof(netdata_exe_file) - 1;
- ret = uv_exepath(netdata_exe_file, &exepath_size);
- if (0 != ret) {
- netdata_log_error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", netdata_exe_file, (unsigned)exepath_size, user,
- uv_strerror(ret));
- fatal("Cannot start netdata without getting execution path.");
+ struct passwd *passwd = getpwuid(getuid());
+ char *user = (passwd && passwd->pw_name) ? passwd->pw_name : "";
+
+ char b[FILENAME_MAX + 1];
+ size_t b_size = sizeof(b) - 1;
+ int ret = uv_exepath(b, &b_size);
+ if (ret != 0) {
+ fatal("Cannot start netdata without getting execution path. "
+ "(uv_exepath(\"%s\", %zu), user: '%s', failed: %s).",
+ b, b_size, user, uv_strerror(ret));
}
+ b[b_size] = '\0';
- netdata_exe_file[exepath_size] = '\0';
-
- // macOS's dirname(3) does not modify passed string
- char *tmpdir = strdupz(netdata_exe_file);
- strcpy(netdata_exe_path, dirname(tmpdir));
- freez(tmpdir);
+ netdata_exe_path = strdupz(b);
}
static void fix_directory_file_permissions(const char *dirname, uid_t uid, gid_t gid, bool recursive)
@@ -68,7 +58,7 @@ static void change_dir_ownership(const char *dir, uid_t uid, gid_t gid, bool rec
fix_directory_file_permissions(dir, uid, gid, recursive);
}
-static void clean_directory(char *dirname)
+static void clean_directory(const char *dirname)
{
DIR *dir = opendir(dirname);
if(!dir) return;
@@ -89,7 +79,7 @@ static void prepare_required_directories(uid_t uid, gid_t gid) {
change_dir_ownership(netdata_configured_varlib_dir, uid, gid, false);
change_dir_ownership(netdata_configured_lock_dir, uid, gid, false);
change_dir_ownership(netdata_configured_log_dir, uid, gid, false);
- change_dir_ownership(claiming_directory, uid, gid, false);
+ change_dir_ownership(netdata_configured_cloud_dir, uid, gid, false);
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/registry", netdata_configured_varlib_dir);
@@ -112,7 +102,7 @@ static int become_user(const char *username, int pid_fd) {
prepare_required_directories(uid, gid);
- if(pidfile[0]) {
+ if(pidfile && *pidfile) {
if(chown(pidfile, uid, gid) == -1)
netdata_log_error("Cannot chown '%s' to %u:%u", pidfile, (unsigned int)uid, (unsigned int)gid);
}
@@ -198,7 +188,7 @@ static void oom_score_adj(void) {
}
// check the environment
- char *s = getenv("OOMScoreAdjust");
+ const char *s = getenv("OOMScoreAdjust");
if(!s || !*s) {
snprintfz(buf, sizeof(buf) - 1, "%d", (int)wanted_score);
s = buf;
@@ -442,9 +432,8 @@ int become_daemon(int dont_fork, const char *user)
perror("cannot fork");
exit(1);
}
- if(i != 0) {
- exit(0); // the parent
- }
+ if(i != 0) exit(0); // the parent
+ gettid_uncached();
// become session leader
if (setsid() < 0) {
@@ -458,14 +447,13 @@ int become_daemon(int dont_fork, const char *user)
perror("cannot fork");
exit(1);
}
- if(i != 0) {
- exit(0); // the parent
- }
+ if(i != 0) exit(0); // the parent
+ gettid_uncached();
}
// generate our pid file
int pidfd = -1;
- if(pidfile[0]) {
+ if(pidfile && *pidfile) {
pidfd = open(pidfile, O_WRONLY | O_CREAT | O_CLOEXEC, 0644);
if(pidfd >= 0) {
if(ftruncate(pidfd, 0) != 0)
@@ -490,9 +478,6 @@ int become_daemon(int dont_fork, const char *user)
// never become a problem
sched_setscheduler_set();
- // Set claiming directory based on user config directory with correct ownership
- snprintfz(claiming_directory, FILENAME_MAX, "%s/cloud.d", netdata_configured_varlib_dir);
-
if(user && *user) {
if(become_user(user, pidfd) != 0) {
netdata_log_error("Cannot become user '%s'. Continuing as we are.", user);