[bugfix][T108] [bug-1666 bug-1667] solve ssh and ftp bug
Only Configure: No
Affected branch: GSW_V1453
Affected module: log
Self-test: yes
Doc Update: no
Change-Id: I56e8ecf4ee9e7b4ecf629e7e4e2bc0cd50b0f31f
diff --git a/mbtk/libgsw_lib/gsw_sys_interface.c b/mbtk/libgsw_lib/gsw_sys_interface.c
index 27050c6..21656e3 100755
--- a/mbtk/libgsw_lib/gsw_sys_interface.c
+++ b/mbtk/libgsw_lib/gsw_sys_interface.c
@@ -1,129 +1,265 @@
#include <stdio.h>
#include <stdlib.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <fcntl.h>
#include <string.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <stddef.h>
+#include <dlfcn.h>
#include "gsw_sys_interface.h"
-
#define LOG_LEVLE_CONFIG_FILE "/etc/telinit"
-
+#define SSH_STATE_FILE "/tmp/ssh_mode"
+#define FTP_STATE_FILE "/tmp/ftp_mode"
+#define LIB_PATH "/lib/libmbtk_lib.so"
+typedef void (*mbtk_log)(int level, const char *format,...);
+#ifndef LOG_ERR_LEVEL
+#define LOG_ERR_LEVEL 3 /* error conditions */
+#endif
+#define LOGE(fun_ptr_log, fmt, args...) \
+ do{ \
+ char *file_ptr_1001 = __FILE__; \
+ char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
+ char line_1001[10] = {0}; \
+ sprintf(line_1001, "%d", __LINE__); \
+ while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
+ if(*ptr_1001 == '/') \
+ break; \
+ ptr_1001--; \
+ } \
+ fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: [gsw_sys]" fmt, ptr_1001 + 1, line_1001, ##args); \
+ } while(0)
+void write_mode_to_file(const char *file ,const char *mode)
+{
+ FILE *fp = fopen(file, "w");
+ if (!fp)
+ {
+ perror("Failed to open state file for writing");
+ return;
+ }
+ fprintf(fp, "%s\n", mode);
+ fclose(fp);
+}
+int read_mode_from_file(const char *file, char *mode, size_t size)
+{
+ FILE *fp = fopen(file, "r");
+ if (!fp)
+ {
+ return GSW_HAL_NORMAL_FAIL; // No mode recorded
+ }
+ if (!fgets(mode, size, fp))
+ {
+ fclose(fp);
+ return GSW_HAL_NORMAL_FAIL;
+ }
+ mode[strcspn(mode, "\n")] = '\0';
+ fclose(fp);
+ return GSW_HAL_SUCCESS;
+}
int gsw_sys_svr_ftp_start(const char *cfg)
{
int ret = GSW_HAL_SUCCESS;
- char command[128] = "vsftpd &";
+ char command[128];
+ mbtk_log fun_ptr_log;
+ void *handle = dlopen(LIB_PATH, RTLD_NOW);
+ if(NULL == handle)
+ return GSW_HAL_NORMAL_FAIL;
+
+ fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
+ if(fun_ptr_log == NULL)
+ {
+ ret = GSW_HAL_NORMAL_FAIL;
+ goto exit;
+ }
+
if(NULL == cfg)
{
- ret = system(command);
+ write_mode_to_file(FTP_STATE_FILE, "init.d");
+ strcpy(command, "/etc/init.d/vsftpd start");
}
else
{
- sprintf(command, "vsftpd %s &", cfg);
- ret = system(command);
+ write_mode_to_file(FTP_STATE_FILE, "vsftpd");
+ snprintf(command, sizeof(command), "vsftpd %s &", cfg);
}
-
+ ret = system(command);
if(ret != 0) {
- printf("command execution failed command:%s.\n", command);
- return GSW_HAL_NORMAL_FAIL;
+ LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
+ goto exit;
}
-
- return GSW_HAL_SUCCESS;
+exit:
+ dlclose(handle);
+ handle = NULL;
+ return ret;
}
-
int gsw_sys_svr_ftp_stop()
{
int ret = GSW_HAL_SUCCESS;
- char command[] = "killall -TERM vsftpd";
+ char command[64];
+ char mode[64];
+ mbtk_log fun_ptr_log;
+ void *handle = dlopen(LIB_PATH, RTLD_NOW);
+ if(NULL == handle)
+ return GSW_HAL_NORMAL_FAIL;
+
+ fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
+ if(fun_ptr_log == NULL)
+ {
+ ret = GSW_HAL_NORMAL_FAIL;
+ goto exit;
+ }
+
+ ret = read_mode_from_file(FTP_STATE_FILE, mode, sizeof(mode));
+ if(0 != ret)
+ {
+ LOGE(fun_ptr_log, "read file:%s error\n", FTP_STATE_FILE);
+ goto exit;
+ }
+ if(strlen(mode) == 0 || strcmp("init.d", mode) == 0) //strlen(mode)==0为第一次执行
+ strcpy(command, "/etc/init.d/vsftpd stop");
+ else
+ strcpy(command, "killall -TERM vsftpd");
ret = system(command);
if(ret != 0)
{
- printf("command execution failed command:%s.\n", command);
- return GSW_HAL_NORMAL_FAIL;
+ LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
+ goto exit;
}
- return GSW_HAL_SUCCESS;
-
+exit:
+ dlclose(handle);
+ handle = NULL;
+ return ret;
}
-
int gsw_sys_svr_ssh_start(const char *cfg)
{
int ret= GSW_HAL_SUCCESS;
- char command[128] = "/usr/sbin/sshd &";
+ char command[128];
+ mbtk_log fun_ptr_log;
+ void *handle = dlopen(LIB_PATH, RTLD_NOW);
+ if(NULL == handle)
+ return GSW_HAL_NORMAL_FAIL;
+ fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
+ if(fun_ptr_log == NULL)
+ {
+ ret = GSW_HAL_NORMAL_FAIL;
+ goto exit;
+ }
+
if(NULL == cfg)
- {
- ret = system(command);
+ {
+ write_mode_to_file(SSH_STATE_FILE, "init.d");
+ strcpy(command, "/etc/init.d/sshd start");
}
else
{
- sprintf(command, "/usr/sbin/sshd %s &", cfg);
- ret = system(command);
+ write_mode_to_file(SSH_STATE_FILE, "sshd");
+ snprintf(command, sizeof(command), "/usr/sbin/sshd -f %s &", cfg);
}
+ ret = system(command);
if(ret != 0)
{
- printf("command execution failed command:%s.\n", command);
- return GSW_HAL_NORMAL_FAIL;
-
+ LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
+ goto exit;
}
- return GSW_HAL_SUCCESS;
+
+exit:
+ dlclose(handle);
+ handle = NULL;
+ return ret;
}
-
int gsw_sys_svr_ssh_stop()
{
int ret = GSW_HAL_SUCCESS;
- char command[] = "killall sshd";
+ char command[64];
+ char mode[64];
+ mbtk_log fun_ptr_log;
+ void *handle = dlopen(LIB_PATH, RTLD_NOW);
+ if(NULL == handle)
+ return GSW_HAL_NORMAL_FAIL;
+ fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
+ if(fun_ptr_log == NULL)
+ {
+ ret = GSW_HAL_NORMAL_FAIL;
+ goto exit;
+ }
+
+ ret = read_mode_from_file(SSH_STATE_FILE, mode, sizeof(mode));
+ if(0 != ret)
+ {
+ LOGE(fun_ptr_log, "read file:%s error\n", SSH_STATE_FILE);
+ goto exit;
+ }
+ if(strlen(mode) == 0 || strcmp("init.d", mode) == 0) //strlen(mode)==0为第一次执行
+ strcpy(command, "/etc/init.d/sshd stop");
+ else
+ strcpy(command, "killall sshd");
ret = system(command);
if(ret != 0)
{
- printf("command execution failed command:%s.\n", command);
- return GSW_HAL_NORMAL_FAIL;
+ LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
+ goto exit;
}
- return GSW_HAL_SUCCESS;
+exit:
+ dlclose(handle);
+ handle = NULL;
+ return ret;
}
-
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"};
+ mbtk_log fun_ptr_log;
+ void *handle = dlopen(LIB_PATH, RTLD_NOW);
+ if(NULL == handle)
+ return GSW_HAL_NORMAL_FAIL;
+ fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
+ if(fun_ptr_log == NULL)
+ {
+ ret = GSW_HAL_NORMAL_FAIL;
+ goto exit;
+ }
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)
{
- printf("Parameter error\n");
- return -1;
+ LOGE(fun_ptr_log, "Parameter error\n");
+ ret = GSW_HAL_NORMAL_FAIL;
+ goto exit;
}
- int exists = system(command);
- if (exists != 0) {
- printf("config option not exist.\n");
- return GSW_HAL_NORMAL_FAIL;
+ ret = system(command);
+ if (ret != 0)
+ {
+ LOGE(fun_ptr_log, "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);
- int result = system(command);
- if (result != 0) {
- printf("command execution failed.\n");
- return GSW_HAL_SUCCESS;
+ ret = system(command);
+ if (ret != 0)
+ {
+ LOGE(fun_ptr_log, "command execution failed.\n");
+ goto exit;
}
-
-
- return GSW_HAL_NORMAL_FAIL;
+exit:
+ dlclose(handle);
+ handle = NULL;
+ return ret;
}
+