[bugfix][T108][bug-view-1666] solve problem of ssh and ftp open/close
Only Configure: No
Affected branch: GSW_V1453
Affected module: sys
Self-test: yes
Doc Update: no
Change-Id: Ia679692e0adfc69354a5d25af82a0a8b8e1d0417
diff --git a/mbtk/libgsw_lib/gsw_sys_interface.c b/mbtk/libgsw_lib/gsw_sys_interface.c
index e674fd6..31b7603 100755
--- a/mbtk/libgsw_lib/gsw_sys_interface.c
+++ b/mbtk/libgsw_lib/gsw_sys_interface.c
@@ -5,16 +5,61 @@
#include <pthread.h>
#include <stddef.h>
#include <dlfcn.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/time.h>
+
#include "gsw_sys_interface.h"
#include "gsw_log_interface.h"
#define LOG_LEVLE_CONFIG_FILE "/etc/telinit"
#define GSW_SYS "[HAL][GSW_SYS]"
+
+#define SERVER_IP "127.0.0.1"
+#define TIMEOUT_SEC 3
+
+#define SSH_PORT 22
+#define FTP_PORT 21
+
+static int check_local_port(int port)
+{
+ int sockfd;
+ struct sockaddr_in server_addr;
+ struct timeval timeout;
+
+ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
+ return 1;
+ }
+
+ timeout.tv_sec = TIMEOUT_SEC;
+ timeout.tv_usec = 0;
+ setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
+ setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
+
+ memset(&server_addr, 0, sizeof(server_addr));
+ server_addr.sin_family = AF_INET;
+ server_addr.sin_port = htons(port);
+ inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
+
+ int ret = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
+ close(sockfd);
+
+ return ret == 0 ? 0 : 1;
+}
+
int gsw_sys_svr_ftp_start(const char *cfg)
{
int ret = GSW_HAL_SUCCESS;
char command[128];
+ if(check_local_port(FTP_PORT) == 0)
+ {
+
+ LOGI(GSW_SYS, "ftp service is running");
+ gsw_sys_svr_ftp_stop();
+ }
+
if(NULL == cfg)
{
strcpy(command, "/etc/init.d/vsftpd start");
@@ -34,29 +79,32 @@
int gsw_sys_svr_ftp_stop()
{
int ret = GSW_HAL_SUCCESS;
-
- ret = system("/etc/init.d/vsftpd stop");
- if(ret != 0)
+
+ if(check_local_port(FTP_PORT))
{
- LOGE(GSW_SYS, "command /etc/init.d/vsftpd stop execution failed\n");
- printf("FUNC:[%s] %d ret:%d\n", __FUNCTION__, __LINE__, ret);
- ret = system("killall -TERM vsftpd");
- if(ret != 0)
- {
- printf("FUNC:[%s] %d ret:%d\n", __FUNCTION__, __LINE__, ret);
- LOGE(GSW_SYS, "command killall -TERM vsftpd execution failed\n");
- goto exit;
- }
+ LOGI(GSW_SYS, "ftp service is not running");
+ return GSW_HAL_SUCCESS;
}
-exit:
- return ret;
+ ret = system("/etc/init.d/vsftpd stop 2>/dev/null");
+ LOGI(GSW_SYS, "execute command etc/init.d/vsftpd stop 2>/dev/null ret:%d", ret);
+
+ ret = system("killall -TERM vsftpd 2>/dev/null");
+ LOGI(GSW_SYS, "execute command killall -TERM vsftpd 2>/dev/null ret:%d", ret);
+
+ return GSW_HAL_SUCCESS;
}
int gsw_sys_svr_ssh_start(const char *cfg)
{
int ret= GSW_HAL_SUCCESS;
char command[128];
-
+
+ if(check_local_port(SSH_PORT) == 0)
+ {
+ LOGI(GSW_SYS, "ssh service is running");
+ gsw_sys_svr_ssh_stop();
+ }
+
if(NULL == cfg)
{
strcpy(command, "/etc/init.d/sshd start");
@@ -79,23 +127,20 @@
int gsw_sys_svr_ssh_stop()
{
int ret = GSW_HAL_SUCCESS;
-
- ret = system("/etc/init.d/sshd stop");
- if(ret != 0)
+
+ if(check_local_port(SSH_PORT))
{
- printf("FUNC:[%s] %d ret:%d\n", __FUNCTION__, __LINE__, ret);
- LOGE(GSW_SYS, "command /etc/init.d/sshd stop sshd execution failed\n");
- ret = system("killall sshd");
- if(ret != 0)
- {
- printf("FUNC:[%s] %d ret:%d\n", __FUNCTION__, __LINE__, ret);
- LOGE(GSW_SYS, "command killall sshd execution failed\n");
- goto exit;
- }
+ LOGI(GSW_SYS, "ssh service is not running");
+ return GSW_HAL_SUCCESS;
}
-exit:
- return ret;
+ ret = system("/etc/init.d/sshd stop 2>/dev/null");
+ LOGI(GSW_SYS, "execute command /etc/init.d/sshd stop 2>/dev/null ret:%d", ret);
+
+ ret = system("killall sshd 2>/dev/null");
+ LOGI(GSW_SYS, "execute command killall sshd 2>/dev/null ret:%d", ret);
+
+ return GSW_HAL_SUCCESS;
}
int gsw_sys_svr_syslog_restart(const char *log_lvl)
{
@@ -105,6 +150,8 @@
int level = 0;
char *log_level_params[8] = {"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"};
char *log_level_values[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
+
+
snprintf(command, sizeof(command),
"grep -q 'setprop sys.default.loglevel' %s", LOG_LEVLE_CONFIG_FILE);
for(level = 0; level < 8; level++)