Use only one global variable for snapshot creation pids.
[dss.git] / exec.c
1 /*
2  * Copyright (C) 2003-2008 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 <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <assert.h>
15 #include <stdlib.h>
16 #include <signal.h>
17
18
19 #include "gcc-compat.h"
20 #include "log.h"
21 #include "error.h"
22 #include "string.h"
23
24
25 /**
26  * Spawn a new process and redirect fd 0, 1, and 2.
27  *
28  * \param pid Will hold the pid of the created process upon return.
29  * \param file Path of the executable to execute.
30  * \param args The argument array for the command.
31  * \param fds a Pointer to a value-result array.
32  *
33  * \return Standard.
34  *
35  * \sa null(4), pipe(2), dup2(2), fork(2), exec(3).
36  */
37 int dss_exec(pid_t *pid, const char *file, char *const *const args, int *fds)
38 {
39         int ret, in[2] = {-1, -1}, out[2] = {-1, -1}, err[2] = {-1, -1},
40                 null = -1; /* ;) */
41
42         ret = -E_DUP_PIPE;
43         if (fds[0] > 0 && pipe(in) < 0)
44                 goto err_out;
45         if (fds[1] > 0 && pipe(out) < 0)
46                 goto err_out;
47         if (fds[2] > 0 && pipe(err) < 0)
48                 goto err_out;
49         if (!fds[0] || !fds[1] || !fds[2]) {
50                 ret = -E_NULL_OPEN;
51                 null = open("/dev/null", O_RDWR);
52                 if (null < 0)
53                         goto err_out;
54         }
55         if ((*pid = fork()) < 0)
56                 exit(EXIT_FAILURE);
57         if (!(*pid)) { /* child */
58                 if (fds[0] >= 0) {
59                         if (fds[0]) {
60                                 close(in[1]);
61                                 if (in[0] != STDIN_FILENO)
62                                         dup2(in[0], STDIN_FILENO);
63                         } else
64                                 dup2(null, STDIN_FILENO);
65                 }
66                 if (fds[1] >= 0) {
67                         if (fds[1]) {
68                                 close(out[0]);
69                                 if (out[1] != STDOUT_FILENO)
70                                         dup2(out[1], STDOUT_FILENO);
71                         } else
72                                 dup2(null, STDOUT_FILENO);
73                 }
74                 if (fds[2] >= 0) {
75                         if (fds[2]) {
76                                 close(err[0]);
77                                 if (err[1] != STDERR_FILENO)
78                                         dup2(err[1], STDERR_FILENO);
79                         } else
80                                 dup2(null, STDERR_FILENO);
81                 }
82                 if (null >= 0)
83                         close(null);
84                 signal(SIGINT, SIG_DFL);
85                 signal(SIGTERM, SIG_DFL);
86                 signal(SIGCHLD, SIG_DFL);
87                 execvp(file, args);
88                 _exit(EXIT_FAILURE);
89         }
90         if (fds[0] > 0) {
91                 close(in[0]);
92                 *fds = in[1];
93         }
94         if (fds[1] > 0) {
95                 close(out[1]);
96                 *(fds + 1) = out[0];
97         }
98         if (fds[2] > 0) {
99                 close(err[1]);
100                 *(fds + 2) = err[0];
101         }
102         if (null >= 0)
103                 close(null);
104         return 1;
105 err_out:
106         DSS_ERROR_LOG("failed to exec %s\n", file);
107         if (err[0] >= 0)
108                 close(err[0]);
109         if (err[1] >= 0)
110                 close(err[1]);
111         if (out[0] >= 0)
112                 close(out[0]);
113         if (out[1] >= 0)
114                 close(out[1]);
115         if (in[0] >= 0)
116                 close(in[0]);
117         if (in[1] >= 0)
118                 close(in[1]);
119         if (null >= 0)
120                 close(null);
121         return ret;
122 }
123
124
125 /**
126  * Exec the given command.
127  *
128  * \param pid Will hold the pid of the created process upon return.
129  * \param cmdline Holds the command and its arguments, seperated by spaces.
130  * \param fds A pointer to a value-result array.
131  *
132  * This function uses fork/exec to create a new process. \a fds must be a
133  * pointer to three integers, corresponding to stdin, stdout and stderr
134  * respectively. It specifies how to deal with fd 0, 1, 2 in the child. The
135  * contents of \a fds are interpreted as follows:
136  *
137  *      - fd[i] < 0: leave fd \a i alone.
138  *      - fd[i] = 0: dup fd \a i to \p /dev/null.
139  *      - fd[i] > 0: create a pipe and dup i to one end of that pipe.
140  *      Upon return, fd[i] contains the file descriptor of the pipe.
141  *
142  *      In any case, all unneeded filedescriptors are closed.
143  *
144  * \return Standard.
145  */
146 int dss_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds)
147 {
148         int argc, ret;
149         char **argv;
150         char *tmp = dss_strdup(cmdline);
151
152         if (!tmp)
153                 exit(EXIT_FAILURE);
154         argc = split_args(tmp, &argv, " \t");
155         ret = dss_exec(pid, argv[0], argv, fds);
156         free(argv);
157         free(tmp);
158         return ret;
159 }
160
161