diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2020-04-10 21:04:21 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2020-04-10 21:07:52 +0300 |
commit | a9da878ff22f980cea3bf3102113d7f2e65f61e9 (patch) | |
tree | dd3e21c580b91f6ab3d64e228ccb23170f3ddac3 /src/exec.c | |
parent | da966c314f92d17b45ede9aa77ad3d3624c36725 (diff) | |
download | wydawca-a9da878ff22f980cea3bf3102113d7f2e65f61e9.tar.gz wydawca-a9da878ff22f980cea3bf3102113d7f2e65f61e9.tar.bz2 |
Change indentation to improve readability.
Diffstat (limited to 'src/exec.c')
-rw-r--r-- | src/exec.c | 230 |
1 files changed, 113 insertions, 117 deletions
@@ -25,139 +25,135 @@ static FILE * start_prog(int argc, const char **argv, pid_t * ppid) { - int p[2]; - FILE *fp; - pid_t pid; - int i; - - if (pipe(p)) { - wy_log(LOG_CRIT, "pipe: %s", strerror(errno)); - return NULL; + int p[2]; + FILE *fp; + pid_t pid; + int i; + + if (pipe(p)) { + wy_log(LOG_CRIT, "pipe: %s", strerror(errno)); + return NULL; + } + + switch (pid = fork()) { + case 0: + /* Child process */ + + if (p[1] != 1 && dup2(p[1], 1) == -1) { + wy_log(LOG_CRIT, "dup2: %s", strerror(errno)); + _exit(EX_UNAVAILABLE); } - switch (pid = fork()) { - case 0: - /* Child process */ - - if (p[1] != 1 && dup2(p[1], 1) == -1) { - wy_log(LOG_CRIT, "dup2: %s", strerror(errno)); - _exit(EX_UNAVAILABLE); - } - - if (p[1] != 1 && dup2(p[1], 2) == -1) { - wy_log(LOG_CRIT, "dup2: %s", strerror(errno)); - _exit(EX_UNAVAILABLE); - } - - close(p[0]); - - /* Close unneded descripitors */ - for (i = getdtablesize(); i > 2; i--) - close(i); - - execvp(argv[0], (char **)argv); - wy_log(LOG_CRIT, _("cannot run %s: %s"), argv[0], - strerror(errno)); - exit(EX_UNAVAILABLE); - - case -1: - wy_log(LOG_CRIT, _("cannot run `%s': fork failed: %s"), - argv[0], strerror(errno)); - return NULL; - - default: - /* Master process */ - close(p[1]); - fp = fdopen(p[0], "r"); - if (!fp) - wy_log(LOG_ERR, _("cannot fdopen: %s"), - strerror(errno)); - *ppid = pid; + if (p[1] != 1 && dup2(p[1], 2) == -1) { + wy_log(LOG_CRIT, "dup2: %s", strerror(errno)); + _exit(EX_UNAVAILABLE); } - return fp; + + close(p[0]); + + /* Close unneded descripitors */ + for (i = getdtablesize(); i > 2; i--) + close(i); + + execvp(argv[0], (char **) argv); + wy_log(LOG_CRIT, _("cannot run %s: %s"), argv[0], strerror(errno)); + exit(EX_UNAVAILABLE); + + case -1: + wy_log(LOG_CRIT, _("cannot run `%s': fork failed: %s"), + argv[0], strerror(errno)); + return NULL; + + default: + /* Master process */ + close(p[1]); + fp = fdopen(p[0], "r"); + if (!fp) + wy_log(LOG_ERR, _("cannot fdopen: %s"), strerror(errno)); + *ppid = pid; + } + return fp; } /* Log everything read from FP as the output from the program PROG, using syslog priority PRIO. */ void -log_output(int prio, const char *prog, FILE *fp) +log_output(int prio, const char *prog, FILE * fp) { - size_t size = 0; - char *buf = NULL; - - wy_log(prio, _("%s output follows:"), prog); - while (grecs_getline(&buf, &size, fp) > 0) - wy_log(prio, "%s", buf); - wy_log(prio, _("end of %s output"), prog); - free(buf); + size_t size = 0; + char *buf = NULL; + + wy_log(prio, _("%s output follows:"), prog); + while (grecs_getline(&buf, &size, fp) > 0) + wy_log(prio, "%s", buf); + wy_log(prio, _("end of %s output"), prog); + free(buf); } /* Execute ARGC/ARGV. Return the exit code in RETCODE. */ enum exec_result wydawca_exec(int argc, const char **argv, int *retcode) { - FILE *fp; - pid_t pid, npid; - int status; - int i; - enum exec_result res; - - fp = start_prog(5, argv, &pid); - if (!fp) { - wy_log(LOG_CRIT, _("cannot start %s"), argv[0]); - return exec_error; - } - - for (i = 0; i < 5 && (npid = waitpid(pid, &status, WNOHANG)) == 0; i++) - sleep(1); - - switch (npid) { - case -1: - wy_log(LOG_CRIT, _("cannot execute %s: waitpid failed: %s"), - argv[0], strerror(errno)); - fclose(fp); - return exec_error; - - case 0: - wy_log(LOG_CRIT, - _("cannot execute %s: the process did not respond " - "within 5 seconds: %s"), argv[0], strerror(errno)); - kill(pid, SIGKILL); - fclose(fp); - return exec_error; - - default: - break; - } + FILE *fp; + pid_t pid, npid; + int status; + int i; + enum exec_result res; + + fp = start_prog(5, argv, &pid); + if (!fp) { + wy_log(LOG_CRIT, _("cannot start %s"), argv[0]); + return exec_error; + } + + for (i = 0; i < 5 && (npid = waitpid(pid, &status, WNOHANG)) == 0; i++) + sleep(1); + + switch (npid) { + case -1: + wy_log(LOG_CRIT, _("cannot execute %s: waitpid failed: %s"), + argv[0], strerror(errno)); + fclose(fp); + return exec_error; - if (WIFEXITED(status)) { - int rc = WEXITSTATUS(status); - if (rc) { - res = exec_fail; - wy_log(LOG_ERR, _("command %s returned %d"), argv[0], - rc); - log_output(LOG_ERR, argv[0], fp); - } else { - res = exec_success; - if (wy_debug_level > 1) - log_output(LOG_DEBUG, argv[0], fp); - } - if (retcode) - *retcode = rc; + case 0: + wy_log(LOG_CRIT, + _("cannot execute %s: the process did not respond " + "within 5 seconds: %s"), argv[0], strerror(errno)); + kill(pid, SIGKILL); + fclose(fp); + return exec_error; + + default: + break; + } + + if (WIFEXITED(status)) { + int rc = WEXITSTATUS(status); + if (rc) { + res = exec_fail; + wy_log(LOG_ERR, _("command %s returned %d"), argv[0], rc); + log_output(LOG_ERR, argv[0], fp); } else { - res = exec_error; - if (WIFSIGNALED(status)) - wy_log(LOG_ERR, _("%s terminated on signal %d"), - argv[0], WTERMSIG(status)); - else if (WIFSTOPPED(status)) - wy_log(LOG_ERR, _("%s stopped on signal %d"), - argv[0], WTERMSIG(status)); - else - wy_log(LOG_ERR, - _("%s terminated with unrecognized status"), - argv[0]); + res = exec_success; + if (wy_debug_level > 1) + log_output(LOG_DEBUG, argv[0], fp); } - fclose(fp); - - return res; + if (retcode) + *retcode = rc; + } else { + res = exec_error; + if (WIFSIGNALED(status)) + wy_log(LOG_ERR, _("%s terminated on signal %d"), + argv[0], WTERMSIG(status)); + else if (WIFSTOPPED(status)) + wy_log(LOG_ERR, _("%s stopped on signal %d"), + argv[0], WTERMSIG(status)); + else + wy_log(LOG_ERR, + _("%s terminated with unrecognized status"), argv[0]); + } + fclose(fp); + + return res; } |