From c4fd4aa07c2abb18f5839e1b910294c39c3e30be Mon Sep 17 00:00:00 2001 From: Markos Fountoulakis <44345837+mfundul@users.noreply.github.com> Date: Tue, 16 Jun 2020 19:34:19 +0300 Subject: Get netdata execution path early to avoid user permission issues (#9339) * Get netdata execution path early to avoid user permission issues --- daemon/daemon.c | 21 +++++++++++++++++++++ daemon/daemon.h | 4 +++- daemon/main.c | 3 +++ 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'daemon') 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(); } { -- cgit v1.2.3