370 lines
7.5 KiB
C
370 lines
7.5 KiB
C
/***
|
|
* @文件名称: utils.cpp
|
|
* @系统名称: 公共库
|
|
* @模块名称: 底层函数操作库
|
|
* @功能说明: 工具类函数
|
|
* @当前版本: 1.0.0
|
|
* @修改人员: 吕世健
|
|
* @修改日期: 2012-06-01
|
|
* @修改说明: 创建文件
|
|
*/
|
|
#include "utils.h"
|
|
|
|
|
|
void EntireSleep(UINT nEntireSecs)
|
|
{
|
|
struct timeval struTimeout;
|
|
|
|
struTimeout.tv_sec = nEntireSecs % 10000000;
|
|
struTimeout.tv_usec = 0;
|
|
|
|
select(0, NULL, NULL, NULL, &struTimeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
void MilliSleep(UINT nMilliSecs)
|
|
{
|
|
struct timeval struTimeout;
|
|
|
|
struTimeout.tv_sec = nMilliSecs / 1000;
|
|
struTimeout.tv_usec = (nMilliSecs % 1000) * 1000;
|
|
|
|
select(0, NULL, NULL, NULL, &struTimeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int killprg(char *prgname,int sig)
|
|
{
|
|
FILE *fp;
|
|
char *p1, *p2;
|
|
int i;
|
|
long pid[200];
|
|
int ret=0;
|
|
char temp_prgname[80];
|
|
int account = 0;
|
|
char buffer[256];
|
|
|
|
signal(SIGTERM, SIG_IGN); /*防止被自己kill掉*/
|
|
|
|
memset(temp_prgname, 0, sizeof(temp_prgname));
|
|
p1 = strtok(prgname, "/");
|
|
if (p1 == NULL)
|
|
return 0;
|
|
p2 = p1;
|
|
for ( ; ; )
|
|
{
|
|
p1 = strtok(NULL, "/");
|
|
if (p1 == NULL)
|
|
break;
|
|
else
|
|
p2 = p1;
|
|
}
|
|
strcpy(temp_prgname, p2);
|
|
memset(buffer, 0, sizeof(buffer));
|
|
sprintf(buffer, "ps -ef|grep -v grep|grep %s|grep start|awk '{print $2}'|cut -c -5", temp_prgname);
|
|
if ((fp = popen(buffer, "r")) == NULL)
|
|
return -1;
|
|
|
|
while (!feof(fp))
|
|
{
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
ret=fscanf(fp, "%s", buffer);
|
|
if( ret == -1)
|
|
return ret;
|
|
pid[account++] = atoi(buffer);
|
|
}
|
|
pclose(fp);
|
|
|
|
for (i = 0;i < account - 1;i++)
|
|
{
|
|
if(kill(pid[i],sig) == -1)
|
|
{
|
|
//fprintf(stderr,"向进程发送SIGTERM信号失败,可能是进程已经被关闭!\n");
|
|
}
|
|
}
|
|
/*如果上面没有关闭程序,那么进行强行关闭*/
|
|
MilliSleep(200);
|
|
account = 0;
|
|
memset(buffer, 0, sizeof(buffer));
|
|
sprintf(buffer, "ps -ef|grep -v grep|grep %s|grep start|awk '{print $2}'|cut -c -5", temp_prgname);
|
|
if ((fp = popen(buffer, "r")) == NULL)
|
|
return -1;
|
|
|
|
while (!feof(fp))
|
|
{
|
|
memset(buffer, '\0', sizeof(buffer));
|
|
ret = fscanf(fp, "%s", buffer);
|
|
if( ret == -1)
|
|
return ret;
|
|
pid[account++] = atoi(buffer);
|
|
}
|
|
pclose(fp);
|
|
|
|
for (i = 0;i < account - 1;i++)
|
|
{
|
|
if(kill(pid[i], SIGKILL) == -1)
|
|
{
|
|
fprintf(stderr,"Send Singal SIGKILL hsm_test Failed ,maybe hsm_test Closed \n");
|
|
return account;
|
|
}
|
|
fprintf(stderr,"exit hsm_test successful \n");
|
|
}
|
|
return account;
|
|
}
|
|
|
|
/**
|
|
* @函数说明:创建进程号记录文件
|
|
* @pszProgName: 要创建的进程文件名称
|
|
* @返回说明: 成功NO_ERROR 失败 ERROR_FALSE
|
|
*/
|
|
int CreateIdFile(PSTR pszProgName)
|
|
{
|
|
FILE *pstruFile;
|
|
STR szFileName[MAX_PATH_LEN];
|
|
|
|
memset(szFileName, 0, sizeof(szFileName));
|
|
/* snprintf(szFileName, sizeof(szFileName),"%s/.%sid", \
|
|
getenv("HOME"), pszProgName);*/
|
|
snprintf(szFileName, sizeof(szFileName),"/tmp/.%sid", pszProgName);
|
|
|
|
pstruFile = fopen(szFileName,"w");
|
|
if(pstruFile == NULL)
|
|
{
|
|
return ERROR_FALSE;
|
|
}
|
|
fprintf(pstruFile,"%d",getpid());
|
|
fclose(pstruFile);
|
|
|
|
chmod(szFileName, 0600);
|
|
|
|
return NO_ERROR;
|
|
}
|
|
|
|
/**
|
|
* @函数说明:停止进程的执行
|
|
* @pszProgName: 要停止的进程名称
|
|
* @返回说明: 无
|
|
*/
|
|
void stopPrg(PSTR pszProgName)
|
|
{
|
|
FILE *pstruFile;
|
|
STR szFileName[MAX_PATH_LEN];
|
|
UINT nProgId;
|
|
int ret;
|
|
memset(szFileName, 0, sizeof(szFileName));
|
|
/* snprintf(szFileName, sizeof(szFileName), "%s/.%sid", \
|
|
getenv("HOME"), pszProgName);*/
|
|
snprintf(szFileName, sizeof(szFileName), "/tmp/.%sid", pszProgName);
|
|
|
|
pstruFile = fopen(szFileName,"r");
|
|
if(pstruFile == NULL)
|
|
{
|
|
fprintf(stderr,
|
|
"close %s failed ... the process maybe not start or Access denied\n", \
|
|
pszProgName);
|
|
return;
|
|
}
|
|
ret = fscanf(pstruFile, "%d", &nProgId);
|
|
if( ret == -1)
|
|
return;
|
|
if(kill(nProgId, SIGTERM) == -1)
|
|
{
|
|
EntireSleep(1);
|
|
if(kill(nProgId, SIGKILL) == -1)
|
|
{
|
|
fprintf(stderr,
|
|
"close %s failed ... the process maybe not start or Access denied\n", \
|
|
pszProgName);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
fprintf(stderr,"close %s successful \n", pszProgName);
|
|
}
|
|
|
|
fclose(pstruFile);
|
|
unlink(szFileName);
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* @函数说明:判断进程是否启动
|
|
* @pszProgName: 要判断的进程名称
|
|
* @返回说明: 进程启动返回NO_ERROR 否则返回ERROR_FALSE
|
|
*/
|
|
int TestPrgStat(PSTR pszProgName)
|
|
{
|
|
STR szFileName[MAX_PATH_LEN];
|
|
STR szShellCmd[MAX_STR_LEN];
|
|
UINT nProgId;
|
|
UINT nTempLen;
|
|
FILE *pstruFile;
|
|
int ret;
|
|
memset(szFileName, 0, sizeof(szFileName));
|
|
/* snprintf(szFileName, sizeof(szFileName), "%s/.%sid", \
|
|
getenv("HOME"), pszProgName);*/
|
|
snprintf(szFileName, sizeof(szFileName), "/tmp/.%sid", pszProgName);
|
|
|
|
pstruFile = fopen(szFileName, "r");
|
|
if(pstruFile == NULL)
|
|
{
|
|
return ERROR_FALSE;
|
|
}
|
|
nProgId = 0;
|
|
ret = fscanf(pstruFile, "%d", &nProgId);
|
|
if( ret == -1)
|
|
return ret;
|
|
fclose(pstruFile);
|
|
if(nProgId < 1)
|
|
{
|
|
return ERROR_FALSE;
|
|
}
|
|
memset(szShellCmd, 0, sizeof(szShellCmd));
|
|
snprintf(szShellCmd, sizeof(szShellCmd), \
|
|
"ps -e|awk '$1 == %d {print $4}'", nProgId);
|
|
|
|
pstruFile = popen(szShellCmd, "r");
|
|
if(pstruFile == NULL)
|
|
{
|
|
return ERROR_FALSE;
|
|
}
|
|
char * iRet = fgets(szFileName, sizeof(szFileName), pstruFile);
|
|
if( iRet == NULL)
|
|
return ERROR_FALSE;
|
|
fclose(pstruFile);
|
|
nTempLen = strlen(szFileName);
|
|
if(nTempLen < 1)
|
|
{
|
|
return ERROR_FALSE;
|
|
}
|
|
szFileName[nTempLen - 1]='\0';
|
|
if(strcmp(szFileName, pszProgName) == 0)
|
|
{
|
|
return NO_ERROR;
|
|
}
|
|
return ERROR_FALSE;
|
|
}
|
|
|
|
/**
|
|
* @函数说明:将进程变为后台起动进程
|
|
* @返回说明: 进程启动返回NO_ERROR 否则返回ERROR_FALSE
|
|
*/
|
|
RESULT DaemonStart()
|
|
{
|
|
register int nChildProgId;
|
|
|
|
signal(SIGTTOU, SIG_IGN);
|
|
signal(SIGTTIN, SIG_IGN);
|
|
signal(SIGTSTP, SIG_IGN);
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
nChildProgId = fork();
|
|
if(nChildProgId < 0)
|
|
{
|
|
printf("create pid error %s\n", strerror(errno));
|
|
return ERROR_FALSE;
|
|
}
|
|
else if(nChildProgId > 0)
|
|
exit(0);
|
|
|
|
if (setpgrp() == -1)
|
|
{
|
|
printf("setpgrp 设置进程组错误 %s\n", \
|
|
strerror(errno));
|
|
exit(0);
|
|
}
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
if((nChildProgId = fork()) < 0)
|
|
{
|
|
printf("创建第2个子进程错误 %s\n", \
|
|
strerror(errno));
|
|
exit(0);
|
|
}
|
|
else if(nChildProgId > 0)
|
|
exit(0);
|
|
|
|
//chdir("/");
|
|
umask(0);
|
|
return NO_ERROR;
|
|
}
|
|
|
|
/**
|
|
* @函数说明: 扫描目录
|
|
* @pszDirName: 目录名称
|
|
* @pstruDirentList: 目录结构
|
|
* @返回说明: 成功操作返回NO_ERROR 失败操作返回ERROR_FALSE
|
|
*/
|
|
int ScanDir(const char *pszDirName, struct dirent ***pstruDirentList)
|
|
{
|
|
#ifdef SYSTEM_LINUX
|
|
return scandir(pszDirName, pstruDirentList, NULL, alphasort);
|
|
#else
|
|
DIR *pstruDir;
|
|
struct dirent *pstruDirent, *pFirst, *pSecond;
|
|
struct dirent **pDirent;
|
|
int nNumber, nLeftNumber, nSize;
|
|
int i,j;
|
|
|
|
/*
|
|
* 打开目录
|
|
*/
|
|
if((pstruDir = opendir(pszDirName)) == NULL)
|
|
{
|
|
printf("打开目录错误[%s][%s]\n",
|
|
pszDirName, strerror(errno));
|
|
return -1;
|
|
}
|
|
nNumber = 0;
|
|
pDirent = (struct dirent **)
|
|
malloc((nNumber + 100) * sizeof(struct dirent *));
|
|
nLeftNumber = 100;
|
|
|
|
/*
|
|
* 生成数组
|
|
*/
|
|
while((pstruDirent = readdir(pstruDir)) != NULL)
|
|
{
|
|
nSize = sizeof(struct dirent) + pstruDirent->d_reclen;
|
|
pDirent[nNumber] = (struct dirent *)malloc(nSize);
|
|
memset(pDirent[nNumber], 0, nSize);
|
|
memcpy(pDirent[nNumber], pstruDirent, nSize);
|
|
nNumber++;
|
|
nLeftNumber--;
|
|
if(nLeftNumber == 0)
|
|
{
|
|
pDirent = (struct dirent **)realloc(pDirent,
|
|
(nNumber + 100) * sizeof(struct dirent *));
|
|
nLeftNumber = 100;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 数组排序
|
|
*/
|
|
for(i = 0;i < (nNumber - 1);i++)
|
|
{
|
|
pFirst = pDirent[i];
|
|
for(j = i + 1;j < nNumber;j++)
|
|
{
|
|
pSecond = pDirent[j];
|
|
if(strcmp(pFirst->d_name, pSecond->d_name) > 0)
|
|
{
|
|
pstruDirent = pFirst;
|
|
pDirent[i] = pDirent[j];
|
|
pDirent[j] = pstruDirent;
|
|
pFirst = pDirent[i];
|
|
}
|
|
}
|
|
}
|
|
*pstruDirentList = pDirent;
|
|
return nNumber;
|
|
#endif
|
|
}
|
|
|