Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • d29ae2d10783f448b264dc30d3482568c4872292
  • master par défaut protégée
2 résultats

events.go

Blame
  • server.c 4,12 Kio
    #define _POSIX_C_SOURCE 200809L
    
    #include <common/common.h>
    #include <lektor/config.h>
    #include <lektor/net.h>
    #include <lektor/cmd.h>
    #include <lektor/reg.h>
    #include <lektor/database.h>
    #include <mthread/mthread.h>
    #include <lektor/commands.h>
    
    #include <assert.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <strings.h>
    #include <pwd.h>
    #include <pthread.h>
    
    #ifdef _STATIC_SDL2
    #include <lektor/module/module_sdl2.h>
    #endif
    #ifdef _STATIC_X11
    #include <lektor/module/module_x11.h>
    #endif
    
    REG_BEGIN(server_reg)
    REG_ADD(load_repo_https)
    #ifdef _STATIC_SDL2
    REG_ADD(load_sdl2)
    #endif
    #ifdef _STATIC_X11
    REG_ADD(load_x11)
    #endif
    REG_END()
    
    int
    main(int argc, char *argv[])
    {
        struct passwd *pw = getpwuid(getuid());
        char exe[PATH_MAX];
        int autoclear, check_exclusive = 1, opt;
        char *conf_file = safe_zero_malloc(PATH_MAX * sizeof(char));
        pthread_t th;
        executable_name = "lektord";
    
        if (argc <= 1)
            goto normal_launch;
    
        /* Check args */
        while ((opt = getopt(argc, argv, "hFf:")) != -1) {
            switch (opt) {
            case 'F':
                check_exclusive = 0;
                break;
            case 'f':
                strncpy(conf_file, optarg, PATH_MAX);
                break;
            case 'h':
            default:
                help__();
                exit(EXIT_SUCCESS);
            }
        }
    
    normal_launch:
        reg_export(server_reg);
        mthread_init();
        pthread_create(&th, NULL, mthread_main, NULL);
        if (read_self_exe(exe, PATH_MAX))
            LOG_WARN("GENERAL", "%s", "Failed to read self executable path, "
                     "restart may not work");
        executable_name = exe;
    
        /* Init the server */
        char *db_path   = safe_malloc(PATH_MAX * sizeof(char));
        char *kara_dir  = safe_malloc(PATH_MAX * sizeof(char));
        struct lkt_state srv = {
            .kara_prefix = kara_dir,
        };
        if (lkt_queue_new(&srv.queue)) {
            LOG_ERROR("INIT", "%s", "Faield to create server queue");
            exit(EXIT_FAILURE);
        }
    
        /* Initialize the system. */
        if (!database_new(&srv.db)) {
            LOG_ERROR("INIT", "%s", "Failed to init memory db");
            exit(EXIT_FAILURE);
        }
    
        if (conf_file[0] == '\0' && config_detect_file(conf_file, PATH_MAX)) {
            LOG_ERROR("INIT", "%s", "Failed to find a config file");
            exit(EXIT_FAILURE);
        }
    
        if (config_new(srv.db, conf_file)) {
            LOG_ERROR("INIT", "%s", "Failed to read the config");
            exit(EXIT_FAILURE);
        }
        free(conf_file);
    
        /* Finish to initialize. */
        database_config_get_text(srv.db, "database", "db_path", db_path, PATH_MAX);
        RETURN_UNLESS(database_open(srv.db, db_path, check_exclusive), "Can't open database", 1);
        free(db_path);
    
        /* Read the configuration. We already know that the config is valid */
        database_config_get_int (srv.db, "player",   "autoclear", &autoclear);
        database_config_get_text(srv.db, "database", "kara_dir",  kara_dir, PATH_MAX);
        database_config_get_text(srv.db, "server",   "host",      srv.host, HOST_NAME_MAX);
        database_config_get_text(srv.db, "server",   "port",      srv.port, 5);
    
        if (kara_dir[strlen(kara_dir) - 1] != '/')
            strncat(kara_dir, "/", PATH_MAX - 1);
        srv.kara_prefix = kara_dir;
    
        database_config_queue_default(srv.db);
        if (!env_get(LKT_ENV_RESTART) && autoclear)
            database_queue_clear(srv.db);
    
        RETURN_IF(load_module_by_name(&srv, "player", &srv.win),  "Can't load module player", 3);
        RETURN_IF(load_module_by_name(&srv, "repo",   &srv.repo), "Can't load module repo", 3);
    
        /* Get ENV */
        /* Not working -> race condition with player module */
        char *env_current = env_get(LKT_ENV_CURRENT);
        if (env_current && env_current[0] && !STR_MATCH(env_current, "NULL")) {
            LOG_INFO("INIT", "Restart playback from %s", env_current);
            lkt_queue_send(&srv.queue, lkt_event_play_pos, (void *) (size_t) strtol(env_current, NULL, 0));
        }
    
        LOG_INFO("GENERAL", "Lektor was %s, user: %s, shell: %s, home: %s",
                 env_get(LKT_ENV_RESTART) ? "restarted" : "started",
                 pw->pw_name, pw->pw_shell, pw->pw_dir);
    
        lkt_listen(&srv);
        return EXIT_FAILURE;
    }