diff --git a/inc/lektor/common.h b/inc/lektor/common.h index 3bfe7e42ae06cf81ad29e1b0a2edfaf0c1ce9798..4b171116a63c63a3866c81979ccaea873b3159cd 100644 --- a/inc/lektor/common.h +++ b/inc/lektor/common.h @@ -49,7 +49,12 @@ LOG_DEBUG("DEBUG", "%s", msg); \ goto label; \ } +#define FAIL_IF(cond, ...) { \ + if (cond) { \ + LOG_FATAL(__VA_ARGS__); \ + }} +#define FAIL_UNLESS(cond, ...) FAIL_IF(!(cond), __VA_ARGS__) #define GOTO_UNLESS(cond, msg, label) GOTO_IF(!(cond), msg, label) #define RETURN_UNLESS(cond, msg, ret) RETURN_IF(!(cond), msg, ret) #define NOTHING /* Usefull to return nothing in previous macros */ @@ -124,10 +129,10 @@ void __lkt_log(enum log_level, const char *section, const char *func, const char #define LKT_MESSAGE_MAX 2048 #define LKT_DEFAULT_LIST_SIZE 10 -#define LKT_BACKLOG 32 -#define COMMAND_LIST_MAX 64 -#define BUFFER_OUT_MAX 16 -#define MPD_VERSION "0.21.16" +#define LKT_BACKLOG 32 +#define COMMAND_LIST_MAX 64 +#define BUFFER_OUT_MAX 16 +#define MPD_VERSION "0.21.16" typedef volatile enum { MPD_IDLE_NONE = 0, diff --git a/src/main/server.c b/src/main/server.c index cb68297692854d88348f5a0b9bba7ef3dd182496..ea8d5b7fe93eb5f1661bee42707606f98f2ffbd5 100644 --- a/src/main/server.c +++ b/src/main/server.c @@ -61,15 +61,11 @@ error: __attribute__((destructor)) static void ___tty_enable_echo(void) { - if (!___tty_must_be_restored) { - LOG_DEBUG("INIT", "No need to restore the tty"); - return; - } - if (tcsetattr(STDIN_FILENO, TCSANOW, &___tty_save) == -1) - LOG_ERROR("INIT", "Failed to reset termios flags"); + RETURN_UNLESS(___tty_must_be_restored, "No need to restore the tty", NOTHING); + RETURN_IF(tcsetattr(STDIN_FILENO, TCSANOW, &___tty_save) == -1, + "Failed to reset termios flags", NOTHING); errno = 0; - if (fflush(stdin) != 0) - LOG_ERROR("INIT", "Failed to flush stdin: %s", strerror(errno)); + RETURN_IF(fflush(stdin) != 0, "Failed to flush stdin", NOTHING); } /* Recursive mkdir, where the last word of the string is a file, not a folder. */ @@ -319,44 +315,32 @@ main(int argc, char *argv[]) memset(&srv, 0, sizeof(struct lkt_state)); srv.kara_prefix = kara_dir; - if (lkt_queue_new(&srv.queue)) { - LOG_ERROR("INIT", "Faield to create server queue"); - exit(EXIT_FAILURE); - } - - /* Initialize the system. */ - if (!database_new(&srv.db)) { - LOG_ERROR("INIT", "Failed to init memory db"); - exit(EXIT_FAILURE); - } + FAIL_IF(lkt_queue_new(&srv.queue), "Failed to create server queue"); + FAIL_UNLESS(database_new(&srv.db), "Failed to init memory database"); /* Read or create default config file. */ int retry_config_once = 0; retry_config: if (conf_file[0] == '\0' && config_detect_file(conf_file, PATH_MAX)) { - if (retry_config_once) { - LOG_ERROR("INIT", "Failed to find a config file"); - exit(EXIT_FAILURE); - } else { - LOG_INFO("INIT", "Creating default config file"); - config_default_file(conf_file, PATH_MAX); - __mkdir(conf_file); /* Create the folder for the file. */ - FILE *file_desc = fopen(conf_file, "w+"); - if (NULL == file_desc) - LOG_FATAL("Failed to open default config file '%s' to initialize it", conf_file); - config_default(file_desc); - fclose(file_desc); - LOG_INFO("INIT", "Default configuration file has been writen to %s", conf_file); - retry_config_once = 1; - goto retry_config; - } - } + FAIL_IF(retry_config_once, "Failed to find a config file"); + + LOG_INFO("INIT", "Creating default config file"); + config_default_file(conf_file, PATH_MAX); + __mkdir(conf_file); /* Create the folder for the file. */ - if (config_new(srv.db, conf_file)) { - LOG_ERROR("INIT", "Failed to read the config"); - exit(EXIT_FAILURE); + FILE *file_desc = fopen(conf_file, "w+"); + FAIL_UNLESS(file_desc, "Failed to open default config file '%s' to initialize it", + conf_file); + config_default(file_desc); + fclose(file_desc); + + LOG_INFO("INIT", "Default configuration file has been writen to %s", conf_file); + retry_config_once = 1; + goto retry_config; } + FAIL_IF(config_new(srv.db, conf_file), "Failed to read the config"); + /* Dump and abort here, if we are dumping informations about the server */ if (dump_and_abort) { fprintf(stderr, "Config file is: %s\n", conf_file); @@ -374,15 +358,13 @@ retry_config: database_config_get(srv.db, "server", "host", srv.host, HOST_NAME_MAX); database_config_get(srv.db, "server", "port", srv.port, 5); - /* Quick check with an explicit error message, to remide the users to create the kara folder */ - if (access(kara_dir, R_OK | W_OK)) { - LOG_ERROR("INIT", "No access in read / write for folder %s", kara_dir); - exit(EXIT_FAILURE); - } + /* Quick check with an explicit error message, to remide the users to + * create the kara folder */ + FAIL_IF(access(kara_dir, R_OK | W_OK), "No access in read / write for folder %s", kara_dir); /* Finish to initialize. */ database_config_get(srv.db, "database", "db_path", db_path, PATH_MAX); - RETURN_UNLESS(database_open(srv.db, db_path, check_exclusive), "Can't open database", 1); + FAIL_UNLESS(database_open(srv.db, db_path, check_exclusive), "Can't open database"); if (kara_dir[strlen(kara_dir) - 1] != '/') strncat(kara_dir, "/", PATH_MAX - 1); @@ -396,22 +378,18 @@ retry_config: lkt_queue_make_available(&srv.queue, lkt_event_prop); lkt_queue_make_available(&srv.queue, lkt_event_update); - { /* Module initialization */ - char *module = safe_malloc(sizeof(char) * PATH_MAX); - if (!module) { - LOG_ERROR("INIT", "Out of memory"); - return 1; - } + /* Module initialization */ + { + char module[PATH_MAX]; + database_config_get(srv.db, "player", "module", module, PATH_MAX); reg_import(module, &srv.window_mod.reg, &srv.window_mod.handle); + database_config_get(srv.db, "repo", "module", module, PATH_MAX); reg_import(module, &srv.repo_mod.reg, &srv.repo_mod.handle); - free(module); - if (MOD_CALL(srv.repo_mod, "new", &srv.queue, srv.db)) - return 100; - if (MOD_CALL(srv.window_mod, "new", &srv.queue, srv.db)) - return 101; + FAIL_IF(MOD_CALL(srv.repo_mod, "new", &srv.queue, srv.db), "Can't init repo module"); + FAIL_IF(MOD_CALL(srv.window_mod, "new", &srv.queue, srv.db), "Can't init player module"); } /* Get ENV */ @@ -424,10 +402,9 @@ retry_config: } } - LOG_INFO("INIT", "Lektor was %s", - env_get(LKT_ENV_RESTART) ? "restarted" : "started"); - + LOG_INFO("INIT", "Lektor was %s", env_get(LKT_ENV_RESTART) ? "restarted" : "started"); config_handle_hook(srv.db, "launched"); lkt_listen(&srv); + LOG_FATAL("The lkt_listen function returned, it shouldn't happen"); return EXIT_FAILURE; }