修复:防止 ATSC 应用重复启动
This commit is contained in:
@@ -5,68 +5,107 @@
|
||||
|
||||
***************************************/
|
||||
#include "common.h"
|
||||
#include <dirent.h>
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
|
||||
//程序名字
|
||||
#define PROGRAM_NAME "imx_atsc_app"
|
||||
#define NODE_NAME "http"
|
||||
|
||||
//查找进程中程序名字
|
||||
#define PROGRAM_NAME_FIND "imx_atsc_app"
|
||||
#define PROGRAM_NODE_NAME_FIND "http"
|
||||
|
||||
//输出目录
|
||||
#define PROGRAM_DIR_OUT_FILE "/tmp/atsc_app_status"
|
||||
#define NODE_PROGRAM_DIR_OUT_FILE "/tmp/web_p_status"
|
||||
|
||||
//要运行的程序
|
||||
//#define RUN_NAME_PROGRAM "/aw/anywave/app/imx_atsc_app 1 &"
|
||||
#define RUN_NAME_PROGRAM "/aw/anywave/app/imx_atsc_app 1 2>&1 | loglog /var/module_log/ imx_atsc_app.log 1 &"
|
||||
#define PROGRAM_EXE_PATH "/aw/anywave/app/imx_atsc_app"
|
||||
#define RUN_NAME_PROGRAM PROGRAM_EXE_PATH " 2>&1 | /usr/sbin/loglog /var/module_log/ imx_atsc_app.log 1 &"
|
||||
#define RUN_NODE_NAME_PROGRAM "cd /aw/anywave/web/sh && ./start_web.sh &"
|
||||
|
||||
/* The application renames its main task with pthread_setname_np(), therefore
|
||||
* BusyBox `ps | grep imx_atsc_app` can report it as missing and the daemon may
|
||||
* start a second instance. /proc/<pid>/exe remains stable across task renames
|
||||
* and also lets us match the exact executable instead of a substring. */
|
||||
static int is_app_running(void)
|
||||
{
|
||||
DIR *proc_dir;
|
||||
struct dirent *entry;
|
||||
char exe_link[64];
|
||||
char exe_path[PATH_MAX];
|
||||
ssize_t exe_len;
|
||||
size_t expected_len = strlen(PROGRAM_EXE_PATH);
|
||||
|
||||
int main(int argc,char *argv[])
|
||||
proc_dir = opendir("/proc");
|
||||
if(proc_dir == NULL)
|
||||
{
|
||||
/* Fail safe: an unreadable /proc must not cause duplicate app starts. */
|
||||
return -1;
|
||||
}
|
||||
|
||||
while((entry = readdir(proc_dir)) != NULL)
|
||||
{
|
||||
const char *p = entry->d_name;
|
||||
|
||||
if(*p == '\0')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
while(*p >= '0' && *p <= '9')
|
||||
{
|
||||
p++;
|
||||
}
|
||||
if(*p != '\0')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
snprintf(exe_link, sizeof(exe_link), "/proc/%s/exe", entry->d_name);
|
||||
exe_len = readlink(exe_link, exe_path, sizeof(exe_path) - 1);
|
||||
if(exe_len < 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
exe_path[exe_len] = '\0';
|
||||
|
||||
if(strcmp(exe_path, PROGRAM_EXE_PATH) == 0 ||
|
||||
(strncmp(exe_path, PROGRAM_EXE_PATH, expected_len) == 0 &&
|
||||
strcmp(exe_path + expected_len, " (deleted)") == 0))
|
||||
{
|
||||
closedir(proc_dir);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
closedir(proc_dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fd = 0;
|
||||
int ret = 0;
|
||||
int remux_ok = 0;
|
||||
int node_ok = 0;
|
||||
|
||||
char buf[100];
|
||||
char cmd[100] = {0};
|
||||
char cmd[256] = {0};
|
||||
|
||||
//开启守护进程
|
||||
daemon(0,0);
|
||||
|
||||
while (1)
|
||||
{
|
||||
//新建输出文件
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "echo 0 > %s", PROGRAM_DIR_OUT_FILE);
|
||||
ret = os_system(cmd);
|
||||
|
||||
//获得程序ID
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "ps | grep %s | grep -v grep >> %s", PROGRAM_NAME_FIND, PROGRAM_DIR_OUT_FILE);
|
||||
ret = os_system(cmd);
|
||||
|
||||
//打开输出文件
|
||||
fd = open(PROGRAM_DIR_OUT_FILE, O_CREAT|O_RDONLY, 0777);
|
||||
memset(buf, 0, 100);
|
||||
read(fd, buf, 100);
|
||||
close(fd);
|
||||
remux_ok = match(buf, PROGRAM_NAME, 90);
|
||||
remux_ok = is_app_running();
|
||||
|
||||
// web
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "echo 0 > %s", NODE_PROGRAM_DIR_OUT_FILE);
|
||||
ret = os_system(cmd);
|
||||
(void)os_system(cmd);
|
||||
|
||||
//获得程序ID
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "ps | grep %s | grep -v grep >> %s", PROGRAM_NODE_NAME_FIND, NODE_PROGRAM_DIR_OUT_FILE);
|
||||
ret = os_system(cmd);
|
||||
(void)os_system(cmd);
|
||||
|
||||
//打开输出文件
|
||||
fd = open(NODE_PROGRAM_DIR_OUT_FILE, O_CREAT|O_RDONLY, 0777);
|
||||
@@ -80,14 +119,17 @@ int main(int argc,char *argv[])
|
||||
//判断是否有程序文件运行
|
||||
if(1 != remux_ok)
|
||||
{
|
||||
//运行程序
|
||||
/* Debounce once before starting. This prevents a duplicate launch
|
||||
* when the app is between exec/startup states during an upgrade. */
|
||||
usleep(1000000);
|
||||
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "%s", RUN_NAME_PROGRAM);
|
||||
printf("imx_atsc_app stop , daemon work.\n%s\n\n", cmd);
|
||||
system(cmd);
|
||||
usleep(100000);
|
||||
if(!is_app_running())
|
||||
{
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
snprintf(cmd, sizeof(cmd), "%s", RUN_NAME_PROGRAM);
|
||||
printf("imx_atsc_app stop, daemon restart without legacy argument.\n%s\n\n", cmd);
|
||||
system(cmd);
|
||||
usleep(100000);
|
||||
}
|
||||
}
|
||||
if(1 != node_ok)
|
||||
{
|
||||
@@ -102,13 +144,9 @@ int main(int argc,char *argv[])
|
||||
|
||||
|
||||
//删除输出文件
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "rm %s", PROGRAM_DIR_OUT_FILE);
|
||||
ret = os_system(cmd);
|
||||
|
||||
memset(cmd, 0 ,sizeof(cmd));
|
||||
sprintf(cmd, "rm %s", NODE_PROGRAM_DIR_OUT_FILE);
|
||||
ret = os_system(cmd);
|
||||
(void)os_system(cmd);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user