-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathapp_launcher.cpp
More file actions
210 lines (182 loc) · 6.83 KB
/
Copy pathapp_launcher.cpp
File metadata and controls
210 lines (182 loc) · 6.83 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
LINUX only
This is a simple C++ application to start process and completely detach it
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 <thread>
#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();
// Check if pid_file exists for environment variable
std::string pidFileValue;
bool hasPidFile = false;
auto pidFileIt = root.find("pid_file");
if (pidFileIt != root.end() && pidFileIt->is_string()) {
pidFileValue = pidFileIt->get<std::string>();
if (!pidFileValue.empty()) {
hasPidFile = true;
}
}
auto env = root.find("env");
char **new_environ = NULL;
int env_count = 0;
if (env != root.end() && env->is_object()) {
int env_size = env->size();
int total_size = env_size + (hasPidFile ? 1 : 0);
new_environ = (char **)malloc((total_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++;
}
}
if (hasPidFile) {
std::string pid_env = "AYON_PID_FILE=" + pidFileValue;
new_environ[i] = strdup(pid_env.c_str());
i++;
}
env_count = i;
new_environ[env_count] = NULL;
} else if (hasPidFile) {
// No env object but we need to pass AYON_PID_FILE
new_environ = (char **)malloc(2 * sizeof(char *));
std::string pid_env = "AYON_PID_FILE=" + pidFileValue;
new_environ[0] = strdup(pid_env.c_str());
new_environ[1] = NULL;
env_count = 1;
}
auto stdoutIt = root.find("stdout");
std::string outPathStr;
bool addStdoutRedirection = true;
if (stdoutIt != root.end()) {
if (stdoutIt->is_null()) {
addStdoutRedirection = false; // do not redirect stdout if explicitly null
} else if (stdoutIt->is_string()) {
outPathStr = stdoutIt->get<std::string>();
}
}
if (addStdoutRedirection && outPathStr.empty()) outPathStr = "/dev/null";
auto stderrIt = root.find("stderr");
std::string errPathStr;
bool addStderrRedirection = true;
if (stderrIt != root.end()) {
if (stderrIt->is_null()) {
addStderrRedirection = false; // do not redirect stderr if explicitly null
} else if (stderrIt->is_string()) {
errPathStr = stderrIt->get<std::string>();
}
}
if (addStderrRedirection && errPathStr.empty()) errPathStr = "/dev/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 only if not explicitly disabled by null
if (addStdoutRedirection) {
posix_spawn_file_actions_addopen(&file_actions, STDOUT_FILENO, outPathStr.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
// Redirect stderr only if not explicitly disabled by null
if (addStderrRedirection) {
posix_spawn_file_actions_addopen(&file_actions, STDERR_FILENO, errPathStr.c_str(), 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) {
// Check if shell script provided actual application PID via PID file
auto pid_file_it = root.find("pid_file");
if (pid_file_it != root.end() && pid_file_it->is_string()) {
std::string pid_file_path = pid_file_it->get<std::string>();
// Wait a short time for shell script to potentially write actual PID
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::ifstream pid_file(pid_file_path);
if (pid_file.is_open()) {
std::string pid_content;
std::getline(pid_file, pid_content);
pid_file.close();
// Remove any whitespace
pid_content.erase(0, pid_content.find_first_not_of(" \t\r\n"));
pid_content.erase(pid_content.find_last_not_of(" \t\r\n") + 1);
if (!pid_content.empty()) {
try {
pid_t script_pid = std::stoi(pid_content);
if (script_pid != pid && script_pid > 0) {
pid = script_pid;
}
} catch (const std::exception& e) {
// Invalid PID in file, use initial_pid
}
}
}
}
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);
if (new_environ) {
for (int i = 0; i < env_count; i++) {
free(new_environ[i]);
}
free(new_environ);
}
free(exec_args);
}
setsid();
return 0;
}