EdgeGateway_FSU/DevicePortGet/Public_Src/ProcessisRunning.c

413 lines
14 KiB
C
Raw Normal View History

2024-03-15 17:25:04 +08:00
/***************************************************************
Copyright © huijue Network Co., Ltd. 1998-2129. All rights reserved.
Copyright © 1998-2129. All rights reserved.
: ProcessisRunning.c
: kooloo
: V1.0
: /FSU kooloo add 202310
: iMX6ULL
: linux-imx-4.1.15-2.1.0-g3dc0a4b-v2.7
gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf
: V1.0 2023/7/15 kooloo
***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "public.h" //公共函数头文件
#include "mslog.h"
#include "ProcessisRunning.h"
#include <fcntl.h>
#include <sys/mman.h> //posix 内存共享 消息队列
#include <sys/stat.h>
#include<mqueue.h>
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : System_KillProcess
** :
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void System_KillProcess(void)
{
int ret;
ret =isRunning(S_TimeAlignedApp_ISRUNNING);
if(ret==1) //子进程正常运行 kooloo add 202311
{
SystemProcess_Exe(S_TimeAlignedApp_KillNAME); //关闭子进程
sleep(1); //延时再开启 kooloo add
}
ret =isRunning(S_MqttClientApp_ISRUNNING);
if(ret==1) //子进程正常运行 kooloo add 202311
{
SystemProcess_Exe(S_MqttClientApp_KillNAME); //关闭子进程
sleep(1); //延时再开启 kooloo add
}
ret =isRunning(S_DevicePortGetApp_ISRUNNING);
if(ret==1) //子进程正常运行 kooloo add 202311
{
SystemProcess_Exe(S_DevicePortGetApp_KillNAME); //关闭子进程
sleep(1); //延时再开启 kooloo add
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : SystemProcess_Iint
** :
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void SystemProcess_Iint(void)
{
System_KillProcess();
SystemProcess_Exe(S_DevicePortGetApp_PATH_EXE); //启动子进程
SystemProcess_Exe(S_MqttClientApp_PATH_EXE); //启动子进程
sleep(1); //延时后再做判断 kooloo add 20240311
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : SystemProcess_Exe
** :
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void SystemProcess_Exe(const char * string)
{
int ret;
ret=system(string); //后台运行
if(ret==0)
{
ms_info1("%s ok!\n",string);
}
else
{
ms_error1("%s Fail!\n",string);
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : SystemInfo_PrintLog
** :
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void SystemInfo_PrintLog(void)
{
int ret;
/* 打印进程信息及编译时间 */
ms_info1("EdgeGateway_FSU DevicePortGet process\n");
ms_info1("Make time is: %s %s\n", __DATE__, __TIME__);
/* 获取信息 */
ret = uname(&pSysPara->os_info);
if (-1 == ret)
{
ms_error1("uname error!\n");
exit(-1);
}
else
{
/* 打印系统信息 */
ms_info1("操作系统名称: %s\n", pSysPara->os_info.sysname);
ms_info1("主机名: %s\n", pSysPara->os_info.nodename);
ms_info1("内核版本: %s\n", pSysPara->os_info.release);
ms_info1("发行版本: %s\n", pSysPara->os_info.version);
ms_info1("硬件架构: %s\n", pSysPara->os_info.machine);
}
/* 获取信息 */
ret = sysinfo(&pSysPara->sys_info);
if (-1 == ret)
{
ms_error1("sysinfo error!\n");
exit(-1);
}
else
{
/* 打印系统信息 */
ms_info1("uptime: %ld\n", pSysPara->sys_info.uptime); /* 自系统启动之后所经过的时间(以秒为单位) */
ms_info1("totalram: %lu\n", pSysPara->sys_info.totalram);/* 总的可用内存大小 */
ms_info1("freeram: %lu\n", pSysPara->sys_info.freeram);/* 还未被使用的内存大小 */
ms_info1("procs: %u\n", pSysPara->sys_info.procs); /* 系统当前进程数量 */
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : SystemIpc_Init
** : IPC
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void SystemIpc_Init(void)
{
int fd; //临时变量
//设置系统变量共享内存相关设置 kooloo add 202311
#if 0 //IPC 共享内存创建 方便折叠
fd=shm_open(S_SYSDATASHARE_PATH,O_CREAT|O_RDWR,0666); //创建共享内存 可读写,如果不存在,则创建 kooloo add 202311
if(fd<0)
{
ms_fatal1("SysDat error open shm_region!\n"); //内存共享 打开失败
exit(-1);
}
ftruncate(fd,C_SYSDATASHARE_LEN); //文件截断函数 长度为预设值大小 kooloo add 202311
pSysPara=(SysPara_t *)mmap(NULL,C_SYSDATASHARE_LEN,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); //映射到内存 kooloo add 202311
if(pSysPara==MAP_FAILED) //如果申请失败 则进入对应的分支 kooloo add 202311
{
ms_fatal1("SysDat error map alloc!\n"); //内存申请空间失败
exit(-1);
}
memset(pSysPara,0,C_SYSDATASHARE_LEN); //申请内存后将其数据清空 kooloo add 202311
//设置系统变量共享内存相关设置 数据库相关 kooloo add 202311
fd=shm_open(S_DATBASESHARE_PATH,O_CREAT|O_RDWR,0666); //创建共享内存 可读写,如果不存在,则创建 kooloo add 202311
if(fd<0)
{
ms_fatal1("DatBase error open shm_region!\n"); //内存共享 打开失败
exit(-1);
}
ftruncate(fd,C_DATBASESHARE_LEN); //文件截断函数 长度为预设值大小 kooloo add 202311
pSysDatBase=(SysDatBase_t *)mmap(NULL,C_DATBASESHARE_LEN,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); //映射到内存 kooloo add 202311
if(pSysDatBase==MAP_FAILED) //如果申请失败 则进入对应的分支 kooloo add 202311
{
ms_fatal1("DatBase error map alloc!\n"); //内存申请空间失败
exit(-1);
}
memset(pSysDatBase,0,C_DATBASESHARE_LEN); //申请内存后将其数据清空 kooloo add 202311
#else //不使用共享 直接申请内存 kooloo add 202312
//为全局RS485配置存储分配内存
if ((pSysPara = (SysPara_t *)malloc(C_SYSDATASHARE_LEN )) == NULL)
{
ms_fatal1("malloc pSysPara error\n");
exit(EXIT_FAILURE); //内存出错退出进程
}
else
{
ms_info1("malloc pSysPara ok\n");
}
if ((pSysDatBase = (SysDatBase_t *)malloc(C_DATBASESHARE_LEN )) == NULL)
{
ms_fatal1("malloc pSysDatBase error\n");
exit(EXIT_FAILURE); //内存出错退出进程
}
else
{
ms_info1("malloc pSysDatBase ok\n");
}
#endif
#if 1 //Posix IPC 队列创建
struct mq_attr mq_attr_param = {
.mq_flags = O_NONBLOCK, //
.mq_maxmsg = C_SRCToDST_MQ_MSGMAX,
.mq_msgsize = C_SRCToDST_MQ_MSGSIZE
};
//源头到目的地 读取
MqdSrcToDst= mq_open(S_SRCToDST_MQ_PATH, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG, &mq_attr_param);
if (MqdSrcToDst == -1)
{
ms_error1("MqdSrcToDst open failed: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
mq_setattr(MqdSrcToDst, &mq_attr_param, NULL); // 设置为非阻塞
memset(&mq_attr_param, 0, sizeof(struct mq_attr));
mq_getattr(MqdSrcToDst, &mq_attr_param);
ms_info1("mq_attr_param.mq_flags = %d\n", (mq_attr_param.mq_flags &= O_NONBLOCK) != 0);
ms_info1("mq_attr_param.mq_maxmsg = %ld\n", mq_attr_param.mq_maxmsg);
ms_info1("mq_attr_param.mq_msgsize = %ld\n", mq_attr_param.mq_msgsize);
//目的地到源头 设置
MqdDstToSrc= mq_open(S_DSTToSRC_MQ_PATH, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG, &mq_attr_param);
if (MqdDstToSrc == -1)
{
ms_error1("MqdDstToSrc open failed: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
mq_setattr(MqdDstToSrc, &mq_attr_param, NULL); // 设置为非阻塞
memset(&mq_attr_param, 0, sizeof(struct mq_attr));
mq_getattr(MqdDstToSrc, &mq_attr_param);
ms_info1("mq_attr_param.mq_flags = %d\n", (mq_attr_param.mq_flags &= O_NONBLOCK) != 0);
ms_info1("mq_attr_param.mq_maxmsg = %ld\n", mq_attr_param.mq_maxmsg);
ms_info1("mq_attr_param.mq_msgsize = %ld\n", mq_attr_param.mq_msgsize);
#endif
#if 1 //Posix IPC 队列创建 普通串口和虚拟串口 kooloo add 202403
struct mq_attr mq_attr_param_SP = {
.mq_flags = O_NONBLOCK, //
.mq_maxmsg = C_SERIALPORT_MQ_MSGMAX,
.mq_msgsize = C_SERIALPORT_MQ_MSGSIZE
};
//普通串口 读取
MqdSerialPort= mq_open(S_SERIALPORT_MQ_PATH, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG, &mq_attr_param_SP);
if (MqdSerialPort == -1)
{
ms_error1("MqSerialPort open failed: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
mq_setattr(MqdSerialPort, &mq_attr_param_SP, NULL); // 设置为非阻塞
memset(&mq_attr_param_SP, 0, sizeof(struct mq_attr));
mq_getattr(MqdSerialPort, &mq_attr_param_SP);
ms_info1("mq_attr_param_SP.mq_flags = %d\n", (mq_attr_param_SP.mq_flags &= O_NONBLOCK) != 0);
ms_info1("mq_attr_param_SP.mq_maxmsg = %ld\n", mq_attr_param_SP.mq_maxmsg);
ms_info1("mq_attr_param_SP.mq_msgsize = %ld\n", mq_attr_param_SP.mq_msgsize);
//目的地到源头 设置
MqdVtSerialPort= mq_open(S_VTSERIALPORT_MQ_PATH, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG, &mq_attr_param_SP);
if (MqdVtSerialPort == -1)
{
ms_error1("MqVtSerialPort open failed: %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
mq_setattr(MqdVtSerialPort, &mq_attr_param_SP, NULL); // 设置为非阻塞
memset(&mq_attr_param_SP, 0, sizeof(struct mq_attr));
mq_getattr(MqdVtSerialPort, &mq_attr_param_SP);
ms_info1("mq_attr_param_SP.mq_flags = %d\n", (mq_attr_param_SP.mq_flags &= O_NONBLOCK) != 0);
ms_info1("mq_attr_param_SP.mq_maxmsg = %ld\n", mq_attr_param_SP.mq_maxmsg);
ms_info1("mq_attr_param_SP.mq_msgsize = %ld\n", mq_attr_param_SP.mq_msgsize);
#endif
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : System_Exit
** : IPC 退 退
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void System_Exit(void)
{
munmap(pSysPara,C_SYSDATASHARE_LEN); //系统调用解除指定地址范围内的映射 kooloo add 202311 退出主进程 解除映射
munmap(pSysDatBase,C_DATBASESHARE_LEN); //系统调用解除指定地址范围内的映射 kooloo add 202311 退出主进程 解除映射
mq_close(MqdSrcToDst);
mq_close(MqdDstToSrc);
//mq_unlink(S_SRCToDST_MQ_PATH);
//mq_unlink(S_DSTToSRC_MQ_PATH);
mslog_api_deinit();//退出释放日志资源;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : ProcessIsRunning_ServiceLoop
** :
** :
**   : 2023902
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
void ProcessIsRunning_ServiceLoop(void)
{
int ret;
if(pSysPara->g_ExeIndex==C_SUBPRCESS_DevicePortGetApp)
{
ret =isRunning(S_DevicePortGetApp_ISRUNNING);
if(ret==1) //子进程正常运行 kooloo add 202311
{
}
else
{
SystemProcess_Exe(S_DevicePortGetApp_KillNAME); //关闭子进程
sleep(1); //延时再开启 kooloo add
SystemProcess_Exe(S_DevicePortGetApp_PATH_EXE); //启动子进程
}
pSysPara->g_ExeIndex=C_SUBPRCESS_MqttClientApp;
}
else
{
ret =isRunning(S_MqttClientApp_ISRUNNING);
if(ret==1) //子进程正常运行 kooloo add 202311
{
}
else
{
SystemProcess_Exe(S_MqttClientApp_KillNAME); //关闭子进程
sleep(1); //延时再开启 kooloo add
SystemProcess_Exe(S_MqttClientApp_PATH_EXE); //启动子进程
}
pSysPara->g_ExeIndex= C_SUBPRCESS_DevicePortGetApp;
}
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : getPidFromStr
** : pid
** :
**  : https://blog.csdn.net/dezhihuang/article/details/78769599
**   : 20220321
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
char* getPidFromStr(const char *str)
{
static char sPID[8] = {0};
int tmp = 0;
int pos1 = 0;
int pos2 = 0;
int i = 0;
int j = 0;
for (i=0; i<strlen(str); i++) {
if ( (tmp==0) && (str[i]>='0' && str[i]<='9') ) {
tmp = 1;
pos1 = i;
}
if ( (tmp==1) && (str[i]<'0' || str[i]>'9') ) {
pos2 = i;
break;
}
}
for (j=0,i=pos1; i<pos2; i++,j++) {
sPID[j] = str[i];
}
return sPID;
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
** : isRunning
** :
** :
**  : https://blog.csdn.net/dezhihuang/article/details/78769599
**   : 20220321
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
int isRunning(char * CommandStr)
{
int ret = -1;
char sCurrPid[16] = {0};
sprintf(sCurrPid, "%d", getpid());
printf("sCurrPid=%s\n", sCurrPid);
FILE *fstream=NULL;
char buff[1024] = {0};
// if(NULL==(fstream=popen("ps -e -o pid,comm | grep a.out | grep -v PID | grep -v grep", "r"))) //原始
if(NULL==(fstream=popen(CommandStr,"r")))
{
// fprintf(stderr,"execute command failed: %s", strerror(errno));
ms_error1("execute command failed: %s\n",strerror(errno));
pclose(fstream);
return -1;
}
printf("Cmd %s\n",CommandStr);
char *PID_Str=fgets(buff, sizeof(buff), fstream);
printf("PID_Str %s\n",PID_Str);
while(NULL!=PID_Str)
{
printf("buff %s",buff);
char *oldPID = getPidFromStr(buff);
//if ( strcmp(sCurrPid, oldPID) != 0 )
//if ( oldPID>0 )
{
printf("程序已经运行PID=%s\n", oldPID);
ret = 1;
}
PID_Str=fgets(buff, sizeof(buff), fstream);
}
pclose(fstream);
// if(ret!=1)
{
system("ps -e");
}
return ret;
}