aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2026-04-22 12:45:16 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2026-04-22 12:45:16 -0400
commit4a993b0be12031dfad207f07044f46242fb3f6b1 (patch)
treea4976575e6f73bebea6f96e142692141af612f2f
parentadd80c7fbbe323c930538d5a5e1dd94d28274226 (diff)
downloadstasis-4a993b0be12031dfad207f07044f46242fb3f6b1.tar.gz
shell: fix possible bug in dup2 usage
* Also exit on error from here using _exit(2) instead of exit(2).
-rw-r--r--src/lib/core/system.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/lib/core/system.c b/src/lib/core/system.c
index 608994e..5b47a62 100644
--- a/src/lib/core/system.c
+++ b/src/lib/core/system.c
@@ -72,12 +72,24 @@ int shell(struct Process *proc, char *args) {
if (proc->redirect_stderr) {
if (fp_err) {
+ if (dup2(fileno(fp_err), STDERR_FILENO) < 0) {
+ fprintf(stderr, "Unable to redirect stderr to %s: %s\n", proc->f_stderr, strerror(errno));
+ if (fp_out) {
+ fclose(fp_out);
+ }
+ fclose(fp_err);
+ _exit(1);
+ }
fclose(fp_err);
- fclose(stderr);
- }
- if (dup2(fileno(stdout), fileno(stderr)) < 0) {
- fprintf(stderr, "Unable to redirect stderr to stdout: %s\n", strerror(errno));
- exit(1);
+ } else {
+ // redirect stderr to stdout
+ if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
+ fprintf(stderr, "Unable to redirect stderr to stdout: %s\n", strerror(errno));
+ if (fp_out) {
+ fclose(fp_out);
+ }
+ _exit(1);
+ }
}
}