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

authentification.php

Blame
  • server.c 3,67 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 <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()
    
    char *db_path   = NULL;
    char *kara_dir  = NULL;
    char *conf_file = NULL;
    struct lkt_state srv;
    
    static void
    __garbage_collect(void)
    {
        if (db_path)
            free(db_path);
        if (kara_dir)
            free(kara_dir);
        if (conf_file)
            free(conf_file);
        if (srv.win.free)
            srv.win.free(&srv.win);
        LOG_INFO_SCT("GARBAGE", "%s", "Cleaning a bit with global variables");
    }
    
    static void
    __sig_exit(int sig)
    {
        LOG_ERROR_SCT("SIGNAL", "Exit because of signal %d", sig);
        exit(EXIT_FAILURE);
    }
    
    int
    main(int argc, char *argv[])
    {
        struct passwd *pw = getpwuid(getuid());
        char exe[PATH_MAX];
        pthread_t th;
        executable_name = "lektord";
        UNUSED(argv);
        assert(!signal(SIGTERM, __sig_exit));
        assert(!signal(SIGQUIT, __sig_exit));
        assert(!signal(SIGINT, __sig_exit));
    
        if (argc <= 1)
            goto normal_launch;
        help__();
        return EXIT_FAILURE;
    
    normal_launch:
        LOG_INFO_SCT("GENERAL", "Lektor launched by user %s (shell: %s, home: %s)",
                     pw->pw_name, pw->pw_shell, pw->pw_dir);
        reg_set(server_reg);
        mthread_init();
        pthread_create(&th, NULL, mthread_main, NULL);
        if (read_self_exe(exe, PATH_MAX))
            LOG_WARN_SCT("GENERAL", "%s", "Failed to read self executable path, "
                         "restart may not work");
        executable_name = exe;
    
        /* Init the server */
        int autoclear, check_exclusive = 1;
        assert(!atexit(__garbage_collect));
        db_path   = safe_malloc(PATH_MAX * sizeof(char));
        kara_dir  = safe_malloc(PATH_MAX * sizeof(char));
        conf_file = safe_malloc(PATH_MAX * sizeof(char));
        memset(&srv, 0, sizeof(struct lkt_state));
    
        /* Initialize the system. */
        RETURN_UNLESS(database_new(&srv.db),                   "Failed to initialize the memory database", 1);
        RETURN_IF    (config_detect_file(conf_file, PATH_MAX), "Failed to find a config file",             1);
        RETURN_IF    (config_new(srv.db, conf_file),           "Failed to read configuration file",        1);
    
        /* 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);
    
        /* 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 (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);
        lkt_listen(&srv);
        return EXIT_FAILURE;
    }