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