blob: 31b76035366fee7a378ab40f68d585becfde7936 [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.shaoadfa3b62025-07-28 19:10:08 -07008#include <sys/socket.h>
9#include <arpa/inet.h>
10#include <netinet/in.h>
11#include <sys/time.h>
12
hj.shaofe9d0022025-06-11 20:22:48 -070013#include "gsw_sys_interface.h"
hj.shao4a9a5052025-07-19 03:56:43 -070014#include "gsw_log_interface.h"
hj.shao0bbdedc2025-06-20 04:18:11 -070015#define LOG_LEVLE_CONFIG_FILE "/etc/telinit"
hj.shao4a9a5052025-07-19 03:56:43 -070016
17#define GSW_SYS "[HAL][GSW_SYS]"
hj.shaoadfa3b62025-07-28 19:10:08 -070018
19#define SERVER_IP "127.0.0.1"
20#define TIMEOUT_SEC 3
21
22#define SSH_PORT 22
23#define FTP_PORT 21
24
25static int check_local_port(int port)
26{
27 int sockfd;
28 struct sockaddr_in server_addr;
29 struct timeval timeout;
30
31 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
32 return 1;
33 }
34
35 timeout.tv_sec = TIMEOUT_SEC;
36 timeout.tv_usec = 0;
37 setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
38 setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
39
40 memset(&server_addr, 0, sizeof(server_addr));
41 server_addr.sin_family = AF_INET;
42 server_addr.sin_port = htons(port);
43 inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
44
45 int ret = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
46 close(sockfd);
47
48 return ret == 0 ? 0 : 1;
49}
50
hj.shaofe9d0022025-06-11 20:22:48 -070051int gsw_sys_svr_ftp_start(const char *cfg)
52{
53 int ret = GSW_HAL_SUCCESS;
hj.shao07bd13f2025-07-07 03:09:04 -070054 char command[128];
hj.shao4a9a5052025-07-19 03:56:43 -070055
hj.shaoadfa3b62025-07-28 19:10:08 -070056 if(check_local_port(FTP_PORT) == 0)
57 {
58
59 LOGI(GSW_SYS, "ftp service is running");
60 gsw_sys_svr_ftp_stop();
61 }
62
hj.shao0bbdedc2025-06-20 04:18:11 -070063 if(NULL == cfg)
64 {
hj.shao07bd13f2025-07-07 03:09:04 -070065 strcpy(command, "/etc/init.d/vsftpd start");
hj.shao0bbdedc2025-06-20 04:18:11 -070066 }
67 else
68 {
hj.shao07bd13f2025-07-07 03:09:04 -070069 snprintf(command, sizeof(command), "vsftpd %s &", cfg);
hj.shao0bbdedc2025-06-20 04:18:11 -070070 }
hj.shao07bd13f2025-07-07 03:09:04 -070071 ret = system(command);
hj.shao0bbdedc2025-06-20 04:18:11 -070072 if(ret != 0) {
hj.shao4a9a5052025-07-19 03:56:43 -070073 LOGE(GSW_SYS, "command execution failed command:%s.\n", command);
hj.shao07bd13f2025-07-07 03:09:04 -070074 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -070075 }
hj.shao07bd13f2025-07-07 03:09:04 -070076exit:
hj.shao07bd13f2025-07-07 03:09:04 -070077 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -070078}
hj.shaofe9d0022025-06-11 20:22:48 -070079int gsw_sys_svr_ftp_stop()
80{
81 int ret = GSW_HAL_SUCCESS;
hj.shaoadfa3b62025-07-28 19:10:08 -070082
83 if(check_local_port(FTP_PORT))
hj.shao0bbdedc2025-06-20 04:18:11 -070084 {
hj.shaoadfa3b62025-07-28 19:10:08 -070085 LOGI(GSW_SYS, "ftp service is not running");
86 return GSW_HAL_SUCCESS;
hj.shao0bbdedc2025-06-20 04:18:11 -070087 }
88
hj.shaoadfa3b62025-07-28 19:10:08 -070089 ret = system("/etc/init.d/vsftpd stop 2>/dev/null");
90 LOGI(GSW_SYS, "execute command etc/init.d/vsftpd stop 2>/dev/null ret:%d", ret);
91
92 ret = system("killall -TERM vsftpd 2>/dev/null");
93 LOGI(GSW_SYS, "execute command killall -TERM vsftpd 2>/dev/null ret:%d", ret);
94
95 return GSW_HAL_SUCCESS;
hj.shaofe9d0022025-06-11 20:22:48 -070096}
hj.shaofe9d0022025-06-11 20:22:48 -070097int gsw_sys_svr_ssh_start(const char *cfg)
98{
99 int ret= GSW_HAL_SUCCESS;
hj.shao07bd13f2025-07-07 03:09:04 -0700100 char command[128];
hj.shaoadfa3b62025-07-28 19:10:08 -0700101
102 if(check_local_port(SSH_PORT) == 0)
103 {
104 LOGI(GSW_SYS, "ssh service is running");
105 gsw_sys_svr_ssh_stop();
106 }
107
hj.shao0bbdedc2025-06-20 04:18:11 -0700108 if(NULL == cfg)
hj.shao07bd13f2025-07-07 03:09:04 -0700109 {
hj.shao07bd13f2025-07-07 03:09:04 -0700110 strcpy(command, "/etc/init.d/sshd start");
hj.shao0bbdedc2025-06-20 04:18:11 -0700111 }
112 else
113 {
hj.shao07bd13f2025-07-07 03:09:04 -0700114 snprintf(command, sizeof(command), "/usr/sbin/sshd -f %s &", cfg);
hj.shao0bbdedc2025-06-20 04:18:11 -0700115 }
116
hj.shao07bd13f2025-07-07 03:09:04 -0700117 ret = system(command);
hj.shaofe9d0022025-06-11 20:22:48 -0700118 if(ret != 0)
hj.shao0bbdedc2025-06-20 04:18:11 -0700119 {
hj.shao4a9a5052025-07-19 03:56:43 -0700120 LOGE(GSW_SYS, "command execution failed command:%s.\n", command);
hj.shao07bd13f2025-07-07 03:09:04 -0700121 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700122 }
hj.shao07bd13f2025-07-07 03:09:04 -0700123
hj.shao4a9a5052025-07-19 03:56:43 -0700124exit:
hj.shao07bd13f2025-07-07 03:09:04 -0700125 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -0700126}
hj.shaofe9d0022025-06-11 20:22:48 -0700127int gsw_sys_svr_ssh_stop()
128{
129 int ret = GSW_HAL_SUCCESS;
hj.shaoadfa3b62025-07-28 19:10:08 -0700130
131 if(check_local_port(SSH_PORT))
hj.shao0bbdedc2025-06-20 04:18:11 -0700132 {
hj.shaoadfa3b62025-07-28 19:10:08 -0700133 LOGI(GSW_SYS, "ssh service is not running");
134 return GSW_HAL_SUCCESS;
hj.shao0bbdedc2025-06-20 04:18:11 -0700135 }
hj.shaofe9d0022025-06-11 20:22:48 -0700136
hj.shaoadfa3b62025-07-28 19:10:08 -0700137 ret = system("/etc/init.d/sshd stop 2>/dev/null");
138 LOGI(GSW_SYS, "execute command /etc/init.d/sshd stop 2>/dev/null ret:%d", ret);
139
140 ret = system("killall sshd 2>/dev/null");
141 LOGI(GSW_SYS, "execute command killall sshd 2>/dev/null ret:%d", ret);
142
143 return GSW_HAL_SUCCESS;
hj.shaofe9d0022025-06-11 20:22:48 -0700144}
hj.shaofe9d0022025-06-11 20:22:48 -0700145int gsw_sys_svr_syslog_restart(const char *log_lvl)
146{
147 (void)log_lvl;
hj.shao07bd13f2025-07-07 03:09:04 -0700148 int ret = GSW_HAL_SUCCESS;
hj.shao0bbdedc2025-06-20 04:18:11 -0700149 char command[256] = {0};
hj.shaoc85a46f2025-06-23 23:59:51 -0700150 int level = 0;
151 char *log_level_params[8] = {"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"};
152 char *log_level_values[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
hj.shaoadfa3b62025-07-28 19:10:08 -0700153
154
hj.shao0bbdedc2025-06-20 04:18:11 -0700155 snprintf(command, sizeof(command),
156 "grep -q 'setprop sys.default.loglevel' %s", LOG_LEVLE_CONFIG_FILE);
hj.shaoc85a46f2025-06-23 23:59:51 -0700157 for(level = 0; level < 8; level++)
158 {
159 if(0 == strcmp(log_lvl, log_level_params[level]))
160 break;
161 }
hj.shaoc85a46f2025-06-23 23:59:51 -0700162 if(8 == level)
163 {
hj.shao4a9a5052025-07-19 03:56:43 -0700164 LOGE(GSW_SYS, "Parameter error\n");
hj.shao07bd13f2025-07-07 03:09:04 -0700165 ret = GSW_HAL_NORMAL_FAIL;
166 goto exit;
hj.shaoc85a46f2025-06-23 23:59:51 -0700167 }
168
hj.shao07bd13f2025-07-07 03:09:04 -0700169 ret = system(command);
170 if (ret != 0)
171 {
hj.shao4a9a5052025-07-19 03:56:43 -0700172 LOGE(GSW_SYS, "config option not exist.\n");
hj.shao07bd13f2025-07-07 03:09:04 -0700173 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700174 }
hj.shaoc85a46f2025-06-23 23:59:51 -0700175 snprintf(command, sizeof(command),
176 "sed -i 's/\\(setprop sys.default.loglevel \\)\\S*/\\1%s/' %s",
177 log_level_values[level], LOG_LEVLE_CONFIG_FILE);
178
hj.shao07bd13f2025-07-07 03:09:04 -0700179 ret = system(command);
180 if (ret != 0)
181 {
hj.shao4a9a5052025-07-19 03:56:43 -0700182 LOGE(GSW_SYS, "command execution failed.\n");
hj.shao07bd13f2025-07-07 03:09:04 -0700183 goto exit;
hj.shao0bbdedc2025-06-20 04:18:11 -0700184 }
hj.shao4a9a5052025-07-19 03:56:43 -0700185exit:
hj.shao07bd13f2025-07-07 03:09:04 -0700186 return ret;
hj.shaofe9d0022025-06-11 20:22:48 -0700187}
hj.shao07bd13f2025-07-07 03:09:04 -0700188