Skip to content

Commit d4e011a

Browse files
committed
Use syscall.Exec() on compatible operating systems
1 parent 656bba4 commit d4e011a

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

canteen/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@ The `canteen-maven-plugin` uses the same technique to prepend a simple platform
9393
cross-compiled with Go to the front of a jar.
9494

9595
When you execute a Canteen packaged jar, the bootstrap program captures all the command line arguments and the name
96-
of the current file. It then spawns a child process as `java -jar $args`, proxying `stdin`, `stdout`, and `stderr`
97-
between the shell and the child process.
96+
of the current file. On Windows, the bootstrap spawns a child process as `java -jar $args`, proxying `stdin`, `stdout`,
97+
and `stderr` between the shell and the child process. On Linux and MacOS, the bootstrap transfers process control to
98+
Java by invoking `syscall.Exec()`;

canteen/canteen-bootstrap/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
# Canteen Bootstrap
22

33
This module contains the golang bootstrap shim used to invoke `java -jar`. The shim tries to be transparent. It passes
4-
all command line arguments to the child process, so you can use it as if it were the Java process itself.
4+
all command line arguments to the java process, so you can use it as if you invoked Java itself.
55

6-
Once the bootstrap is running, stdin, stdout, and stderr are proxied to the child process. When the child process
7-
exits, the bootstrap assumes the child's exit code. At the moment, the bootstrap does not proxy signals between the
8-
shell and the child process.
6+
On Linux and MacOS, the bootstrap invokes `syscall.Exec()`, transparently transferring control of the process to Java.
7+
8+
On Windows, once the bootstrap is running, stdin, stdout, and stderr are proxied to the child process. When the child
9+
process exits, the bootstrap assumes the child's exit code. At the moment, the bootstrap does not proxy signals between
10+
the shell and the child process.
911

1012
The bootstrap is cross-compiled for 64-bit Linux, MacOS, and Windows as part of the Maven build and attached as
1113
additional artifacts with classifiers compatible with the [os-maven-plugin](https://github.com/trustin/os-maven-plugin).
1214

1315
## Improvements
1416

15-
* Propagate signals to and from child process
1617
* More platforms
1718
* More architectures

canteen/canteen-bootstrap/src/main/go/main.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"log"
1212
"os"
1313
"os/exec"
14+
"runtime"
1415
"strings"
1516
"syscall"
1617
)
@@ -27,19 +28,26 @@ func main() {
2728
args := []string{"-jar", strings.TrimPrefix(jarName, "./")}
2829
args = append(args, jarArgs...)
2930

30-
cmd := exec.Command(binary, args...)
31-
cmd.Stdout = os.Stdout
32-
cmd.Stderr = os.Stderr
33-
cmd.Stdin = os.Stdin
31+
if runtime.GOOS == "windows" {
32+
cmd := exec.Command(binary, args...)
33+
cmd.Stdout = os.Stdout
34+
cmd.Stderr = os.Stderr
35+
cmd.Stdin = os.Stdin
3436

35-
if err := cmd.Run(); err != nil {
36-
if err, ok := err.(*exec.ExitError); ok {
37-
if status, ok := err.Sys().(syscall.WaitStatus); ok {
38-
// Exit with the same code as the Java program
39-
os.Exit(status.ExitStatus())
37+
if err := cmd.Run(); err != nil {
38+
if err, ok := err.(*exec.ExitError); ok {
39+
if status, ok := err.Sys().(syscall.WaitStatus); ok {
40+
// Exit with the same code as the Java program
41+
os.Exit(status.ExitStatus())
42+
}
43+
} else {
44+
log.Fatalf("Bootstrap execution error: %v", err)
4045
}
41-
} else {
42-
log.Fatalf("Bootstrap execution error: %v", err)
46+
}
47+
} else {
48+
err = syscall.Exec(binary, append([]string{"java"}, args...), nil)
49+
if err != nil {
50+
log.Fatalf("Bootstrap execution error: %v", os.Environ())
4351
}
4452
}
4553
}

0 commit comments

Comments
 (0)