#include #include #include #include #include #include #include #include #include #include "MESA_shm_ring_queue.h" #define BUFSIZE 256 void init_ring_queue_head_arrray(struct MESA_shm_overview *ovw, struct MESA_shm_queue_head **ring_queue_head) { int i = 0; struct MESA_shm_overview *tmp_ovw = NULL; for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){ tmp_ovw = ovw + i; if(tmp_ovw->shmid == -1){ break; } ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0); } return ; } void consumer_daemo() { int fd = -1; switch (fork()) { case -1: printf("fork error\n"); return ; case 0: break; default: exit(0); } if (setsid() == -1) { return ; } umask(0); fd = open("/dev/null", O_RDWR); if (fd == -1) { return ; } if (dup2(fd, STDIN_FILENO) == -1) { return ; } if (dup2(fd, STDOUT_FILENO) == -1) { return ; } if (fd > STDERR_FILENO) { if (close(fd) == -1) { return ; } } return ; } int consumer_is_running(char *process_name) { FILE *fp = NULL; char buf[BUFSIZE] = {0}; int count = 0; char command[BUFSIZE] = {0}; if(process_name == NULL){ return 0; } snprintf(command, sizeof(command), "ps -ef | grep %s | grep -v grep | wc -l", process_name); fp = popen(command, "r"); if(fp == NULL){ return 0; } if((fgets(buf, sizeof(buf), fp)) != NULL){ count = atoi(buf); } pclose(fp); if(count > 1){ return 1; }else{ return 0; } return 0; } char *get_exe_name(char *argv) { char * p = NULL; p = rindex(argv, '/'); if(p == NULL){ p = argv; }else{ p = p + 1; } return p; } int main(int argc, char **argv) { char *exe_name = get_exe_name(argv[0]); if(consumer_is_running(exe_name)){ printf("consumer process is already running\n"); return 0; } consumer_daemo(); int i = 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}; int log_file_fd = -1; shm_overview = MESA_shm_alloc_overview(); if(shm_overview == NULL){ return 0; } init_ring_queue_head_arrray(shm_overview, ring_queue_head); log_file_fd = open("/root/MESA_log", O_RDWR | O_CREAT | O_APPEND, 0666); if(log_file_fd < 0){ return 0; } while(1){ for(i = 0; i< MESA_SHM_RING_QUEUE_NUM; i++){ tmp_ovw = shm_overview + i; if(tmp_ovw->shmid == -1){ break; } if(ring_queue_head[i] == NULL){ ring_queue_head[i] = shmat(tmp_ovw->shmid, NULL, 0); if(ring_queue_head[i] == NULL){ break ; } } if(tmp_ovw->status == MESA_SHM_RING_QUEUE_USED || tmp_ovw->status == MESA_SHM_RING_QUEUE_HALF_IDLE){ MESA_shm_write_ring_queue_to_file(log_file_fd, ring_queue_head[i]); if(tmp_ovw->status == MESA_SHM_RING_QUEUE_HALF_IDLE){ tmp_ovw->status = MESA_SHM_RING_QUEUE_IDLE; } } } usleep(5000); } return 0; }