From e8d34d11799fc79c7c53bdcd40f9b4ee7be7a2c5 Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Wed, 25 Sep 2024 17:42:22 +0800 Subject: [PATCH] Merge branch 'master' of ssh://weike-iot.com:2280/framwork --- booster/util_proc.c | 286 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 144 insertions(+), 142 deletions(-) diff --git a/booster/util_proc.c b/booster/util_proc.c index 9474851..dfdabae 100644 --- a/booster/util_proc.c +++ b/booster/util_proc.c @@ -4,21 +4,23 @@ * * Filename: util_proc.c * Description: This file is the process API - * + * * Version: 1.0.0(7/06/2020) * Author: Guo Wenxue <guowenxue@gmail.com> * ChangeLog: 1, Release initial version on "7/06/2020 09:19:02 PM" - * - ********************************************************************************/ + * + ********************************************************************************/ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> -#include <libgen.h> +#include <errno.h> #include <fcntl.h> +#include <libgen.h> +#include <pthread.h> #include <sys/types.h> #include <sys/stat.h> -#include <pthread.h> #include "util_proc.h" #include "logger.h" @@ -52,7 +54,7 @@ case SIGPIPE: log_warn("SIGPIPE - warnning\n"); break; - + default: break; } @@ -64,7 +66,7 @@ { struct sigaction sigact, sigign; - log_nrml("Install default signal handler.\n"); + log_info("Install default signal handler.\n"); /* Initialize the catch signal structure. */ sigemptyset(&sigact.sa_mask); @@ -82,7 +84,7 @@ sigaction(SIGPIPE, &sigact, 0); /* catch broken pipe */ #if 0 sigaction(SIGCHLD, &sigact, 0); /* catch child process return */ - sigaction(SIGUSR2, &sigact, 0); /* catch USER signal */ + sigaction(SIGUSR2, &sigact, 0); /* catch USER signal */ #endif } @@ -96,25 +98,25 @@ * Return : NONE * *****************************************************************************/ void daemonize(int nochdir, int noclose) -{ - int rv, fd; - int i; - - /* already a daemon */ - if (1 == getppid()) - return; - +{ + int rv, fd; + int i; + + /* already a daemon */ + if (1 == getppid()) + return; + /* fork error */ - rv = fork(); - if (rv < 0) exit(1); - + rv = fork(); + if (rv < 0) exit(1); + /* parent process exit */ if (rv > 0) - exit(0); - + exit(0); + /* obtain a new process session group */ - setsid(); - + setsid(); + if (!noclose) { /* close all descriptors */ @@ -122,29 +124,29 @@ { //if (i != g_logPtr->fd) close(i); - } + } /* Redirect Standard input [0] to /dev/null */ - fd = open("/dev/null", O_RDWR); + fd = open("/dev/null", O_RDWR); /* Redirect Standard output [1] to /dev/null */ - dup(fd); + dup(fd); /* Redirect Standard error [2] to /dev/null */ - dup(fd); - } - - umask(0); - + dup(fd); + } + + umask(0); + if (!nochdir) - chdir("/"); - + chdir("/"); + return; } /* **************************************************************************** * FunctionName: check_set_program_running - * Description : check program already running or not, if not then run it and + * Description : check program already running or not, if not then run it and * record pid into $pidfile * Inputs : daemon: set program running in daemon or not * pid_file:The record PID file path @@ -157,17 +159,17 @@ if( !pidfile ) return 0; - if( check_daemon_running(pidfile) ) + if( check_daemon_running(pidfile) ) { - log_err("Program already running, process exit now"); + log_error("Program already running, process exit now"); return -1; } - if( daemon ) + if( daemon ) { if( set_daemon_running(pidfile) < 0 ) { - log_err("set program running as daemon failure\n"); + log_error("set program running as daemon failure\n"); return -2; } } @@ -175,7 +177,7 @@ { if( record_daemon_pid(pidfile) < 0 ) { - log_err("record program running PID failure\n"); + log_error("record program running PID failure\n"); return -3; } } @@ -193,43 +195,43 @@ * Return : 0: Record successfully Else: Failure * *****************************************************************************/ int record_daemon_pid(const char *pid_file) -{ - struct stat fStatBuf; - int fd = -1; +{ + struct stat fStatBuf; + int fd = -1; int mode = S_IROTH | S_IXOTH | S_IRGRP | S_IXGRP | S_IRWXU; - char ipc_dir[64] = { 0 }; - - strncpy(ipc_dir, pid_file, 64); + char ipc_dir[64] = { 0 }; - /* dirname() will modify ipc_dir and save the result */ - dirname(ipc_dir); - + strncpy(ipc_dir, pid_file, 64); + + /* dirname() will modify ipc_dir and save the result */ + dirname(ipc_dir); + /* If folder pid_file PATH doesnot exist, then we will create it" */ - if (stat(ipc_dir, &fStatBuf) < 0) - { - if (mkdir(ipc_dir, mode) < 0) - { - log_err("cannot create %s: %s\n", ipc_dir, strerror(errno)); - return -1; - } - - (void)chmod(ipc_dir, mode); - } - - /* Create the process running PID file */ + if (stat(ipc_dir, &fStatBuf) < 0) + { + if (mkdir(ipc_dir, mode) < 0) + { + log_error("cannot create %s: %s\n", ipc_dir, strerror(errno)); + return -1; + } + + (void)chmod(ipc_dir, mode); + } + + /* Create the process running PID file */ mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; if ((fd = open(pid_file, O_RDWR | O_CREAT | O_TRUNC, mode)) >= 0) { - char pid[PID_ASCII_SIZE]; - snprintf(pid, sizeof(pid), "%u\n", (unsigned)getpid()); - write(fd, pid, strlen(pid)); - close(fd); + char pid[PID_ASCII_SIZE]; + snprintf(pid, sizeof(pid), "%u\n", (unsigned)getpid()); + write(fd, pid, strlen(pid)); + close(fd); - log_dbg("Record PID<%u> to file %s.\n", getpid(), pid_file); - } - else + log_debug("Record PID<%u> to file %s.\n", getpid(), pid_file); + } + else { - log_err("cannot create %s: %s\n", pid_file, strerror(errno)); + log_error("cannot create %s: %s\n", pid_file, strerror(errno)); return -1; } @@ -244,24 +246,24 @@ * Return : pid_t: The daemon process PID number * *****************************************************************************/ pid_t get_daemon_pid(const char *pid_file) -{ - FILE *f; - pid_t pid; - +{ + FILE *f; + pid_t pid; + if ((f = fopen(pid_file, "rb")) != NULL) - { - char pid_ascii[PID_ASCII_SIZE]; - (void)fgets(pid_ascii, PID_ASCII_SIZE, f); - (void)fclose(f); - pid = atoi(pid_ascii); - } + { + char pid_ascii[PID_ASCII_SIZE]; + (void)fgets(pid_ascii, PID_ASCII_SIZE, f); + (void)fclose(f); + pid = atoi(pid_ascii); + } else { - log_err("Can't open PID record file %s: %s\n", pid_file, strerror(errno)); + log_error("Can't open PID record file %s: %s\n", pid_file, strerror(errno)); return -1; - } + } return pid; -} +} /* **************************************************************************** * FunctionName: check_daemon_running @@ -272,43 +274,43 @@ * *****************************************************************************/ int check_daemon_running(const char *pid_file) { - int rv = -1; + int rv = -1; struct stat fStatBuf; - rv = stat(pid_file, &fStatBuf); - if (0 == rv) - { - pid_t pid = -1; + rv = stat(pid_file, &fStatBuf); + if (0 == rv) + { + pid_t pid = -1; printf("PID record file \"%s\" exist.\n", pid_file); pid = get_daemon_pid(pid_file); if (pid > 0) /* Process pid exist */ - { - if ((rv = kill(pid, 0)) == 0) - { - printf("Program with PID[%d] seems running.\n", pid); - return 1; - } - else /* Send signal to the old process get no reply. */ - { - printf("Program with PID[%d] seems exit.\n", pid); - remove(pid_file); - return 0; - } - } - else if (0 == pid) - { - printf("Can not read program PID form record file.\n"); - remove(pid_file); - return 0; - } + { + if ((rv = kill(pid, 0)) == 0) + { + printf("Program with PID[%d] seems running.\n", pid); + return 1; + } + else /* Send signal to the old process get no reply. */ + { + printf("Program with PID[%d] seems exit.\n", pid); + remove(pid_file); + return 0; + } + } + else if (0 == pid) + { + printf("Can not read program PID form record file.\n"); + remove(pid_file); + return 0; + } else /* Read pid from file "pid_file" failure */ - { - printf("Read record file \"%s\" failure, maybe program still running.\n", pid_file); - return 1; - } - } - + { + printf("Read record file \"%s\" failure, maybe program still running.\n", pid_file); + return 1; + } + } + return 0; } @@ -321,25 +323,25 @@ * *****************************************************************************/ int stop_daemon_running(const char *pid_file) { - pid_t pid = -1; + pid_t pid = -1; struct stat fStatBuf; if ( stat(pid_file, &fStatBuf) < 0) return 0; - printf("PID record file \"%s\" exist.\n", pid_file); + printf("PID record file \"%s\" exist.\n", pid_file); pid = get_daemon_pid(pid_file); if (pid > 0) /* Process pid exist */ - { - while ( (kill(pid, 0) ) == 0) - { + { + while ( (kill(pid, 0) ) == 0) + { kill(pid, SIGTERM); sleep(1); - } - - remove(pid_file); - } - + } + + remove(pid_file); + } + return 0; } @@ -347,20 +349,20 @@ /* **************************************************************************** * FunctionName: set_daemon_running - * Description : Set the programe running as daemon if it's not running and record + * Description : Set the programe running as daemon if it's not running and record * its PID to the pid_file. * Inputs : pid_file: The record running daemon program PID * Output : NONE * Return : 0: Successfully. 1: Failure * *****************************************************************************/ int set_daemon_running(const char *pid_file) -{ - daemonize(0, 1); - log_nrml("Program running as daemon [PID:%d].\n", getpid()); - - if (record_daemon_pid(pid_file) < 0) - { - log_err("Record PID to file \"%s\" failure.\n", pid_file); +{ + daemonize(0, 1); + log_info("Program running as daemon [PID:%d].\n", getpid()); + + if (record_daemon_pid(pid_file) < 0) + { + log_error("Record PID to file \"%s\" failure.\n", pid_file); return -2; } @@ -373,23 +375,23 @@ int rv = 0; pthread_t tid; - pthread_attr_t thread_attr; + pthread_attr_t thread_attr; /* Initialize the thread attribute */ - rv = pthread_attr_init(&thread_attr); + rv = pthread_attr_init(&thread_attr); if(rv) return -1; /* Set the stack size of the thread */ - rv = pthread_attr_setstacksize(&thread_attr, 120 * 1024); + rv = pthread_attr_setstacksize(&thread_attr, 120 * 1024); if(rv) goto CleanUp; - + /* Set thread to detached state:Don`t need pthread_join */ - rv = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); + rv = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); if(rv) goto CleanUp; - + /* Create the thread */ rv = pthread_create(&tid, &thread_attr, thread_workbody, thread_arg); if(rv) @@ -407,7 +409,7 @@ } /* Destroy the attributes of thread */ - pthread_attr_destroy(&thread_attr); + pthread_attr_destroy(&thread_attr); return rv; } @@ -417,13 +419,13 @@ { char cmd[256]; va_list args; - - memset(cmd, 0, sizeof(cmd)); - - va_start(args, format); + + memset(cmd, 0, sizeof(cmd)); + + va_start(args, format); vsnprintf(cmd, sizeof(cmd), format, args); - va_end(args); - + va_end(args); + system(cmd); } -- Gitblit v1.9.1