b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1 | /* |
| 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 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/6/12 LiuBin Initial version |
| 14 | |
| 15 | ******************************************************************************/ |
| 16 | #include <stdio.h> |
b.liu | deb8e42 | 2024-12-14 17:36:56 +0800 | [diff] [blame] | 17 | #include <stdint.h> |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 18 | #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.liu | 62240ee | 2024-11-07 17:52:45 +0800 | [diff] [blame] | 26 | #include "mbtk_str.h" |
| 27 | #include "mbtk_utils.h" |
r.xiao | ab6f4ec | 2024-12-19 03:42:17 -0800 | [diff] [blame] | 28 | #include "mbtk_led.h" |
yq.wang | 6c876f5 | 2025-05-17 16:56:50 +0800 | [diff] [blame^] | 29 | #include "reboot_reason.h" |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 30 | |
| 31 | #define MBTK_SERVICES_PID_FILE "/var/run/mbtk_servicesd.pid" |
| 32 | #define MBTK_SERVICES_CONF_FILE "/etc/mbtk_servicesd.conf" |
| 33 | |
| 34 | extern int instance_info_size; |
| 35 | extern instance_info_t instance_infos[]; |
| 36 | int ins_monitor_service_start(); |
| 37 | |
| 38 | static void config_parse(const char *conf_file) |
| 39 | { |
| 40 | FILE *fptr = fopen(conf_file, "r"); |
| 41 | if(fptr != NULL) { |
| 42 | char line[1024] = {0}; |
| 43 | bool respawn_process = FALSE; |
| 44 | while(fgets(line, sizeof(line), fptr) != NULL && strlen(line) > 0) { |
| 45 | if(str_startwith(line, "#")) { |
| 46 | memset(line, 0, sizeof(line)); |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | if(!respawn_process && str_startwith(line, "respawn_start")) { |
| 51 | respawn_process = TRUE; |
| 52 | memset(line, 0, sizeof(line)); |
| 53 | continue; |
| 54 | } else if(respawn_process && str_startwith(line, "respawn_end")) { |
| 55 | respawn_process = FALSE; |
| 56 | memset(line, 0, sizeof(line)); |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | char *ptr = line + strlen(line) - 1; |
| 61 | while(ptr >= line && (*ptr == '\r' || *ptr == '\n' || *ptr == ' ')) { |
| 62 | *ptr-- = '\0'; |
| 63 | } |
| 64 | |
| 65 | if(ptr < line) { // Empty line |
| 66 | memset(line, 0, sizeof(line)); |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | if(respawn_process && instance_info_size < INSTANCE_NUM_MAX) { |
| 71 | ptr = strstr(line, ":"); |
| 72 | if(ptr) { |
| 73 | *ptr++ = '\0'; |
| 74 | memcpy(instance_infos[instance_info_size].ins_name, line, strlen(line)); |
| 75 | memcpy(instance_infos[instance_info_size].ins_cmd, ptr, strlen(ptr)); |
| 76 | instance_infos[instance_info_size].ins_pid = -1; |
| 77 | instance_info_size++; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | memset(line, 0, sizeof(line)); |
| 82 | } |
| 83 | fclose(fptr); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | int main(int argc, char *argv[]) |
| 88 | { |
| 89 | mbtk_log_init("radio", "MBTK_SERVICES"); |
| 90 | |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 91 | MBTK_SOURCE_INFO_PRINT("mbtk_services"); |
| 92 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 93 | #ifdef MBTK_DUMP_SUPPORT |
| 94 | mbtk_debug_open(NULL, TRUE); |
| 95 | #endif |
| 96 | |
| 97 | if(app_already_running(MBTK_SERVICES_PID_FILE)) { |
| 98 | LOGW("daemon already running."); |
| 99 | exit(1); |
| 100 | } |
| 101 | |
| 102 | config_parse(MBTK_SERVICES_CONF_FILE); |
| 103 | |
| 104 | // Start services. |
| 105 | if(ins_monitor_service_start()) { |
| 106 | LOGW("ins_monitor_service_start() fail."); |
| 107 | } |
| 108 | |
yq.wang | 6c876f5 | 2025-05-17 16:56:50 +0800 | [diff] [blame^] | 109 | reboot_reason_init(); |
| 110 | |
r.xiao | ab6f4ec | 2024-12-19 03:42:17 -0800 | [diff] [blame] | 111 | #ifdef MBTK_LED |
| 112 | //led services |
| 113 | if(mbtk_led_init()) |
| 114 | { |
| 115 | LOGW("LED_service_start() fail."); |
| 116 | } |
| 117 | |
| 118 | if(mbtk_led_contril_server_init()) |
| 119 | { |
| 120 | LOGW("LED_contril_service_start() fail."); |
| 121 | } |
| 122 | #endif |
| 123 | |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 124 | while(1) { |
| 125 | sleep(24 * 60 * 60); |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |