summaryrefslogtreecommitdiff
path: root/exec.c
blob: ece7ec29aa233051cf10bd5c4f3b1b6adfd14cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* SPDX-License-Identifier: GPL-2.0 */

/** \file exec.c Helper functions for spawning new processes. */

#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>

#include "gcc-compat.h"
#include "log.h"
#include "err.h"
#include "str.h"
#include "exec.h"

/**
 * Spawn a new process using execvp().
 *
 * \param pid Will hold the pid of the created process upon return.
 * \param file Path of the executable to execute.
 * \param args The argument array for the command.
 *
 * \return Standard.
 *
 * \sa fork(2), exec(3).
 */
void dss_exec(pid_t *pid, const char *file, char *const *const args)
{
	if ((*pid = fork()) < 0) {
		DSS_EMERG_LOG(("fork error: %s\n", strerror(errno)));
		exit(EXIT_FAILURE);
	}
	if (*pid) /* parent */
		return;
	signal(SIGINT, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
	signal(SIGCHLD, SIG_DFL);
	execvp(file, args);
	DSS_EMERG_LOG(("execvp error: %s\n", strerror(errno)));
	_exit(EXIT_FAILURE);
}

/**
 * Exec the command given as a command line.
 *
 * \param pid Will hold the pid of the created process upon return.
 * \param cmdline Holds the command and its arguments, separated by spaces.
 *
 * This function uses fork/exec to create a new process.
 *
 * \return Standard.
 */
void dss_exec_cmdline_pid(pid_t *pid, const char *cmdline)
{
	char **argv, *tmp = dss_strdup(cmdline);

	split_args(tmp, &argv);
	dss_exec(pid, argv[0], argv);
	free(argv);
	free(tmp);
}