2019-07-26 03:53:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2019-07-31 09:32:43 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <semaphore.h>
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
#include "rpc_server.h"
|
|
|
|
#include "ulog_api.h"
|
|
|
|
#include "log_file.h"
|
|
|
|
#include "log_console.h"
|
2019-07-31 09:32:43 +00:00
|
|
|
#include "log_pty.h"
|
2019-07-29 10:20:17 +00:00
|
|
|
#include "log_remote.h"
|
2019-07-26 03:53:33 +00:00
|
|
|
#include "rpc_module.h"
|
|
|
|
|
|
|
|
|
2019-07-29 10:20:17 +00:00
|
|
|
#define LOG_SCHED_MODULE_NAME "log-sched"
|
|
|
|
|
|
|
|
#define SERVICE_LOG_FILE_NAME "log-file"
|
|
|
|
#define SERIVCE_LOG_CONSOLE_NAME "log-console"
|
2019-07-31 09:32:43 +00:00
|
|
|
#define SERVICE_LOG_PTY_NAME "log-pty"
|
|
|
|
#define SERVICE_LOG_REMOTE_NAME "log-remote"
|
2019-07-26 03:53:33 +00:00
|
|
|
|
2019-08-01 08:44:18 +00:00
|
|
|
#define DEFAULT_CONFIG_FILE "/etc/log-sched.conf"
|
|
|
|
|
2019-07-26 03:53:33 +00:00
|
|
|
ulog_t *g_log = NULL;
|
2019-08-01 08:44:18 +00:00
|
|
|
FILE *g_conf_fp;
|
|
|
|
char g_conf_file[MAX_PATH_SZ];
|
2019-07-31 09:32:43 +00:00
|
|
|
|
2019-08-01 08:44:18 +00:00
|
|
|
static sem_t g_sem;
|
2019-07-31 09:32:43 +00:00
|
|
|
|
|
|
|
void signal_exit()
|
|
|
|
{
|
|
|
|
if (sem_post(&g_sem) == -1) {
|
|
|
|
ULOG_ERR(g_log, "Posting semaphore is failure:%s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
ULOG_DEBUG(g_log, "Receive shutdown signal");
|
|
|
|
}
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int run_daemon = 0;
|
|
|
|
char *options = "d";
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, options)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
|
|
|
run_daemon = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon);
|
|
|
|
if (g_log == NULL) {
|
|
|
|
fprintf(stderr, "Initiating ulog is failure");
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(g_conf_file, DEFAULT_CONFIG_FILE);
|
|
|
|
g_conf_fp = fopen(g_conf_file, "a+");
|
|
|
|
if (g_conf_fp == NULL) {
|
|
|
|
ULOG_ERR(g_log, "Opening configure:%s is failure:%s", g_conf_file, strerror(errno));
|
|
|
|
goto END;
|
2019-07-26 03:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (run_daemon) {
|
|
|
|
if (daemon(0, 0) == -1) {
|
|
|
|
ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno));
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpc_server *server = rpc_server_create_ex(RPC_MODULE_SYSLOG_NAME);
|
|
|
|
if (server == NULL)
|
|
|
|
{
|
|
|
|
ULOG_ERR(g_log, "start server error");
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
2019-07-31 09:32:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sem_init(&g_sem, 0, 0) != 0) {
|
|
|
|
ULOG_ERR(g_log, "Init sem is failure:%s", strerror(errno));
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
2019-07-31 09:32:43 +00:00
|
|
|
}
|
|
|
|
signal(SIGINT, signal_exit);
|
2019-07-26 03:53:33 +00:00
|
|
|
ULOG_INFO(g_log, "Server of log schedule is started");
|
|
|
|
|
|
|
|
/* 注册配置处理函数 */
|
|
|
|
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file);
|
2019-07-29 10:20:17 +00:00
|
|
|
rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, "conf_log_console", rpc_conf_log_console);
|
2019-07-31 09:32:43 +00:00
|
|
|
rpc_server_regservice(server, SERVICE_LOG_PTY_NAME, "conf_log_pty", rpc_conf_log_pty);
|
2019-08-01 08:44:18 +00:00
|
|
|
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_add_remote", rpc_conf_log_add_remote);
|
|
|
|
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_del_remote", rpc_conf_log_del_remote);
|
|
|
|
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_remote_level", rpc_conf_log_remote_level);
|
2019-07-31 09:32:43 +00:00
|
|
|
/*
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
log_file_t log = {0};
|
|
|
|
log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS;
|
|
|
|
log.del_over_days = 10;
|
|
|
|
log.level = LOG_DEBUG;
|
|
|
|
rpc_conf_log_file(NULL, &log, sizeof(log), NULL);
|
|
|
|
|
|
|
|
log_console_t console = {0};
|
|
|
|
strcpy(console.module_name, "111");
|
|
|
|
console.level = LOG_DEBUG;
|
|
|
|
console.on = 1;
|
|
|
|
rpc_conf_log_console(NULL, &console, sizeof(console), NULL);
|
2019-07-26 10:22:43 +00:00
|
|
|
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
log_pty_t tty = {0};
|
2019-07-29 10:20:17 +00:00
|
|
|
//strcpy(tty.module_name, "111");
|
2019-07-26 10:22:43 +00:00
|
|
|
tty.level = LOG_DEBUG;
|
|
|
|
tty.on = 1;
|
2019-07-31 09:32:43 +00:00
|
|
|
rpc_conf_log_pty(NULL, &tty, sizeof(tty), NULL);
|
2019-07-29 10:20:17 +00:00
|
|
|
|
|
|
|
log_remote_host_t remote = {0};
|
2019-07-31 09:32:43 +00:00
|
|
|
strcpy(remote.host, "192.168.3.11");
|
2019-07-30 10:22:02 +00:00
|
|
|
remote.rfc = LOG_RFC_5424;
|
2019-07-29 10:20:17 +00:00
|
|
|
remote.port = 514;
|
2019-08-01 08:44:18 +00:00
|
|
|
rpc_conf_log_add_remote(NULL, &remote, sizeof(remote), NULL);
|
|
|
|
|
|
|
|
log_remote_level_t level;
|
|
|
|
level.level = 5;
|
|
|
|
rpc_conf_log_remote_level(NULL, &level, sizeof(level), NULL);
|
2019-07-31 09:32:43 +00:00
|
|
|
*/
|
2019-08-01 08:44:18 +00:00
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
if (sem_wait(&g_sem) == -1) {
|
|
|
|
ULOG_ERR(g_log, "Waitting semaphore is failure:%s", strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ULOG_INFO(g_log, "%s is shutdown", LOG_SCHED_MODULE_NAME);
|
2019-07-26 03:53:33 +00:00
|
|
|
END:
|
2019-08-01 08:44:18 +00:00
|
|
|
if (g_conf_fp != NULL) {
|
|
|
|
fclose(g_conf_fp);
|
|
|
|
}
|
|
|
|
|
2019-07-26 03:53:33 +00:00
|
|
|
ulog_close(g_log);
|
|
|
|
return 0;
|
|
|
|
}
|