commit a56faccd57563e9ae3608cb1e4a4e2d5eb578eb8
parent 606a495120e0e06ca2ebfad372888a15308bafc0
Author: Santtu Lakkala <inz@inz.fi>
Date: Fri, 31 Jan 2025 11:57:38 +0200
Replace vfork() & execl() with posix_spawn()
vfork() was obsoleted in POSIX.1-2008, use posix_spawn() instead, leaving
the fork() type as an implementation detail of C library.
Diffstat:
1 file changed, 15 insertions(+), 11 deletions(-)
diff --git a/activitypub.c b/activitypub.c
@@ -16,6 +16,9 @@
#include "snac.h"
#include <sys/wait.h>
+#include <spawn.h>
+
+extern char **environ;
const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
@@ -2570,25 +2573,26 @@ int send_email(const xs_dict *mailinfo)
/* revert back to old sendmail pipe behaviour */
const char *msg = xs_dict_get(mailinfo, "body");
FILE *f;
- int status;
+ int status = -1;
int fds[2];
+ posix_spawn_file_actions_t facts;
pid_t pid;
-
if (pipe(fds) == -1) return -1;
- pid = vfork();
- if (pid == -1) return -1;
- else if (pid == 0) {
- dup2(fds[0], 0);
- close(fds[0]);
- close(fds[1]);
- execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
- _exit(1);
+ if (posix_spawn_file_actions_init(&facts) == 0) {
+ if (posix_spawn_file_actions_adddup2(&facts, fds[0], 0) == 0 &&
+ posix_spawn_file_actions_addclose(&facts, fds[0]) == 0 &&
+ posix_spawn_file_actions_addclose(&facts, fds[1]) == 0)
+ status = posix_spawn(&pid, "/usr/sbin/sendmail", &facts, NULL,
+ (char * const []){ "sendmail", "-t", NULL }, environ);
+ posix_spawn_file_actions_destroy(&facts);
}
close(fds[0]);
- if ((f = fdopen(fds[1], "w")) == NULL) {
+
+ if (status != 0 || (f = fdopen(fds[1], "w")) == NULL) {
close(fds[1]);
return -1;
}
+
fprintf(f, "%s\n", msg);
fclose(f);
if (waitpid(pid, &status, 0) == -1) return -1;