forked from flexibity-team/boost-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspawnUtil.hpp
48 lines (39 loc) · 1.04 KB
/
spawnUtil.hpp
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
#ifndef INCLUDE_FLEXIBITY_SPAWNUTIL_HPP_
#define INCLUDE_FLEXIBITY_SPAWNUTIL_HPP_
#include <flexibity/log.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#include <tuple>
extern char **environ;
namespace Flexibity {
namespace spawnUtil {
std::tuple<int, pid_t> run_cmd(const char *const argv[], bool wait = false)
{
pid_t pid;
int status;
const char *cmd = argv[0];
GINFO("Run command: " << cmd);
status = posix_spawnp(&pid, cmd, NULL, NULL, (char **)argv, ::environ);
if (status == 0) {
GINFO("Child pid: " << pid);
if (wait) {
if (waitpid(pid, &status, 0) != -1) {
GINFO("Child exited with status " << status);
} else {
GERROR("waitpid");
}
}
} else {
GERROR("posix_spawn: " << strerror(status));
}
return std::make_tuple(WEXITSTATUS(status), pid);
}
std::tuple<int, pid_t> mpg123(std::string cmd)
{
const char *const argv[] = {"mpg123", "-a", "dmix", cmd.c_str(), NULL};
return run_cmd(argv);
}
}
}
#endif // INCLUDE_FLEXIBITY_SPAWNUTIL_HPP_