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