summaryrefslogtreecommitdiffstats
path: root/daemon
diff options
context:
space:
mode:
authorMarkos Fountoulakis <44345837+mfundul@users.noreply.github.com>2020-06-16 19:34:19 +0300
committerGitHub <noreply@github.com>2020-06-16 19:34:19 +0300
commitc4fd4aa07c2abb18f5839e1b910294c39c3e30be (patch)
treeccdb55838aad45b01b9686604168be7ff54ff0ff /daemon
parentac9c33c3443b5de4fc96f815eccab435eda504f9 (diff)
Get netdata execution path early to avoid user permission issues (#9339)
* Get netdata execution path early to avoid user permission issues
Diffstat (limited to 'daemon')
-rw-r--r--daemon/daemon.c21
-rw-r--r--daemon/daemon.h4
-rw-r--r--daemon/main.c3
3 files changed, 27 insertions, 1 deletions
diff --git a/daemon/daemon.c b/daemon/daemon.c
index 7842ecb0be..8dbcdd6d3f 100644
--- a/daemon/daemon.c
+++ b/daemon/daemon.c
@@ -5,6 +5,27 @@
char pidfile[FILENAME_MAX + 1] = "";
char claimingdirectory[FILENAME_MAX + 1];
+char exepath[FILENAME_MAX + 1];
+
+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(exepath) - 1;
+ ret = uv_exepath(exepath, &exepath_size);
+ if (0 != ret) {
+ error("uv_exepath(\"%s\", %u) (user: %s) failed (%s).", exepath, (unsigned)exepath_size, user,
+ uv_strerror(ret));
+ fatal("Cannot start netdata without getting execution path.");
+ }
+ exepath[exepath_size] = '\0';
+}
static void chown_open_file(int fd, uid_t uid, gid_t gid) {
if(fd == -1) return;
diff --git a/daemon/daemon.h b/daemon/daemon.h
index 5d176341a9..bec3df9fc5 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -10,7 +10,9 @@ extern int become_daemon(int dont_fork, const char *user);
extern void netdata_cleanup_and_exit(int i);
extern void send_statistics(const char *action, const char *action_result, const char *action_data);
-extern char pidfile[];
+extern void get_netdata_execution_path(void);
+extern char pidfile[];
+extern char exepath[];
#endif /* NETDATA_DAEMON_H */
diff --git a/daemon/main.c b/daemon/main.c
index 098c749d0b..b0232d7416 100644
--- a/daemon/main.c
+++ b/daemon/main.c
@@ -1272,6 +1272,9 @@ int main(int argc, char **argv) {
// files using relative filenames
if(chdir(netdata_configured_user_config_dir) == -1)
fatal("Cannot cd to '%s'", netdata_configured_user_config_dir);
+
+ // Get execution path before switching user to avoid permission issues
+ get_netdata_execution_path();
}
{