From dcb27dcd9329d965f52dd35defc0f3c508b5e4b8 Mon Sep 17 00:00:00 2001 From: "shingil.kang" Date: Tue, 29 Jul 2014 00:31:24 +0900 Subject: [PATCH] SDB: refined messages Change-Id: Ie00fe14e34215f44e28df233a971b9bc298676d0 Signed-off-by: shingil.kang --- Makefile | 3 +- src/auto_complete.c | 24 ++++++--- src/command_function.c | 45 ++++++++++------- src/commandline.c | 17 ++++--- src/fdevent.c | 3 +- src/file_sync_client.c | 9 ++-- src/file_sync_functions.c | 92 ++++++++++++++++++++-------------- src/listener.c | 4 +- src/log.c | 68 ++++++++++++++++++++----- src/log.h | 22 +++++--- src/sdb.c | 4 +- src/sdb_client.c | 34 +++++++------ src/sdb_constants.c | 35 +++---------- src/sdb_constants.h | 30 ++--------- src/sdb_messages.c | 100 ++++++++++++++++++++++++++++++++++++ src/sdb_messages.h | 103 ++++++++++++++++++++++++++++++++++++++ src/sdb_model.c | 8 +-- src/sockets.c | 31 ++++++------ src/transport.c | 11 ++-- src/utils.c | 3 +- src/utils_unix.c | 21 ++++---- 21 files changed, 468 insertions(+), 199 deletions(-) create mode 100644 src/sdb_messages.c create mode 100644 src/sdb_messages.h diff --git a/Makefile b/Makefile index 1b26243..e7fad81 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,8 @@ SDB_SRC_FILES := \ src/command_function.c \ src/log.c \ src/listener.c \ - src/sdb_map.c + src/sdb_map.c \ + src/sdb_messages.c SDB_CFLAGS := -O2 -g -Wall -Wno-unused-parameter SDB_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE diff --git a/src/auto_complete.c b/src/auto_complete.c index 86e184b..2ec8813 100644 --- a/src/auto_complete.c +++ b/src/auto_complete.c @@ -35,6 +35,7 @@ #include "auto_complete.h" #include "file_sync_service.h" #include "log.h" +#include "sdb_messages.h" static int parse_opt(int argc, char** argv); static int parse_cmd(int argc, char** argv); @@ -184,12 +185,17 @@ static struct ac_element ac_devices= { .func = no_parse }; +static struct ac_element ac_forward_list= { + .keyword = "forward-list", + .func = no_parse +}; + static const AC_ELEMENT* pre_options[] = {&emulator_short, &emulator_long, &device_short, &device_long, &serial_short, &serial_long}; static const int pre_options_size = GET_ARRAY_SIZE(pre_options, AC_ELEMENT*); static const AC_ELEMENT* commands[] = {&ac_root, &ac_swindow , &ac_gserial, &ac_gstate, &ac_kserver, &ac_sserver, &ac_version, &ac_help, &ac_forward, &ac_uninstall, &ac_install, &ac_dlog, &ac_shell, &ac_pull, &ac_push, &ac_disconnect, - &ac_connect, &ac_devices}; + &ac_connect, &ac_devices, &ac_forward_list}; static const int cmds_size = GET_ARRAY_SIZE(commands, AC_ELEMENT*); static int initialize_ac(int complete) { @@ -198,7 +204,8 @@ static int initialize_ac(int complete) { int ac_stdout_fd = dup(STDOUT_FILENO); if(ac_stdout_fd < 0) { - fprintf(stderr, "error: exception happend while duplicating stdout '%s'\n", strerror(errno)); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_INITIALIZE_ENV_FAIL, F(ERR_GENERAL_DUPLICATE_FAIL, "stdout")); + LOG_ERROR(strerror(errno)); return -1; } @@ -209,7 +216,8 @@ static int initialize_ac(int complete) { int ac_stderr_fd = dup(STDOUT_FILENO); if(ac_stderr_fd < 0) { - fprintf(stderr, "error: exception happend while duplicating stdout '%s'\n", strerror(errno)); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_INITIALIZE_ENV_FAIL, F(ERR_GENERAL_DUPLICATE_FAIL, "stdout")); + LOG_ERROR(strerror(errno)); return -1; } @@ -222,19 +230,23 @@ static int initialize_ac(int complete) { if(null_fd < 0) { sdb_close(null_fd); - fprintf(stderr, "error: exception happend while opening /dev/null '%s'\n", strerror(errno)); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_INITIALIZE_ENV_FAIL, F(ERR_SYNC_OPEN_FAIL, "/dev/null")); + LOG_ERROR(strerror(errno)); return -1; } if(dup2(null_fd, STDOUT_FILENO) < 0){ sdb_close(null_fd); - fprintf(stderr, "error: exception happend while duplicating /dev/null to the stdout '%s'\n", strerror(errno)); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_INITIALIZE_ENV_FAIL, F(ERR_GENERAL_DUPLICATE_FAIL, "/dev/null")); + LOG_ERROR(strerror(errno)); return -1; } if(dup2(null_fd, STDERR_FILENO) < 0){ sdb_close(null_fd); fprintf(stderr, "error: exception happend while duplicating /dev/null to the stderr '%s'\n", strerror(errno)); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_INITIALIZE_ENV_FAIL, F(ERR_GENERAL_DUPLICATE_FAIL, "/dev/null")); + LOG_ERROR(strerror(errno)); return -1; } @@ -533,7 +545,7 @@ static void print_local_dirlist(char* src_dir, char** not_complete_char) { // get file stat if(stat(src_full_path, &statbuf) == -1) { - fprintf(stderr, "error: exception occurred while getting file stat: %s\n", src_full_path); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_STAT_FAIL, src_full_path), strerror(errno)); goto finalize; } diff --git a/src/command_function.c b/src/command_function.c index 6958855..abd3c57 100644 --- a/src/command_function.c +++ b/src/command_function.c @@ -42,6 +42,7 @@ #include "log.h" #include "sdb.h" +#include "sdb_messages.h" static const char *SDK_TOOL_PATH="/home/developer/sdk_tools"; static const char *APP_PATH_PREFIX="/opt/apps"; @@ -94,7 +95,7 @@ int launch(int argc, char ** argv) { char flag = 0; if (argc < 7 || argc > 15 ) { - fprintf(stderr,"usage: sdb launch -p -e -m [-P ] [-attach ] [-t ] []\n"); + print_info("sdb launch -p -e -m [-P ] [-attach ] [-t ] []"); return -1; } @@ -151,11 +152,11 @@ int launch(int argc, char ** argv) { } else if (!strcmp(argv[i], "debug")) { mode = 1; } else if (!strcmp(argv[i], "da") || !strcmp(argv[i], "oprofile")) { - fprintf(stderr,"The -m option for da and oprofile is supported in sdbd higher than 2.2.0\n"); + print_error(SDB_MESSAGE_ERROR, ERR_LAUNCH_M_OPTION_SUPPORT, NULL); return -1; } else { - fprintf(stderr,"The -m option accepts arguments only run or debug options\n"); + print_error(SDB_MESSAGE_ERROR, ERR_LAUNCH_M_OPTION_ARGUMENT, NULL); return -1; } flag = 0; @@ -163,7 +164,7 @@ int launch(int argc, char ** argv) { } case 'P': { if (mode != 1) { - fprintf(stderr,"The -P option should be used in debug mode\n"); + print_error(SDB_MESSAGE_ERROR, ERR_LAUNCH_P_OPTION_DEBUG_MODE, NULL); return -1; } port = atoi(argv[i]); @@ -172,7 +173,7 @@ int launch(int argc, char ** argv) { } case 'a': { if (mode != 1) { - fprintf(stderr, "The -attach option should be used in debug mode\n"); + print_error(SDB_MESSAGE_ERROR, ERR_LAUNCH_P_OPTION_DEBUG_MODE, NULL); return -1; } pid = atoi(argv[i]); @@ -223,8 +224,9 @@ int launch(int argc, char ** argv) { if (verify_gdbserver_exist() < 0) { return -1; } - if (!port) { - fprintf(stderr,"The port number is not valid\n"); + if (port <= 0 || port > 65535) { + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_LAUNCH_APP_FAIL, F(ERR_GENERAL_INVALID_PORT, port)); + return -1; } if (pid) { @@ -251,6 +253,7 @@ int launch(int argc, char ** argv) { } int devices(int argc, char ** argv) { + char *tmp; char full_cmd[PATH_MAX]; @@ -386,7 +389,7 @@ int status_window(int argc, char ** argv) { } printf("%c[2J%c[2H", 27, 27); - printf("Samsung Development Bridge\n"); + printf("Smart Development Bridge\n"); printf("State: %s\n", state ? state : "offline"); fflush(stdout); } @@ -397,8 +400,8 @@ int status_window(int argc, char ** argv) { int kill_server(int argc, char ** argv) { int fd; fd = _sdb_connect("host:kill"); - if(fd == -1) { - fprintf(stderr,"* server not running *\n"); + if(fd == -2) { + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_KILL_SERVER_FAIL, ERR_GENERAL_SERVER_NOT_RUN); return 1; } return 0; @@ -595,7 +598,7 @@ int forkserver(int argc, char** argv) { return r; } else { - fprintf(stderr,COMMANDLINE_ERROR_ARG_MISSING, "server", "forkserver"); + print_error(SDB_MESSAGE_ERROR, F(ERR_COMMAND_MISSING_ARGUMENT, "fork-server"), NULL); return 1; } } @@ -618,7 +621,7 @@ int install(int argc, char **argv) { D("Install path%s\n", destination); int tpk = get_pkgtype_file_name(srcpath); if (tpk == -1) { - fprintf(stderr, "error: unknown package type '%s'\n", srcpath); + print_error(SDB_MESSAGE_ERROR, F(ERR_PACKAGE_TYPE_UNKNOWN, srcpath), NULL); return 1; } @@ -644,6 +647,9 @@ int install(int argc, char **argv) { else if(tpk == 0){ snprintf(full_cmd, sizeof full_cmd, SHELL_INSTALL_CMD, "wgt", destination); } + else if(tpk == 2){ + snprintf(full_cmd, sizeof full_cmd, SHELL_INSTALL_CMD, "rpm", destination); + } D(COMMANDLINE_MSG_FULL_CMD, argv[0], full_cmd); if(__sdb_command(full_cmd) < 0) { @@ -704,7 +710,7 @@ static int get_pkg_tmp_dir(char* pkg_tmp_dir){ return 0; } - fprintf(stderr, "failed to get package temporary path : %s", buf); + print_error(SDB_MESSAGE_ERROR, F(ERR_PACKAGE_GET_TEMP_PATH_FAIL, buf), NULL); sdb_close(fd); return -1; } @@ -725,6 +731,9 @@ static int get_pkgtype_file_name(const char* file_name) { else if(!strcmp(pkg_type, "tpk")) { result = 1; } + else if(!strcmp(pkg_type, "rpm")) { + result = 2; + } } return result; @@ -745,12 +754,12 @@ static int get_pkgtype_from_app_id(const char* app_id) { int rl_result = read_lines(result, buf, 100); if(rl_result <= 0) { - fprintf(stderr, "error: package '%s' does not exist\n", app_id); + print_error(SDB_MESSAGE_ERROR, ERR_PACKAGE_GET_TYPE_FAIL, F(ERR_PACKAGE_ID_NOT_EXIST, app_id)); return -1; } if(rl_result > 1) { - fprintf(stderr, "error: '%s' is not unique package id\n", app_id); + print_error(SDB_MESSAGE_ERROR, ERR_PACKAGE_GET_TYPE_FAIL, F(ERR_PACKAGE_ID_NOT_UNIQUE, app_id)); return -1; } @@ -771,10 +780,10 @@ static int get_pkgtype_from_app_id(const char* app_id) { } else { if(strstr(buf, "error") != NULL) { - fprintf(stderr, "%s\n", buf); + print_error(SDB_MESSAGE_ERROR, ERR_PACKAGE_GET_TYPE_FAIL, buf); } else { - fprintf(stderr, "error: not supported package type '%s'\n", buf); + print_error(SDB_MESSAGE_ERROR, ERR_PACKAGE_TYPE_UNKNOWN, buf); } } return result; @@ -838,7 +847,7 @@ static int verify_gdbserver_exist() { return -1; } if (read_line(result, buf, sizeof(buf)) > 0) { - fprintf(stderr, "error: %s\n", buf); + print_error(SDB_MESSAGE_ERROR, buf, NULL); sdb_close(result); return -1; } diff --git a/src/commandline.c b/src/commandline.c index c95e22a..57077b8 100755 --- a/src/commandline.c +++ b/src/commandline.c @@ -44,6 +44,7 @@ #include "sdb_model.h" #include "commandline.h" #include "command_function.h" +#include "sdb_messages.h" static void print_help(LIST_NODE* optlist, LIST_NODE* cmdlist); static void create_opt_list(LIST_NODE** opt_list); @@ -174,9 +175,9 @@ static void *stdin_read_thread(void *x) n++; if(buf[n] == '.') { fprintf(stderr,"\n* disconnect *\n"); - stdin_raw_restore(INPUT_FD, tio_save); - free(tio_save); - exit(0); + stdin_raw_restore(INPUT_FD, tio_save); + free(tio_save); + exit(0); } } } @@ -592,7 +593,7 @@ int process_cmdline(int argc, char** argv) { if (tmp) { serial = strdup(tmp); } else { - fprintf(stderr, "wrong serial number '%s'\n", serial); + print_error(SDB_MESSAGE_ERROR, F(ERR_CONNECT_WRONG_SERIAL, serial), NULL); return 1; } } @@ -621,14 +622,16 @@ int process_cmdline(int argc, char** argv) { int maxargs = command->maxargs; if(argc < minargs + 1) { - fprintf(stderr, "%s command has following args: %s, and it requires at least %d arguments\n", argv[0], command->argdesc, minargs); + print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_TOO_FEW_ARGUMENTS , NULL); + print_info("sdb %s %s", argv[0], command->argdesc); if (serial != NULL) { free(serial); } return 1; } if(argc > maxargs + 1 && maxargs > -1) { - fprintf(stderr, "command %s require at most %d arguments\n", argv[0], maxargs); + print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_TOO_MANY_ARGUMENTS , NULL); + print_info("sdb %s %s", argv[0], command->argdesc); if (serial != NULL) { free(serial); } @@ -652,7 +655,7 @@ int process_cmdline(int argc, char** argv) { static void print_help(LIST_NODE* optlist, LIST_NODE* cmdlist) { - fprintf(stderr, "Smart Development Bridge version %d.%d.%d\n", + fprintf(stderr, "Smart Development Bridge %d.%d.%d\n", SDB_VERSION_MAJOR, SDB_VERSION_MINOR, SDB_VERSION_PATCH); fprintf(stderr, "\n Usage : sdb [option] [parameters]\n\n"); fprintf(stderr, " options:\n"); diff --git a/src/fdevent.c b/src/fdevent.c index fbaf954..59fd2fa 100755 --- a/src/fdevent.c +++ b/src/fdevent.c @@ -30,6 +30,7 @@ #include "utils.h" #include "fdevent.h" #include "log.h" +#include "sdb_messages.h" #ifndef OS_WINDOWS int max_select = 0; @@ -93,7 +94,7 @@ void fdevent_install(FD_EVENT *fde, int fd, fd_func func, void *arg) #ifndef OS_WINDOWS int ret = fcntl(fd, F_SETFL, O_NONBLOCK); if(ret == -1) - fprintf(stderr, "fail to set the file status flag\n"); + LOG_ERROR("failed to install FD event '%d': %s\n", fd, "cannot set the status of FD"); if(fd >= max_select) { max_select = fd + 1; } diff --git a/src/file_sync_client.c b/src/file_sync_client.c index 4e54246..fefb571 100644 --- a/src/file_sync_client.c +++ b/src/file_sync_client.c @@ -36,6 +36,7 @@ #include "strutils.h" #include "fdevent.h" #include "log.h" +#include "sdb_messages.h" static __inline__ void finalize(int srcfd, int dstfd, SYNC_INFO* sync_info); @@ -201,7 +202,7 @@ int do_sync_copy(char* srcp, char* dstp, SYNC_INFO* sync_info, int is_utf8) { int src_dir = srcF->is_dir(srcp, &src_stat); if(src_dir == -1) { - fprintf(stderr, ERR_REASON_SYNC_NOT_FILE, srcp); + print_error(SDB_MESSAGE_ERROR, ERR_SYNC_COPY_FAIL, F(ERR_SYNC_NOT_FILE, srcp)); goto error; } @@ -218,7 +219,7 @@ int do_sync_copy(char* srcp, char* dstp, SYNC_INFO* sync_info, int is_utf8) { } if(dst_dir == -1) { - fprintf(stderr, ERR_REASON_SYNC_NOT_FILE, dstp); + print_error(SDB_MESSAGE_ERROR, ERR_SYNC_COPY_FAIL, F(ERR_SYNC_NOT_FILE, dstp)); goto error; } @@ -268,6 +269,7 @@ int do_sync_copy(char* srcp, char* dstp, SYNC_INFO* sync_info, int is_utf8) { while(dir_list != NULL) { LIST_NODE* entry_list = NULL; COPY_INFO* _info = (COPY_INFO*)dir_list->data; + if(srcF->get_dirlist(src_fd, _info->src, _info->dst, &entry_list, sync_info)) { fprintf(stderr,"skipped: %s -> %s\n", _info->src, _info->dst); sync_info->skipped++; @@ -286,11 +288,12 @@ int do_sync_copy(char* srcp, char* dstp, SYNC_INFO* sync_info, int is_utf8) { src_dir = srcF->is_dir(src_p, &(copy_info->_stat)); if(src_dir < 0) { - fprintf(stderr,ERR_REASON_SYNC_NOT_FILE, src_p); + print_error(SDB_MESSAGE_ERROR, ERR_SYNC_COPY_FAIL, F(ERR_SYNC_NOT_FILE, src_p)); goto skip_in; } if(src_dir == 1) { append(&dir_list, copy_info); + D("copy destination info = %s\n", copy_info->dst); continue; } else { diff --git a/src/file_sync_functions.c b/src/file_sync_functions.c index 58f3ca2..1e5d122 100644 --- a/src/file_sync_functions.c +++ b/src/file_sync_functions.c @@ -35,6 +35,7 @@ #include "strutils.h" #include "file_sync_client.h" #include "log.h" +#include "sdb_messages.h" const unsigned sync_stat = MKSYNC('S','T','A','T'); const unsigned sync_list = MKSYNC('L','I','S','T'); @@ -88,7 +89,8 @@ int initialize_remote(char* path) { int fd = sdb_connect("sync:"); if(fd < 0) { - print_error(1, ERR_SITU_SYNC_OPEN_CHANNEL, ERR_REASON_GENERAL_CONNECTION_FAIL, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, ERR_SYNC_OPEN_CHANNEL_FAIL, ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } return fd; @@ -117,7 +119,7 @@ int _stat_local(int fd, char* path, struct stat* st, int print_err) { D("stat local file 'fd:%d' '%s'\n", fd, path); if(stat(path, st)) { if(print_err) { - print_error(0, ERR_SITU_SYNC_STAT_FILE, strerror(errno), path); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_STAT_FAIL, path), strerror(errno)); } st->st_mode = 0; return -1; @@ -135,11 +137,13 @@ int _stat_remote(int fd, char* path, struct stat* st, int print_err) { if(writex(fd, &msg.req, sizeof(msg.req)) || writex(fd, path, len)) { - print_error(1, ERR_SITU_SYNC_STAT_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, path, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_STAT_FAIL, path), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } if(readx(fd, &msg.stat, sizeof(msg.stat))) { - print_error(1, ERR_SITU_SYNC_STAT_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, path, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_STAT_FAIL, path), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } if(msg.stat.id != sync_stat) { @@ -148,7 +152,8 @@ int _stat_remote(int fd, char* path, struct stat* st, int print_err) { MKCHAR(expected, sync_stat); MKCHAR(result, msg.stat.id); - print_error(1, ERR_SITU_SYNC_STAT_FILE, ERR_REASON_GENERAL_PROTOCOL_WRONG_ID, path, expected, result); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_STAT_FAIL, path), F(ERR_GENERAL_PROTOCOL_WRONG_ID, expected, result)); + } st->st_mode = ltohl(msg.stat.mode); @@ -159,7 +164,7 @@ int _stat_remote(int fd, char* path, struct stat* st, int print_err) { */ if(!st->st_mode) { if(print_err) { - print_error(0, ERR_SITU_SYNC_STAT_FILE, ERR_REASON_GENERAL_UNKNOWN, path); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_STAT_FAIL, path), ERR_GENERAL_UNKNOWN); } return -1; } @@ -187,7 +192,7 @@ int readopen_local(int fd, char* srcp, struct stat* st) { fd = sdb_open(srcp, O_RDONLY); if(fd < 0) { - print_error(0, ERR_SITU_SYNC_OPEN_FILE, strerror(errno), srcp); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_OPEN_FAIL, srcp), strerror(errno)); return -1; } @@ -201,7 +206,7 @@ int readopen_remote(int fd, char* srcp, struct stat* st) { len = strlen(srcp); if(len > SYNC_CHAR_MAX) { - print_error(0, ERR_SITU_SYNC_OPEN_FILE, ERR_REASON_GENERAL_PROTOCOL_DATA_OVERRUN, srcp, len, SYNC_CHAR_MAX); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_OPEN_FAIL, srcp), F(ERR_GENERAL_PROTOCOL_DATA_OVERRUN, len, SYNC_CHAR_MAX)); return -1; } @@ -209,7 +214,8 @@ int readopen_remote(int fd, char* srcp, struct stat* st) { msg.req.namelen = htoll(len); if(writex(fd, &msg.req, sizeof(msg.req)) || writex(fd, srcp, len)) { - print_error(1, ERR_SITU_SYNC_OPEN_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, srcp, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_OPEN_FAIL, srcp), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } return fd; } @@ -231,10 +237,12 @@ int writeopen_local(int fd, char* dstp, struct stat* st) { D("write open local file 'fd:%d' '%s'\n", fd, dstp); unix_unlink(dstp); mkdirs(dstp); +// mkdir(dstp, 0755); fd = sdb_creat(dstp, 0644); if(fd < 0) { - print_error(0, ERR_SITU_SYNC_CREATE_FILE, strerror(errno), dstp); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_CREATE_FAIL, dstp), strerror(errno)); + D("FAIL to writeopne_local() \n", fd, dstp); return -1; } @@ -254,7 +262,7 @@ int writeopen_remote(int fd, char* dstp, struct stat* st) { len = strlen(dstp); if(len > SYNC_CHAR_MAX) { - print_error(0, ERR_SITU_SYNC_CREATE_FILE, ERR_REASON_GENERAL_PROTOCOL_DATA_OVERRUN, dstp, len, SYNC_CHAR_MAX); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_CREATE_FAIL, dstp), F(ERR_GENERAL_PROTOCOL_DATA_OVERRUN, len, SYNC_CHAR_MAX)); return -1; } @@ -267,7 +275,8 @@ int writeopen_remote(int fd, char* dstp, struct stat* st) { if(writex(fd, &msg.req, sizeof(msg.req)) || writex(fd, dstp, len) || writex(fd, tmp, r)) { - print_error(1, ERR_SITU_SYNC_OPEN_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, dstp, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_OPEN_FAIL, dstp), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } return fd; @@ -288,11 +297,14 @@ int writeclose_remote(int fd, char* dstp, struct stat* st) { msg.data.size = htoll(st->st_mtime); if(writex(fd, &msg.data, sizeof(msg.data))) { - print_error(1, ERR_SITU_SYNC_CLOSE_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, dstp, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_CLOSE_FAIL, dstp), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } + if(readx(fd, &msg.status, sizeof(msg.status))) { - print_error(1, ERR_SITU_SYNC_CLOSE_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, dstp, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_CLOSE_FAIL, dstp), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } if(msg.status.id != sync_okay) { @@ -303,17 +315,17 @@ int writeclose_remote(int fd, char* dstp, struct stat* st) { len = 255; } if(!readx(fd, buf, len)) { - print_error(0, ERR_SITU_SYNC_CLOSE_FILE, buf, dstp); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_CLOSE_FAIL, dstp), buf); return -1; } - print_error(1, ERR_SITU_SYNC_CLOSE_FILE, ERR_REASON_GENERAL_UNKNOWN, dstp); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_CLOSE_FAIL, dstp), ERR_GENERAL_UNKNOWN); } char expected[5]; char result[5]; MKCHAR(expected, sync_fail); MKCHAR(result, msg.status.id); - print_error(1, ERR_SITU_SYNC_CLOSE_FILE, ERR_REASON_GENERAL_PROTOCOL_WRONG_ID, dstp, expected, result); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_CLOSE_FAIL, dstp), F(ERR_GENERAL_PROTOCOL_WRONG_ID, expected, result)); } return fd; @@ -341,7 +353,7 @@ int readfile_local(int lfd, char* srcpath, struct stat* st, FILE_BUFFER* sbuf) { return 2; } //fail. - print_error(0, ERR_SITU_SYNC_READ_FILE, strerror(errno), srcpath); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_READ_FAIL, srcpath), strerror(errno)); return -1; } @@ -356,7 +368,7 @@ int readfile_local(int lfd, char* srcpath, struct stat* st, FILE_BUFFER* sbuf) { len = readlink(srcpath, sbuf->data, SYNC_DATA_MAX-1); //fail if(len < 0) { - print_error(0, ERR_SITU_SYNC_READ_FILE, strerror(errno), srcpath); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_READ_FAIL, srcpath), strerror(errno)); return -1; } sbuf->data[len] = '\0'; @@ -369,7 +381,7 @@ int readfile_local(int lfd, char* srcpath, struct stat* st, FILE_BUFFER* sbuf) { //fail - print_error(0, ERR_SITU_SYNC_READ_FILE, ERR_REASON_SYNC_NOT_FILE, srcpath, srcpath); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_READ_FAIL, srcpath), F(ERR_SYNC_NOT_FILE, srcpath)); return -1; } @@ -379,7 +391,8 @@ int readfile_remote(int fd, char* srcpath, struct stat* st, FILE_BUFFER* buffer) unsigned id; if(readx(fd, &(msg.data), sizeof(msg.data))) { - print_error(1, ERR_SITU_SYNC_READ_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, srcpath, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_READ_FAIL, srcpath), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } id = msg.data.id; buffer->size = ltohl(msg.data.size); @@ -398,26 +411,27 @@ int readfile_remote(int fd, char* srcpath, struct stat* st, FILE_BUFFER* buffer) } if(!readx(fd, buffer->data, len)) { buffer->data[len] = 0; - print_error(0, ERR_SITU_SYNC_READ_FILE, buffer->data, srcpath); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_READ_FAIL, srcpath), buffer->data); return -1; } - print_error(1, ERR_SITU_SYNC_READ_FILE, ERR_REASON_GENERAL_UNKNOWN, srcpath); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_READ_FAIL, srcpath), ERR_GENERAL_UNKNOWN); } char expected[5]; char result[5]; MKCHAR(expected, sync_fail); MKCHAR(result, id); - print_error(1, ERR_SITU_SYNC_READ_FILE, ERR_REASON_GENERAL_PROTOCOL_WRONG_ID, srcpath, expected, result); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_READ_FAIL, srcpath), F(ERR_GENERAL_PROTOCOL_WRONG_ID, expected, result)); } //fail if(buffer->size > SYNC_DATA_MAX) { - print_error(1, ERR_SITU_SYNC_READ_FILE, ERR_REASON_GENERAL_PROTOCOL_DATA_OVERRUN, srcpath, buffer->size, SYNC_DATA_MAX); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_READ_FAIL, srcpath), F(ERR_GENERAL_PROTOCOL_DATA_OVERRUN, buffer->size, SYNC_DATA_MAX)); } //fail if(readx(fd, buffer->data, buffer->size)) { - print_error(1, ERR_SITU_SYNC_READ_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, srcpath, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_READ_FAIL, srcpath), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } //write and continue load @@ -434,7 +448,8 @@ int writefile_local(int fd, char* dstp, FILE_BUFFER* sbuf, SYNC_INFO* sync_info) * remote channel is already opend * if local write fails, protocol conflict happens unless we receive sync_done from remote */ - print_error(1, ERR_SITU_SYNC_WRITE_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, dstp, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_WRITE_FAIL, dstp), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } sync_info->total_bytes += len; @@ -446,8 +461,9 @@ int writefile_remote(int fd, char* dstp, FILE_BUFFER* sbuf, SYNC_INFO* sync_info int size = ltohl(sbuf->size); if(writex(fd, sbuf, sizeof(unsigned)*2 + size)) { - print_error(1, ERR_SITU_SYNC_WRITE_FILE, ERR_REASON_GENERAL_CONNECTION_FAIL, dstp, strerror(errno)); - } + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_WRITE_FAIL, dstp), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); + } sync_info->total_bytes += size; return 0; @@ -459,7 +475,7 @@ int getdirlist_local(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, d = opendir(src_dir); if(d == 0) { - print_error(0, ERR_SITU_SYNC_GET_DIRLIST, strerror(errno), src_dir); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_GET_DIRLIST_FAIL, src_dir), strerror(errno)); readclose_local(fd); return -1; } @@ -489,6 +505,7 @@ int getdirlist_local(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, if(!_stat_local(fd, src_full_path, &src_stat, 1)) { COPY_INFO* info; create_copy_info(&info, src_full_path, dst_full_path, &src_stat); + D("copy list (destination path) add '%s'\n", dst_full_path); prepend(dirlist, info); } else { @@ -511,7 +528,7 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, len = strlen(src_dir); if(len > SYNC_CHAR_MAX) { - print_error(0, ERR_SITU_SYNC_GET_DIRLIST, ERR_REASON_GENERAL_PROTOCOL_DATA_OVERRUN, src_dir, len, SYNC_CHAR_MAX); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_GET_DIRLIST_FAIL, src_dir), F(ERR_GENERAL_PROTOCOL_DATA_OVERRUN, len, SYNC_CHAR_MAX)); return -1; } @@ -520,12 +537,12 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, if(writex(fd, &msg.req, sizeof(msg.req)) || writex(fd, src_dir, len)) { - print_error(1, ERR_SITU_SYNC_GET_DIRLIST, ERR_REASON_GENERAL_CONNECTION_FAIL, src_dir, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_GET_DIRLIST_FAIL, src_dir), strerror(errno)); } while(1) { if(readx(fd, &msg.dent, sizeof(msg.dent))) { - print_error(1, ERR_SITU_SYNC_GET_DIRLIST, ERR_REASON_GENERAL_CONNECTION_FAIL, src_dir, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_GET_DIRLIST_FAIL, src_dir), strerror(errno)); } if(msg.dent.id == sync_done) { LOG_INFO("getting list of remote file 'fd:%d' '%s' is done\n", fd, src_dir); @@ -537,11 +554,11 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, MKCHAR(expected, sync_dent); MKCHAR(result, msg.dent.id); - print_error(1, ERR_SITU_SYNC_GET_DIRLIST, ERR_REASON_GENERAL_PROTOCOL_WRONG_ID, src_dir, expected, result); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_GET_DIRLIST_FAIL, src_dir), F(ERR_GENERAL_PROTOCOL_WRONG_ID, expected, result)); } len = ltohl(msg.dent.namelen); if(len > 256) { - fprintf(stderr,"error: name of a file in the remote directory '%s' exceeds 256\n", src_dir); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_CREATE_FAIL , src_dir), ERR_SYNC_TOO_LONG_FILENAME); fprintf(stderr,"skipped: %s/? -> %s/?\n", src_dir, dst_dir); sync_info->skipped++; continue; @@ -549,7 +566,8 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, char file_name[257]; if(readx(fd, file_name, len)) { - print_error(1, ERR_SITU_SYNC_GET_DIRLIST, ERR_REASON_GENERAL_CONNECTION_FAIL, src_dir, strerror(errno)); + print_error(SDB_MESSAGE_FATAL, F(ERR_SYNC_GET_DIRLIST_FAIL, src_dir), ERR_GENERAL_CONNECTION_FAIL); + LOG_ERROR(strerror(errno)); } file_name[len] = 0; @@ -578,7 +596,7 @@ int getdirlist_remote(int fd, char* src_dir, char* dst_dir, LIST_NODE** dirlist, * We cannot know the reason before we change sync protocol. */ if(!st.st_mode) { - print_error(0, ERR_SITU_SYNC_STAT_FILE, ERR_REASON_GENERAL_UNKNOWN, file_name); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_STAT_FAIL, file_name), ERR_GENERAL_UNKNOWN); fprintf(stderr,"skipped: %s -> %s\n", src_full_path, dst_full_path); sync_info->skipped++; free(src_full_path); diff --git a/src/listener.c b/src/listener.c index cd6de76..b5b2d36 100755 --- a/src/listener.c +++ b/src/listener.c @@ -78,10 +78,10 @@ int install_listener(int local_port, int connect_port, TRANSPORT* transport, LIS if(fd < 0) { if(ltype == serverListener) { - LOG_FATAL("server LN(%d) cannot bind \n", local_port); + print_error(SDB_MESSAGE_FATAL, F("server listener(%d) cannot bind", local_port), NULL); } else { - LOG_ERROR("LN(%d) cannot bind \n", local_port); + print_error(SDB_MESSAGE_ERROR, F("listener(%d) cannot bind", local_port), NULL); } return -2; } diff --git a/src/log.c b/src/log.c index 6b0571f..1780144 100644 --- a/src/log.c +++ b/src/log.c @@ -120,25 +120,67 @@ void logging(LogLevel level, const char *filename, const char *funcname, int lin fflush(stderr); va_end(args); } +void print_error(int level, const char* situation, const char* reason) { -void print_error(int fatal, const char* situation, const char* reason, ...) { + fprintf(stderr, "%s\n", error_message(level, situation, reason)); + if(level == 1) { + fflush(stderr); + exit(255); + } +} + +char* error_message(int level, const char* situation, const char* reason) { + char *format; + static char error_buf[255]; + switch(level){ + case 0: + format = "error"; + break; + case 1: + format = "fatal"; + break; + default: + format ="sdb"; + break; + } + + if(reason != NULL) { + snprintf(error_buf, sizeof(error_buf), "%s: %s: %s", format, situation, reason); + } else { + snprintf(error_buf, sizeof(error_buf), "%s: %s", format, situation); + } + return error_buf; +} + +/* + * print usage message to user + */ +void print_info(const char* message, ...){ + + char info_buf_format[INFO_LENGTH]; + + char *format = "usage"; - char error_buf_format[ERR_LENGTH]; - snprintf(error_buf_format, ERR_LENGTH, ERR_FORMAT, ERR_TAG, situation, reason); + snprintf(info_buf_format, INFO_LENGTH, INFO_FORMAT, format, message); va_list args; - va_start(args, reason); + va_start(args, message); - char error_buf[ERR_LENGTH]; - vsnprintf(error_buf, ERR_LENGTH, error_buf_format, args); - fprintf(stderr, "%s", error_buf); + char info_buf[INFO_LENGTH]; + vsnprintf(info_buf, INFO_LENGTH, info_buf_format, args); + fprintf(stdout, "%s\n", info_buf); va_end(args); +} - if(fatal) { - LOG_ERROR("FATAL\n"); - fflush(stderr); - exit(255); - } +char* F(const char* message, ...) { + va_list args; + va_start(args, message); + + static char error_buf[255]; + vsnprintf(error_buf, sizeof(error_buf), message, args); + + va_end(args); + return error_buf; } static void log_parse(char* args) { @@ -171,7 +213,7 @@ static void log_parse(char* args) { free(levels); } -void log_init(void) +void log_init(void) { char* sdb_debug = NULL; diff --git a/src/log.h b/src/log.h index 4cb93f7..3d4f9e5 100644 --- a/src/log.h +++ b/src/log.h @@ -17,12 +17,14 @@ #ifndef __LOG_H #define __LOG_H -#define SDB_TRACE 1 -#define DEBUG_ENV "SDB_DEBUG" -#define TRACE_PACKET "SDB_TRACE_PACKET" +#define SDB_TRACE 1 +#define DEBUG_ENV "SDB_DEBUG" +#define TRACE_PACKET "SDB_TRACE_PACKET" #define ERR_LENGTH 255 -#define ERR_FORMAT "%s: %s: %s\n" //tag:situation:reason -#define ERR_TAG "sdb" +#define INFO_LENGTH 255 +#define ERR_FORMAT "%s: %s: %s" //tag:situation:reason +#define INFO_FORMAT "%s: %s" //tag:message +//#define ERR_TAG "sdb" extern int loglevel_mask; extern int trace_packet; @@ -35,6 +37,11 @@ typedef enum { SDBLOG_FIXME } LogLevel; +typedef enum { + SDB_MESSAGE_ERROR = 0, + SDB_MESSAGE_FATAL +} MessageLevel; + #define LOG_FATAL(args...) \ do { \ logging(SDBLOG_FATAL, __FILE__, __FUNCTION__, __LINE__, args);\ @@ -65,7 +72,10 @@ typedef enum { void log_init(void); void logging(LogLevel level, const char *filename, const char *funcname, int line_number, const char *fmt, ...); void logging_hex(char* hex, char* asci); -void print_error(int fatal, const char* situation, const char* reason, ...); +void print_error(int level, const char* situation, const char* reason); +char* error_message(int level, const char* situation, const char* reason); +void print_info(const char* message, ...); +char* F(const char *message, ...); // define for a while for testing #undef D diff --git a/src/sdb.c b/src/sdb.c index 68d8491..9a9adc3 100755 --- a/src/sdb.c +++ b/src/sdb.c @@ -183,7 +183,7 @@ int main(int argc, char **argv) int complete_checker = atoi(argv[2]); if(complete_checker == 0) { - fprintf(stderr, "error:zero_complete_cheker\n"); + LOG_ERROR("zero_complete_cheker\n"); return -1; } if(complete_checker == argc - 3) { @@ -192,7 +192,7 @@ int main(int argc, char **argv) else if(complete_checker == argc - 2) { return auto_complete(argc - 3, argv + 3, 1); } - fprintf(stderr, "error:wrong_complete_cheker\n"); + LOG_ERROR("wrong-complete_cheker\n"); return -1; } #endif diff --git a/src/sdb_client.c b/src/sdb_client.c index ff57cb9..43d9ae0 100644 --- a/src/sdb_client.c +++ b/src/sdb_client.c @@ -30,6 +30,7 @@ #define TRACE_TAG TRACE_SDB #include "sdb_client.h" #include "log.h" +#include "sdb_messages.h" static int switch_socket_transport(int fd); static int __inline__ write_msg_size(int fd, int size, int host_fd); @@ -61,13 +62,13 @@ int send_service_with_length(int fd, const char* service, int host_fd) { fprintf(stderr,"error: service name is empty\n"); } else { - sendfailmsg(host_fd, "error: service name is empty\n"); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_EMPTY_SERVICE_NAME, NULL); } return -1; } else if (len > 1024) { if(host_fd == 0) { - fprintf(stderr,"error: service name too long\n"); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_TOO_LONG_SERVICE_NAME, NULL); } else { sendfailmsg(host_fd, "error: service name too long\n"); @@ -86,7 +87,9 @@ int send_service_with_length(int fd, const char* service, int host_fd) { if(writex(fd, service, len)) { D("error: write failure during connection\n"); if(host_fd == 0) { - fprintf(stderr,"error: write failure during connection\n"); + char buf[10]; + sprintf(buf, "%d", fd); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_WRITE_FAIL, buf),NULL); } else { sendfailmsg(host_fd, "error: write failure during connection\n"); @@ -255,7 +258,7 @@ int sdb_status(int fd, int host_fd) if(readx(fd, buf, 4)) { if(host_fd == 0) { - fprintf(stderr,"error: protocol fault (no status)\n"); + print_error(SDB_MESSAGE_ERROR, "protocol fault", "no status"); } else { sendfailmsg(host_fd, "error: protocol fault (no status)\n"); @@ -269,8 +272,7 @@ int sdb_status(int fd, int host_fd) if(memcmp(buf, "FAIL", 4)) { if(host_fd == 0) { - fprintf(stderr,"error: protocol fault (status %02x %02x %02x %02x?!)\n", - buf[0], buf[1], buf[2], buf[3]); + print_error(SDB_MESSAGE_ERROR, "protocol fault", F("status %02x %02x %02x %02x?!", buf[0], buf[1], buf[2], buf[3])); } else { char err_msg[255]; @@ -284,7 +286,7 @@ int sdb_status(int fd, int host_fd) int len = read_msg_size(fd); if(len < 0) { if(host_fd == 0) { - fprintf(stderr,"error: protocol fault (status len)\n"); + print_error(SDB_MESSAGE_ERROR, "protocol fault", "status len"); } else { sendfailmsg(host_fd, "error: protocol fault (status len)\n"); @@ -297,7 +299,7 @@ int sdb_status(int fd, int host_fd) char error[255]; if(readx(fd, error, len)) { if(host_fd == 0) { - fprintf(stderr,"error: protocol fault (status read)\n"); + print_error(SDB_MESSAGE_ERROR, "protocol fault", "status read"); } else { sendfailmsg(host_fd, "error: protocol fault (status read)\n"); @@ -306,7 +308,7 @@ int sdb_status(int fd, int host_fd) } error[len] = '\0'; if(host_fd == 0) { - fprintf(stderr,"error msg: %s\n", error); + fprintf(stderr,"%s\n", error); } else { char err_msg[255]; @@ -374,7 +376,7 @@ static int __inline__ write_msg_size(int fd, int size, int host_fd) { if(writex(fd, tmp, 4)) { D("error: write msg size failure\n"); if(host_fd == 0) { - fprintf(stderr,"error: write msg size failure\n"); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_WRITE_MESSAGE_SIZE_FAIL, NULL); } else { sendfailmsg(host_fd, "error: write msg size failure\n"); @@ -417,7 +419,7 @@ int sdb_connect(const char *service) int patch = strtoul(tokens[2], 0, 10); if (major != SDB_VERSION_MAJOR || minor != SDB_VERSION_MINOR || patch != SDB_VERSION_PATCH ) { fprintf(stdout, - "* sdb (%s) already running, and restarting sdb(%d.%d.%d) again *\n", + "* sdb (%s) already running, and restarting sdb (%d.%d.%d) again *\n", buf, SDB_VERSION_MAJOR, SDB_VERSION_MINOR, SDB_VERSION_PATCH); restarting = 1; @@ -431,7 +433,7 @@ int sdb_connect(const char *service) } else { if (ver != SDB_VERSION_PATCH) { fprintf(stdout, - "* another version of sdb already running, and restarting sdb(%d.%d.%d) again *\n", + "* another version of sdb already running, and restarting sdb (%d.%d.%d) again *\n", SDB_VERSION_MAJOR, SDB_VERSION_MINOR, SDB_VERSION_PATCH); restarting = 1; } @@ -449,19 +451,19 @@ int sdb_connect(const char *service) } if(fd == -2) { - fprintf(stdout,"* daemon not running. starting it now on port %d *\n", DEFAULT_SDB_PORT); + fprintf(stdout,"* server not running. starting it now on port %d *\n", DEFAULT_SDB_PORT); launch_server: if(launch_server()) { - fprintf(stderr,"* failed to start daemon *\n"); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_START_SERVER_FAIL, NULL); return -1; } else { - fprintf(stdout,"* daemon started successfully *\n"); + fprintf(stdout,"* server started successfully *\n"); } } fd = _sdb_connect(service); if(fd == -2) { - fprintf(stderr,"** daemon still not running\n"); + print_error(SDB_MESSAGE_ERROR, ERR_GENERAL_SERVER_NOT_RUN, NULL); } D("sdb_connect: return fd %d\n", fd); diff --git a/src/sdb_constants.c b/src/sdb_constants.c index 038783a..32c7162 100644 --- a/src/sdb_constants.c +++ b/src/sdb_constants.c @@ -204,6 +204,14 @@ const int COMMANDLINE_FORWARD_REMOVE_ALL_MAX_ARG = 0; const int COMMANDLINE_FORWARD_REMOVE_ALL_MIN_ARG = 0; + const char* COMMANDLINE_FORWARDLIST_NAME = "forward-list"; + const char* COMMANDLINE_FORWARDLIST_DESC[] = { + "list forwarding ports", + }; + const int COMMANDLINE_FORWARDLIST_DESC_SIZE = GET_ARRAY_SIZE(COMMANDLINE_FORWARDLIST_DESC, char*); + const int COMMANDLINE_FORWARDLIST_MAX_ARG = 0; + const int COMMANDLINE_FORWARDLIST_MIN_ARG = 0; + const char* COMMANDLINE_PUSH_NAME = "push"; const char* COMMANDLINE_PUSH_DESC[] = { "copy file/dir to device", @@ -296,30 +304,3 @@ const char* STATE_LOCKED = "locked"; const char* STATE_UNKNOWN = "unknown"; const char* STATE_SUSPENDED = "suspended"; - - const char* ERR_REASON_GENERAL_PROTOCOL_WRONG_ID = "protocol error. expected '%s' but get '%s'"; - const char* ERR_REASON_GENERAL_PROTOCOL_DATA_OVERRUN = "protocol error. data length '%d' overruns '%d'"; - const char* ERR_REASON_GENERAL_CONNECTION_FAIL = "connection fails. %s"; - const char* ERR_REASON_GENERAL_UNKNOWN = "unknown reason"; - const char* ERR_REASON_SYNC_NOT_FILE = "'%s' is not a file"; - - const char* ERR_SITU_SYNC_OPEN_CHANNEL = "fail to open sync channel"; - const char* ERR_SITU_SYNC_STAT_FILE = "fail to stat '%s'"; - const char* ERR_SITU_SYNC_GET_DIRLIST = "fail to read dirlist of '%s'"; - const char* ERR_SITU_SYNC_READ_FILE = "fail to read '%s'"; - const char* ERR_SITU_SYNC_OPEN_FILE = "fail to open '%s'"; - const char* ERR_SITU_SYNC_CREATE_FILE = "fail to create '%s'"; - const char* ERR_SITU_SYNC_CLOSE_FILE = "fail to close '%s'"; - const char* ERR_SITU_SYNC_WRITE_FILE = "fail to write '%s'"; - - const char* ERR_TRANSPORT_MORE_THAN_ONE_TARGET = "error: there are more than one target. Please specify the target using -s option."; - const char* ERR_TRANSPORT_MORE_THAN_ONE_EMUL = "error: there are more than one emulator. Please specify the emulator using -s option."; - const char* ERR_TRANSPORT_MORE_THAN_ONE_DEV = "error: there are more than one device. Please specify the device using -s option."; - const char* ERR_TRANSPORT_TARGET_OFFLINE = "error: target offline"; - const char* ERR_TRANSPORT_TARGET_SUSPENDED = "error: emulator is in suspended mode"; - const char* ERR_TRANSPORT_TARGET_NOT_FOUND = "error: target not found"; - - const char* ERR_FORWARD_UNKNOWN_LOCAL_PORT = "error: wrong local port format"; - const char* ERR_FORWARD_UNKNOWN_REMOTE_PORT = "error: wrong remote port format"; - const char* ERR_FORWARD_INSTALL_FAIL = "error: cannot install listener"; - const char* ERR_FORWARD_REMOVE_FAIL = "error: cannot remove listener"; diff --git a/src/sdb_constants.h b/src/sdb_constants.h index ff637fd..fdd60f7 100644 --- a/src/sdb_constants.h +++ b/src/sdb_constants.h @@ -36,6 +36,9 @@ enum host_type { typedef enum host_type HOST_TYPE; +// extern const char* SDB = "sdb"; +// extern const char* SDB_FULL = "Smart Development Bridge"; + extern const char* SDB_LAUNCH_SCRIPT; extern const char* HELP_APPEND_STR; @@ -248,31 +251,4 @@ typedef enum host_type HOST_TYPE; extern const char* STATE_UNKNOWN; extern const char* STATE_SUSPENDED; - extern const char* ERR_REASON_GENERAL_PROTOCOL_WRONG_ID; - extern const char* ERR_REASON_GENERAL_PROTOCOL_DATA_OVERRUN; - extern const char* ERR_REASON_GENERAL_CONNECTION_FAIL; - extern const char* ERR_REASON_GENERAL_UNKNOWN; - extern const char* ERR_REASON_SYNC_NOT_FILE; - - extern const char* ERR_SITU_SYNC_OPEN_CHANNEL; - extern const char* ERR_SITU_SYNC_STAT_FILE; - extern const char* ERR_SITU_SYNC_GET_DIRLIST; - extern const char* ERR_SITU_SYNC_READ_FILE; - extern const char* ERR_SITU_SYNC_OPEN_FILE; - extern const char* ERR_SITU_SYNC_CREATE_FILE; - extern const char* ERR_SITU_SYNC_CLOSE_FILE; - extern const char* ERR_SITU_SYNC_WRITE_FILE; - - extern const char* ERR_TRANSPORT_MORE_THAN_ONE_TARGET; - extern const char* ERR_TRANSPORT_MORE_THAN_ONE_EMUL; - extern const char* ERR_TRANSPORT_MORE_THAN_ONE_DEV; - extern const char* ERR_TRANSPORT_TARGET_OFFLINE; - extern const char* ERR_TRANSPORT_TARGET_SUSPENDED; - extern const char* ERR_TRANSPORT_TARGET_NOT_FOUND; - - extern const char* ERR_FORWARD_UNKNOWN_LOCAL_PORT; - extern const char* ERR_FORWARD_UNKNOWN_REMOTE_PORT; - extern const char* ERR_FORWARD_INSTALL_FAIL; - extern const char* ERR_FORWARD_REMOVE_FAIL; - #endif /* SDB_CONSTANTS_H_*/ diff --git a/src/sdb_messages.c b/src/sdb_messages.c new file mode 100644 index 0000000..03e7b81 --- /dev/null +++ b/src/sdb_messages.c @@ -0,0 +1,100 @@ +/* + * SDB - Smart Development Bridge + * + * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Shingil Kang + * Yoonki Park + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + * + */ +#include "sdb_messages.h" + +const char* NO_ERR = "no error, successful exit"; +const char* ERR_GENERAL_CONNECTION_FAIL = "connection fails"; +const char* ERR_GENERAL_UNKNOWN = "unknown reason"; +const char* ERR_GENERAL_INITIALIZE_ENV_FAIL = "failed to initialize SBD environment"; +const char* ERR_GENERAL_EMPTY_SERVICE_NAME = "empty service name"; +const char* ERR_GENERAL_TOO_LONG_SERVICE_NAME = "service name too long"; +const char* ERR_GENERAL_INVALID_SERVICE_NAME = "invalid service name"; +const char* ERR_GENERAL_WRITE_MESSAGE_SIZE_FAIL = "failed to write message size"; +const char* ERR_GENERAL_INVALID_PORT = "invalid port '%s'"; +const char* ERR_GENERAL_START_SERVER_FAIL = "failed to start server"; +const char* ERR_GENERAL_SERVER_NOT_RUN = "server not running"; +const char* ERR_GENERAL_LOG_FAIL = "failed to log"; +const char* ERR_GENERAL_DUPLICATE_FAIL = "failed to duplicate '%s'"; +const char* ERR_GENERAL_LAUNCH_APP_FAIL = "failed to launch application"; +const char* ERR_GENERAL_KILL_SERVER_FAIL = "failed to kill server"; +const char* ERR_GENERAL_SET_FD_FAIL = "cannot set the FD status"; +const char* ERR_GENERAL_INVALID_IP = "invalid IP '%s'"; +const char* ERR_GENERAL_NO_READ_PERMISSION = "no read permission"; +const char* ERR_GENERAL_NO_WRITE_PERMISSION = "no write permission"; +const char* ERR_GENERAL_NO_EXECUTE_PERMISSION = "no execute permission"; +const char* ERR_GENERAL_PROTOCOL_WRONG_ID = "protocol error; expected '%s' but got '%s'"; +const char* ERR_GENERAL_PROTOCOL_DATA_OVERRUN = "protocol error; data length '%d' overruns '%d'"; + +const char* ERR_SYNC_NOT_FILE = "'%s' is not a file"; +const char* ERR_SYNC_OPEN_CHANNEL_FAIL = "failed to open sync channel"; +const char* ERR_SYNC_STAT_FAIL = "failed to get status of '%s'"; +const char* ERR_SYNC_GET_DIRLIST_FAIL = "failed to read directory list of '%s'"; +const char* ERR_SYNC_READ_FAIL = "failed to read '%s'"; +const char* ERR_SYNC_OPEN_FAIL = "failed to open '%s'"; +const char* ERR_SYNC_CREATE_FAIL = "failed to create '%s'"; +const char* ERR_SYNC_CLOSE_FAIL = "failed to close '%s'"; +const char* ERR_SYNC_WRITE_FAIL = "failed to write '%s'"; +const char* ERR_SYNC_COPY_FAIL = "failed to copy"; +const char* ERR_SYNC_TOO_LONG_FILENAME = "file name too long"; +const char* ERR_SYNC_INVALID_FILENAME = "invalid file name"; +const char* ERR_SYNC_UNKNOWN_TYPE_FILE = "invalid file type"; +const char* ERR_SYNC_NOT_EXIST_FILE = "'%s' does not exist"; +const char* ERR_SYNC_NOT_DIRECTORY = "'%s' is not a directory"; +const char* ERR_SYNC_LOCKED = "'%s' is locked"; +const char* ERR_SYNC_CANNOT_ACCESS = "cannot access '%s'"; + +const char* ERR_CONNECT_MORE_THAN_ONE_TARGET = "more than one target found. Specify the target with -s option."; +const char* ERR_CONNECT_MORE_THAN_ONE_EMUL = "more than one emulator found. Specify the emulator with -e option."; +const char* ERR_CONNECT_MORE_THAN_ONE_DEV = "more than one device found. Specify the device with -d option."; +const char* ERR_CONNECT_TARGET_NOT_FOUND = "target not found"; +const char* ERR_CONNECT_TARGET_OFFLINE = "target offline"; +const char* ERR_CONNECT_TARGET_LOCKED = "target locked"; +const char* ERR_CONNECT_TARGET_SUSPENDED = "target suspended"; +const char* ERR_CONNECT_CONNECT_REMOTE_TARGET_FAILED = "failed to connect to remote target '%s'"; +const char* ERR_CONNECT_TARGET_NO_RESPONSE = "no response from target"; +const char* ERR_CONNECT_WRONG_SERIAL = "serial number '%s' wrong"; + +const char* ERR_COMMAND_MISSING_ARGUMENT = "missing arguments for '%s' command"; +const char* ERR_COMMAND_TOO_FEW_ARGUMENTS = "too few arguments"; +const char* ERR_COMMAND_TOO_MANY_ARGUMENTS = "too many arguments"; +const char* ERR_COMMAND_RUN_COMMAND_FAILED = "failed to run command"; +const char* ERR_COMMAND_COMMAND_NO_SUPPORT = "command '%s' not supported"; +const char* ERR_COMMAND_OPTION_NO_SUPPORT = "option '%s' not supported"; +const char* ERR_COMMAND_OPTION_MUST_HAVE_ARGUMENT = "option '%s' must have an argument"; +const char* ERR_FORWARD_UNSUPPORT_TRANSMISSION_PROTOCOL = "unsupported transmission protocol"; +const char* ERR_FORWARD_INSTALL_FAIL = "cannot install forward listener"; +const char* ERR_FORWARD_REMOVE_FAIL = "cannot remove forward listener"; +const char* ERR_FORWARD_INVALID_PROTOCOL = "invalid protocol"; +const char* ERR_FORWARD_BIND_PORT_FAIL = "failed to bind port '%s'"; +const char* ERR_PACKAGE_TYPE_UNKNOWN = "package type unknown"; +const char* ERR_PACKAGE_GET_TEMP_PATH_FAIL = "failed to get package temporary path"; +const char* ERR_PACKAGE_GET_TYPE_FAIL = "failed to get package type"; +const char* ERR_PACKAGE_ID_NOT_EXIST = "package ID '%s' does not exist"; +const char* ERR_PACKAGE_ID_NOT_UNIQUE = "package ID '%s' not unique"; +const char* ERR_LAUNCH_M_OPTION_SUPPORT = "In DA and Oprofile, the -m option is supported in sdbd higher than 2.2.0"; +const char* ERR_LAUNCH_M_OPTION_ARGUMENT = "The -m option accepts arguments only for run or debug options"; +const char* ERR_LAUNCH_P_OPTION_DEBUG_MODE = "The -P option must be used in debug mode"; +const char* ERR_LAUNCH_ATTACH_OPTION_DEBUG_MODE = "The -attach option must be used in debug mode"; diff --git a/src/sdb_messages.h b/src/sdb_messages.h new file mode 100644 index 0000000..9f9b037 --- /dev/null +++ b/src/sdb_messages.h @@ -0,0 +1,103 @@ +/* +* SDB - Smart Development Bridge +* +* Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved. +* +* Contact: +* Shingil Kang +* Yoonki Park +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* Contributors: +* - S-Core Co., Ltd +* +*/ +#ifndef SDB_MESSAGES_H_ +#define SDB_MESSAGES_H_ + +extern const char* NO_ERR; +extern const char* ERR_GENERAL_CONNECTION_FAIL; +extern const char* ERR_GENERAL_UNKNOWN; +extern const char* ERR_GENERAL_INITIALIZE_ENV_FAIL; +extern const char* ERR_GENERAL_EMPTY_SERVICE_NAME; +extern const char* ERR_GENERAL_TOO_LONG_SERVICE_NAME; +extern const char* ERR_GENERAL_INVALID_SERVICE_NAME; +extern const char* ERR_GENERAL_WRITE_MESSAGE_SIZE_FAIL; +extern const char* ERR_GENERAL_INVALID_PORT; +extern const char* ERR_GENERAL_START_SERVER_FAIL; +extern const char* ERR_GENERAL_SERVER_NOT_RUN; +extern const char* ERR_GENERAL_LOG_FAIL; +extern const char* ERR_GENERAL_DUPLICATE_FAIL; +extern const char* ERR_GENERAL_LAUNCH_APP_FAIL; +extern const char* ERR_GENERAL_KILL_SERVER_FAIL; +extern const char* ERR_GENERAL_SET_FD_FAIL; +extern const char* ERR_GENERAL_INVALID_IP; +extern const char* ERR_GENERAL_NO_READ_PERMISSION; +extern const char* ERR_GENERAL_NO_WRITE_PERMISSION; +extern const char* ERR_GENERAL_NO_EXECUTE_PERMISSION; +extern const char* ERR_GENERAL_PROTOCOL_WRONG_ID; +extern const char* ERR_GENERAL_PROTOCOL_DATA_OVERRUN; + +extern const char* ERR_SYNC_NOT_FILE; +extern const char* ERR_SYNC_OPEN_CHANNEL_FAIL; +extern const char* ERR_SYNC_STAT_FAIL; +extern const char* ERR_SYNC_GET_DIRLIST_FAIL; +extern const char* ERR_SYNC_READ_FAIL; +extern const char* ERR_SYNC_OPEN_FAIL; +extern const char* ERR_SYNC_CREATE_FAIL; +extern const char* ERR_SYNC_CLOSE_FAIL; +extern const char* ERR_SYNC_WRITE_FAIL; +extern const char* ERR_SYNC_COPY_FAIL; +extern const char* ERR_SYNC_TOO_LONG_FILENAME; +extern const char* ERR_SYNC_INVALID_FILENAME; +extern const char* ERR_SYNC_UNKNOWN_TYPE_FILE; +extern const char* ERR_SYNC_NOT_EXIST_FILE; +extern const char* ERR_SYNC_NOT_DIRECTORY; +extern const char* ERR_SYNC_LOCKED; +extern const char* ERR_SYNC_CANNOT_ACCESS; + +extern const char* ERR_CONNECT_MORE_THAN_ONE_TARGET; +extern const char* ERR_CONNECT_MORE_THAN_ONE_EMUL; +extern const char* ERR_CONNECT_MORE_THAN_ONE_DEV; +extern const char* ERR_CONNECT_TARGET_NOT_FOUND; +extern const char* ERR_CONNECT_TARGET_OFFLINE; +extern const char* ERR_CONNECT_TARGET_LOCKED; +extern const char* ERR_CONNECT_TARGET_SUSPENDED; +extern const char* ERR_CONNECT_CONNECT_REMOTE_TARGET_FAILED; +extern const char* ERR_CONNECT_TARGET_NO_RESPONSE; +extern const char* ERR_CONNECT_WRONG_SERIAL; + +extern const char* ERR_COMMAND_MISSING_ARGUMENT; +extern const char* ERR_COMMAND_TOO_FEW_ARGUMENTS; +extern const char* ERR_COMMAND_TOO_MANY_ARGUMENTS; +extern const char* ERR_COMMAND_RUN_COMMAND_FAILED; +extern const char* ERR_COMMAND_COMMAND_NO_SUPPORT; +extern const char* ERR_COMMAND_OPTION_NO_SUPPORT; +extern const char* ERR_COMMAND_OPTION_MUST_HAVE_ARGUMENT; +extern const char* ERR_FORWARD_UNSUPPORT_TRANSMISSION_PROTOCOL; +extern const char* ERR_FORWARD_INSTALL_FAIL; +extern const char* ERR_FORWARD_REMOVE_FAIL; +extern const char* ERR_FORWARD_INVALID_PROTOCOL; +extern const char* ERR_FORWARD_BIND_PORT_FAIL; +extern const char* ERR_PACKAGE_TYPE_UNKNOWN; +extern const char* ERR_PACKAGE_GET_TEMP_PATH_FAIL; +extern const char* ERR_PACKAGE_GET_TYPE_FAIL; +extern const char* ERR_PACKAGE_ID_NOT_EXIST; +extern const char* ERR_PACKAGE_ID_NOT_UNIQUE; +extern const char* ERR_LAUNCH_M_OPTION_SUPPORT; +extern const char* ERR_LAUNCH_M_OPTION_ARGUMENT; +extern const char* ERR_LAUNCH_P_OPTION_DEBUG_MODE; +extern const char* ERR_LAUNCH_ATTACH_OPTION_DEBUG_MODE; + +#endif /* SDB_MESSAGE_H_ */ diff --git a/src/sdb_model.c b/src/sdb_model.c index 7928906..31e9a45 100644 --- a/src/sdb_model.c +++ b/src/sdb_model.c @@ -32,6 +32,7 @@ #include "sdb_model.h" #include "linkedlist.h" #include "log.h" +#include "sdb_messages.h" const COMMAND NULL_COMMAND = { NULL, @@ -138,7 +139,7 @@ int parse_opt(int argc, char** argv, LIST_NODE* opt_list, LIST_NODE** result_lis D("Parse option: %s with longname %d\n", name, longname); OPTION* option = get_option(opt_list, name, longname); if(option == NULL) { - fprintf(stderr, "unrecognized option: %s\n", name); + print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_RUN_COMMAND_FAILED, F(ERR_COMMAND_OPTION_NO_SUPPORT, name)); return -1; } @@ -149,7 +150,8 @@ int parse_opt(int argc, char** argv, LIST_NODE* opt_list, LIST_NODE** result_lis local_pass_arg++; } else { - fprintf(stderr, "option: %s should have a argument\n", name); + print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_RUN_COMMAND_FAILED, F(ERR_COMMAND_OPTION_MUST_HAVE_ARGUMENT, name)); + print_info(ERR_COMMAND_OPTION_MUST_HAVE_ARGUMENT, name); return -1; } } @@ -171,6 +173,6 @@ int parse_opt(int argc, char** argv, LIST_NODE* opt_list, LIST_NODE** result_lis } int null_function (int argc, char** argv) { - fprintf(stderr, "unsupported command: %s\n", argv[0]); + print_error(SDB_MESSAGE_ERROR, ERR_COMMAND_RUN_COMMAND_FAILED, F(ERR_COMMAND_COMMAND_NO_SUPPORT, argv[0])); return -1; } diff --git a/src/sockets.c b/src/sockets.c index 82c8647..58a931e 100755 --- a/src/sockets.c +++ b/src/sockets.c @@ -30,6 +30,7 @@ #include "log.h" #include "listener.h" #include "sdb.h" +#include "sdb_messages.h" #define TRACE_TAG TRACE_SOCKETS @@ -612,7 +613,7 @@ static int parse_host_service(char* host_str, char** service_ptr, TRANSPORT** t, char* serial = host_str + prefix_len; char* end = strchr(serial, ':'); if(end == NULL) { - *err_str = (char*)ERR_TRANSPORT_TARGET_NOT_FOUND; + *err_str = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_TARGET_NOT_FOUND, NULL); return 0; } @@ -665,17 +666,17 @@ static int handle_request_with_t(SDB_SOCKET* socket, char* service, TRANSPORT* t local = strtok(request, ";"); remote = strtok(NULL , ";"); if(remote == 0 || remote[1] == '\0') { - forward_err = "malformed forward spec"; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_FORWARD_INVALID_PROTOCOL, NULL); goto sendfail; } if(strncmp("tcp:", remote, 4)){ - forward_err = (char*)ERR_FORWARD_UNKNOWN_REMOTE_PORT; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_FORWARD_UNSUPPORT_TRANSMISSION_PROTOCOL, NULL); goto sendfail; } if (t == NULL || t->connection_state == CS_OFFLINE) { if(t != NULL) { - forward_err = (char*)ERR_TRANSPORT_TARGET_OFFLINE; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_TARGET_OFFLINE, NULL); } else { forward_err = err_str; @@ -685,7 +686,7 @@ static int handle_request_with_t(SDB_SOCKET* socket, char* service, TRANSPORT* t } if(strncmp("tcp:", local, 4)){ - forward_err = (char*)ERR_FORWARD_UNKNOWN_LOCAL_PORT; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_FORWARD_UNSUPPORT_TRANSMISSION_PROTOCOL, NULL); goto sendfail; } @@ -695,7 +696,7 @@ static int handle_request_with_t(SDB_SOCKET* socket, char* service, TRANSPORT* t return 0; } else { - forward_err = (char*)ERR_FORWARD_INSTALL_FAIL; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_FORWARD_INSTALL_FAIL, NULL); goto sendfail; } } else if (forward == 15) { @@ -703,7 +704,7 @@ static int handle_request_with_t(SDB_SOCKET* socket, char* service, TRANSPORT* t writex(socket->fd, "OKAYOKAY", 8); return 0; } else { - forward_err = (char*)ERR_FORWARD_REMOVE_FAIL; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_FORWARD_REMOVE_FAIL, NULL); goto sendfail; } } @@ -720,7 +721,7 @@ sendfail: LISTENER* l = currentptr->data; if (l->type == forwardListener) { if(remove_listener(l->local_port)){ - forward_err = (char*)ERR_FORWARD_REMOVE_FAIL; + forward_err = error_message(SDB_MESSAGE_ERROR, ERR_FORWARD_REMOVE_FAIL, NULL); goto sendfail; } } @@ -819,9 +820,9 @@ static int find_transports(char **serial_out, const char *prefix) if (nr == 1 && match) { *serial_out = strdup(match); } else if (nr == 0) { - asprintf(serial_out, "device not found"); + asprintf(serial_out, error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_TARGET_NOT_FOUND, NULL)); } else if (nr > 1) { - asprintf(serial_out, "more than one device and emulator"); + asprintf(serial_out, error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_MORE_THAN_ONE_TARGET, NULL)); } return nr; @@ -926,7 +927,7 @@ connect_done: cmd_buf[0] = '\0'; kick_transport(t); } else { - snprintf(cmd_buf, cbuf_size, "No such device %s", serial); + snprintf(cmd_buf, cbuf_size, error_message(SDB_MESSAGE_ERROR, F("remote target '%s' not connected", serial), NULL)); } } else { unregister_all_tcp_transports(); @@ -1182,8 +1183,8 @@ static int smart_socket_enqueue(SDB_SOCKET *s, PACKET *p) if(result == 1) { return result; - } + } char* host_str = (char *)p->data + 4; char *service = NULL; char* err_str = NULL; @@ -1196,10 +1197,10 @@ static int smart_socket_enqueue(SDB_SOCKET *s, PACKET *p) } else { if(t != NULL) { if(t->suspended) { - err_str =(char*)ERR_TRANSPORT_TARGET_SUSPENDED; + err_str = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_TARGET_SUSPENDED, NULL); } else { - err_str = (char*)ERR_TRANSPORT_TARGET_OFFLINE; + err_str = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_TARGET_OFFLINE, NULL); } } LOG_ERROR("LS(%X) get no transport", s->local_id); @@ -1364,7 +1365,7 @@ static void connect_emulator(char* host, int port, char* buf, int buf_len) { int fd = sdb_host_connect(host, port, SOCK_STREAM); if (fd < 0) { - snprintf(buf, buf_len, "fail to connect to %s", host); + snprintf(buf, buf_len, error_message(SDB_MESSAGE_ERROR, F(ERR_CONNECT_CONNECT_REMOTE_TARGET_FAILED, host), NULL)); return; } diff --git a/src/transport.c b/src/transport.c index c0b4d97..eaec087 100755 --- a/src/transport.c +++ b/src/transport.c @@ -28,6 +28,7 @@ #include "memutils.h" #include "listener.h" #include "log.h" +#include "sdb_messages.h" #define TRACE_TAG TRACE_TRANSPORT @@ -113,6 +114,7 @@ static void dump_hex( const unsigned char* ptr, size_t len) } } + void kick_transport(TRANSPORT* t) { @@ -158,7 +160,6 @@ void run_transport_close(TRANSPORT* t) } } - void dump_packet(const char* name, const char* func, PACKET* p) { if(trace_packet) { @@ -545,7 +546,7 @@ TRANSPORT *acquire_one_transport(transport_type ttype, const char* serial, char* } else { if(ttype == kTransportAny) { if (result) { - *error_out = (char*)ERR_TRANSPORT_MORE_THAN_ONE_TARGET; + *error_out = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_MORE_THAN_ONE_TARGET, NULL); result = NULL; goto exit; } @@ -555,10 +556,10 @@ TRANSPORT *acquire_one_transport(transport_type ttype, const char* serial, char* (ttype == kTransportLocal && transport_->type == kTransportConnect)) { if (result) { if(ttype == kTransportUsb) { - *error_out = (char*)ERR_TRANSPORT_MORE_THAN_ONE_DEV; + *error_out = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_MORE_THAN_ONE_DEV, NULL); } else if(ttype == kTransportLocal) { - *error_out = (char*)ERR_TRANSPORT_MORE_THAN_ONE_EMUL; + *error_out = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_MORE_THAN_ONE_EMUL, NULL); } result = NULL; goto exit; @@ -569,7 +570,7 @@ TRANSPORT *acquire_one_transport(transport_type ttype, const char* serial, char* } if (result == NULL ) { - *error_out = (char*)ERR_TRANSPORT_TARGET_NOT_FOUND; + *error_out = error_message(SDB_MESSAGE_ERROR, ERR_CONNECT_TARGET_NOT_FOUND, NULL); } exit: diff --git a/src/utils.c b/src/utils.c index 449f9ea..cddd915 100755 --- a/src/utils.c +++ b/src/utils.c @@ -26,6 +26,7 @@ #include "fdevent.h" #include "sdb_constants.h" #include "strutils.h" +#include "sdb_messages.h" #define TRACE_TAG TRACE_SDB @@ -56,7 +57,7 @@ int is_directory(char* path) { } #endif - fprintf(stderr,"cannot stat '%s': %s\n", path, strerror(errno)); + print_error(SDB_MESSAGE_ERROR, F(ERR_SYNC_STAT_FAIL, path), strerror(errno)); return -1; } diff --git a/src/utils_unix.c b/src/utils_unix.c index aec8ad9..4f9f7db 100755 --- a/src/utils_unix.c +++ b/src/utils_unix.c @@ -48,6 +48,7 @@ #define TRACE_TAG TRACE_SYSDEPS #include "utils_backend.h" #include "log.h" +#include "sdb_messages.h" static int _launch_server(void) { @@ -55,7 +56,7 @@ static int _launch_server(void) switch (pid = fork()) { case -1: - fprintf(stderr, "Couldn't fork: %s\n", strerror(errno)); + LOG_ERROR("failed to fork process: %s\n", strerror(errno)); return -1; // never reached! case 0: { @@ -81,7 +82,7 @@ static int _launch_server(void) setsid(); execl(path, "sdb", "fork-server", "server", NULL); - fprintf(stderr, "Couldn't exec: '%s'\n", strerror(errno)); + LOG_ERROR("failed to execute process: %s\n", strerror(errno)); _exit(-1); } default: @@ -113,11 +114,11 @@ static void _start_logging(void) fd = unix_open("/tmp/sdb.log", O_WRONLY | O_CREAT | O_APPEND, 0640); if(fd < 0) { - fprintf(stderr, "fail to open '/tmp/sdb.log' logging fails\n"); + print_error(SDB_MESSAGE_ERROR ,ERR_GENERAL_LOG_FAIL, F(ERR_SYNC_OPEN_FAIL, "/tmp/sdb/log")); fd = unix_open("/dev/null", O_WRONLY); if( fd < 0 ) { - fprintf(stderr, "fail to open /dev/null\n"); - fprintf(stderr,"--- sdb starting (pid %d) ---\n", getpid()); + print_error(SDB_MESSAGE_ERROR ,ERR_GENERAL_LOG_FAIL, F(ERR_SYNC_OPEN_FAIL, "/dev/null")); + LOG_DEBUG("--- sdb starting (pid %d) ---\n", getpid()); return; } } @@ -126,7 +127,9 @@ static void _start_logging(void) dup2(fd, 2); sdb_close(fd); - fprintf(stderr,"--- sdb starting (pid %d) ---\n", getpid()); + LOG_DEBUG("--- sdb starting (pid %d) ---\n", getpid()); + return; + } static char* _ansi_to_utf8(const char *str) @@ -146,7 +149,7 @@ static void _close_on_exec(int fd) { int ret = fcntl( fd, F_SETFD, FD_CLOEXEC ); if (ret == -1) - fprintf(stderr, "fail to set the file descriptor to be closed when the process executes another program\n"); + LOG_ERROR("failed to set the file descriptor '%d': %s",fd ,strerror(errno)); } static int _sdb_open( const char* pathname, int options ) @@ -253,7 +256,7 @@ static void _disable_tcp_nagle(int fd) int on = 1; int ret = setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) ); if (ret == -1) - fprintf(stderr, "fail to set level option of FD(%d)", fd); + LOG_ERROR("failed to set the file descriptor '%d': %s\n", fd, strerror(errno)); } static int _sdb_thread_create( sdb_thread_t *pthread, sdb_thread_func_t start, void* arg ) @@ -344,7 +347,7 @@ static int _sdb_port_listen(uint32_t inet, int port, int type) n = 1; int ret = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)); if (ret == -1) - fprintf(stderr, "fail to set level option of FD(%d)", s); + LOG_ERROR("failed to set the file descriptor '%d': %s\n", s, strerror(errno)); if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { sdb_close(s); -- 2.34.1