pid-in-outputToFile-fix.diff
src/com/goldencode/p2j/main/ClientCore.java 2023-01-26 12:12:44 +0000 | ||
---|---|---|
120 | 120 | |
121 | 121 |
package com.goldencode.p2j.main; |
122 | 122 | |
123 |
import java.io.*; |
|
123 | 124 |
import java.util.*; |
124 | 125 |
import java.util.function.*; |
125 | 126 |
import java.util.logging.*; |
... | ... | |
188 | 189 |
public static void start(BootstrapConfig config, boolean file, boolean single, Supplier<String> diag) |
189 | 190 |
throws Exception |
190 | 191 |
{ |
192 |
// Load native library |
|
193 |
loadNativeLibrary(); |
|
194 |
|
|
195 |
setOutputToFile(config); |
|
196 |
|
|
191 | 197 |
String uuid = config.getString("server", "spawner", "uuid", null); |
192 | 198 |
boolean dbg = config.getBoolean("logging", "debug", "enable", false); |
193 | 199 |
String referrer = config.getString("web", "referrer", "url", null); |
... | ... | |
198 | 204 |
// iterations of the core loop |
199 | 205 |
ScreenDriver<?> driver = (uuid == null) ? null : processTemporaryClient(uuid, config); |
200 | 206 |
|
201 |
// Load native library |
|
202 |
loadNativeLibrary(); |
|
203 |
|
|
204 | 207 |
while (running) |
205 | 208 |
{ |
206 | 209 |
Object context = null; |
... | ... | |
431 | 434 |
return isStop; |
432 | 435 |
} |
433 | 436 | |
437 |
private static void setOutputToFile(BootstrapConfig config) |
|
438 |
{ |
|
439 |
String outputToFile = config.getString("server", "clientConfig", "outputToFile", null); |
|
440 |
if (outputToFile == null) |
|
441 |
{ |
|
442 |
return; |
|
443 |
} |
|
444 |
|
|
445 |
try |
|
446 |
{ |
|
447 |
outputToFile = outputToFile.replace("%pid%", String.valueOf(getPid())); |
|
448 |
System.setOut(new PrintStream(new FileOutputStream(outputToFile), true)); |
|
449 |
} |
|
450 |
catch (Throwable t) |
|
451 |
{ |
|
452 |
System.err.println("ClientCore: Unable to redirect stdout to outputToFile " + outputToFile); |
|
453 |
} |
|
454 |
} |
|
455 | ||
434 | 456 |
/** |
435 | 457 |
* Loads native library and performs required initial steps for P2J library to working. |
436 | 458 |
*/ |
src/com/goldencode/p2j/main/ProcessClientBuilder.java 2023-01-26 09:43:24 +0000 | ||
---|---|---|
132 | 132 |
if (outputToFile != null) |
133 | 133 |
{ |
134 | 134 |
cmd.add("-O"); |
135 |
cmd.add(outputToFile); |
|
135 |
cmd.add("server:clientConfig:outputToFile=" + outputToFile);
|
|
136 | 136 |
} |
137 | 137 | |
138 | 138 |
return cmd; |
139 | 139 |
} |
140 |
} |
|
140 |
} |
src/native/spawn.c 2023-01-26 12:21:45 +0000 | ||
---|---|---|
162 | 162 |
#define ALLPERMS (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)/* 07777 */ |
163 | 163 |
#endif |
164 | 164 | |
165 |
/* prototypes */ |
|
166 |
void stdout_redirect(); |
|
167 | ||
168 | 165 |
/* Global data */ |
169 | 166 |
struct passwd *pw; |
170 | 167 |
int extra_logging = 0; |
... | ... | |
400 | 397 |
logMessage(message_buffer); |
401 | 398 |
exit(audit(ERR_SPAWN_CHDIR_WD, "chdir(workdir)")); |
402 | 399 |
} |
403 | ||
404 |
/* Redirect STDOUT if required */ |
|
405 |
if (output_file_name[0] != 0) |
|
406 |
{ |
|
407 |
stdout_redirect(); |
|
408 |
} |
|
409 | 400 |
|
410 | 401 |
/* Execute command */ |
411 | 402 |
exit(execvp(command, argv)); |
... | ... | |
921 | 912 | |
922 | 913 |
int nopts; |
923 | 914 |
int debug_opt_found = 0; |
915 |
int output_to_file_opt_found = 0; |
|
924 | 916 |
|
925 | 917 |
JavaVMOption options[8]; |
926 | 918 |
options[0].optionString = "-Djava.class.path=./p2j.jar:."; |
... | ... | |
1015 | 1007 |
jsize jlength = (*env)->GetArrayLength(env, (jarray) command); |
1016 | 1008 |
int length = (int) jlength; |
1017 | 1009 |
|
1010 |
if (output_file_name[0] != 0) |
|
1011 |
{ |
|
1012 |
output_to_file_opt_found = 1; |
|
1013 |
} |
|
1014 |
|
|
1018 | 1015 |
/* make it 1-based, so it follows the structure of the command line arguments |
1019 | 1016 |
(0 is program name); also, one more position is needed for execvp, which requires for the |
1020 | 1017 |
arguments to be null-terminated. */ |
1021 |
char **arguments = (char**) malloc((length + 2) * sizeof(char*)); |
|
1018 |
char **arguments = (char**) malloc((length + 2 + output_to_file_opt_found) * sizeof(char*));
|
|
1022 | 1019 |
arguments[0] = NULL; |
1023 |
arguments[length + 1] = NULL; |
|
1020 |
|
|
1021 |
arguments[length + 1 + output_to_file_opt_found] = NULL; |
|
1022 | ||
1023 |
if (output_to_file_opt_found == 1) |
|
1024 |
{ |
|
1025 |
int len = strlen(output_file_name) + 1; |
|
1026 |
arguments[length + 1] = (char*) malloc(len * sizeof(char)); |
|
1027 |
memset(arguments[length + 1], '\0', len); |
|
1028 |
strcpy(arguments[length + 1], output_file_name); |
|
1029 |
} |
|
1024 | 1030 |
|
1025 | 1031 |
int i; |
1026 | 1032 |
for (i = 0; i < (int) length; i++) |
... | ... | |
1151 | 1157 |
} |
1152 | 1158 | |
1153 | 1159 |
/** |
1154 |
* Redirect STDOUT to an output file. |
|
1155 |
*/ |
|
1156 |
void stdout_redirect() |
|
1157 |
{ |
|
1158 |
int output_fd; |
|
1159 |
char pid[32]; |
|
1160 |
|
|
1161 |
/* convert pid to string */ |
|
1162 |
sprintf(pid, "%u", getpid()); |
|
1163 |
|
|
1164 |
/* replace %pid% placeholder */ |
|
1165 |
char* filename = replace_str(output_file_name, "%pid%", pid); |
|
1166 |
|
|
1167 |
// Open file |
|
1168 |
output_fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0644); |
|
1169 |
|
|
1170 |
if (output_fd == -1) |
|
1171 |
{ |
|
1172 |
fprintf(stderr, "Cannot open file %s\n", filename); |
|
1173 |
} |
|
1174 |
else |
|
1175 |
{ |
|
1176 |
/* Redirect STDOUT to file */ |
|
1177 |
if (dup2(output_fd, STDOUT_FILENO) == -1) |
|
1178 |
{ |
|
1179 |
close(output_fd); |
|
1180 |
fprintf(stderr, "Cannot redirect STDOUT to file %s\n", filename); |
|
1181 |
} |
|
1182 |
} |
|
1183 |
} |
|
1184 | ||
1185 |
/** |
|
1186 | 1160 |
* Main function which allows both password and P2J server-style authentication. |
1187 | 1161 |
* <p> |
1188 | 1162 |
* For P2J server-style authentiation, the following files need to exist in current directory: |