summaryrefslogtreecommitdiff
path: root/exec.c
blob: 3d3963f29c9e8e6e0eaddba05c676432df51a3ea (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
/* SPDX-License-Identifier: GPL-2.0 */

#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(). */
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);
}

/*
 * Execute the space-separated command line. On return, the pid pointer is
 * initialized to pid of the newly created process.
 */
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);
}