blob: 016583574e56fd859ae23fbce5580c30e560ce36 [file] [log] [blame]
b.liu87afc4c2024-08-14 17:33:45 +08001/*
2* main.c
3*
4* MBTK important service support.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/6/12 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
b.liudeb8e422024-12-14 17:36:56 +080017#include <stdint.h>
b.liu87afc4c2024-08-14 17:33:45 +080018#include <stdlib.h>
19#include <unistd.h>
20#include <errno.h>
21#include <fcntl.h>
22
23#include "mbtk_type.h"
24#include "mbtk_log.h"
25#include "instance_info.h"
b.liu62240ee2024-11-07 17:52:45 +080026#include "mbtk_str.h"
27#include "mbtk_utils.h"
b.liu87afc4c2024-08-14 17:33:45 +080028
29#define MBTK_SERVICES_PID_FILE "/var/run/mbtk_servicesd.pid"
30#define MBTK_SERVICES_CONF_FILE "/etc/mbtk_servicesd.conf"
31
32extern int instance_info_size;
33extern instance_info_t instance_infos[];
34int ins_monitor_service_start();
35
36static void config_parse(const char *conf_file)
37{
38 FILE *fptr = fopen(conf_file, "r");
39 if(fptr != NULL) {
40 char line[1024] = {0};
41 bool respawn_process = FALSE;
42 while(fgets(line, sizeof(line), fptr) != NULL && strlen(line) > 0) {
43 if(str_startwith(line, "#")) {
44 memset(line, 0, sizeof(line));
45 continue;
46 }
47
48 if(!respawn_process && str_startwith(line, "respawn_start")) {
49 respawn_process = TRUE;
50 memset(line, 0, sizeof(line));
51 continue;
52 } else if(respawn_process && str_startwith(line, "respawn_end")) {
53 respawn_process = FALSE;
54 memset(line, 0, sizeof(line));
55 continue;
56 }
57
58 char *ptr = line + strlen(line) - 1;
59 while(ptr >= line && (*ptr == '\r' || *ptr == '\n' || *ptr == ' ')) {
60 *ptr-- = '\0';
61 }
62
63 if(ptr < line) { // Empty line
64 memset(line, 0, sizeof(line));
65 continue;
66 }
67
68 if(respawn_process && instance_info_size < INSTANCE_NUM_MAX) {
69 ptr = strstr(line, ":");
70 if(ptr) {
71 *ptr++ = '\0';
72 memcpy(instance_infos[instance_info_size].ins_name, line, strlen(line));
73 memcpy(instance_infos[instance_info_size].ins_cmd, ptr, strlen(ptr));
74 instance_infos[instance_info_size].ins_pid = -1;
75 instance_info_size++;
76 }
77 }
78
79 memset(line, 0, sizeof(line));
80 }
81 fclose(fptr);
82 }
83}
84
85int main(int argc, char *argv[])
86{
87 mbtk_log_init("radio", "MBTK_SERVICES");
88
b.liubcf86c92024-08-19 19:48:28 +080089 MBTK_SOURCE_INFO_PRINT("mbtk_services");
90
b.liu87afc4c2024-08-14 17:33:45 +080091#ifdef MBTK_DUMP_SUPPORT
92 mbtk_debug_open(NULL, TRUE);
93#endif
94
95 if(app_already_running(MBTK_SERVICES_PID_FILE)) {
96 LOGW("daemon already running.");
97 exit(1);
98 }
99
100 config_parse(MBTK_SERVICES_CONF_FILE);
101
102 // Start services.
103 if(ins_monitor_service_start()) {
104 LOGW("ins_monitor_service_start() fail.");
105 }
106
107 while(1) {
108 sleep(24 * 60 * 60);
109 }
110
111 return 0;
112}
113