mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[starter] Add a process starter for use by the installer for launching tools (#4931)
This commit is contained in:
86
processstarter/src/main/native/linux/main.cpp
Normal file
86
processstarter/src/main/native/linux/main.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <poll.h>
|
||||
#include <spawn.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <climits>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
char path[PATH_MAX];
|
||||
char dest[PATH_MAX];
|
||||
std::memset(dest, 0, sizeof(dest)); // readlink does not null terminate!
|
||||
pid_t pid = getpid();
|
||||
std::snprintf(path, PATH_MAX, "/proc/%d/exe", pid);
|
||||
int readlink_len = readlink(path, dest, PATH_MAX);
|
||||
if (readlink_len < 0) {
|
||||
std::perror("readlink");
|
||||
return 1;
|
||||
} else if (readlink_len == PATH_MAX) {
|
||||
std::printf("Truncation occured\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::filesystem::path exePath{dest};
|
||||
if (exePath.empty()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!exePath.has_stem()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!exePath.has_parent_path()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::filesystem::path jarPath{exePath};
|
||||
jarPath.replace_extension("jar");
|
||||
std::filesystem::path parentPath{exePath.parent_path()};
|
||||
|
||||
if (!parentPath.has_parent_path()) {
|
||||
return 1;
|
||||
}
|
||||
std::filesystem::path toolsFolder{parentPath.parent_path()};
|
||||
|
||||
std::filesystem::path Java = toolsFolder / "jdk" / "bin" / "java";
|
||||
|
||||
pid = 0;
|
||||
std::string data = jarPath;
|
||||
std::string jarArg = "-jar";
|
||||
char* const arguments[] = {jarArg.data(), data.data(), nullptr};
|
||||
|
||||
int status =
|
||||
posix_spawn(&pid, Java.c_str(), nullptr, nullptr, arguments, environ);
|
||||
if (status != 0) {
|
||||
char* home = std::getenv("JAVA_HOME");
|
||||
std::string javaLocal = "java";
|
||||
if (home != nullptr) {
|
||||
std::filesystem::path javaHomePath{home};
|
||||
javaHomePath /= "bin";
|
||||
javaHomePath /= "java";
|
||||
javaLocal = javaHomePath;
|
||||
}
|
||||
|
||||
status = posix_spawn(&pid, javaLocal.c_str(), nullptr, nullptr, arguments,
|
||||
environ);
|
||||
if (status != 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int childPid = syscall(SYS_pidfd_open, pid, 0);
|
||||
if (childPid <= 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct pollfd pfd = {childPid, POLLIN, 0};
|
||||
return poll(&pfd, 1, 3000);
|
||||
}
|
||||
80
processstarter/src/main/native/osx/main.mm
Normal file
80
processstarter/src/main/native/osx/main.mm
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
NSString* exePathPlat = [[NSBundle mainBundle] bundlePath];
|
||||
NSString* identifier = [[NSBundle mainBundle] bundleIdentifier];
|
||||
if (identifier == nil) {
|
||||
exePathPlat = [[NSBundle mainBundle] executablePath];
|
||||
}
|
||||
|
||||
std::filesystem::path exePath{[exePathPlat UTF8String]};
|
||||
if (exePath.empty()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!exePath.has_stem()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!exePath.has_parent_path()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::filesystem::path jarPath{exePath};
|
||||
jarPath.replace_extension("jar");
|
||||
std::filesystem::path parentPath{exePath.parent_path()};
|
||||
|
||||
if (!parentPath.has_parent_path()) {
|
||||
return 1;
|
||||
}
|
||||
std::filesystem::path toolsFolder{parentPath.parent_path()};
|
||||
|
||||
std::filesystem::path java = toolsFolder / "jdk" / "bin" / "java";
|
||||
|
||||
NSArray<NSString*>* Arguments =
|
||||
@[ @"-jar", [NSString stringWithFormat:@"%s", jarPath.c_str()] ];
|
||||
|
||||
NSTask* task = [[NSTask alloc] init];
|
||||
task.launchPath = [NSString stringWithFormat:@"%s", java.c_str()];
|
||||
task.arguments = Arguments;
|
||||
task.terminationHandler = ^(NSTask* t) {
|
||||
(void)t;
|
||||
CFRunLoopStop(CFRunLoopGetMain());
|
||||
};
|
||||
|
||||
if (![task launchAndReturnError:nil]) {
|
||||
task.terminationHandler = nil;
|
||||
|
||||
NSString* javaHome =
|
||||
[[[NSProcessInfo processInfo] environment] objectForKey:@"JAVA_HOME"];
|
||||
task = [[NSTask alloc] init];
|
||||
task.launchPath = @"java";
|
||||
if (javaHome != nil) {
|
||||
std::filesystem::path javaHomePath{[javaHome UTF8String]};
|
||||
javaHomePath /= "bin";
|
||||
javaHomePath /= "java";
|
||||
task.launchPath = [NSString stringWithFormat:@"%s", javaHomePath.c_str()];
|
||||
}
|
||||
task.arguments = Arguments;
|
||||
task.terminationHandler = ^(NSTask* t) {
|
||||
(void)t;
|
||||
CFRunLoopStop(CFRunLoopGetMain());
|
||||
};
|
||||
|
||||
if (![task launchAndReturnError:nil]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 3, false);
|
||||
|
||||
return task.running ? 0 : 1;
|
||||
}
|
||||
85
processstarter/src/main/native/windows/main.cpp
Normal file
85
processstarter/src/main/native/windows/main.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include "Windows.h"
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
||||
LPTSTR pCmdLine, int nCmdShow) {
|
||||
DWORD Status;
|
||||
WCHAR ExePathRaw[1024];
|
||||
Status = GetModuleFileNameW(NULL, ExePathRaw, 1024);
|
||||
if (Status == 0) {
|
||||
DWORD LastError = GetLastError();
|
||||
return LastError;
|
||||
}
|
||||
|
||||
std::filesystem::path ExePath{ExePathRaw};
|
||||
if (ExePath.empty()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!ExePath.has_stem()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!ExePath.has_parent_path()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::filesystem::path JarPath{ExePath};
|
||||
JarPath.replace_extension(L"jar");
|
||||
std::filesystem::path ParentPath{ExePath.parent_path()};
|
||||
|
||||
if (!ParentPath.has_parent_path()) {
|
||||
return 1;
|
||||
}
|
||||
std::filesystem::path ToolsFolder{ParentPath.parent_path()};
|
||||
|
||||
std::filesystem::path Javaw = ToolsFolder / L"jdk" / L"bin" / L"javaw.exe";
|
||||
|
||||
std::wstring ToRun = L" -jar \"";
|
||||
ToRun += JarPath;
|
||||
ToRun += L"\"";
|
||||
|
||||
STARTUPINFOW StartupInfo;
|
||||
PROCESS_INFORMATION ProcessInfo;
|
||||
|
||||
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
|
||||
StartupInfo.cb = sizeof(StartupInfo);
|
||||
ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
|
||||
|
||||
if (!CreateProcessW(Javaw.c_str(), ToRun.data(), NULL, NULL, FALSE, 0, NULL,
|
||||
NULL, &StartupInfo, &ProcessInfo)) {
|
||||
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
|
||||
StartupInfo.cb = sizeof(StartupInfo);
|
||||
ZeroMemory(&ProcessInfo, sizeof(ProcessInfo));
|
||||
|
||||
ToRun = L" -jar \"";
|
||||
ToRun += JarPath;
|
||||
ToRun += L"\"";
|
||||
|
||||
Status = GetEnvironmentVariableW(L"JAVA_HOME", ExePathRaw, 1024);
|
||||
std::wstring JavawLocal = L"javaw";
|
||||
if (Status != 0 && Status < 1024) {
|
||||
std::filesystem::path JavaHomePath{ExePathRaw};
|
||||
JavaHomePath /= "bin";
|
||||
JavaHomePath /= "javaw.exe";
|
||||
JavawLocal = JavaHomePath;
|
||||
}
|
||||
|
||||
if (!CreateProcessW(JavawLocal.c_str(), ToRun.data(), NULL, NULL, FALSE, 0,
|
||||
NULL, NULL, &StartupInfo, &ProcessInfo)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
Status =
|
||||
WaitForSingleObject(ProcessInfo.hProcess, 3000); // Wait for 3 seconds
|
||||
CloseHandle(ProcessInfo.hProcess);
|
||||
CloseHandle(ProcessInfo.hThread);
|
||||
|
||||
return Status == WAIT_TIMEOUT ? 0 : 1;
|
||||
}
|
||||
Reference in New Issue
Block a user