hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 3 | #include <string.h> |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <pthread.h> |
| 6 | #include <stddef.h> |
| 7 | #include <dlfcn.h> |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> |
| 9 | #include <arpa/inet.h> |
| 10 | #include <netinet/in.h> |
| 11 | #include <sys/time.h> |
| 12 | |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 13 | #include "gsw_sys_interface.h" |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 14 | #include "gsw_log_interface.h" |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 15 | #define LOG_LEVLE_CONFIG_FILE "/etc/telinit" |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 16 | |
| 17 | #define GSW_SYS "[HAL][GSW_SYS]" |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 18 | |
| 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 | |
| 25 | static 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.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 51 | int gsw_sys_svr_ftp_start(const char *cfg) |
| 52 | { |
| 53 | int ret = GSW_HAL_SUCCESS; |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 54 | char command[128]; |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 55 | |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 56 | 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.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 63 | if(NULL == cfg) |
| 64 | { |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 65 | strcpy(command, "/etc/init.d/vsftpd start"); |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 69 | snprintf(command, sizeof(command), "vsftpd %s &", cfg); |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 70 | } |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 71 | ret = system(command); |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 72 | if(ret != 0) { |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 73 | LOGE(GSW_SYS, "command execution failed command:%s.\n", command); |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 74 | goto exit; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 75 | } |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 76 | exit: |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 77 | return ret; |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 78 | } |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 79 | int gsw_sys_svr_ftp_stop() |
| 80 | { |
| 81 | int ret = GSW_HAL_SUCCESS; |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 82 | |
| 83 | if(check_local_port(FTP_PORT)) |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 84 | { |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 85 | LOGI(GSW_SYS, "ftp service is not running"); |
| 86 | return GSW_HAL_SUCCESS; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 87 | } |
| 88 | |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 89 | 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.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 96 | } |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 97 | int gsw_sys_svr_ssh_start(const char *cfg) |
| 98 | { |
| 99 | int ret= GSW_HAL_SUCCESS; |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 100 | char command[128]; |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 101 | |
| 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.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 108 | if(NULL == cfg) |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 109 | { |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 110 | strcpy(command, "/etc/init.d/sshd start"); |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 114 | snprintf(command, sizeof(command), "/usr/sbin/sshd -f %s &", cfg); |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 115 | } |
| 116 | |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 117 | ret = system(command); |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 118 | if(ret != 0) |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 119 | { |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 120 | LOGE(GSW_SYS, "command execution failed command:%s.\n", command); |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 121 | goto exit; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 122 | } |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 123 | |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 124 | exit: |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 125 | return ret; |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 126 | } |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 127 | int gsw_sys_svr_ssh_stop() |
| 128 | { |
| 129 | int ret = GSW_HAL_SUCCESS; |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 130 | |
| 131 | if(check_local_port(SSH_PORT)) |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 132 | { |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 133 | LOGI(GSW_SYS, "ssh service is not running"); |
| 134 | return GSW_HAL_SUCCESS; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 135 | } |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 136 | |
hj.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 137 | 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.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 144 | } |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 145 | int gsw_sys_svr_syslog_restart(const char *log_lvl) |
| 146 | { |
| 147 | (void)log_lvl; |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 148 | int ret = GSW_HAL_SUCCESS; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 149 | char command[256] = {0}; |
hj.shao | c85a46f | 2025-06-23 23:59:51 -0700 | [diff] [blame] | 150 | 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.shao | adfa3b6 | 2025-07-28 19:10:08 -0700 | [diff] [blame] | 153 | |
| 154 | |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 155 | snprintf(command, sizeof(command), |
| 156 | "grep -q 'setprop sys.default.loglevel' %s", LOG_LEVLE_CONFIG_FILE); |
hj.shao | c85a46f | 2025-06-23 23:59:51 -0700 | [diff] [blame] | 157 | for(level = 0; level < 8; level++) |
| 158 | { |
| 159 | if(0 == strcmp(log_lvl, log_level_params[level])) |
| 160 | break; |
| 161 | } |
hj.shao | c85a46f | 2025-06-23 23:59:51 -0700 | [diff] [blame] | 162 | if(8 == level) |
| 163 | { |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 164 | LOGE(GSW_SYS, "Parameter error\n"); |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 165 | ret = GSW_HAL_NORMAL_FAIL; |
| 166 | goto exit; |
hj.shao | c85a46f | 2025-06-23 23:59:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 169 | ret = system(command); |
| 170 | if (ret != 0) |
| 171 | { |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 172 | LOGE(GSW_SYS, "config option not exist.\n"); |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 173 | goto exit; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 174 | } |
hj.shao | c85a46f | 2025-06-23 23:59:51 -0700 | [diff] [blame] | 175 | 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.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 179 | ret = system(command); |
| 180 | if (ret != 0) |
| 181 | { |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 182 | LOGE(GSW_SYS, "command execution failed.\n"); |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 183 | goto exit; |
hj.shao | 0bbdedc | 2025-06-20 04:18:11 -0700 | [diff] [blame] | 184 | } |
hj.shao | 4a9a505 | 2025-07-19 03:56:43 -0700 | [diff] [blame] | 185 | exit: |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 186 | return ret; |
hj.shao | fe9d002 | 2025-06-11 20:22:48 -0700 | [diff] [blame] | 187 | } |
hj.shao | 07bd13f | 2025-07-07 03:09:04 -0700 | [diff] [blame] | 188 | |