engines/exec: style cleanups
[fio.git] / engines / exec.c
CommitLineData
b50590bc
EV
1/*
2 * Exec engine
3 *
4 * Doesn't transfer any data, merely run 3rd party tools
5 *
6 */
7#include "../fio.h"
8#include "../optgroup.h"
9#include <signal.h>
10
11struct exec_options {
12 void *pad;
13 char *program;
14 char *arguments;
15 int grace_time;
16 unsigned int std_redirect;
17 pid_t pid;
18};
19
20static struct fio_option options[] = {
21 {
65739ddf
JA
22 .name = "program",
23 .lname = "Program",
24 .type = FIO_OPT_STR_STORE,
25 .off1 = offsetof(struct exec_options, program),
26 .help = "Program to execute",
27 .category = FIO_OPT_C_ENGINE,
28 .group = FIO_OPT_G_INVALID,
29 },
b50590bc 30 {
65739ddf
JA
31 .name = "arguments",
32 .lname = "Arguments",
33 .type = FIO_OPT_STR_STORE,
34 .off1 = offsetof(struct exec_options, arguments),
35 .help = "Arguments to pass",
36 .category = FIO_OPT_C_ENGINE,
37 .group = FIO_OPT_G_INVALID,
38 },
b50590bc 39 {
65739ddf
JA
40 .name = "grace_time",
41 .lname = "Grace time",
42 .type = FIO_OPT_INT,
43 .minval = 0,
44 .def = "1",
45 .off1 = offsetof(struct exec_options, grace_time),
46 .help = "Grace time before sending a SIGKILL",
47 .category = FIO_OPT_C_ENGINE,
48 .group = FIO_OPT_G_INVALID,
49 },
b50590bc 50 {
65739ddf
JA
51 .name = "std_redirect",
52 .lname = "Std redirect",
53 .type = FIO_OPT_BOOL,
54 .def = "1",
55 .off1 = offsetof(struct exec_options, std_redirect),
56 .help = "Redirect stdout & stderr to files",
57 .category = FIO_OPT_C_ENGINE,
58 .group = FIO_OPT_G_INVALID,
59 },
b50590bc 60 {
65739ddf
JA
61 .name = NULL,
62 },
b50590bc
EV
63};
64
65char *str_replace(char *orig, const char *rep, const char *with)
66{
67 /*
65739ddf
JA
68 * Replace a substring by another.
69 *
70 * Returns the new string if occurences were found
71 * Returns orig if no occurence is found
b50590bc
EV
72 */
73 char *result, *insert, *tmp;
74 int len_rep, len_with, len_front, count;
75
65739ddf 76 /* sanity checks and initialization */
b50590bc
EV
77 if (!orig || !rep)
78 return orig;
79
80 len_rep = strlen(rep);
81 if (len_rep == 0)
82 return orig;
83
84 if (!with)
85 with = "";
86 len_with = strlen(with);
87
88 insert = orig;
89 for (count = 0; (tmp = strstr(insert, rep)); ++count) {
90 insert = tmp + len_rep;
91 }
92
93 tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
94
95 if (!result)
96 return orig;
97
98 while (count--) {
99 insert = strstr(orig, rep);
100 len_front = insert - orig;
101 tmp = strncpy(tmp, orig, len_front) + len_front;
102 tmp = strcpy(tmp, with) + len_with;
103 orig += len_front + len_rep;
104 }
105 strcpy(tmp, orig);
106 return result;
107}
108
109char *expand_variables(struct thread_options *o, char *arguments)
110{
111 char str[16];
112 char *expanded_runtime, *expanded_name;
113 snprintf(str, sizeof(str), "%lld", o->timeout / 1000000);
114
115 /* %r is replaced by the runtime in seconds */
116 expanded_runtime = str_replace(arguments, "%r", str);
117
118 /* %n is replaced by the name of the running job */
119 expanded_name = str_replace(expanded_runtime, "%n", o->name);
120
121 return expanded_name;
122}
123
124static int exec_background(struct thread_options *o, struct exec_options *eo)
125{
126 char *outfilename = NULL, *errfilename = NULL;
127 int outfd = 0, errfd = 0;
128 pid_t pid;
129 char *expanded_arguments = NULL;
130 /* For the arguments splitting */
131 char **arguments_array = NULL;
132 char *p;
133 char *exec_cmd = NULL;
134 size_t arguments_nb_items = 0, q;
135
136 if (asprintf(&outfilename, "%s.stdout", o->name) < 0)
137 return -1;
138
139 if (asprintf(&errfilename, "%s.stderr", o->name) < 0) {
140 free(outfilename);
141 return -1;
142 }
143
144 /* If we have variables in the arguments, let's expand them */
145 expanded_arguments = expand_variables(o, eo->arguments);
146
147 if (eo->std_redirect) {
148 log_info("%s : Saving output of %s %s : stdout=%s stderr=%s\n",
149 o->name, eo->program, expanded_arguments, outfilename,
150 errfilename);
151
152 /* Creating the stderr & stdout output files */
153 outfd = open(outfilename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
154 if (!outfd) {
155 log_err("fio: cannot open output file %s : %s\n",
156 outfilename, strerror(errno));
157 free(outfilename);
158 free(errfilename);
159 return -1;
160 }
161
162 errfd = open(errfilename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
163 if (!errfd) {
164 log_err("fio: cannot open output file %s : %s\n",
165 errfilename, strerror(errno));
166 free(outfilename);
167 free(errfilename);
168 return -1;
169 }
170 } else {
171 log_info("%s : Running %s %s\n",
172 o->name, eo->program, expanded_arguments);
173 }
174
175 pid = fork();
176
177 /* We are on the control thread (parent side of the fork */
178 if (pid > 0) {
179 eo->pid = pid;
180 if (eo->std_redirect) {
181 /* The output file is for the client side of the fork */
182 close(outfd);
183 close(errfd);
184 free(outfilename);
185 free(errfilename);
186 }
187 return 0;
188 }
189
190 /* If the fork failed */
191 if (pid < 0) {
192 log_err("fio: forking failed %s \n", strerror(errno));
193 if (eo->std_redirect) {
194 close(outfd);
195 close(errfd);
196 free(outfilename);
197 free(errfilename);
198 }
199 return -1;
200 }
201
202 /* We are in the worker (child side of the fork) */
203 if (pid == 0) {
204 if (eo->std_redirect) {
65739ddf
JA
205 /* replace stdout by the output file we create */
206 dup2(outfd, 1);
207 /* replace stderr by the output file we create */
208 dup2(errfd, 2);
b50590bc
EV
209 close(outfd);
210 close(errfd);
211 free(outfilename);
212 free(errfilename);
213 }
214
65739ddf
JA
215 /*
216 * Let's split the command line into a null terminated array to
217 * be passed to the exec'd program.
218 * But don't asprintf expanded_arguments if NULL as it would be
219 * converted to a '(null)' argument, while we want no arguments
220 * at all.
221 */
b50590bc
EV
222 if (expanded_arguments != NULL) {
223 if (asprintf(&exec_cmd, "%s %s", eo->program, expanded_arguments) < 0)
224 return -1;
225 } else {
226 if (asprintf(&exec_cmd, "%s", eo->program) < 0)
227 return -1;
228 }
229
65739ddf
JA
230 /*
231 * Let's build an argv array to based on the program name and
232 * arguments
233 */
b50590bc
EV
234 p = exec_cmd;
235 for (;;) {
236 p += strspn(p, " ");
237
238 if (!(q = strcspn(p, " ")))
239 break;
240
241 if (q) {
242 arguments_array =
243 realloc(arguments_array,
244 (arguments_nb_items +
245 1) * sizeof(char *));
246 arguments_array[arguments_nb_items] =
247 malloc(q + 1);
248 strncpy(arguments_array[arguments_nb_items], p,
249 q);
250 arguments_array[arguments_nb_items][q] = 0;
251 arguments_nb_items++;
252 p += q;
253 }
254 }
255
256 /* Adding a null-terminated item to close the list */
257 arguments_array =
258 realloc(arguments_array,
259 (arguments_nb_items + 1) * sizeof(char *));
260 arguments_array[arguments_nb_items] = NULL;
261
65739ddf
JA
262 /*
263 * Replace the fio program from the child fork by the target
264 * program
265 */
b50590bc
EV
266 execvp(arguments_array[0], arguments_array);
267 }
65739ddf 268 /* We never reach this place */
b50590bc
EV
269 return 0;
270}
271
272static enum fio_q_status
273fio_exec_queue(struct thread_data *td, struct io_u fio_unused * io_u)
274{
275 struct thread_options *o = &td->o;
276 struct exec_options *eo = td->eo;
277
278 /* Let's execute the program the first time we get queued */
279 if (eo->pid == -1) {
280 exec_background(o, eo);
281 } else {
65739ddf
JA
282 /*
283 * The program is running in background, let's check on a
284 * regular basis
285 * if the time is over and if we need to stop the tool
286 */
b50590bc
EV
287 usleep(o->thinktime);
288 if (utime_since_now(&td->start) > o->timeout) {
289 /* Let's stop the child */
290 kill(eo->pid, SIGTERM);
65739ddf
JA
291 /*
292 * Let's give grace_time (1 sec by default) to the 3rd
293 * party tool to stop
294 */
b50590bc
EV
295 sleep(eo->grace_time);
296 }
297 }
298
299 return FIO_Q_COMPLETED;
300}
301
302static int fio_exec_init(struct thread_data *td)
303{
304 struct thread_options *o = &td->o;
305 struct exec_options *eo = td->eo;
306 int td_previous_state;
307
308 eo->pid = -1;
309
310 if (!eo->program) {
311 td_vmsg(td, EINVAL,
312 "no program is defined, it is mandatory to define one",
313 "exec");
314 return 1;
315 }
316
317 log_info("%s : program=%s, arguments=%s\n",
318 td->o.name, eo->program, eo->arguments);
319
320 /* Saving the current thread state */
321 td_previous_state = td->runstate;
322
65739ddf
JA
323 /*
324 * Reporting that we are preparing the engine
b50590bc
EV
325 * This is useful as the qsort() calibration takes time
326 * This prevents the job from starting before init is completed
327 */
328 td_set_runstate(td, TD_SETTING_UP);
329
330 /*
331 * set thinktime_sleep and thinktime_spin appropriately
332 */
333 o->thinktime_blocks = 1;
334 o->thinktime_blocks_type = THINKTIME_BLOCKS_TYPE_COMPLETE;
335 o->thinktime_spin = 0;
65739ddf
JA
336 /* 50ms pause when waiting for the program to complete */
337 o->thinktime = 50000;
b50590bc
EV
338
339 o->nr_files = o->open_files = 1;
340
341 /* Let's restore the previous state. */
342 td_set_runstate(td, td_previous_state);
343 return 0;
344}
345
346static void fio_exec_cleanup(struct thread_data *td)
347{
348 struct exec_options *eo = td->eo;
349 /* Send a sigkill to ensure the job is well terminated */
350 if (eo->pid > 0)
351 kill(eo->pid, SIGKILL);
352}
353
354static int
355fio_exec_open(struct thread_data fio_unused * td,
356 struct fio_file fio_unused * f)
357{
358 return 0;
359}
360
361static struct ioengine_ops ioengine = {
362 .name = "exec",
363 .version = FIO_IOOPS_VERSION,
364 .queue = fio_exec_queue,
365 .init = fio_exec_init,
366 .cleanup = fio_exec_cleanup,
367 .open_file = fio_exec_open,
368 .flags = FIO_SYNCIO | FIO_DISKLESSIO | FIO_NOIO,
369 .options = options,
370 .option_struct_size = sizeof(struct exec_options),
371};
372
373static void fio_init fio_exec_register(void)
374{
375 register_ioengine(&ioengine);
376}
377
378static void fio_exit fio_exec_unregister(void)
379{
380 unregister_ioengine(&ioengine);
381}