Fix bad grammar "allows <infinitive>".
[dss.git] / exec.c
1 /*
2  * Copyright (C) 2003-2011 Andre Noll <maan@tuebingen.mpg.de>
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 #include "exec.h"
21
22 /**
23  * Spawn a new process using execvp().
24  *
25  * \param pid Will hold the pid of the created process upon return.
26  * \param file Path of the executable to execute.
27  * \param args The argument array for the command.
28  *
29  * \return Standard.
30  *
31  * \sa fork(2), exec(3).
32  */
33 void dss_exec(pid_t *pid, const char *file, char *const *const args)
34 {
35         if ((*pid = fork()) < 0) {
36                 DSS_EMERG_LOG(("fork error: %s\n", strerror(errno)));
37                 exit(EXIT_FAILURE);
38         }
39         if (*pid) /* parent */
40                 return;
41         signal(SIGINT, SIG_DFL);
42         signal(SIGTERM, SIG_DFL);
43         signal(SIGCHLD, SIG_DFL);
44         execvp(file, args);
45         DSS_EMERG_LOG(("execvp error: %s\n", strerror(errno)));
46         _exit(EXIT_FAILURE);
47 }
48
49 /**
50  * Exec the command given as a command line.
51  *
52  * \param pid Will hold the pid of the created process upon return.
53  * \param cmdline Holds the command and its arguments, separated by spaces.
54  *
55  * This function uses fork/exec to create a new process.
56  *
57  * \return Standard.
58  */
59 void dss_exec_cmdline_pid(pid_t *pid, const char *cmdline)
60 {
61         char **argv, *tmp = dss_strdup(cmdline);
62
63         split_args(tmp, &argv, " \t");
64         dss_exec(pid, argv[0], argv);
65         free(argv);
66         free(tmp);
67 }