Add mbtk_servicesd

Change-Id: Ie2dcb3420468035f2a2089ac967ab3c1ccb3bf1a
diff --git a/mbtk/Makefile b/mbtk/Makefile
index d9e355f..3b1956a 100755
--- a/mbtk/Makefile
+++ b/mbtk/Makefile
@@ -9,7 +9,7 @@
 DIRS += libql_lib liblynq_lib
 
 # Build bin file.
-DIRS += mbtk_adbd mbtk_rild mbtk_logd mbtk_utils mbtk_utils_linux mbtk_sdk_ready mbtk_gnssd
+DIRS += mbtk_adbd mbtk_rild mbtk_logd mbtk_utils mbtk_utils_linux mbtk_sdk_ready mbtk_gnssd mbtk_servicesd
 
 # Build test file.
 DIRS += test
diff --git a/mbtk/mbtk_servicesd/Makefile b/mbtk/mbtk_servicesd/Makefile
new file mode 100755
index 0000000..359ec7b
--- /dev/null
+++ b/mbtk/mbtk_servicesd/Makefile
@@ -0,0 +1,47 @@
+BUILD_ROOT = $(shell pwd)/..
+include $(BUILD_ROOT)/Make.defines
+
+LOCAL_PATH=$(BUILD_ROOT)/mbtk_servicesd
+
+INC_DIR += \
+		-I$(LOCAL_PATH) \
+		-I$(BUILD_ROOT)/libmbtk_ril
+
+LIB_DIR +=
+
+LIBS += -lmbtk_lib -lmbtk_net -lmbtk_ril -lrilutil -lprop2uci -lmtel -laudio-apu -lcutils -ltinyalsa -lacm -lubus -lubox -lutil
+
+CFLAGS +=
+
+DEFINE +=
+
+#MY_FILES_PATH:=$(LOCAL_PATH)
+#ifeq ($(CONFIG_MBTK_QL_SUPPORT),y)
+#MY_FILES_PATH += $(LOCAL_PATH)/ql
+#endif
+
+#ifeq ($(CONFIG_MBTK_PLATFORM),linux)
+#MY_FILES_PATH += $(LOCAL_PATH)/platform/linux
+#endif
+#LOCAL_SRC_FILES = $(wildcard *.c) $(wildcard *.cpp)
+LOCAL_SRC_FILES = main.c instance_monitor_service.c
+OBJS = $(patsubst %.c, %.o, $(patsubst %.cpp, %.o, $(LOCAL_SRC_FILES)))
+$(info OBJS = $(OBJS))
+
+dtarget := $(OUT_DIR)/bin/mbtk_servicesd
+
+all: $(dtarget)
+
+$(dtarget): $(OBJS)
+	@echo "  BIN     $@"
+	$(CC) $(CFLAGS) $(LIB_DIR) $(LIBS) $(OBJS) -o $@
+
+%.o:%.c
+	$(CC) $(CFLAGS) $(INC_DIR) $(DEFINE) -c $< -o $@
+
+%.o:%.cpp
+	$(CC) $(CFLAGS) $(INC_DIR) $(DEFINE) -c $< -o $@
+
+clean:
+	rm -f $(OBJS) $(dtarget)
+
diff --git a/mbtk/mbtk_servicesd/instance_info.h b/mbtk/mbtk_servicesd/instance_info.h
new file mode 100755
index 0000000..02bedfb
--- /dev/null
+++ b/mbtk/mbtk_servicesd/instance_info.h
@@ -0,0 +1,23 @@
+/*
+* instance_info.h
+*
+* Instance monitor service informations.
+*
+* Author : lb
+* Date   : 2024/6/12 16:56:22
+*/
+#ifndef _INSTANCE_INFO_H
+#define _INSTANCE_INFO_H
+#include "mbtk_type.h"
+
+#define INSTANCE_NUM_MAX 10
+
+typedef struct {
+    char ins_name[64];
+    char ins_cmd[128];
+    int ins_pid;
+} instance_info_t;
+
+
+
+#endif /* _INSTANCE_INFO_H */
diff --git a/mbtk/mbtk_servicesd/instance_monitor_service.c b/mbtk/mbtk_servicesd/instance_monitor_service.c
new file mode 100755
index 0000000..2557304
--- /dev/null
+++ b/mbtk/mbtk_servicesd/instance_monitor_service.c
@@ -0,0 +1,106 @@
+/*
+*    instance_monitor_service.c
+*
+*    $file_describe$
+*
+*/
+/******************************************************************************
+
+                          EDIT HISTORY FOR FILE
+
+  WHEN        WHO       WHAT,WHERE,WHY
+--------    --------    -------------------------------------------------------
+2024/6/12     LiuBin      Initial version
+
+******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <pthread.h>
+
+#include "instance_info.h"
+#include "mbtk_log.h"
+#include "mbtk_utils.h"
+
+#define INS_MONITOR_INTERVAL 3  // s
+
+int instance_info_size = 0;
+instance_info_t instance_infos[INSTANCE_NUM_MAX] = {0};
+
+static void program_start(instance_info_t *info)
+{
+    LOGD("Will start program : %s", info->ins_name);
+    system(info->ins_cmd);
+}
+
+static int pidof(const char *program)
+{
+    char buff[128] = {0};
+    char cmd[128] = {0};
+    snprintf(cmd, sizeof(cmd), "pidof %s", program);
+    if(mbtk_cmd_line(cmd, buff, sizeof(buff)) && strlen(buff)) {
+        int pid = atoi(buff);
+        if(pid > 0) {
+            return pid;
+        } else {
+            return -1;
+        }
+    }
+    return -1;
+}
+
+static void* ins_monitor_service_run(void *arg)
+{
+    int i = 0;
+    LOGD("Will monitor program:");
+    while(i < instance_info_size) {
+        LOGD("%s:%s", instance_infos[i].ins_name, instance_infos[i].ins_cmd);
+        if(instance_infos[i].ins_pid < 0) {
+            instance_infos[i].ins_pid = pidof(instance_infos[i].ins_name);
+        }
+
+        if(instance_infos[i].ins_pid < 0) {
+            LOGE("%s has not running,will not monitor...", instance_infos[i].ins_name);
+        }
+        i++;
+    }
+
+    while(1) {
+        sleep(INS_MONITOR_INTERVAL);
+        i = 0;
+        while(i < instance_info_size) {
+            if(instance_infos[i].ins_pid > 0) {
+                int pid = pidof(instance_infos[i].ins_name);
+                if(pid > 0) {
+                    // Programe is running, do nothing.
+                } else {
+                    program_start(instance_infos + i);
+                }
+            }
+            i++;
+        }
+    }
+
+    return NULL;
+}
+
+int ins_monitor_service_start()
+{
+    pthread_t pid;
+    pthread_attr_t thread_attr;
+    pthread_attr_init(&thread_attr);
+    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+    {
+        LOGE("pthread_attr_setdetachstate() fail.");
+        return -1;
+    }
+
+    if(pthread_create(&pid, &thread_attr, ins_monitor_service_run, NULL))
+    {
+        LOGE("pthread_create() fail.");
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/mbtk/mbtk_servicesd/main.c b/mbtk/mbtk_servicesd/main.c
new file mode 100755
index 0000000..c832588
--- /dev/null
+++ b/mbtk/mbtk_servicesd/main.c
@@ -0,0 +1,108 @@
+/*
+*    main.c
+*
+*    MBTK important service support.
+*
+*/
+/******************************************************************************
+
+                          EDIT HISTORY FOR FILE
+
+  WHEN        WHO       WHAT,WHERE,WHY
+--------    --------    -------------------------------------------------------
+2024/6/12     LiuBin      Initial version
+
+******************************************************************************/
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "mbtk_type.h"
+#include "mbtk_log.h"
+#include "instance_info.h"
+
+#define MBTK_SERVICES_PID_FILE "/var/run/mbtk_servicesd.pid"
+#define MBTK_SERVICES_CONF_FILE "/etc/mbtk_servicesd.conf"
+
+extern int instance_info_size;
+extern instance_info_t instance_infos[];
+int ins_monitor_service_start();
+
+static void config_parse(const char *conf_file)
+{
+    FILE *fptr = fopen(conf_file, "r");
+    if(fptr != NULL) {
+        char line[1024] = {0};
+        bool respawn_process = FALSE;
+        while(fgets(line, sizeof(line), fptr) != NULL && strlen(line) > 0) {
+            if(str_startwith(line, "#")) {
+                memset(line, 0, sizeof(line));
+                continue;
+            }
+
+            if(!respawn_process && str_startwith(line, "respawn_start")) {
+                respawn_process = TRUE;
+                memset(line, 0, sizeof(line));
+                continue;
+            } else if(respawn_process && str_startwith(line, "respawn_end")) {
+                respawn_process = FALSE;
+                memset(line, 0, sizeof(line));
+                continue;
+            }
+
+            char *ptr = line + strlen(line) - 1;
+            while(ptr >= line && (*ptr == '\r' || *ptr == '\n' || *ptr == ' ')) {
+                *ptr-- = '\0';
+            }
+
+            if(ptr < line) { // Empty line
+                memset(line, 0, sizeof(line));
+                continue;
+            }
+
+            if(respawn_process && instance_info_size < INSTANCE_NUM_MAX) {
+                ptr = strstr(line, ":");
+                if(ptr) {
+                    *ptr++ = '\0';
+                    memcpy(instance_infos[instance_info_size].ins_name, line, strlen(line));
+                    memcpy(instance_infos[instance_info_size].ins_cmd, ptr, strlen(ptr));
+                    instance_infos[instance_info_size].ins_pid = -1;
+                    instance_info_size++;
+                }
+            }
+
+            memset(line, 0, sizeof(line));
+        }
+        fclose(fptr);
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    mbtk_log_init("radio", "MBTK_SERVICES");
+
+#ifdef MBTK_DUMP_SUPPORT
+    mbtk_debug_open(NULL, TRUE);
+#endif
+
+    if(app_already_running(MBTK_SERVICES_PID_FILE)) {
+        LOGW("daemon already running.");
+        exit(1);
+    }
+
+    config_parse(MBTK_SERVICES_CONF_FILE);
+
+    // Start services.
+    if(ins_monitor_service_start()) {
+        LOGW("ins_monitor_service_start() fail.");
+    }
+
+    while(1) {
+        sleep(24 * 60 * 60);
+    }
+
+    return 0;
+}
+