create_rsync_argv(): Allocate correctly sized arg array.
[dss.git] / exec.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /** \file exec.c Helper functions for spawning new processes. */
4
5 #include <string.h>
6 #include <unistd.h>
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <errno.h>
11
12 #include "gcc-compat.h"
13 #include "log.h"
14 #include "err.h"
15 #include "str.h"
16 #include "exec.h"
17
18 /**
19  * Spawn a new process using execvp().
20  *
21  * \param pid Will hold the pid of the created process upon return.
22  * \param file Path of the executable to execute.
23  * \param args The argument array for the command.
24  *
25  * \return Standard.
26  *
27  * \sa fork(2), exec(3).
28  */
29 void dss_exec(pid_t *pid, const char *file, char *const *const args)
30 {
31         if ((*pid = fork()) < 0) {
32                 DSS_EMERG_LOG(("fork error: %s\n", strerror(errno)));
33                 exit(EXIT_FAILURE);
34         }
35         if (*pid) /* parent */
36                 return;
37         signal(SIGINT, SIG_DFL);
38         signal(SIGTERM, SIG_DFL);
39         signal(SIGCHLD, SIG_DFL);
40         execvp(file, args);
41         DSS_EMERG_LOG(("execvp error: %s\n", strerror(errno)));
42         _exit(EXIT_FAILURE);
43 }
44
45 /**
46  * Exec the command given as a command line.
47  *
48  * \param pid Will hold the pid of the created process upon return.
49  * \param cmdline Holds the command and its arguments, separated by spaces.
50  *
51  * This function uses fork/exec to create a new process.
52  *
53  * \return Standard.
54  */
55 void dss_exec_cmdline_pid(pid_t *pid, const char *cmdline)
56 {
57         char **argv, *tmp = dss_strdup(cmdline);
58
59         split_args(tmp, &argv, " \t");
60         dss_exec(pid, argv[0], argv);
61         free(argv);
62         free(tmp);
63 }