Ejemplo n.º 1
0
void taskmain(int argc, char *argv[])
{
    LOG_FILE = stderr;
    glob_t profile_glob;
    int rc = 0;
    int i = 0;
    Action *action = NULL;
    tst_t *targets = NULL;
    bstring pid_file = NULL;

    check(argc == 3, "USAGE: procer <profile_dir> <procer_pid_file>");
    pid_file = bfromcstr(argv[2]);

    rc = Unixy_remove_dead_pidfile(pid_file);
    check(rc == 0, "Failed to remove %s, procer is probably already running.", bdata(pid_file));

    rc = Unixy_daemonize();
    check(rc == 0, "Couldn't daemonize, that's not good.");

    rc = chdir(argv[1]);
    check(rc == 0, "Couldn't change to %s profile dir.", argv[1]);

    rc = Unixy_pid_file(pid_file);
    check(rc == 0, "Failed to make the PID file: %s", bdata(pid_file));

    FILE *log = fopen("error.log", "a+");
    check(log, "Couldn't open error.log");
    setbuf(log, NULL);
    LOG_FILE = log;

    bstring dir_glob = bformat("%s/[A-Za-z0-9]*", argv[1]);
    check(dir_glob, "Couldn't make the directory glob.");

    rc = glob(bdata(dir_glob), GLOB_ERR | GLOB_ONLYDIR, NULL, &profile_glob);
    check(rc == 0, "Failed to find directories in the profiles.");

    debug("Loading %zu actions.", profile_glob.gl_pathc);
    for(i = 0; i < profile_glob.gl_pathc; i++) {
        action = Action_create(profile_glob.gl_pathv[i]);
        targets = tst_insert(targets, bdata(action->name), blength(action->name), action);
    }

    // now we setup the dependencies from the settings they've got
    tst_traverse(targets, Action_dependency_assign, targets);
    tst_traverse(targets, Action_start_all, NULL);

    taskexit(0);

error:
    taskexitall(1);
}
Ejemplo n.º 2
0
int attempt_chroot_drop(Server *srv)
{
    int rc = 0;

    if(Unixy_chroot(srv->chroot) == 0) {
        log_info("All loaded up, time to turn into a server.");
        log_info("-- Starting " VERSION ". Copyright (C) Zed A. Shaw. Licensed BSD.\n");
        log_info("-- Look in %s for startup messages and errors.", bdata(srv->error_log));

        check(access("/run", F_OK) == 0, "/run directory doesn't exist in %s or isn't owned right.", bdata(srv->chroot));
        check(access("/tmp", F_OK) == 0, "/tmp directory doesn't exist in %s or isn't owned right.", bdata(srv->chroot));

        rc = Unixy_daemonize();
        check(rc == 0, "Failed to daemonize, looks like you're hosed.");

        FILE *log = fopen(bdata(srv->error_log), "a+");
        check(log, "Couldn't open %s log file.", bdata(srv->error_log));
        setbuf(log, NULL);

        dbg_set_log(log);

        rc = Unixy_pid_file(srv->pid_file);
        check(rc == 0, "Failed to make the PID file %s", bdata(srv->pid_file));

        rc = Unixy_drop_priv(&PRIV_DIR);
        check(rc == 0, "Failed to drop priv to the owner of %s", bdata(&PRIV_DIR));

    } else {
        log_warn("Couldn't chroot too %s, assuming running in test mode.", bdata(srv->chroot));

        // rewrite the access log to be in the right location
        bstring temp = bformat("%s%s", bdata(srv->chroot), bdata(srv->access_log));
        bassign(srv->access_log, temp);
        bdestroy(temp);

        temp = bformat(".%s", bdata(srv->pid_file));
        bassign(srv->pid_file, temp);
        bdestroy(temp);

        rc = Unixy_pid_file(srv->pid_file);
        check(rc == 0, "Failed to make the PID file %s", bdata(srv->pid_file));
    }

    return 0;

error:
    return -1;
}
Ejemplo n.º 3
0
int attempt_chroot_drop(Server *srv)
{
    int rc = 0;

    log_info("All loaded up, time to turn into a server.");
    log_info("-- Starting " VERSION ". Copyright (C) Zed A. Shaw. Licensed BSD.\n");
    log_info("-- Look in %s for startup messages and errors.", bdata(srv->error_log));

    int testmode = 0;

    if(srv->chroot != NULL) {
        if(Unixy_chroot(srv->chroot) == 0) {
            if(Setting_get_int("server.daemonize", 1)) {
                rc = Unixy_daemonize(1); // 1 == chdir /
                check(rc == 0, "Failed to daemonize, looks like you're hosed.");
            }
            else {
                rc = chdir("/");
                check(rc == 0, "Failed to change working directory to '/'.");
            }
        } else {
            log_warn("Couldn't chroot to %s, assuming running in test mode.", bdata(srv->chroot));

            // rewrite the access log to be in the right location
            bstring temp = bformat("%s%s", bdata(srv->chroot), bdata(srv->access_log));
            bassign(srv->access_log, temp);
            bdestroy(temp);

            temp = bformat(".%s", bdata(srv->pid_file));
            bassign(srv->pid_file, temp);
            bdestroy(temp);

            testmode = 1;
        }
    } else {
        if(Setting_get_int("server.daemonize", 1)) {
            rc = Unixy_daemonize(0); // 0 == don't chdir
            check(rc == 0, "Failed to daemonize, looks like you're hosed.");
        }
    }

    rc = Unixy_pid_file(srv->pid_file);
    check(rc == 0, "Failed to make the PID file %s", bdata(srv->pid_file));

    if(srv->chroot != NULL && ! testmode) {
        rc = Unixy_drop_priv(&PRIV_DIR);
        check(rc == 0, "Failed to drop priv to the owner of %s", bdata(&PRIV_DIR));
    } else {
        rc = Unixy_drop_priv(NULL);
        check(rc == 0, "Failed to drop priv");
    }

    if(!testmode) {
        FILE *log = fopen(bdata(srv->error_log), "a+");
        check(log, "Couldn't open %s log file.", bdata(srv->error_log));
        setbuf(log, NULL);
        check(0==add_log_to_rotate_list(srv->error_log,log),"Unable to add error log to list of files to rotate.");

        dbg_set_log(log);
    }

    return 0;

error:
    return -1;
}
Ejemplo n.º 4
0
void taskmain(int argc, char *argv[])
{
    dbg_set_log(stderr);
    glob_t profile_glob;
    int rc = 0;
    int i = 0;
    Action *action = NULL;
    tst_t *targets = NULL;
    bstring pid_file = NULL;
    char *procer_error_log = NULL;

    m2program = procer_program;

    check(argc == 3, "USAGE: %s <profile_dir> <procer_pid_file>", m2program);
    pid_file = bfromcstr(argv[2]);

    srand(time(NULL)); // simple randomness

    rc = Unixy_remove_dead_pidfile(pid_file);
    check(rc == 0, "Failed to remove %s, %s is probably already running.", bdata(pid_file), m2program);

    rc = Unixy_daemonize(1);
    check(rc == 0, "Couldn't daemonize, that's not good.");

    rc = chdir(argv[1]);
    check(rc == 0, "Couldn't change to %s profile dir.", argv[1]);

    rc = Unixy_pid_file(pid_file);
    check(rc == 0, "Failed to make the PID file: %s", bdata(pid_file));

    if( (procer_error_log = getenv("PROCER_ERROR_LOG")) == NULL)
        procer_error_log = "error.log";
    FILE *log = fopen(procer_error_log, "a+");
    check(log, "Couldn't open error.log");
    setbuf(log, NULL);
    dbg_set_log(log);

    bstring dir_glob = bformat("%s/[A-Za-z0-9]*", argv[1]);
    check(dir_glob, "Couldn't make the directory glob.");

    rc = glob(bdata(dir_glob), GLOB_ERR, NULL, &profile_glob);
    check(rc == 0, "Failed to find directories in the profiles.");

    struct stat sb;
    debug("Loading %zu actions.", profile_glob.gl_pathc);

    start_terminator();

    while(RUNNING) {
        for(i = 0; i < profile_glob.gl_pathc; i++) {
            rc = lstat(profile_glob.gl_pathv[i], &sb);
            check(rc == 0, "Failed to stat file or directory: %s", profile_glob.gl_pathv[i]);

            if (sb.st_mode & S_IFDIR) {
                action = Action_create(profile_glob.gl_pathv[i]);
                targets = tst_insert(targets, bdata(action->name), blength(action->name), action);
            }
        }

        // now we setup the dependencies from the settings they've got
        tst_traverse(targets, Action_dependency_assign, targets);
        tst_traverse(targets, Action_start_all, NULL);

        while(RUNNING) {
            Action_sleep(1);
        }
    }

    log_warn("Exiting as requested from procer.");

    taskexit(0);

error:
    taskexitall(1);
}