PID file is created for logrotate

This commit is contained in:
Sukchan Lee 2017-10-24 15:55:28 +09:00
parent 6eb05495aa
commit 6f5ec19388
15 changed files with 118 additions and 3 deletions

View File

@ -56,6 +56,7 @@ dist_freeDiameter_DATA = \
install-data-hook:
$(MKDIR_P) $(DESTDIR)/$(localstatedir)/log
$(MKDIR_P) $(DESTDIR)/$(localstatedir)/run
CLEANFILES = symtbl.c
DISTCLEANFILES = $(DIST_ARCHIVES)

View File

@ -153,6 +153,32 @@ AC_SUBST(uint64_t_hex_fmt)
AC_SUBST(int64_literal)
AC_SUBST(uint64_literal)
AC_CHECK_SIZEOF(pid_t, 8)
if test "$ac_cv_sizeof_pid_t" = "$ac_cv_sizeof_short"; then
pid_t_fmt='#define C_PID_T_FMT "hd"'
elif test "$ac_cv_sizeof_pid_t" = "$ac_cv_sizeof_int"; then
pid_t_fmt='#define C_PID_T_FMT "d"'
elif test "$ac_cv_sizeof_pid_t" = "$ac_cv_sizeof_long"; then
pid_t_fmt='#define C_PID_T_FMT "ld"'
elif test "$ac_cv_sizeof_pid_t" = "$ac_cv_sizeof_long_long"; then
pid_t_fmt='#define C_PID_T_FMT APR_INT64_T_FMT'
else
pid_t_fmt='#error Can not determine the proper size for pid_t'
fi
case $host in
*-solaris*)
if test "$ac_cv_sizeof_long" = "8"; then
pid_t_fmt='#define C_PID_T_FMT "d"'
else
pid_t_fmt='#define C_PID_T_FMT "ld"'
fi
;;
esac
AC_SUBST(pid_t_fmt)
AC_DEFINE_UNQUOTED([PACKAGE_VERSION_MAJOR],
[`echo $PACKAGE_VERSION | $SED 's/^\([[^\.]]\+\)\.\([[^\.]]\+\)\.\([[^\.]]\+\).*/\1/'`],
[Major version of this package])

View File

@ -13,7 +13,7 @@ AM_CPPFLAGS = \
AM_CFLAGS = \
-Wall -Werror
DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
DEFS = @DEFS@ -DSYSCONF_DIR=\"$(sysconfdir)/\"
MAINTAINERCLEANFILES = Makefile.in
MOSTLYCLEANFILES = *.stackdump

View File

@ -6,7 +6,7 @@
#include "context.h"
#define DEFAULT_CONFIG_FILE_PATH SYSCONFDIR PACKAGE ".conf"
#define DEFAULT_CONFIG_FILE_PATH SYSCONF_DIR PACKAGE ".conf"
static context_t self;

View File

@ -211,6 +211,8 @@ typedef c_int32_t c_intptr_t;
@uint64_t_fmt@
@uint64_t_hex_fmt@
@pid_t_fmt@
#ifdef INT16_MIN
#define C_INT16_MIN INT16_MIN
#else

6
main.c
View File

