blob: 21656e3a440faae3708a2a26a4f348fc06f7e2a1 [file] [log] [blame]
hong.liud2417072025-06-27 07:10:37 -07001#include <stdio.h>
2#include <stdlib.h>
hong.liud2417072025-06-27 07:10:37 -07003#include <string.h>
hj.shao07bd13f2025-07-07 03:09:04 -07004#include <unistd.h>
5#include <pthread.h>
6#include <stddef.h>
7#include <dlfcn.h>
hj.shaofe9d0022025-06-11 20:22:48 -07008#include "gsw_sys_interface.h"
hj.shao0bbdedc2025-06-20 04:18:11 -07009#define LOG_LEVLE_CONFIG_FILE "/etc/telinit"
hj.shao07bd13f2025-07-07 03:09:04 -070010#define SSH_STATE_FILE "/tmp/ssh_mode"
11#define FTP_STATE_FILE "/tmp/ftp_mode"
12#define LIB_PATH "/lib/libmbtk_lib.so"
13typedef void (*mbtk_log)(int level, const char *format,...);
14#ifndef LOG_ERR_LEVEL
15#define LOG_ERR_LEVEL 3 /* error conditions */
16#endif
17#define LOGE(fun_ptr_log, fmt, args...) \
18 do{ \
19 char *file_ptr_1001 = __FILE__; \
20 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
21 char line_1001[10] = {0}; \
22 sprintf(line_1001, "%d", __LINE__); \
23 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
24 if(*ptr_1001 == '/') \
25 break; \
26 ptr_1001--; \
27 } \
28 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: [gsw_sys]" fmt, ptr_1001 + 1, line_1001, ##args); \
29 } while(0)
30void write_mode_to_file(const char *file ,const char *mode)
31{
32 FILE *fp = fopen(file, "w");
33 if (!fp)
34 {
35 perror("Failed to open state file for writing");
36 return;
37 }
38 fprintf(fp, "%s\n", mode);
39 fclose(fp);
40}
41int read_mode_from_file(const char *file, char *mode, size_t size)
42{
43 FILE *fp = fopen(file, "r");
44 if (!fp)
45 {
46 return GSW_HAL_NORMAL_FAIL; // No mode recorded
47 }
48 if (!fgets(mode, size, fp))
49 {
50 fclose(fp);
51 return GSW_HAL_NORMAL_FAIL;
52 }
53 mode[strcspn(mode, "\n")] = '\0';
54 fclose(fp);
55 return GSW_HAL_SUCCESS;
56}
hj.shaofe9d0022025-06-11 20:22:48 -070057int gsw_sys_svr_ftp_start(const char *cfg)
58{
59 int ret = GSW_HAL_SUCCESS;
hj.shao07bd13f2025-07-07 03:09:04 -070060 char command[128];
61 mbtk_log fun_ptr_log;
62 void *handle = dlopen(LIB_PATH, RTLD_NOW);
63 if(NULL == handle)
64 return GSW_HAL_NORMAL_FAIL;
65
66 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
67 if(fun_ptr_log == NULL)
68 {
69 ret = GSW_HAL_NORMAL_FAIL;
70 goto exit;
71 }
72
hj.shao0bbdedc2025-06-20 04:18:11 -070073 if(NULL == cfg)
74 {
hj.shao07bd13f2025-07-07 03:09:04 -070075 write_mode_to_file(FTP_STATE_FILE, "init.d");
76 strcpy(command, "/etc/init.d/vsftpd start");
hj.shao0bbdedc2025-06-20 04:18:11 -070077 }
78 else
79 {
hj.shao07bd13f2025-07-07 03:09:04 -070080 write_mode_to_file(FTP_STATE_FILE, "vsftpd");
81 snprintf(command, sizeof(command), "vsftpd %s &", cfg);
hj.shao0bbdedc2025-06-20 04:18:11 -070082 }
hj.shao07bd13f2025-07-07 03:09:04 -070083 ret = system(command);
hj.shao0bbdedc2025-06-20 04:18:11 -070084 if(ret != 0) {
hj.shao07bd13f2025-07-07 03:09:04 -070085 LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
86 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -070087 }
hj.shao07bd13f2025-07-07 03:09:04 -070088exit:
89 dlclose(handle);
90 handle = NULL;
91 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -070092}
hj.shaofe9d0022025-06-11 20:22:48 -070093int gsw_sys_svr_ftp_stop()
94{
95 int ret = GSW_HAL_SUCCESS;
hj.shao07bd13f2025-07-07 03:09:04 -070096 char command[64];
97 char mode[64];
98 mbtk_log fun_ptr_log;
99 void *handle = dlopen(LIB_PATH, RTLD_NOW);
100 if(NULL == handle)
101 return GSW_HAL_NORMAL_FAIL;
102
103 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
104 if(fun_ptr_log == NULL)
105 {
106 ret = GSW_HAL_NORMAL_FAIL;
107 goto exit;
108 }
109
110 ret = read_mode_from_file(FTP_STATE_FILE, mode, sizeof(mode));
111 if(0 != ret)
112 {
113 LOGE(fun_ptr_log, "read file:%s error\n", FTP_STATE_FILE);
114 goto exit;
115 }
116 if(strlen(mode) == 0 || strcmp("init.d", mode) == 0) //strlen(mode)==0为第一次执行
117 strcpy(command, "/etc/init.d/vsftpd stop");
118 else
119 strcpy(command, "killall -TERM vsftpd");
hj.shao0bbdedc2025-06-20 04:18:11 -0700120
121 ret = system(command);
122 if(ret != 0)
123 {
hj.shao07bd13f2025-07-07 03:09:04 -0700124 LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
125 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700126 }
127
hj.shao07bd13f2025-07-07 03:09:04 -0700128exit:
129 dlclose(handle);
130 handle = NULL;
131 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -0700132}
hj.shaofe9d0022025-06-11 20:22:48 -0700133int gsw_sys_svr_ssh_start(const char *cfg)
134{
135 int ret= GSW_HAL_SUCCESS;
hj.shao07bd13f2025-07-07 03:09:04 -0700136 char command[128];
137 mbtk_log fun_ptr_log;
138 void *handle = dlopen(LIB_PATH, RTLD_NOW);
139 if(NULL == handle)
140 return GSW_HAL_NORMAL_FAIL;
hj.shao0bbdedc2025-06-20 04:18:11 -0700141
hj.shao07bd13f2025-07-07 03:09:04 -0700142 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
143 if(fun_ptr_log == NULL)
144 {
145 ret = GSW_HAL_NORMAL_FAIL;
146 goto exit;
147 }
148
hj.shao0bbdedc2025-06-20 04:18:11 -0700149 if(NULL == cfg)
hj.shao07bd13f2025-07-07 03:09:04 -0700150 {
151 write_mode_to_file(SSH_STATE_FILE, "init.d");
152 strcpy(command, "/etc/init.d/sshd start");
hj.shao0bbdedc2025-06-20 04:18:11 -0700153 }
154 else
155 {
hj.shao07bd13f2025-07-07 03:09:04 -0700156 write_mode_to_file(SSH_STATE_FILE, "sshd");
157 snprintf(command, sizeof(command), "/usr/sbin/sshd -f %s &", cfg);
hj.shao0bbdedc2025-06-20 04:18:11 -0700158 }
159
hj.shao07bd13f2025-07-07 03:09:04 -0700160 ret = system(command);
hj.shaofe9d0022025-06-11 20:22:48 -0700161 if(ret != 0)
hj.shao0bbdedc2025-06-20 04:18:11 -0700162 {
hj.shao07bd13f2025-07-07 03:09:04 -0700163 LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
164 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700165 }
hj.shao07bd13f2025-07-07 03:09:04 -0700166
167exit:
168 dlclose(handle);
169 handle = NULL;
170 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -0700171}
hj.shaofe9d0022025-06-11 20:22:48 -0700172int gsw_sys_svr_ssh_stop()
173{
174 int ret = GSW_HAL_SUCCESS;
hj.shao07bd13f2025-07-07 03:09:04 -0700175 char command[64];
176 char mode[64];
177 mbtk_log fun_ptr_log;
178 void *handle = dlopen(LIB_PATH, RTLD_NOW);
179 if(NULL == handle)
180 return GSW_HAL_NORMAL_FAIL;
hj.shao0bbdedc2025-06-20 04:18:11 -0700181
hj.shao07bd13f2025-07-07 03:09:04 -0700182 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
183 if(fun_ptr_log == NULL)
184 {
185 ret = GSW_HAL_NORMAL_FAIL;
186 goto exit;
187 }
188
189 ret = read_mode_from_file(SSH_STATE_FILE, mode, sizeof(mode));
190 if(0 != ret)
191 {
192 LOGE(fun_ptr_log, "read file:%s error\n", SSH_STATE_FILE);
193 goto exit;
194 }
195 if(strlen(mode) == 0 || strcmp("init.d", mode) == 0) //strlen(mode)==0为第一次执行
196 strcpy(command, "/etc/init.d/sshd stop");
197 else
198 strcpy(command, "killall sshd");
hj.shao0bbdedc2025-06-20 04:18:11 -0700199 ret = system(command);
hj.shaofe9d0022025-06-11 20:22:48 -0700200 if(ret != 0)
hj.shao0bbdedc2025-06-20 04:18:11 -0700201 {
hj.shao07bd13f2025-07-07 03:09:04 -0700202 LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
203 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700204 }
hj.shaofe9d0022025-06-11 20:22:48 -0700205
hj.shao07bd13f2025-07-07 03:09:04 -0700206exit:
207 dlclose(handle);
208 handle = NULL;
209 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -0700210}
hj.shaofe9d0022025-06-11 20:22:48 -0700211int gsw_sys_svr_syslog_restart(const char *log_lvl)
212{
213 (void)log_lvl;
hj.shao07bd13f2025-07-07 03:09:04 -0700214 int ret = GSW_HAL_SUCCESS;
hj.shao0bbdedc2025-06-20 04:18:11 -0700215 char command[256] = {0};
hj.shaoc85a46f2025-06-23 23:59:51 -0700216 int level = 0;
217 char *log_level_params[8] = {"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"};
218 char *log_level_values[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
hj.shao07bd13f2025-07-07 03:09:04 -0700219 mbtk_log fun_ptr_log;
220 void *handle = dlopen(LIB_PATH, RTLD_NOW);
221 if(NULL == handle)
222 return GSW_HAL_NORMAL_FAIL;
hj.shao0bbdedc2025-06-20 04:18:11 -0700223
hj.shao07bd13f2025-07-07 03:09:04 -0700224 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
225 if(fun_ptr_log == NULL)
226 {
227 ret = GSW_HAL_NORMAL_FAIL;
228 goto exit;
229 }
hj.shao0bbdedc2025-06-20 04:18:11 -0700230 snprintf(command, sizeof(command),
231 "grep -q 'setprop sys.default.loglevel' %s", LOG_LEVLE_CONFIG_FILE);
hj.shaoc85a46f2025-06-23 23:59:51 -0700232 for(level = 0; level < 8; level++)
233 {
234 if(0 == strcmp(log_lvl, log_level_params[level]))
235 break;
236 }
hj.shaoc85a46f2025-06-23 23:59:51 -0700237 if(8 == level)
238 {
hj.shao07bd13f2025-07-07 03:09:04 -0700239 LOGE(fun_ptr_log, "Parameter error\n");
240 ret = GSW_HAL_NORMAL_FAIL;
241 goto exit;
hj.shaoc85a46f2025-06-23 23:59:51 -0700242 }
243
hj.shao07bd13f2025-07-07 03:09:04 -0700244 ret = system(command);
245 if (ret != 0)
246 {
247 LOGE(fun_ptr_log, "config option not exist.\n");
248 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700249 }
hj.shaoc85a46f2025-06-23 23:59:51 -0700250 snprintf(command, sizeof(command),
251 "sed -i 's/\\(setprop sys.default.loglevel \\)\\S*/\\1%s/' %s",
252 log_level_values[level], LOG_LEVLE_CONFIG_FILE);
253
hj.shao07bd13f2025-07-07 03:09:04 -0700254 ret = system(command);
255 if (ret != 0)
256 {
257 LOGE(fun_ptr_log, "command execution failed.\n");
258 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700259 }
hj.shao07bd13f2025-07-07 03:09:04 -0700260exit:
261 dlclose(handle);
262 handle = NULL;
263 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -0700264}
hj.shao07bd13f2025-07-07 03:09:04 -0700265