From 2306044fc85c2285c3f37666e7c2825d88b11aee Mon Sep 17 00:00:00 2001 From: "Costa Tsaousis (ktsaou)" Date: Mon, 16 Nov 2015 00:49:53 +0200 Subject: [PATCH] removed NETDATA_DAEMON - it can be controlled via command line; now it opens log files before releasing root permissions; added preprocessor warnings if zlib or math are disabled; netdata.start adapted for installed netdata --- configure.ac | 6 ---- netdata.start | 76 +++++++++++++++++++++++++------------------- src/daemon.c | 19 +++++++---- src/daemon.h | 2 +- src/main.c | 16 +++------- src/storage_number.c | 2 ++ src/web_client.c | 2 ++ 7 files changed, 65 insertions(+), 58 deletions(-) diff --git a/configure.ac b/configure.ac index fbfbec6e..56279792 100644 --- a/configure.ac +++ b/configure.ac @@ -26,12 +26,6 @@ AC_PROG_INSTALL PKG_PROG_PKG_CONFIG AC_USE_SYSTEM_EXTENSIONS -AC_ARG_ENABLE( - [daemon], - [AS_HELP_STRING([--disable-daemon], [disable daemon])], - , - [enable_daemon="yes"] -) AC_ARG_ENABLE( [plugin-plugin-nfacct], [AS_HELP_STRING([--enable-plugin-plugin-nfacct], [enable nfacct plugin, requires root])], diff --git a/netdata.start b/netdata.start index b8fa2f9e..c1260898 100755 --- a/netdata.start +++ b/netdata.start @@ -1,5 +1,33 @@ #!/bin/bash +netdata=$(which netdata) + +if [ -z "${netdata}" -o ! -f src/netdata ] +then + echo >&2 "Running ./autogen.sh ..." + ./autogen.sh || exit 1 + + echo >&2 "Running ./configure ..." + ./configure --prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-zlib --with-math --with-user=netdata CFLAGS="-march=native -O3 -Wall -Wextra" || exit 1 + + echo >&2 "Compiling netdata ..." + make || exit 1 +fi + +if [ -z "${netdata}" -o src/netdata -nt "${netdata}" ] +then + echo >&2 "Installing netdata ..." + make install + + echo >&2 "Adding netdata group ..." + getent group netdata > /dev/null || groupadd -r netdata + + echo >&2 "Adding netdata user ..." + getent passwd netdata > /dev/null || useradd -r -g netdata -c netdata -s /sbin/nologin -d / netdata +fi + +netdata=$(which netdata) + base="`dirname "$0"`" if [ ! -d "${base}" ] @@ -20,7 +48,7 @@ cd "${base}" || exit 1 config_option() { local key="${1}" value="${2}" line= - if [ -s "conf.d/netdata.conf" ] + if [ -s "/etc/netdata/netdata.conf" ] then line="$( grep "^[[:space:]]*${key}[[:space:]]*=[[:space:]]*" "conf.d/netdata.conf" | head -n 1 )" [ ! -z "${line}" ] && value="$( echo "${line}" | cut -d '=' -f 2 | sed -e "s/^[[:space:]]\+//g" -e "s/[[:space:]]\+$//g" )" @@ -30,7 +58,7 @@ config_option() { } # user -defuser="nobody" +defuser="netdata" [ ! "${UID}" = "0" ] && defuser="${USER}" NETDATA_USER="$( config_option "run as user" "${defuser}" )" @@ -43,28 +71,12 @@ defport=19999 NETDATA_PORT="$( config_option "port" ${defport} )" # directories -NETDATA_CACHE_DIR="$( config_option "database directory" "cache" )" -NETDATA_WEB_DIR="$( config_option "web files directory" "web" )" +NETDATA_CACHE_DIR="$( config_option "database directory" "/var/cache/netdata" )" +NETDATA_WEB_DIR="$( config_option "web files directory" "/usr/share/netdata/web" )" NETDATA_LOG_DIR="log" NETDATA_CONF_DIR="conf.d" -# ----------------------------------------------------------------------------- -# compile netdata - -echo >&2 "Compiling netdata (debug flags = $NETDATA_DEBUG)..." -if [ $[ NETDATA_DEBUG ] -ne 0 ] - then - make install debug=1 || exit 1 # this installs in the current directory - - # let netdata core dump if it crashes - ulimit -c unlimited - -else - make install || exit 1 # this installs in the current directory -fi - - # ----------------------------------------------------------------------------- # prepare the directories @@ -110,7 +122,7 @@ echo >&2 export TZ=":/etc/localtime" echo >&2 "Starting netdata..." -`pwd`/netdata "${@}" +netdata "${@}" if [ $? -ne 0 ] then @@ -125,35 +137,35 @@ fi # ----------------------------------------------------------------------------- # save a config file, if it is not already there -if [ ! -s conf.d/netdata.conf ] +if [ ! -s /etc/netdata/netdata.conf ] then echo >&2 "Downloading default configuration from netdata..." sleep 5 # remove a possibly obsolete download - [ -f conf.d/netdata.conf.new ] && rm conf.d/netdata.conf.new + [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new # try wget - wget 2>/dev/null -O conf.d/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf" + wget 2>/dev/null -O /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf" ret=$? # try curl - if [ $ret -ne 0 -o ! -s conf.d/netdata.conf.net ] + if [ $ret -ne 0 -o ! -s /etc/netdata/netdata.conf.net ] then - curl -s -o conf.d/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf" + curl -s -o /etc/netdata/netdata.conf.new "http://localhost:${NETDATA_PORT}/netdata.conf" ret=$? fi - if [ $ret -eq 0 -a -s conf.d/netdata.conf.new ] + if [ $ret -eq 0 -a -s /etc/netdata/netdata.conf.new ] then - mv conf.d/netdata.conf.new conf.d/netdata.conf - echo >&2 "New configuration saved for you to edit at conf.d/netdata.conf" + mv /etc/netdata/netdata.conf.new /etc/netdata/netdata.conf + echo >&2 "New configuration saved for you to edit at /etc/netdata/netdata.conf" - chown -R "${NETDATA_USER}" conf.d/netdata.conf - chmod 0664 conf.d/netdata.conf + chown "${NETDATA_USER}" /etc/netdata/netdata.conf + chmod 0664 /etc/netdata/netdata.conf else echo >&2 "Cannnot download configuration from netdata daemon using url 'http://localhost:${NETDATA_PORT}/netdata.conf'" - [ -f conf.d/netdata.conf.new ] && rm conf.d/netdata.conf.new + [ -f /etc/netdata/netdata.conf.new ] && rm /etc/netdata/netdata.conf.new fi fi diff --git a/src/daemon.c b/src/daemon.c index 19c7657e..e2315f0b 100755 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,7 +1,6 @@ #ifdef HAVE_CONFIG_H #include #endif -#ifdef NETDATA_DAEMON #include #include #include @@ -142,7 +141,7 @@ int become_user(const char *username) return(0); } -int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp) +int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp) { fflush(NULL); @@ -151,14 +150,14 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c if(input && *input) { if((input_fd = open(input, O_RDONLY, 0666)) == -1) { - fprintf(stderr, "Cannot open input file '%s' (%s).", input, strerror(errno)); + fprintf(stderr, "Cannot open input file '%s' (%s).\n", input, strerror(errno)); return -1; } } if(output && *output) { if((output_fd = open(output, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) { - fprintf(stderr, "Cannot open output log file '%s' (%s).", output, strerror(errno)); + fprintf(stderr, "Cannot open output log file '%s' (%s).\n", output, strerror(errno)); if(input_fd != -1) close(input_fd); return -1; } @@ -166,7 +165,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c if(error && *error) { if((error_fd = open(error, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) { - fprintf(stderr, "Cannot open error log file '%s' (%s).", error, strerror(errno)); + fprintf(stderr, "Cannot open error log file '%s' (%s).\n", error, strerror(errno)); if(input_fd != -1) close(input_fd); if(output_fd != -1) close(output_fd); return -1; @@ -175,7 +174,7 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c if(access && *access && access_fd) { if((*access_fd = open(access, O_RDWR | O_APPEND | O_CREAT, 0666)) == -1) { - fprintf(stderr, "Cannot open access log file '%s' (%s).", access, strerror(errno)); + fprintf(stderr, "Cannot open access log file '%s' (%s).\n", access, strerror(errno)); if(input_fd != -1) close(input_fd); if(output_fd != -1) close(output_fd); if(error_fd != -1) close(error_fd); @@ -316,6 +315,12 @@ int become_daemon(int dont_fork, int close_all_files, const char *input, const c } } + if(user && *user) { + if(become_user(user) != 0) { + error("Cannot become user '%s'. Continuing as we are.", user); + } + else debug(D_OPTIONS, "Successfully became user '%s'.", user); + } + return(0); } -#endif diff --git a/src/daemon.h b/src/daemon.h index 5821163c..9f8694f9 100755 --- a/src/daemon.h +++ b/src/daemon.h @@ -7,6 +7,6 @@ extern void prepare_rundir(); extern int become_user(const char *username); -extern int become_daemon(int dont_fork, int close_all_files, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp); +extern int become_daemon(int dont_fork, int close_all_files, const char *user, const char *input, const char *output, const char *error, const char *access, int *access_fd, FILE **access_fp); #endif /* NETDATA_DAEMON_H */ diff --git a/src/main.c b/src/main.c index 138277df..44dff9d7 100755 --- a/src/main.c +++ b/src/main.c @@ -239,6 +239,7 @@ int main(int argc, char **argv) char *output_log_file = NULL; char *error_log_file = NULL; char *access_log_file = NULL; + char *user = NULL; { char buffer[1024]; @@ -343,14 +344,7 @@ int main(int argc, char **argv) // -------------------------------------------------------------------- prepare_rundir(); - char *user = config_get("global", "run as user", (getuid() == 0)?NETDATA_USER:""); - if(*user) { - if(become_user(user) != 0) { - fprintf(stderr, "Cannot become user %s.\n", user); - exit(1); - } - else debug(D_OPTIONS, "Successfully became user %s.", user); - } + user = config_get("global", "run as user", (getuid() == 0)?NETDATA_USER:""); // -------------------------------------------------------------------- @@ -382,12 +376,10 @@ int main(int argc, char **argv) // never become a problem if(nice(20) == -1) fprintf(stderr, "Cannot lower my CPU priority. Error: %s.\n", strerror(errno)); -#ifdef NETDATA_DAEMON - if(become_daemon(dont_fork, 0, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) { - fprintf(stderr, "Cannot demonize myself (%s).", strerror(errno)); + if(become_daemon(dont_fork, 0, user, input_log_file, output_log_file, error_log_file, access_log_file, &access_fd, &stdaccess) == -1) { + fatal("Cannot demonize myself (%s).", strerror(errno)); exit(1); } -#endif if(output_log_syslog || error_log_syslog || access_log_syslog) openlog("netdata", LOG_PID, LOG_DAEMON); diff --git a/src/storage_number.c b/src/storage_number.c index fe8e4f1f..823619e6 100755 --- a/src/storage_number.c +++ b/src/storage_number.c @@ -3,6 +3,8 @@ #endif #ifdef STORAGE_WITH_MATH #include +#else +#warning "You are building without math. math allows accurate calculations. It should be enabled." #endif #include "common.h" diff --git a/src/web_client.c b/src/web_client.c index 48fd47be..0e18f7ac 100755 --- a/src/web_client.c +++ b/src/web_client.c @@ -229,6 +229,8 @@ void web_client_reset(struct web_client *w) #ifdef NETDATA_WITH_ZLIB if(likely(w->zoutput)) sent = (long)w->zstream.total_out; +#else +#warning "You are building without zlib. zlib allows netdata to trasnfer a lot less data with web clients. It should be enabled." #endif long size = (w->mode == WEB_CLIENT_MODE_FILECOPY)?w->data->rbytes:w->data->bytes; -- 2.39.2