168 lines
3.8 KiB
C
Executable File
168 lines
3.8 KiB
C
Executable File
#include <errno.h>
|
||
#include <signal.h>
|
||
|
||
#include <event2/event-config.h>
|
||
#include <event2/event.h>
|
||
|
||
#include "log_common.h"
|
||
#include "ulog_api.h"
|
||
|
||
#define SEV_EVENT_TIMEOUT 1
|
||
#define SEV_CMD "systemctl restart rsyslog"
|
||
|
||
static struct event_base* g_ev_base = NULL;
|
||
static struct event *g_ev_exit = NULL;
|
||
static struct event *g_ev_sev = NULL;
|
||
|
||
static void signal_exit(evutil_socket_t fd, short event, void *arg)
|
||
{
|
||
struct event *signal = arg;
|
||
|
||
ULOG_DEBUG(g_log, "%s: got signal %d\n", __FUNCTION__, event_get_signal(signal));
|
||
|
||
event_del(signal);
|
||
}
|
||
|
||
static void sev_restart(evutil_socket_t fd, short event, void *arg)
|
||
{
|
||
struct event *signal = arg;
|
||
|
||
ULOG_DEBUG(g_log, "%s: got signal %d\n", __FUNCTION__, event_get_signal(signal));
|
||
|
||
FILE *pp = popen(SEV_CMD, "r");
|
||
if (pp == NULL) {
|
||
ULOG_ERR(g_log, "Open cmd:%s is failure:%s", SEV_CMD, strerror(errno));
|
||
goto END;
|
||
}
|
||
|
||
char buff[256];
|
||
while(fread(buff, 1, sizeof(buff), pp) > 0)
|
||
{
|
||
ULOG_ERR(g_log, "Executing cmd:%s is failure, response content is %s", SEV_CMD, buff);
|
||
break;
|
||
}
|
||
|
||
END:
|
||
if (pp != NULL) {
|
||
pclose(pp);
|
||
}
|
||
event_del(signal);
|
||
}
|
||
|
||
int called = 0;
|
||
|
||
static void
|
||
signal_cb(evutil_socket_t fd, short event, void *arg)
|
||
{
|
||
struct event *signal = arg;
|
||
|
||
printf("signal_cb: got signal %d\n", event_get_signal(signal));
|
||
|
||
if (called >= 2)
|
||
event_del(signal);
|
||
|
||
called++;
|
||
}
|
||
|
||
|
||
int log_sev_init()
|
||
{
|
||
ULOG_INFO(g_log, "Log service is initiating");
|
||
g_ev_base = event_base_new();
|
||
if (g_ev_base == NULL) {
|
||
ULOG_ERR(g_log, "Allacting event base is failure");
|
||
goto FAIL;
|
||
}
|
||
|
||
g_ev_exit = evsignal_new(g_ev_base, SIGINT, signal_exit, event_self_cbarg());
|
||
if (g_ev_exit == NULL) {
|
||
ULOG_ERR(g_log, "Allacting singal of event exit is failure");
|
||
goto FAIL;
|
||
}
|
||
|
||
/*
|
||
g_ev_sev = evsignal_new(g_ev_base, -1, sev_restart, event_self_cbarg());
|
||
if (g_ev_sev == NULL) {
|
||
ULOG_ERR(g_log, "Allacting singal of event service is failure");
|
||
goto FAIL;
|
||
}
|
||
*/
|
||
|
||
//struct timeval tv = {SEV_EVENT_TIMEOUT, 0};
|
||
if (event_add(g_ev_exit, NULL) != 0) {
|
||
ULOG_ERR(g_log, "Adding event exit is failure");
|
||
goto FAIL;
|
||
}
|
||
|
||
ULOG_INFO(g_log, "Log service which is initited is success");
|
||
return 0;
|
||
FAIL:
|
||
log_sev_free();
|
||
ULOG_ERR(g_log, "Log service which is initited is failure");
|
||
return -1;
|
||
}
|
||
|
||
int log_sev_wait()
|
||
{
|
||
int ret = event_base_dispatch(g_ev_base);
|
||
if (ret == -1) {
|
||
ULOG_ERR(g_log, "service waiting is failure");
|
||
} else if (ret == 1) {
|
||
ULOG_INFO(g_log, "No events were registered");
|
||
}
|
||
|
||
return ret;
|
||
}
|
||
|
||
int log_sev_exit()
|
||
{
|
||
if (g_ev_exit == NULL) {
|
||
ULOG_WARNING(g_log, "Event exit was initiated");
|
||
return -1;
|
||
}
|
||
|
||
event_active(g_ev_exit, 0, 0);
|
||
|
||
return 0;
|
||
}
|
||
|
||
int log_sev_free()
|
||
{
|
||
if (g_ev_sev != NULL) {
|
||
event_free(g_ev_sev);
|
||
g_ev_sev = NULL;
|
||
}
|
||
|
||
if (g_ev_exit != NULL) {
|
||
event_free(g_ev_exit);
|
||
g_ev_exit = NULL;
|
||
}
|
||
if (g_ev_base != NULL) {
|
||
event_base_free(g_ev_base);
|
||
g_ev_base = NULL;
|
||
}
|
||
}
|
||
|
||
int log_sev_restart()
|
||
{
|
||
if (g_ev_sev == NULL) {
|
||
ULOG_WARNING(g_log, "Event service was initiated");
|
||
return -1;
|
||
}
|
||
|
||
// 如果pending,就不需要添加事件
|
||
if (event_pending(g_ev_sev, 0, NULL)) {
|
||
ULOG_DEBUG(g_log, "Not need set event service");
|
||
return 0;
|
||
}
|
||
|
||
struct timeval tv = {SEV_EVENT_TIMEOUT, 0};
|
||
if (event_add(g_ev_sev, NULL) != 0) {
|
||
ULOG_ERR(g_log, "Setting event service is failure");
|
||
return -1;
|
||
}
|
||
ULOG_DEBUG(g_log, "Setting event service is success");
|
||
|
||
return 0;
|
||
}
|