Skip to content

Commit 4e3dc80

Browse files
committed
Exec final unix process to avoid bash parent process
1 parent 97d2f32 commit 4e3dc80

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

  • hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ public UnixShellScriptBuilder() {
13081308

13091309
@Override
13101310
public void command(List<String> command) {
1311-
line("exec /bin/bash -c \"", StringUtils.join(" ", command), "\"");
1311+
line("exec /bin/bash -c \"exec ", StringUtils.join(" ", command), "\"");
13121312
}
13131313

13141314
@Override

hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/TestContainerLaunch.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.io.FileOutputStream;
4747
import java.io.FileReader;
4848
import java.io.IOException;
49+
import java.io.InputStreamReader;
4950
import java.io.PrintStream;
5051
import java.io.PrintWriter;
5152
import java.nio.ByteBuffer;
@@ -1477,6 +1478,76 @@ public void testImmediateKill() throws Exception {
14771478
internalKillTest(false);
14781479
}
14791480

1481+
@Test
1482+
@Timeout(value = 30)
1483+
public void testNoBashParentProcess() throws Exception {
1484+
assumeNotWindows();
1485+
containerManager.start();
1486+
1487+
// Construct the Container-id
1488+
ApplicationId appId = ApplicationId.newInstance(1, 1);
1489+
ApplicationAttemptId appAttemptId =
1490+
ApplicationAttemptId.newInstance(appId, 1);
1491+
ContainerId cId = ContainerId.newContainerId(appAttemptId, 0);
1492+
1493+
ContainerLaunchContext containerLaunchContext =
1494+
recordFactory.newRecordInstance(ContainerLaunchContext.class);
1495+
1496+
// set up the rest of the container
1497+
List<String> commands = Arrays.asList(new String[] {"sleep 30 1>/dev/null 2>/dev/null"});
1498+
containerLaunchContext.setCommands(commands);
1499+
Priority priority = Priority.newInstance(10);
1500+
long createTime = 1234;
1501+
Token containerToken = createContainerToken(cId, priority, createTime);
1502+
1503+
StartContainerRequest scRequest =
1504+
StartContainerRequest.newInstance(containerLaunchContext,
1505+
containerToken);
1506+
List<StartContainerRequest> list = new ArrayList<StartContainerRequest>();
1507+
list.add(scRequest);
1508+
StartContainersRequest allRequests =
1509+
StartContainersRequest.newInstance(list);
1510+
containerManager.startContainers(allRequests);
1511+
1512+
NMContainerStatus nmContainerStatus =
1513+
containerManager.getContext().getContainers().get(cId)
1514+
.getNMContainerStatus();
1515+
assertEquals(priority, nmContainerStatus.getPriority());
1516+
1517+
int timeoutSecs = 0;
1518+
String pid = containerManager.getContext().getContainerExecutor().getProcessId(cId);
1519+
while (pid == null && timeoutSecs++ < 20) {
1520+
Thread.sleep(1000);
1521+
pid = containerManager.getContext().getContainerExecutor().getProcessId(cId);
1522+
LOG.info("Waiting for process start-file to be created");
1523+
}
1524+
assertNotNull(pid);
1525+
1526+
Process proc = Runtime.getRuntime().exec(new String[] {"ps", "-o", "command=", pid});
1527+
assertEquals(proc.waitFor(), 0);
1528+
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
1529+
1530+
assertEquals(reader.readLine(), "sleep 30");
1531+
1532+
List<ContainerId> containerIds = new ArrayList<ContainerId>();
1533+
containerIds.add(cId);
1534+
StopContainersRequest stopRequest =
1535+
StopContainersRequest.newInstance(containerIds);
1536+
containerManager.stopContainers(stopRequest);
1537+
1538+
BaseContainerManagerTest.waitForContainerState(containerManager, cId,
1539+
ContainerState.COMPLETE);
1540+
1541+
GetContainerStatusesRequest gcsRequest =
1542+
GetContainerStatusesRequest.newInstance(containerIds);
1543+
1544+
ContainerStatus containerStatus =
1545+
containerManager.getContainerStatuses(gcsRequest)
1546+
.getContainerStatuses().get(0);
1547+
assertEquals(ContainerExitStatus.KILLED_BY_APPMASTER,
1548+
containerStatus.getExitStatus());
1549+
}
1550+
14801551
@SuppressWarnings("rawtypes")
14811552
@Test
14821553
@Timeout(value = 10)

0 commit comments

Comments
 (0)