Add mbtk_servicesd

Change-Id: Ie2dcb3420468035f2a2089ac967ab3c1ccb3bf1a
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;
+}
+