b.liu | 717dc08 | 2024-06-20 10:51:49 +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> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
| 21 | |
| 22 | #include "mbtk_type.h" |
| 23 | #include "mbtk_log.h" |
| 24 | #include "instance_info.h" |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 25 | #include "mbtk_str.h" |
| 26 | #include "mbtk_utils.h" |
b.liu | 717dc08 | 2024-06-20 10:51:49 +0800 | [diff] [blame] | 27 | |
| 28 | #define MBTK_SERVICES_PID_FILE "/var/run/mbtk_servicesd.pid" |
| 29 | #define MBTK_SERVICES_CONF_FILE "/etc/mbtk_servicesd.conf" |
| 30 | |
| 31 | extern int instance_info_size; |
| 32 | extern instance_info_t instance_infos[]; |
| 33 | int ins_monitor_service_start(); |
| 34 | |
| 35 | static void config_parse(const char *conf_file) |
| 36 | { |
| 37 | FILE *fptr = fopen(conf_file, "r"); |
| 38 | if(fptr != NULL) { |
| 39 | char line[1024] = {0}; |
| 40 | bool respawn_process = FALSE; |
| 41 | while(fgets(line, sizeof(line), fptr) != NULL && strlen(line) > 0) { |
| 42 | if(str_startwith(line, "#")) { |
| 43 | memset(line, 0, sizeof(line)); |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | if(!respawn_process && str_startwith(line, "respawn_start")) { |
| 48 | respawn_process = TRUE; |
| 49 | memset(line, 0, sizeof(line)); |
| 50 | continue; |
| 51 | } else if(respawn_process && str_startwith(line, "respawn_end")) { |
| 52 | respawn_process = FALSE; |
| 53 | memset(line, 0, sizeof(line)); |
| 54 | continue; |
| 55 | } |
| 56 | |
| 57 | char *ptr = line + strlen(line) - 1; |
| 58 | while(ptr >= line && (*ptr == '\r' || *ptr == '\n' || *ptr == ' ')) { |
| 59 | *ptr-- = '\0'; |
| 60 | } |
| 61 | |
| 62 | if(ptr < line) { // Empty line |
| 63 | memset(line, 0, sizeof(line)); |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if(respawn_process && instance_info_size < INSTANCE_NUM_MAX) { |
| 68 | ptr = strstr(line, ":"); |
| 69 | if(ptr) { |
| 70 | *ptr++ = '\0'; |
| 71 | memcpy(instance_infos[instance_info_size].ins_name, line, strlen(line)); |
| 72 | memcpy(instance_infos[instance_info_size].ins_cmd, ptr, strlen(ptr)); |
| 73 | instance_infos[instance_info_size].ins_pid = -1; |
| 74 | instance_info_size++; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | memset(line, 0, sizeof(line)); |
| 79 | } |
| 80 | fclose(fptr); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char *argv[]) |
| 85 | { |
| 86 | mbtk_log_init("radio", "MBTK_SERVICES"); |
| 87 | |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame] | 88 | MBTK_SOURCE_INFO_PRINT("mbtk_services"); |
| 89 | |
b.liu | 717dc08 | 2024-06-20 10:51:49 +0800 | [diff] [blame] | 90 | #ifdef MBTK_DUMP_SUPPORT |
| 91 | mbtk_debug_open(NULL, TRUE); |
| 92 | #endif |
| 93 | |
| 94 | if(app_already_running(MBTK_SERVICES_PID_FILE)) { |
| 95 | LOGW("daemon already running."); |
| 96 | exit(1); |
| 97 | } |
| 98 | |
| 99 | config_parse(MBTK_SERVICES_CONF_FILE); |
| 100 | |
| 101 | // Start services. |
| 102 | if(ins_monitor_service_start()) { |
| 103 | LOGW("ins_monitor_service_start() fail."); |
| 104 | } |
| 105 | |
| 106 | while(1) { |
| 107 | sleep(24 * 60 * 60); |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |