b.liu | 717dc08 | 2024-06-20 10:51:49 +0800 | [diff] [blame] | 1 | /* |
| 2 | * instance_monitor_service.c |
| 3 | * |
| 4 | * $file_describe$ |
| 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 <pthread.h> |
| 21 | |
| 22 | #include "instance_info.h" |
| 23 | #include "mbtk_log.h" |
| 24 | #include "mbtk_utils.h" |
| 25 | |
| 26 | #define INS_MONITOR_INTERVAL 3 // s |
| 27 | |
| 28 | int instance_info_size = 0; |
| 29 | instance_info_t instance_infos[INSTANCE_NUM_MAX] = {0}; |
| 30 | |
| 31 | static void program_start(instance_info_t *info) |
| 32 | { |
| 33 | LOGD("Will start program : %s", info->ins_name); |
| 34 | system(info->ins_cmd); |
| 35 | } |
| 36 | |
| 37 | static int pidof(const char *program) |
| 38 | { |
| 39 | char buff[128] = {0}; |
| 40 | char cmd[128] = {0}; |
| 41 | snprintf(cmd, sizeof(cmd), "pidof %s", program); |
| 42 | if(mbtk_cmd_line(cmd, buff, sizeof(buff)) && strlen(buff)) { |
| 43 | int pid = atoi(buff); |
| 44 | if(pid > 0) { |
| 45 | return pid; |
| 46 | } else { |
| 47 | return -1; |
| 48 | } |
| 49 | } |
| 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | static void* ins_monitor_service_run(void *arg) |
| 54 | { |
| 55 | int i = 0; |
| 56 | LOGD("Will monitor program:"); |
| 57 | while(i < instance_info_size) { |
| 58 | LOGD("%s:%s", instance_infos[i].ins_name, instance_infos[i].ins_cmd); |
| 59 | if(instance_infos[i].ins_pid < 0) { |
| 60 | instance_infos[i].ins_pid = pidof(instance_infos[i].ins_name); |
| 61 | } |
| 62 | |
| 63 | if(instance_infos[i].ins_pid < 0) { |
| 64 | LOGE("%s has not running,will not monitor...", instance_infos[i].ins_name); |
| 65 | } |
| 66 | i++; |
| 67 | } |
| 68 | |
| 69 | while(1) { |
| 70 | sleep(INS_MONITOR_INTERVAL); |
| 71 | i = 0; |
| 72 | while(i < instance_info_size) { |
| 73 | if(instance_infos[i].ins_pid > 0) { |
| 74 | int pid = pidof(instance_infos[i].ins_name); |
| 75 | if(pid > 0) { |
| 76 | // Programe is running, do nothing. |
| 77 | } else { |
| 78 | program_start(instance_infos + i); |
| 79 | } |
| 80 | } |
| 81 | i++; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | int ins_monitor_service_start() |
| 89 | { |
| 90 | pthread_t pid; |
| 91 | pthread_attr_t thread_attr; |
| 92 | pthread_attr_init(&thread_attr); |
| 93 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 94 | { |
| 95 | LOGE("pthread_attr_setdetachstate() fail."); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | if(pthread_create(&pid, &thread_attr, ins_monitor_service_run, NULL)) |
| 100 | { |
| 101 | LOGE("pthread_create() fail."); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |