Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8314491: Linux: jexec launched via PATH fails to find java #15343

Closed
wants to merge 9 commits into from
14 changes: 9 additions & 5 deletions src/java.base/unix/native/launcher/jexec.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2017, 2023, Oracle and/or its affiliates. All rights reserved.
vpa1977 marked this conversation as resolved.
Show resolved Hide resolved
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -157,18 +157,22 @@ int main(int argc, const char * argv[]) {
char java[PATH_MAX + 1]; /* path to java binary */
const char ** nargv = NULL; /* new args array */
int nargc = 0; /* new args array count */
int argi = 0; /* index into old array */
int argi = 1; /* index into old array */
size_t alen = 0; /* length of new array */
#ifdef __linux__
const char* executable = "/proc/self/exe";
#else
const char* executable = argv[0];
#endif

/* Make sure we have something to work with */
if ((argc < 1) || (argv == NULL)) {
/* Shouldn't happen... */
errorExit(CRAZY_EXEC, CRAZY_EXEC_MSG);
}

/* Get the path to the java binary, which is in a known position relative
* to our current position, which is in argv[0]. */
if (getJavaPath(argv[argi++], java, RELATIVE_DEPTH) != 0) {
* to our current position, which is in executable. */
if (getJavaPath(executable, java, RELATIVE_DEPTH) != 0) {
errorExit(errno, MISSING_JAVA_MSG);
}
alen = (argc + 2) * (sizeof (const char *));
Expand Down
40 changes: 28 additions & 12 deletions test/jdk/tools/launcher/Jexec.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -23,14 +23,25 @@

/*
* @test
* @bug 8175000
* @bug 8175000 8314491
* @summary test jexec
* @requires os.family == "linux"
* @build TestHelper
* @run main Jexec
*/

import java.beans.Transient;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.HashMap;
vpa1977 marked this conversation as resolved.
Show resolved Hide resolved

public class Jexec extends TestHelper {
private final File testJar;
Expand All @@ -54,20 +65,12 @@ public class Jexec extends TestHelper {
}

public static void main(String... args) throws Exception {
// linux is the only supported platform, give the others a pass
if (!isLinux) {
System.err.println("Warning: unsupported platform test passes vacuously");
return;
}
// ok to run the test now
Jexec t = new Jexec();
t.run(null);
}

@Test
void jexec() throws Exception {
TestResult tr = doExec(jexecCmd.getAbsolutePath(),
testJar.getAbsolutePath(), message);
private void runTest(String... cmds) throws Exception {
TestResult tr = doExec(cmds);
if (!tr.isOK()) {
System.err.println(tr);
throw new Exception("incorrect exit value");
Expand All @@ -77,4 +80,17 @@ void jexec() throws Exception {
throw new Exception("expected message \'" + message + "\' not found");
}
}

@Test
void jexec() throws Exception {
runTest(jexecCmd.getAbsolutePath(),
testJar.getAbsolutePath(), message);
}

@Test
void jexecInPath() throws Exception {
Path jexec = Path.of(jexecCmd.getAbsolutePath());
runTest("/bin/sh", "-c",
String.format("PATH=%s ; jexec %s '%s'",jexec.getParent(), testJar.getAbsolutePath(), message));
}
}