| #include <stdio.h> |
| #include <stdlib.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]; |
| 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); |
| 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); |
| if(ret != 0) |
| { |
| LOGE(fun_ptr_log, "command execution failed command:%s.\n", command); |
| 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); |
| goto exit; |
| } |
| |
| exit: |
| dlclose(handle); |
| handle = NULL; |
| 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); |
| if(ret != 0) |
| { |
| LOGE(fun_ptr_log, "command execution failed command:%s.\n", command); |
| goto exit; |
| } |
| |
| 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) |
| { |
| LOGE(fun_ptr_log, "Parameter error\n"); |
| ret = GSW_HAL_NORMAL_FAIL; |
| goto exit; |
| } |
| |
| 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); |
| |
| ret = system(command); |
| if (ret != 0) |
| { |
| LOGE(fun_ptr_log, "command execution failed.\n"); |
| goto exit; |
| } |
| exit: |
| dlclose(handle); |
| handle = NULL; |
| return ret; |
| } |
| |