From a5bffd2fe7b9e142e2defb16e3db61974fd0265e Mon Sep 17 00:00:00 2001 From: guo_peixu Date: Wed, 29 Jun 2022 18:24:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B6=88=E8=B4=B9=E8=80=85=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E4=BF=A1=E5=8F=B7=E5=A4=84=E7=90=86=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E5=9C=A8=E9=80=80=E5=87=BA=E6=97=B6=E5=90=91?= =?UTF-8?q?=E5=85=B1=E4=BA=AB=E5=86=85=E5=AD=98=E4=B8=AD=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A0=87=E8=AE=B0=EF=BC=8C=E7=94=9F=E4=BA=A7?= =?UTF-8?q?=E8=80=85=E9=80=9A=E8=BF=87=E6=AD=A4=E6=A0=87=E8=AE=B0=E7=A1=AE?= =?UTF-8?q?=E5=AE=9A=E6=B6=88=E8=B4=B9=E8=80=85=E6=98=AF=E5=90=A6=E5=9C=A8?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E3=80=82=E5=88=A0=E9=99=A4=E4=BF=A1=E5=8F=B7?= =?UTF-8?q?=E9=87=8F=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/MESA_shm_ring_queue.h | 30 ++++++++--------- shm_consumer/MESA_shm_consumer.c | 52 ++++++++++++++--------------- src/MESA_handle_logger.c | 17 +--------- src/MESA_shm_ring_queue.c | 56 ++++++++++---------------------- 4 files changed, 56 insertions(+), 99 deletions(-) diff --git a/inc/MESA_shm_ring_queue.h b/inc/MESA_shm_ring_queue.h index ff7c180..27b1e3e 100644 --- a/inc/MESA_shm_ring_queue.h +++ b/inc/MESA_shm_ring_queue.h @@ -18,22 +18,8 @@ #define MESA_SEM_KEY 35719 - -struct MESA_shm_overview *MESA_shm_alloc_overview(); -struct MESA_shm_queue_head *MESA_shm_get_ring_queue(); -void MESA_shm_init_mutex(); -void MESA_shm_init_overview(); -void MESA_shm_recycle_ring_queue(struct MESA_shm_queue_head *ring_queue_head); -int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head, char *log_file, int log_file_len); -int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head); -int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head); -void MESA_shm_ring_queue_set_empty(struct MESA_shm_queue_head *head); -int MESA_shm_alloc_sem(); -int MESA_shm_consumer_is_running(); - - - - +#define MESA_CONSUMER_RUNNING 1 +#define MESA_CONSUMER_NOT_RUNNING 0 struct MESA_shm_overview{ int shmkey; @@ -51,6 +37,18 @@ struct MESA_shm_queue_head{ int ovw_idx; }; + +int MESA_shm_alloc_overview(struct MESA_shm_overview **ovw, int **consumer_status); +struct MESA_shm_queue_head *MESA_shm_get_ring_queue(); +void MESA_shm_init_mutex(); +void MESA_shm_init_overview(); +void MESA_shm_recycle_ring_queue(struct MESA_shm_queue_head *ring_queue_head); +int MESA_shm_copy_buf_to_ring_queue(char *buf, int buflen, struct MESA_shm_queue_head *head, char *log_file, int log_file_len); +int MESA_shm_ring_queue_is_empty(struct MESA_shm_queue_head *head); +int MESA_shm_ring_queue_is_full(struct MESA_shm_queue_head *head); +void MESA_shm_ring_queue_set_empty(struct MESA_shm_queue_head *head); +int MESA_shm_get_consumer_status(); + #endif diff --git a/shm_consumer/MESA_shm_consumer.c b/shm_consumer/MESA_shm_consumer.c index c21fa49..d4105dd 100644 --- a/shm_consumer/MESA_shm_consumer.c +++ b/shm_consumer/MESA_shm_consumer.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -10,8 +9,8 @@ #include #include #include +#include #include "list.h" - #include "MESA_shm_ring_queue.h" #define DEFAUT_BUF_SIZE 256 @@ -44,7 +43,7 @@ struct log_file_list g_log_file_list; struct care_pid_list g_care_pid_list; int g_output_mode = CONSUMER_OUTPUT_MODE_FILE; int g_care_pid = CONSUMER_CARE_PID_ALL; -int g_semid = -1; +int *g_status = NULL; void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head) { @@ -513,31 +512,27 @@ error: pclose(fp); return -1; } -void MESA_shm_sem_p(int semid) +void signal_handler_exit(int signum) { - if(g_semid == -1){ - return ; + if(g_status == NULL){ + exit(0); } - struct sembuf sem_b; - sem_b.sem_num = 0; - sem_b.sem_op = -1; - sem_b.sem_flg = SEM_UNDO; - semop(semid, &sem_b, 1); + *g_status = MESA_CONSUMER_NOT_RUNNING; + exit(0); +} +void register_sginal_handler() +{ + signal(SIGKILL, signal_handler_exit); + signal(SIGTRAP, signal_handler_exit); + signal(SIGABRT, signal_handler_exit); + signal(SIGBUS, signal_handler_exit); + signal(SIGFPE, signal_handler_exit); + signal(SIGSEGV, signal_handler_exit); + signal(SIGTERM, signal_handler_exit); + signal(SIGINT, signal_handler_exit); + signal(SIGQUIT, signal_handler_exit); return ; } -void MESA_shm_sem_v(int semid) -{ - if(g_semid == -1){ - return ; - } - struct sembuf sem_b; - sem_b.sem_num = 0; - sem_b.sem_op = 1; - sem_b.sem_flg = SEM_UNDO; - semop(g_semid, &sem_b, 1); - return ; -} - void print_help(char *exe_name) { printf("-pid default all, optional parameter\n"); @@ -567,17 +562,18 @@ int main(int argc, char **argv) }else{ g_cur_tty_fd = get_cur_tty_fd(); } - g_semid = MESA_shm_alloc_sem(); - MESA_shm_sem_p(g_semid); int i = 0; + int ret = 0; struct MESA_shm_overview *shm_overview = NULL; struct MESA_shm_overview *tmp_ovw = NULL; struct MESA_shm_queue_head *ring_queue_head[MESA_SHM_RING_QUEUE_NUM] = {NULL}; INIT_LIST_HEAD(&g_log_file_list.list); - shm_overview = MESA_shm_alloc_overview(); - if(shm_overview == NULL){ + ret = MESA_shm_alloc_overview(&shm_overview, &g_status); + if(ret < 0){ return 0; } + *g_status = MESA_CONSUMER_RUNNING; + register_sginal_handler(); init_ring_queue_head_arrray(shm_overview, ring_queue_head); while(1){ for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){ diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index bf1f0c4..4c378cd 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -26,10 +26,6 @@ static char tmp_conf_filepath[MAX_HANDLE_LOG_PATH] = ""; pthread_key_t MESA_pthread_key; pthread_once_t MESA_pthread_key_once = PTHREAD_ONCE_INIT; -#define MESA_CONSUMER_RUNNING 1 -#define MESA_CONSUMER_NOT_RUNNING 0 - -int MESA_consumer_status = MESA_CONSUMER_NOT_RUNNING; struct MESA_pthread_private{ char *fmt_buf; @@ -265,21 +261,10 @@ void MESA_free_pthread_private(void *arg) } return ; } -void *MESA_check_consumer_status(void *arg) -{ - while(1){ - MESA_consumer_status = MESA_shm_consumer_is_running(); - sleep(1); - }; - return NULL; -} void MESA_alloc_pthread_key() { pthread_key_create(&MESA_pthread_key, MESA_free_pthread_private); MESA_shm_init_overview(); - pthread_t tid; - pthread_create(&tid, NULL, MESA_check_consumer_status, NULL); - pthread_detach(tid); return ; } @@ -344,7 +329,7 @@ void MESA_destroy_runtime_log_handle(void *handle) void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...) { - if(MESA_consumer_status == MESA_CONSUMER_NOT_RUNNING){ + if(MESA_shm_get_consumer_status() == MESA_CONSUMER_NOT_RUNNING){ return ; } log_handle_t *p_handle = (log_handle_t *)handle; diff --git a/src/MESA_shm_ring_queue.c b/src/MESA_shm_ring_queue.c index 8e85916..ce02b17 100644 --- a/src/MESA_shm_ring_queue.c +++ b/src/MESA_shm_ring_queue.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -14,14 +13,7 @@ struct MESA_shm_overview *MESA_shm_ovw = NULL; -int MESA_shm_semid = -1; -union semun { - int val; - struct semid_ds *buf; - unsigned short *array; - struct seminfo *__buf; -}; - +int *MESA_consumer_status = NULL; void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw) { @@ -33,34 +25,17 @@ void MESA_shm_init_ring_queue_mutex(struct MESA_shm_overview *ovw) return ; } -int MESA_shm_alloc_sem() +int MESA_shm_get_consumer_status() { - union semun semopt; - int semid = semget(MESA_SEM_KEY, 0 , 0); - if(semid == -1){ - semid = semget(MESA_SEM_KEY, 1 ,IPC_CREAT | 0666); - if(semid == -1){ - return -1; - } - semopt.val = 1; - semctl(semid, 0, SETVAL, semopt); + if(MESA_consumer_status == NULL){ + return MESA_CONSUMER_NOT_RUNNING; } - return semid; + return *MESA_consumer_status; } -int MESA_shm_consumer_is_running() +int MESA_shm_alloc_overview(struct MESA_shm_overview **ovw, int **consumer_status) { - if(MESA_shm_semid == -1){ - return 0; - } - int semval = semctl(MESA_shm_semid, 0 ,GETVAL, 0); - return !semval ; -} - - -struct MESA_shm_overview *MESA_shm_alloc_overview() -{ - int shmsize = sizeof(struct MESA_shm_overview) * MESA_SHM_RING_QUEUE_NUM; + int shmsize = (sizeof(struct MESA_shm_overview) * MESA_SHM_RING_QUEUE_NUM) + sizeof(int); struct MESA_shm_overview *shm_overview = NULL; struct MESA_shm_overview *tmp_ovw = NULL; int i = 0; @@ -68,11 +43,11 @@ struct MESA_shm_overview *MESA_shm_alloc_overview() if(shmid == -1){ shmid = shmget(MESA_SHM_KEY_OVERVIEW, shmsize, (SHM_R|SHM_W|IPC_CREAT)); if(shmid == -1){ - return NULL; + return -1; }else{ shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0); if(shm_overview == (struct MESA_shm_overview *)-1){ - return NULL; + return -1; } memset((void *)shm_overview, 0, shmsize); for(i = 0; i < MESA_SHM_RING_QUEUE_NUM; i++){ @@ -82,21 +57,24 @@ struct MESA_shm_overview *MESA_shm_alloc_overview() tmp_ovw->idx = i; MESA_shm_init_ring_queue_mutex(tmp_ovw); } + *ovw = shm_overview; + *consumer_status = (int *)(shm_overview + MESA_SHM_RING_QUEUE_NUM); } }else{ shm_overview = (struct MESA_shm_overview *)shmat(shmid, NULL, 0); if(shm_overview == (struct MESA_shm_overview *)-1){ - return NULL; + return -1; } + *ovw = shm_overview; + *consumer_status = (int *)(shm_overview + MESA_SHM_RING_QUEUE_NUM); } - return shm_overview; + return 0; } void MESA_shm_init_overview() { - if(MESA_shm_ovw == NULL){ - MESA_shm_ovw = MESA_shm_alloc_overview(); + if(MESA_shm_ovw == NULL || MESA_consumer_status == NULL){ + MESA_shm_alloc_overview(&MESA_shm_ovw, &MESA_consumer_status); } - MESA_shm_semid = MESA_shm_alloc_sem(); return ; }