From: Liu Yuan <tailai.ly at taobao.com> v2: - Apply this to sheep binary too -------------------------------------- >8 As a side effect, the coredump file will be located /path/to/store dir instead of previous '/'. We also don't overlap core file when multiple sheeps run on the same machine. Signed-off-by: Liu Yuan <tailai.ly at taobao.com> --- lib/logger.c | 8 ++++++-- sheep/sheep.c | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/logger.c b/lib/logger.c index 5bce85c..04ba933 100644 --- a/lib/logger.c +++ b/lib/logger.c @@ -29,6 +29,7 @@ #include <sys/wait.h> #include <sys/prctl.h> #include <pthread.h> +#include <libgen.h> #include "logger.h" #include "util.h" @@ -404,12 +405,15 @@ notrace int log_init(char *program_name, int size, int to_stdout, int level, { off_t offset; size_t log_size; + char log_dir[PATH_MAX]; log_level = level; logdbg(stderr, "entering log_init\n"); log_name = program_name; log_nowname = outfile; + strcpy(log_dir, outfile); + strcpy(log_dir, dirname(log_dir)); semkey = random(); @@ -465,8 +469,8 @@ notrace int log_init(char *program_name, int size, int to_stdout, int level, dup2(fd, 1); dup2(fd, 2); setsid(); - if (chdir("/") < 0) { - syslog(LOG_ERR, "failed to chdir to /: %m\n"); + if (chdir(log_dir) < 0) { + syslog(LOG_ERR, "failed to chdir to %s: %m\n", log_dir); exit(1); } diff --git a/sheep/sheep.c b/sheep/sheep.c index 5a67289..491f49f 100644 --- a/sheep/sheep.c +++ b/sheep/sheep.c @@ -279,6 +279,11 @@ int main(int argc, char **argv) if (ret) exit(1); + if (chdir(dir) < 0) { + fprintf(stderr, "failed to chdir to %s: %m\n", dir); + exit(1); + } + vprintf(SDOG_NOTICE, "sheepdog daemon (version %s) started\n", PACKAGE_VERSION); while (!sys_stat_shutdown() || sys->nr_outstanding_reqs != 0) -- 1.7.10.2 |