From 9f333e591cd4c39508ec55cd06b238791694709a Mon Sep 17 00:00:00 2001 From: findstr Date: Sun, 24 Sep 2017 11:46:55 +0800 Subject: [PATCH] support logrotate when run as daemon, stdout,stderr will be redirected to argv[0].log file when receive SIGUSR1 signal, it will reopen argv[0].log file for logrotate --- lualib-src/lualib-silly.c | 12 ++++----- silly-src/silly_daemon.c | 57 ++++++++++++++++++++++++--------------- silly-src/silly_daemon.h | 5 ++-- silly-src/silly_log.c | 14 ++++++++-- silly-src/silly_log.h | 3 ++- silly-src/silly_run.c | 11 ++++++++ 6 files changed, 69 insertions(+), 33 deletions(-) diff --git a/lualib-src/lualib-silly.c b/lualib-src/lualib-silly.c index fe185dd6..19af9ccf 100644 --- a/lualib-src/lualib-silly.c +++ b/lualib-src/lualib-silly.c @@ -140,27 +140,27 @@ llog(lua_State *L) int type = lua_type(L, i); switch (type) { case LUA_TSTRING: - silly_lograw("%s ", lua_tostring(L, i)); + silly_log_raw("%s ", lua_tostring(L, i)); break; case LUA_TNUMBER: - silly_lograw("%d ", (int)lua_tointeger(L, i)); + silly_log_raw("%d ", (int)lua_tointeger(L, i)); break; case LUA_TBOOLEAN: - silly_lograw("%s ", + silly_log_raw("%s ", lua_toboolean(L, i) ? "true" : "false"); break; case LUA_TTABLE: - silly_lograw("table: %p ", lua_topointer(L, i)); + silly_log_raw("table: %p ", lua_topointer(L, i)); break; case LUA_TNIL: - silly_lograw("#%d.null ", i); + silly_log_raw("#%d.null ", i); break; default: return luaL_error(L, "log unspport param#%d type:%s", i, lua_typename(L, type)); } } - silly_lograw("\n"); + silly_log_raw("\n"); return 0; } diff --git a/silly-src/silly_daemon.c b/silly-src/silly_daemon.c index 63dc5b78..4c30f05a 100644 --- a/silly-src/silly_daemon.c +++ b/silly-src/silly_daemon.c @@ -14,10 +14,10 @@ static int pidfile; extern int daemon(int, int); static void -pidfile_create(const struct silly_config *config) +pidfile_create(const struct silly_config *conf) { int err; - const char *path = config->pidfile; + const char *path = conf->pidfile; pidfile = -1; if (path[0] == '\0') return ; @@ -55,33 +55,22 @@ pidfile_write() } static inline void -pidfile_delete(const struct silly_config *config) +pidfile_delete(const struct silly_config *conf) { if (pidfile == -1) return ; close(pidfile); - unlink(config->pidfile); + unlink(conf->pidfile); return ; } -void -silly_daemon_start(const struct silly_config *config) +static inline void +logfileopen(const struct silly_config *conf) { int fd; - int err; char path[128]; - if (!config->daemon) - return ; - pidfile_create(config); - err = daemon(1, 0); - if (err < 0) { - pidfile_delete(config); - silly_log("[daemon] %s\n", strerror(errno)); - exit(0); - } - pidfile_write(); - snprintf(path, 128, "%s%s-%d.log", config->logpath, config->selfname, getpid()); - fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 00666); + snprintf(path, 128, "%s%s.log", conf->logpath, conf->selfname); + fd = open(path, O_CREAT | O_WRONLY | O_APPEND, 00666); if (fd >= 0) { dup2(fd, 1); dup2(fd, 2); @@ -89,14 +78,38 @@ silly_daemon_start(const struct silly_config *config) setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); } +} + +void +silly_daemon_start(const struct silly_config *conf) +{ + int err; + if (!conf->daemon) + return ; + pidfile_create(conf); + err = daemon(1, 0); + if (err < 0) { + pidfile_delete(conf); + silly_log("[daemon] %s\n", strerror(errno)); + exit(0); + } + pidfile_write(); + logfileopen(conf); + return ; +} + +void +silly_daemon_sigusr1(const struct silly_config *conf) +{ + logfileopen(conf); return ; } void -silly_daemon_stop(const struct silly_config *config) +silly_daemon_stop(const struct silly_config *conf) { - if (!config->daemon) + if (!conf->daemon) return ; - pidfile_delete(config); + pidfile_delete(conf); return ; } diff --git a/silly-src/silly_daemon.h b/silly-src/silly_daemon.h index b4a333f0..69c157fb 100644 --- a/silly-src/silly_daemon.h +++ b/silly-src/silly_daemon.h @@ -1,8 +1,9 @@ #ifndef _SILLY_DAEMON_H #define _SILLY_DAEMON_H -void silly_daemon_start(const struct silly_config *config); -void silly_daemon_stop(const struct silly_config *config); +void silly_daemon_start(const struct silly_config *conf); +void silly_daemon_sigusr1(const struct silly_config *conf); +void silly_daemon_stop(const struct silly_config *conf); #endif diff --git a/silly-src/silly_log.c b/silly-src/silly_log.c index 74227f85..a57b93a1 100644 --- a/silly-src/silly_log.c +++ b/silly-src/silly_log.c @@ -1,12 +1,22 @@ #include #include #include +#include #include #include "silly.h" #include "silly_log.h" +static pid_t pid; + +void +silly_log_start() +{ + pid = getpid(); + return ; +} + void -silly_lograw(const char *fmt, ...) +silly_log_raw(const char *fmt, ...) { va_list ap; char buffer[LOG_MAX_LEN]; @@ -32,7 +42,7 @@ silly_log(const char *fmt, ...) nr = strftime(head, sizeof(head), "%b %d %H:%M:%S.", localtime(&tv.tv_sec)); snprintf(head + nr, sizeof(head) - nr, "%03d", (int)tv.tv_usec/1000); - fprintf(stdout, "%s %s", head, buffer); + fprintf(stdout, "%d %s %s", pid, head, buffer); return ; } diff --git a/silly-src/silly_log.h b/silly-src/silly_log.h index a87902dc..20bfaf6c 100644 --- a/silly-src/silly_log.h +++ b/silly-src/silly_log.h @@ -3,7 +3,8 @@ #define LOG_MAX_LEN (1024) -void silly_lograw(const char *fmt, ...); +void silly_log_start(); +void silly_log_raw(const char *fmt, ...); void silly_log(const char *fmt, ...); #endif diff --git a/silly-src/silly_run.c b/silly-src/silly_run.c index 1c16df21..c952c24f 100644 --- a/silly-src/silly_run.c +++ b/silly-src/silly_run.c @@ -22,6 +22,7 @@ struct { int exit; int run; + const struct silly_config *conf; pthread_mutex_t mutex; pthread_cond_t cond; } R; @@ -109,12 +110,20 @@ signal_term(int sig) R.exit = 1; } +static void +signal_usr1(int sig) +{ + (void)sig; + silly_daemon_sigusr1(R.conf); +} + static int signal_init() { signal(SIGPIPE, SIG_IGN); signal(SIGTERM, signal_term); signal(SIGINT, signal_term); + signal(SIGUSR1, signal_usr1); return 0; } @@ -126,9 +135,11 @@ silly_run(const struct silly_config *config) pthread_t pid[3]; R.run = 1; R.exit = 0; + R.conf = config; pthread_mutex_init(&R.mutex, NULL); pthread_cond_init(&R.cond, NULL); silly_daemon_start(config); + silly_log_start(); signal_init(); silly_timer_init(); err = silly_socket_init();