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