@cryptotaxi247 / netdata-1 / commits / 22bdfa615

Squashed commit of the following:

commit 950f04df117bf246cfa9a2fa99bf12a64d6bd0a4 Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 23:47:06 2016 +0300 dont keep the pidfd open commit bd45505d0177703940b2393a5e36b3e80b912ff1 Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 23:33:18 2016 +0300 dont unlink pidfile at start commit 4afd30e81d3cd70400162c7603b659fd35160c8b Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 23:30:47 2016 +0300 close pidfile then unlink commit faf66f4f5f39bda8d0b5e947afbdb8cd1ac505be Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 19:38:49 2016 +0300 fix to keep close the file if it has been successfully chwoned commit 0af1e73e6db3b681e8303ee394fc2ac003db6e67 Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 19:35:31 2016 +0300 chown pidfile only when dropping privileges; do not keep the pidfile open when not dropping privileges commit 550c9f8e9757f5a5767d11764a80f7736c197b37 Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 19:15:32 2016 +0300 do not chown pidfile commit ca808d2231ed175397408ea436a50642e10cd281 Author: Costa Tsaousis (ktsaou) <costa@tsaousis.gr> Date: Sun Apr 3 18:57:30 2016 +0300 added command line parameter -pidfile; the pidfile is only generated if this parameter is passed; #151 #156