@ -46,6 +46,11 @@ static int check_signal(int signum)
return 1;
}
case SIGHUP:
{
logger_restart();
break;
}
case SIGUSR1:
{
break;
@ -126,6 +131,7 @@ int main(int argc, char *argv[])
atexit(terminate);
core_initialize();
app_log_pid(app_name);
if (app_initialize(config_path, log_path) != CORE_OK)
{
d_fatal("NextEPC initialization failed. Aborted");

View File

@ -83,7 +83,7 @@ AM_CPPFLAGS = \
AM_CFLAGS = \
-Wall -Werror
DEFS = @DEFS@ -DSYSCONFDIR=\"$(sysconfdir)/\"
DEFS = @DEFS@ -DLOCALSTATE_DIR=\"$(localstatedir)/\"
MAINTAINERCLEANFILES = Makefile.in
MOSTLYCLEANFILES = core *.stackdump

View File

@ -8,6 +8,8 @@
extern "C" {
#endif /* __cplusplus */
extern const char *app_name;
CORE_DECLARE(status_t) app_initialize(char *config_path, char *log_path);
CORE_DECLARE(void) app_terminate(void);
@ -19,6 +21,9 @@ CORE_DECLARE(void) app_did_terminate(void);
CORE_DECLARE(status_t) config_initialize(char *config_path);
CORE_DECLARE(void) config_terminate(void);
CORE_DECLARE(status_t) logger_restart(void);
CORE_DECLARE(status_t) app_log_pid(const char *file);
CORE_DECLARE(status_t) mme_initialize();
CORE_DECLARE(void) mme_terminate(void);

View File

@ -19,6 +19,8 @@ static semaphore_id sgw_sem2 = 0;
static semaphore_id hss_sem1 = 0;
static semaphore_id hss_sem2 = 0;
const char *app_name = "epc";
status_t app_initialize(char *config_path, char *log_path)
{
pid_t pid;

View File

@ -7,6 +7,8 @@
#include "context.h"
#include "app.h"
const char *app_name = "hss";
status_t app_initialize(char *config_path, char *log_path)
{
status_t rv;

View File

@ -2,12 +2,15 @@
#include "core_debug.h"
#include "core_thread.h"
#include "core_file.h"
#include "context.h"
#include "logger.h"
#include "app.h"
#define DEFAULT_PID_DIR_PATH LOCALSTATE_DIR "run/"
static thread_id logger_thread = 0;
static void *THREAD_FUNC logger_main(thread_id id, void *data);
@ -86,3 +89,62 @@ static void *THREAD_FUNC logger_main(thread_id id, void *data)
return NULL;
}
status_t logger_restart()
{
status_t rv;
if (logger_thread)
{
thread_delete(logger_thread);
if (context_self()->log_path)
{
d_print(" Logging '%s'\n", context_self()->log_path);
rv = thread_create(&logger_thread, NULL,
logger_main, context_self()->log_path);
if (rv != CORE_OK) return rv;
}
}
return CORE_OK;
}
status_t app_log_pid(const char *name)
{
file_t *pid_file = NULL;
file_info_t finfo;
static pid_t saved_pid = -1;
pid_t mypid;
status_t rv;
char fname[MAX_FILEPATH_LEN];
char buf[128];
d_assert(name, return CORE_ERROR, );
snprintf(fname, sizeof(fname), "%snextepc-%sd.pid",
DEFAULT_PID_DIR_PATH, name);
mypid = getpid();
if (mypid != saved_pid
&& file_stat(&finfo, fname, FILE_INFO_MTIME) == CORE_OK)
{
#if 0 /* FIXME : we need to check pid file overwritten */
d_warn("pid file %s overwritten -- Unclean "
"shutdown of previous NextEPC run?", fname);
#endif
}
if ((rv = file_open(&pid_file, fname,
FILE_WRITE | FILE_CREATE | FILE_TRUNCATE,
FILE_UREAD | FILE_UWRITE | FILE_GREAD | FILE_WREAD)) != CORE_OK)
{
d_error("could not create %s", fname);
return CORE_ERROR;
}
snprintf(buf, sizeof(buf), "%" C_PID_T_FMT "\r\n", mypid);
file_puts(buf, pid_file);
file_close(pid_file);
saved_pid = mypid;
return CORE_OK;
}

View File

@ -7,6 +7,8 @@
#include "context.h"
#include "app.h"
const char *app_name = "mme";
status_t app_initialize(char *config_path, char *log_path)
{
status_t rv;
@ -42,3 +44,4 @@ void app_terminate(void)
app_did_terminate();
}

View File

@ -7,6 +7,8 @@
#include "context.h"
#include "app.h"
const char *app_name = "pcrf";
status_t app_initialize(char *config_path, char *log_path)
{
status_t rv;

View File

@ -7,6 +7,8 @@
#include "context.h"
#include "app.h"
const char *app_name = "pgw";
status_t app_initialize(char *config_path, char *log_path)
{
status_t rv;

View File

@ -7,6 +7,8 @@
#include "context.h"
#include "app.h"
const char *app_name = "sgw";
status_t app_initialize(char *config_path, char *log_path)
{
status_t rv;