blob: 31b76035366fee7a378ab40f68d585becfde7936 [file] [log] [blame]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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");
}
else
{
snprintf(command, sizeof(command), "vsftpd %s &", cfg);
}
ret = system(command);
if(ret != 0) {
LOGE(GSW_SYS, "command execution failed command:%s.\n", command);
goto exit;
}
exit:
return ret;
}
int gsw_sys_svr_ftp_stop()
{
int ret = GSW_HAL_SUCCESS;
if(check_local_port(FTP_PORT))
{
LOGI(GSW_SYS, "ftp service is not running");
return GSW_HAL_SUCCESS;
}
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");
}
else
{
snprintf(command, sizeof(command), "/usr/sbin/sshd -f %s &", cfg);
}
ret = system(command);
if(ret != 0)
{
LOGE(GSW_SYS, "command execution failed command:%s.\n", command);
goto exit;
}
exit:
return ret;
}
int gsw_sys_svr_ssh_stop()
{
int ret = GSW_HAL_SUCCESS;
if(check_local_port(SSH_PORT))
{
LOGI(GSW_SYS, "ssh service is not running");
return GSW_HAL_SUCCESS;
}
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)
{
(void)log_lvl;
int ret = GSW_HAL_SUCCESS;
char command[256] = {0};
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++)
{
if(0 == strcmp(log_lvl, log_level_params[level]))
break;
}
if(8 == level)
{
LOGE(GSW_SYS, "Parameter error\n");
ret = GSW_HAL_NORMAL_FAIL;
goto exit;
}
ret = system(command);
if (ret != 0)
{
LOGE(GSW_SYS, "config option not exist.\n");
goto exit;
}
snprintf(command, sizeof(command),
"sed -i 's/\\(setprop sys.default.loglevel \\)\\S*/\\1%s/' %s",
log_level_values[level], LOG_LEVLE_CONFIG_FILE);
ret = system(command);
if (ret != 0)
{
LOGE(GSW_SYS, "command execution failed.\n");
goto exit;
}
exit:
return ret;
}