[Feature][T108] [task-view-1792] sys ota adapt log interface
Only Configure: No
Affected branch: GSW_V1453
Affected module: sys ota
Self-test: yes
Doc Update: no
Change-Id: Ie15378f373738d76c3d7f6ddcde8be8b8a33f05d
diff --git a/mbtk/libgsw_lib/gsw_sys_interface.c b/mbtk/libgsw_lib/gsw_sys_interface.c
index 21656e3..e674fd6 100755
--- a/mbtk/libgsw_lib/gsw_sys_interface.c
+++ b/mbtk/libgsw_lib/gsw_sys_interface.c
@@ -6,206 +6,95 @@
#include <stddef.h>
#include <dlfcn.h>
#include "gsw_sys_interface.h"
+#include "gsw_log_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;
-}
+
+#define GSW_SYS "[HAL][GSW_SYS]"
int gsw_sys_svr_ftp_start(const char *cfg)
{
int ret = GSW_HAL_SUCCESS;
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)
{
- write_mode_to_file(FTP_STATE_FILE, "init.d");
strcpy(command, "/etc/init.d/vsftpd start");
}
else
{
- write_mode_to_file(FTP_STATE_FILE, "vsftpd");
snprintf(command, sizeof(command), "vsftpd %s &", cfg);
}
ret = system(command);
if(ret != 0) {
- LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
+ LOGE(GSW_SYS, "command execution failed command:%s.\n", command);
goto exit;
}
exit:
- dlclose(handle);
- handle = NULL;
return ret;
}
int gsw_sys_svr_ftp_stop()
{
int ret = GSW_HAL_SUCCESS;
- 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);
+ ret = system("/etc/init.d/vsftpd stop");
if(ret != 0)
{
- LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
- goto exit;
+ 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;
+ }
}
exit:
- dlclose(handle);
- handle = NULL;
return ret;
}
int gsw_sys_svr_ssh_start(const char *cfg)
{
int ret= GSW_HAL_SUCCESS;
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)
{
- write_mode_to_file(SSH_STATE_FILE, "init.d");
strcpy(command, "/etc/init.d/sshd start");
}
else
{
- write_mode_to_file(SSH_STATE_FILE, "sshd");
snprintf(command, sizeof(command), "/usr/sbin/sshd -f %s &", cfg);
}
ret = system(command);
if(ret != 0)
{
- LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
+ LOGE(GSW_SYS, "command execution failed command:%s.\n", command);
goto exit;
}
-exit:
- dlclose(handle);
- handle = NULL;
+exit:
return ret;
}
int gsw_sys_svr_ssh_stop()
{
int ret = GSW_HAL_SUCCESS;
- 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);
+ ret = system("/etc/init.d/sshd stop");
if(ret != 0)
{
- LOGE(fun_ptr_log, "command execution failed command:%s.\n", command);
- goto exit;
+ 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;
+ }
}
-exit:
- dlclose(handle);
- handle = NULL;
+exit:
return ret;
}
int gsw_sys_svr_syslog_restart(const char *log_lvl)
@@ -216,17 +105,6 @@
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++)
@@ -236,7 +114,7 @@
}
if(8 == level)
{
- LOGE(fun_ptr_log, "Parameter error\n");
+ LOGE(GSW_SYS, "Parameter error\n");
ret = GSW_HAL_NORMAL_FAIL;
goto exit;
}
@@ -244,7 +122,7 @@
ret = system(command);
if (ret != 0)
{
- LOGE(fun_ptr_log, "config option not exist.\n");
+ LOGE(GSW_SYS, "config option not exist.\n");
goto exit;
}
snprintf(command, sizeof(command),
@@ -254,12 +132,10 @@
ret = system(command);
if (ret != 0)
{
- LOGE(fun_ptr_log, "command execution failed.\n");
+ LOGE(GSW_SYS, "command execution failed.\n");
goto exit;
}
-exit:
- dlclose(handle);
- handle = NULL;
+exit:
return ret;
}