int Action_exec(Action *action, Profile *prof) { int rc = 0; debug("ACTION: command=%s, pid_file=%s, restart=%d, depends=%s", bdata(prof->command), bdata(prof->pid_file), prof->restart, bdata(action->depends)); pid_t pid = fork(); check(pid >= 0, "Fork failed, WTF. How can fork fail?"); if(pid == 0) { rc = Unixy_drop_priv(action->profile_dir); if(rc != 0) { log_err("Not fatal, but we couldn't drop priv for %s", bdata(action->name)); } redirect_output("run.log"); rc = execle(bdata(prof->command), bdata(prof->command), NULL, environ); check(rc != -1, "Failed to exec command: %s", bdata(prof->command)); } else { int status = 0; debug("WAITING FOR CHILD."); pid = waitpid(pid, &status, 0); } debug("Command ran and exited successfully, now looking for the PID file."); return 0; error: return -1; }
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; }
int Action_exec(Action *action, Profile *prof) { int rc = 0; char *procer_run_log = NULL; bstring pidfile_env = bformat("PROCER_PIDFILE=%s", bdata(prof->pid_file)); putenv(bdata(pidfile_env)); bstring action_env = bformat("PROCER_ACTION=%s", bdata(action->name)); putenv(bdata(action_env)); debug("ACTION: command=%s, pid_file=%s, restart=%d, depends=%s", bdata(prof->command), bdata(prof->pid_file), prof->restart, bdata(action->depends)); pid_t pid = fork(); check(pid >= 0, "Fork failed, WTF. How can fork fail?"); if(pid == 0) { rc = Unixy_drop_priv(action->profile_dir); if(rc != 0) { log_err("Not fatal, but we couldn't drop priv for %s", bdata(action->name)); } if( (procer_run_log = getenv("PROCER_RUN_LOG")) == NULL) procer_run_log = "run.log"; redirect_output(procer_run_log); rc = execle(bdatae(prof->command, ""), bdatae(prof->command, ""), NULL, environ); check(rc != -1, "Failed to exec command: %s", bdata(prof->command)); } else { int status = 0; debug("WAITING FOR CHILD."); pid = waitpid(pid, &status, 0); } debug("Command ran and exited successfully, now looking for the PID file."); return 0; error: return -1; }
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; }