Costa Tsaousis (ktsaou) committed Apr 3, 2016 at 23:55 UTC 22bdfa615225617c6ee14f48e88b55fa82cbec08
7 files changed +60 -35
.gitignore
+1
@@ -56,3 +56,4 @@ web/control.html
56 web/datasource.css
57 web/gadget.xml
58 web/index_new.html
59 +system/netdata-openrc
netdata-installer.sh
+2 -2
@@ -314,7 +314,7 @@ do
314
315 count=$((count + 1))
316
317 - pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null)
317 + pid=$(cat /var/run/netdata/netdata.pid 2>/dev/null || cat /var/run/netdata.pid 2>/dev/null)
318 isnetdata $pid || pid=
319 if [ ! -z "${pid}" ]
320 then
@@ -334,7 +334,7 @@ echo >&2
334 # run netdata
335
336 echo >&2 "Starting netdata..."
337 -run ${NETDATA_PREFIX}/usr/sbin/netdata "${@}"
337 +run ${NETDATA_PREFIX}/usr/sbin/netdata -pidfile /var/run/netdata.pid "${@}"
338
339 if [ $? -ne 0 ]
340 then
src/daemon.c
+32 -27
@@ -24,6 +24,9 @@
24 #include "main.h"
25 #include "daemon.h"
26
27 +char pidfile[FILENAME_MAX + 1] = "";
28 +int pidfd = -1;
29 +
30 void sig_handler(int signo)
31 {
32 switch(signo) {
@@ -71,23 +74,6 @@ void sig_handler(int signo)
74 }
75 }
76
74 -char rundir[FILENAME_MAX + 1] = RUN_DIR;
75 -char pidfile[FILENAME_MAX + 1] = "";
76 -void prepare_rundir() {
77 - if(getuid() != 0) {
78 - mkdir("/run/user", 0775);
79 - snprintf(rundir, FILENAME_MAX, "/run/user/%d", getuid());
80 - mkdir(rundir, 0775);
81 - snprintf(rundir, FILENAME_MAX, "/run/user/%d/netdata", getuid());
82 - }
83 -
84 - snprintf(pidfile, FILENAME_MAX, "%s/netdata.pid", rundir);
85 -
86 - if(mkdir(rundir, 0775) != 0) {
87 - if(errno != EEXIST) error("Cannot create directory '%s'.", rundir);
88 - }
89 -}
90 -
77 int become_user(const char *username)
78 {
79 struct passwd *pw = getpwnam(username);
@@ -96,9 +82,21 @@ int become_user(const char *username)
82 return -1;
83 }
84
99 - if(chown(rundir, pw->pw_uid, pw->pw_gid) != 0) {
100 - error("Cannot chown directory '%s' to user %s.", rundir, username);
101 - return -1;
85 + if(pidfile[0] && getuid() != pw->pw_uid) {
86 + // we are dropping privileges
87 + if(chown(pidfile, pw->pw_uid, pw->pw_gid) != 0)
88 + error("Cannot chown pidfile '%s' to user '%s'", pidfile, username);
89 +
90 + else if(pidfd != -1) {
91 + // not need to keep it open
92 + close(pidfd);
93 + pidfd = -1;
94 + }
95 + }
96 + else if(pidfd != -1) {
97 + // not need to keep it open
98 + close(pidfd);
99 + pidfd = -1;
100 }
101
102 if(setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
@@ -293,15 +291,20 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
291 close(dev_null);
292
293 // generate our pid file
296 - {
297 - unlink(pidfile);
298 - int fd = open(pidfile, O_RDWR | O_CREAT, 0666);
299 - if(fd >= 0) {
294 + if(pidfile[0]) {
295 + pidfd = open(pidfile, O_RDWR | O_CREAT, 0644);
296 + if(pidfd >= 0) {
297 + if(ftruncate(pidfd, 0) != 0)
298 + error("Cannot truncate pidfile '%s'.", pidfile);
299 +
300 char b[100];
301 sprintf(b, "%d\n", getpid());
302 - ssize_t i = write(fd, b, strlen(b));
303 - if(i <= 0) perror("Cannot write pid to file.");
304 - close(fd);
302 + ssize_t i = write(pidfd, b, strlen(b));
303 + if(i <= 0)
304 + error("Cannot write pidfile '%s'.", pidfile);
305 +
306 + // don't close it, we might need it at exit
307 + // close(pidfd);
308 }
309 }
310
@@ -311,6 +314,8 @@ int become_daemon(int dont_fork, int close_all_files, const char *user, const ch
314 }
315 else info("Successfully became user '%s'.", user);
316 }
317 + else if(pidfd != -1)
318 + close(pidfd);
319
320 return(0);
321 }
src/daemon.h
+3 -2
@@ -3,12 +3,13 @@
3
4 extern void sig_handler(int signo);
5
6 -extern void prepare_rundir();
7 -
6 extern int become_user(const char *username);
7
8 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);
9
10 extern void netdata_cleanup_and_exit(int i);
11
12 +extern char pidfile[];
13 +extern int pidfd;
14 +
15 #endif /* NETDATA_DAEMON_H */
src/main.c
+20 -2
@@ -44,7 +44,20 @@ void netdata_cleanup_and_exit(int ret)
44 netdata_exit = 1;
45 rrdset_save_all();
46 // kill_childs();
47 - unlink(RUN_DIR "/netdata.pid");
47 +
48 + if(pidfd != -1) {
49 + if(ftruncate(pidfd, 0) != 0)
50 + error("Cannot truncate pidfile '%s'.", pidfile);
51 +
52 + close(pidfd);
53 + pidfd = -1;
54 + }
55 +
56 + if(pidfile[0]) {
57 + if(unlink(pidfile) != 0)
58 + error("Cannot unlink pidfile '%s'.", pidfile);
59 + }
60 +
61 info("NetData exiting. Bye bye...");
62 exit(ret);
63 }
@@ -220,6 +233,11 @@ int main(int argc, char **argv)
233 else if(strcmp(argv[i], "-ch") == 0 && (i+1) < argc) { config_set("global", "host access prefix", argv[i+1]); i++; }
234 else if(strcmp(argv[i], "-stacksize") == 0 && (i+1) < argc) { config_set("global", "pthread stack size", argv[i+1]); i++; }
235 else if(strcmp(argv[i], "-nodaemon") == 0 || strcmp(argv[i], "-nd") == 0) dont_fork = 1;
236 + else if(strcmp(argv[i], "-pidfile") == 0 && (i+1) < argc) {
237 + i++;
238 + strncpy(pidfile, argv[i], FILENAME_MAX);
239 + pidfile[FILENAME_MAX] = '\0';
240 + }
241 else if(strcmp(argv[i], "--unittest") == 0) {
242 rrd_update_every = 1;
243 if(run_all_mockup_tests()) exit(1);
@@ -239,6 +257,7 @@ int main(int argc, char **argv)
257 fprintf(stderr, " -nd or -nodeamon to disable forking in the background. Default: unset.\n");
258 fprintf(stderr, " -df FLAGS debug options. Default: 0x%08llx.\n", debug_flags);
259 fprintf(stderr, " -stacksize BYTES to overwrite the pthread stack size.\n");
260 + fprintf(stderr, " -pidfile FILENAME to save a pid while running.\n");
261 exit(1);
262 }
263 }
@@ -401,7 +420,6 @@ int main(int argc, char **argv)
420
421 // --------------------------------------------------------------------
422
404 - prepare_rundir();
423 user = config_get("global", "run as user" , (getuid() == 0)?NETDATA_USER:"");
424 web_files_uid();
425
system/netdata-openrc.in
+1 -1
@@ -25,7 +25,7 @@ extra_started_commands="getconf"
25 pidfile="/run/netdata/netdata.pid"
26 command="${NETDATA_INSTALL_PATH}/usr/sbin/netdata"
27 command_background="yes"
28 -command_args="${NETDATA_EXTRA_ARGS}"
28 +command_args="-pidfile ${pidfile} ${NETDATA_EXTRA_ARGS}"
29 start_stop_daemon_args="-u ${NETDATA_OWNER}"
30
31 depend() {
system/netdata.service.in
+1 -1
@@ -8,7 +8,7 @@ WorkingDirectory=/tmp
8 User=root
9 Group=root
10 PIDFile=@localstatedir_POST@/run/netdata/netdata.pid
11 -ExecStart=@sbindir_POST@/netdata
11 +ExecStart=@sbindir_POST@/netdata -pidfile $PIDFile
12 ExecStop=/bin/kill -SIGTERM $MAINPID
13 TimeoutStopSec=30
14