hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <pthread.h>
#include <paths.h>
#include <sys/wait.h>
 
#include "slog.h"
#include "utility.h"
 
#define DEBUG 1
 
static int system_fd_closexec(const char* command)
{
   int wait_val = 0;
   pid_t pid = -1;
 
   if (!command)
       return 1;
 
   if ((pid = vfork()) < 0)
       return -1;
 
   if (pid == 0) {
       int i = 0;
       int stdin_fd = fileno(stdin);
       int stdout_fd = fileno(stdout);
       int stderr_fd = fileno(stderr);
       long sc_open_max = sysconf(_SC_OPEN_MAX);
       if (sc_open_max < 0) {
           sc_open_max = 20000; /* enough? */
       }
       /* close all descriptors in child sysconf(_SC_OPEN_MAX) */
       for (i = 0; i < sc_open_max; i++) {
           if (i == stdin_fd || i == stdout_fd || i == stderr_fd)
               continue;
           close(i);
       }
 
       execl(_PATH_BSHELL, "sh", "-c", command, (char*)0);
       _exit(127);
   }
 
   while (waitpid(pid, &wait_val, 0) < 0) {
       if (errno != EINTR) {
           wait_val = -1;
           break;
       }
   }
 
   return wait_val;
}
 
int exec_command_system(const char *cmd)
{
   pid_t status;
 
   status = system_fd_closexec(cmd);
 
   if (-1 == status) {
       pr_err("[system_exec_err] -1\n");
       return -1;
   } else {
       if (WIFEXITED(status)) {
           if (0 == WEXITSTATUS(status)) {
               return 0;
           } else {
               pr_err("[system_exec_err %s] -2\n", cmd);
               return -2;
           }
       } else {
           pr_err("[system_exec_err] -3\n");
           return -3;
       }
   }
 
   return 0;
}
 
void exec_command(char cmdline[], char recv_buff[], int len)
{
   if (DEBUG) pr_info("[BT_DEBUG] execute: %s\n", cmdline);
 
   FILE *stream = NULL;
   char *tmp_buff = recv_buff;
 
   memset(recv_buff, 0, len);
 
   if ((stream = popen(cmdline, "r")) != NULL) {
       while (fgets(tmp_buff, len, stream)) {
           //pr_info("tmp_buf[%d]: %s\n", strlen(tmp_buff), tmp_buff);
           tmp_buff += strlen(tmp_buff);
           len -= strlen(tmp_buff);
           if (len <= 1)
               break;
       }
 
       if (DEBUG) pr_info("[BT_DEBUG] execute_r: %s \n", recv_buff);
       pclose(stream);
   } else
       pr_err("[popen] error: %s\n", cmdline);
}
 
int test_pthread(pthread_t tid) /*pthread_kill的返回值:成功(0) 线程不存在(ESRCH) 信号不合法(EINVAL)*/
{
   int pthread_kill_err;
   pthread_kill_err = pthread_kill(tid, 0);
 
   if(pthread_kill_err == ESRCH)
       pr_info("ID 0x%x NOT EXIST OR EXIT\n", (unsigned int)tid);
   else if(pthread_kill_err == EINVAL)
       pr_info("SIGNAL ILL\n");
   else
       pr_info("ID 0x%x ALIVE\n", (unsigned int)tid);
 
   return pthread_kill_err;
}
 
int get_ps_pid(const char Name[])
{
   int len;
   char name[32] = {0};
   char cmdresult[256] = {0};
   char cmd[64] = {0};
   FILE *pFile = NULL;
   int  pid = 0;
   int retry_cnt = 3;
 
retry:
   memset(name, 0, 32);
   memset(cmdresult, 0, 256);
   memset(cmd, 0, 64);
 
   len = strlen(Name);
   strncpy(name,Name,len);
   name[31] ='\0';
 
   sprintf(cmd, "pidof %s", name);
 
   pFile = popen(cmd, "r");
   if (pFile != NULL)  {
       while (fgets(cmdresult, sizeof(cmdresult), pFile)) {
           pid = atoi(cmdresult);
           break;
       }
       pclose(pFile);
   }
 
   if ((pid == 0) && (retry_cnt--))
       goto retry;
 
   return pid;
}
 
int kill_task(char *name)
{
   char cmd[128] = {0};
   int exec_cnt = 3, retry_cnt = 10;
 
   if (!get_ps_pid(name))
       return 0;
 
   memset(cmd, 0, 128);
   sprintf(cmd, "killall %s", name);
 
   exec_command_system(cmd);
 
   if (get_ps_pid(name))
       msleep(600);
 
   if (get_ps_pid(name)) {
       memset(cmd, 0, 128);
       sprintf(cmd, "killall -9 %s", name);
       exec_command_system(cmd);
       msleep(300);
   }
 
   if (get_ps_pid(name)) {
       pr_info("%s: kill %s failure [%d]\n", __func__, name, get_ps_pid(name));
       return -1;
   } else {
       pr_info("%s: kill %s successful\n", __func__, name);
       return 0;
   }
}
 
int run_task(char *name, char *cmd)
{
   int exec_cnt = 3, retry_cnt = 6;
 
   while(exec_cnt) {
       if(!exec_command_system(cmd))
           break;
       exec_cnt--;
   }
 
   if(exec_cnt <= 0) {
       pr_info("%s: run %s failed\n", __func__, name);
       return -1;
   }
   msleep(100);
 
retry:
   if (!get_ps_pid(name) && (retry_cnt--)) {
       msleep(100);
       goto retry;
   }
 
   if (!get_ps_pid(name))
       return -1;
   else
       return 0;
}