return false;
}
- is_ok = spawn_wait(args, NULL, spawn_setstdout, (void *)fd, CRASH_STACK_TIMEOUT_MS, exit_code);
+ spawn_param_u param = { .int_val = fd };
+ is_ok = spawn_wait(args, NULL, spawn_setstdout, ¶m, CRASH_STACK_TIMEOUT_MS, exit_code);
close(fd);
out:
NULL
};
- (void)spawn_wait(args, NULL, spawn_chdir, (void *)cinfo->temp_dir, ZIP_TIMEOUT_MS, NULL);
+ spawn_param_u param = { .char_ptr = cinfo->temp_dir };
+ (void)spawn_wait(args, NULL, spawn_chdir, ¶m, ZIP_TIMEOUT_MS, NULL);
if ((lock_fd = lock_dumpdir()) < 0)
return;
#define spawn_wait_checked(av, env) \
do { \
int err; \
- if (!spawn_wait(av, env, spawn_setstdout, (void *)out_fd, DEFAULT_COMMAND_TIMEOUT_MS, &err) || err != 0) { \
+ spawn_param_u param = { .int_val = out_fd }; \
+ if (!spawn_wait(av, env, spawn_setstdout, ¶m, DEFAULT_COMMAND_TIMEOUT_MS, &err) || err != 0) { \
exit_code |= EXIT_CMDERR; \
fprintf_fd(out_fd, "\nCommand failed with error code: %d", err); \
} \
_D("compress tempdir is %s", tempdir);
char *av[] = {"/bin/zip", "-yr", archive_path, ".", NULL};
- bool is_ok = spawn_wait(av, NULL, spawn_chdir, (void*)tempdir, 0, exit_code);
+ spawn_param_u param = { .char_ptr = tempdir };
+ bool is_ok = spawn_wait(av, NULL, spawn_chdir, ¶m, 0, exit_code);
_I("Storing report at %s", archive_path);
* limitations under the License.
*/
+#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
/* spawn prepare function(s) - to be called in child process */
-int spawn_setstdout(void *userdata)
+int spawn_setstdout(spawn_param_u *param)
{
- int fd = (int)userdata;
- return dup2(fd, STDOUT_FILENO) < 0 ? -1 : 0;
+ assert(param);
+ return dup2(param->int_val, STDOUT_FILENO) < 0 ? -1 : 0;
}
-int spawn_setstdouterr(void *userdata)
+int spawn_setstdouterr(spawn_param_u *param)
{
- int fd = (int)userdata;
- return dup2(fd, STDOUT_FILENO) < 0 || dup2(fd, STDERR_FILENO) < 0 ? -1 : 0;
+ assert(param);
+ return dup2(param->int_val, STDOUT_FILENO) < 0 || dup2(param->int_val, STDERR_FILENO) < 0 ? -1 : 0;
}
-int spawn_chdir(void *userdata)
+int spawn_chdir(spawn_param_u *param)
{
- return chdir((char *)userdata);
+ assert(param);
+ return chdir(param->char_ptr);
}
-int spawn_umask(void *userdata)
+int spawn_umask(spawn_param_u *param)
{
- (void)umask((mode_t)userdata);
+ assert(param);
+ (void)umask(param->mode_t_val);
return 0;
}
return true;
}
-static int spawn_child(char *const av[], char *const ev[], spawn_prepare_fn prep, void *prepdata)
+static int spawn_child(char *const av[], char *const ev[], spawn_prepare_fn prep, spawn_param_u *param)
{
static const int spawn_error = 127;
- int r = prep ? prep(prepdata) : 0;
+ int r = prep ? prep(param) : 0;
if (r < 0)
return spawn_error;
return spawn_error;
}
-bool spawn(char *const av[], char *const ev[], spawn_prepare_fn prep, void *prepdata, pid_t *childpid, int *childfd)
+bool spawn(char *const av[], char *const ev[], spawn_prepare_fn prep, spawn_param_u *param, pid_t *childpid, int *childfd)
{
int pipefd[2];
if (pipe(pipefd) < 0) {
return false;
} else if (pid == 0) {
close(pipefd[0]);
- _exit(spawn_child(av, ev, prep, prepdata));
+ _exit(spawn_child(av, ev, prep, param));
}
close(pipefd[1]);
return true;
}
-bool spawn_wait(char *const av[], char *const ev[], spawn_prepare_fn prep, void *prepdata, int timeout_ms, int *exit_code)
+bool spawn_wait(char *const av[], char *const ev[], spawn_prepare_fn prep, spawn_param_u *param, int timeout_ms, int *exit_code)
{
pid_t childpid;
int childfd;
- if (!spawn(av, ev, prep, prepdata, &childpid, &childfd)) {
+ if (!spawn(av, ev, prep, param, &childpid, &childfd)) {
_E("spawn() returned an error - aborting waiting");
return false;
}
#define DEFAULT_COMMAND_TIMEOUT_MS (60*1000) /* 60sec */
-typedef int (*spawn_prepare_fn)(void *);
+typedef union {
+ int int_val;
+ mode_t mode_t_val;
+ char *char_ptr;
+} spawn_param_u;
-int spawn_setstdout(void *userdata);
-int spawn_setstdouterr(void *userdata);
-int spawn_chdir(void *userdata);
-int spawn_umask(void *userdata);
+typedef int (*spawn_prepare_fn)(spawn_param_u *param);
+
+int spawn_setstdout(spawn_param_u *param);
+int spawn_setstdouterr(spawn_param_u *param);
+int spawn_chdir(spawn_param_u *param);
+int spawn_umask(spawn_param_u *param);
bool wait_for_pid(pid_t pid, int *exit_code);
-bool spawn(char *const av[], char *const ev[], spawn_prepare_fn prep, void *prepdata, pid_t *childpid, int *childfd);
-bool spawn_wait(char *const av[], char *const ev[], spawn_prepare_fn prep, void *prepdata, int timeout_ms, int *exit_code);
+bool spawn(char *const av[], char *const ev[], spawn_prepare_fn prep, spawn_param_u *param, pid_t *childpid, int *childfd);
+bool spawn_wait(char *const av[], char *const ev[], spawn_prepare_fn prep, spawn_param_u *param, int timeout_ms, int *exit_code);
#ifdef __cplusplus
}