summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/run.h
blob: f0b8330f4c5367af1f988d66b5ce3513479748c8 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
   Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
   This file is part of GlusterFS.

   GlusterFS is free software; you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation; either version 3 of the License,
   or (at your option) any later version.

   GlusterFS is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.
*/

#ifndef __RUN_H__
#define __RUN_H__

#define RUN_PIPE -1

struct runner {
        char **argv;
        unsigned argvlen;
        int runerr;
        pid_t chpid;
        int chfd[3];
        FILE *chio[3];
};

typedef struct runner runner_t;

/**
 * initialize runner_t instance.
 *
 * @param runner pointer to runner_t instance
 */
void runinit (runner_t *runner);

/**
 * get FILE pointer to which child's stdio is redirected.
 *
 * @param runner pointer to runner_t instance
 * @param fd specifies which standard file descriptor is
 *        is asked for
 *
 * @see runner_redir()
 */
FILE *runner_chio (runner_t *runner, int fd);

/**
 * add an argument.
 *
 * 'arg' is duplicated.
 *
 * Errors are deferred, no error handling is necessary.
 *
 * @param runner pointer to runner_t instance
 * @param arg    command line argument
 */
void runner_add_arg (runner_t *runner, const char *arg);

/**
 * add a sequence of arguments.
 *
 * Variadic function, calls runner_add_arg() on each vararg.
 * Argument sequence MUST be NULL terminated.
 *
 * Errors are deferred, no error handling is necessary.
 *
 * @param runner pointer to runner_t instance
 *
 * @see runner_add_arg()
 */
void runner_add_args (runner_t *runner, ...);

/**
 * add an argument with printf style formatting.
 *
 * Errors are deferred, no error handling is necessary.
 *
 * @param runner pointer to runner_t instance
 * @param format printf style format specifier
 */
void runner_argprintf (runner_t *runner, const char *format, ...);

/**
 * log a message about the command to be run.
 *
 * @param runner  pointer to runner_t instance
 *
 * @param dom  log domain
 * @param lvl  log level
 * @param msg  message with which the command is prefixed in log
 *
 * @see gf_log()
 */
void runner_log (runner_t *runner, const char *dom, gf_loglevel_t lvl,
                 const char *msg);

/**
 * set up redirection for child.
 *
 * @param runner  pointer to runner_t instance
 *
 * @param fd      fd of child to redirect (0, 1, or 2)
 * @param tgt_fd  fd on parent side to redirect to.
 *                Note that runner_end() will close tgt_fd,
 *                if user needs it in another context it should
 *                be dup'd beforehand.
 *                RUN_PIPE can be used for requiring a
 *                pipe from child to parent. The FILE
 *                created for this purpose will be
 *                accessible via runner_chio() (after
 *                runner_start() has been invoked).
 *
 * @see  runner_start(), dup(2), runner_chio(), runner_start()
 */
void
runner_redir (runner_t *runner, int fd, int tgt_fd);

/**
 * spawn child with accumulated arg list.
 *
 * @param runner  pointer to runner_t instance
 *
 * @return  0 on succesful spawn
 *          -1 on failure (either due to earlier errors or execve(2) failing)
 *
 * @see runner_cout()
 */
int runner_start (runner_t *runner);

/**
 * complete operation and free resources.
 *
 * If child exists, waits for it. Redirections will be closed.
 * Dynamically allocated memory shall be freed.
 *
 * @param runner  pointer to runner_t instance
 *
 * @return  0 if child terminated successfully
 *          -1 if there is no running child
 *          n > 0 if child failed; value to be interpreted as status
 *                in waitpid(2)
 *
 * @see waitpid(2)
 */
int runner_end (runner_t *runner);

/**
 * variant of runner_end() which does not free internal data
 * so that the runner instance can be run again.
 *
 * @see runner_end()
 */
int runner_end_reuse (runner_t *runner);

/**
 * spawn and child, take it to completion and free resources.
 *
 * Essentially it's a concatenation of runner_start() and runner_end()
 * with simplified return semantics.
 *
 * @param runner  pointer to runner_t instance
 *
 * @return  0 on success
 *          -1 on failuire
 *
 * @see runner_start(), runner_end()
 */
int runner_run (runner_t *runner);

/**
 * variant of runner_run() which does not free internal data
 * so that the runner instance can be run again.
 *
 * @see runner_run()
 */
int runner_run_reuse (runner_t *runner);

/**
 * run a command with args.
 *
 * Variadic function, child process is spawned with
 * the given sequence of args and waited for.
 * Argument sequence MUST be NULL terminated.
 *
 * @return 0 on success
 *         -1 on failure
 */
int runcmd (const char *arg, ...);

#endif