summaryrefslogtreecommitdiffstats
path: root/lib/spawn.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 20:46:53 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 20:46:53 +0000
commit19da58be2d9359a9641381feb559be0b918ef710 (patch)
tree109724175f07436696f51b14b5abbd3f4d704d6d /lib/spawn.c
parentInitial commit. (diff)
downloadshadow-19da58be2d9359a9641381feb559be0b918ef710.tar.xz
shadow-19da58be2d9359a9641381feb559be0b918ef710.zip
Adding upstream version 1:4.13+dfsg1.upstream/1%4.13+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/spawn.c')
-rw-r--r--lib/spawn.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/spawn.c b/lib/spawn.c
new file mode 100644
index 0000000..ce1a97d
--- /dev/null
+++ b/lib/spawn.c
@@ -0,0 +1,63 @@
+/*
+ * SPDX-FileCopyrightText: 2011 , Jonathan Nieder
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <errno.h>
+#include <string.h>
+#include "exitcodes.h"
+#include "prototypes.h"
+
+#include "shadowlog_internal.h"
+
+int run_command (const char *cmd, const char *argv[],
+ /*@null@*/const char *envp[], /*@out@*/int *status)
+{
+ pid_t pid, wpid;
+
+ if (NULL == envp) {
+ envp = (const char **)environ;
+ }
+
+ (void) fflush (stdout);
+ (void) fflush (shadow_logfd);
+
+ pid = fork ();
+ if (0 == pid) {
+ (void) execve (cmd, (char * const *) argv,
+ (char * const *) envp);
+ if (ENOENT == errno) {
+ exit (E_CMD_NOTFOUND);
+ }
+ fprintf (shadow_logfd, "%s: cannot execute %s: %s\n",
+ shadow_progname, cmd, strerror (errno));
+ exit (E_CMD_NOEXEC);
+ } else if ((pid_t)-1 == pid) {
+ fprintf (shadow_logfd, "%s: cannot execute %s: %s\n",
+ shadow_progname, cmd, strerror (errno));
+ return -1;
+ }
+
+ do {
+ wpid = waitpid (pid, status, 0);
+ if ((pid_t)-1 == wpid && errno == ECHILD)
+ break;
+ } while ( ((pid_t)-1 == wpid && errno == EINTR)
+ || ((pid_t)-1 != wpid && wpid != pid));
+
+ if ((pid_t)-1 == wpid) {
+ fprintf (shadow_logfd, "%s: waitpid (status: %d): %s\n",
+ shadow_progname, *status, strerror (errno));
+ return -1;
+ }
+
+ return 0;
+}
+