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