forked from ynput/ayon-launcher
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_launcher.cpp
More file actions
116 lines (100 loc) · 3.37 KB
/
app_launcher.cpp
File metadata and controls
116 lines (100 loc) · 3.37 KB
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
/**
This is a simple C++ equivalent of the `app_launcher.py` with one difference:
it completely detach from the parent process. This is needed to avoid
hanging child processes when the parent process is killed.
You can use it instead of the `app_launcher.py` by building it with:
```shell
CPLUS_INCLUDE_PATH=/../ayon-launcher/vendor/include
g++ app_launcher.cpp -o app_launcher
```
**/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#include <string.h>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s <json_file>\n", argv[0]);
return 1;
}
std::ifstream json_file(argv[1]);
if (!json_file.is_open()) {
fprintf(stderr, "error: could not open file %s\n", argv[1]);
return 1;
}
json root;
try {
json_file >> root;
} catch (json::parse_error& e) {
fprintf(stderr, "error: %s\n", e.what());
return 1;
}
json_file.close();
auto env = root.find("env");
char **new_environ = NULL;
if (env != root.end() && env->is_object()) {
int env_size = env->size();
new_environ = (char **)malloc((env_size + 1) * sizeof(char *));
int i = 0;
for (auto& [key, value] : env->items()) {
if (value.is_string()) {
std::string env_var = key + "=" + value.get<std::string>();
new_environ[i] = strdup(env_var.c_str());
i++;
}
}
new_environ[env_size] = NULL;
}
auto args = root.find("args");
if (args != root.end() && args->is_array()) {
char **exec_args = (char **)malloc((args->size() + 2) * sizeof(char *));
int index = 0;
for (const auto& value : *args) {
if (value.is_string()) {
exec_args[index] = strdup(value.get<std::string>().c_str());
index++;
}
}
exec_args[args->size()] = NULL;
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);
// Redirect stdout to /dev/null
posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO, "/dev/null", O_WRONLY | O_CREAT | O_TRUNC, 0644);
// Redirect stderr to /dev/null
posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO, "/dev/null", O_WRONLY | O_CREAT | O_TRUNC, 0644);
posix_spawnattr_t spawnattr;
posix_spawnattr_init(&spawnattr);
pid_t pid;
int status = posix_spawn(&pid, exec_args[0], &file_actions, &spawnattr, exec_args, new_environ);
if (status == 0) {
root["pid"] = pid;
} else {
root["pid"] = nullptr;
}
std::ofstream output_file(argv[1]);
if (output_file.is_open()) {
output_file << root.dump();
output_file.close();
} else {
fprintf(stderr, "error: could not write back to file %s\n", argv[1]);
}
if (status != 0) {
printf("posix_spawn: %s\n", strerror(status));
setsid();
return 1;
}
posix_spawn_file_actions_destroy(&file_actions);
for (int i = 0; i < env->size(); i++) {
free(new_environ[i]);
}
free(exec_args);
}
setsid();
return 0;
}