Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix: Case when process name is a substring of another process handled (
Browse files Browse the repository at this point in the history
  • Loading branch information
lholota authored May 25, 2020
1 parent ac5a838 commit 7ca851c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public GenericContainerEx<SELF> withRelativeFileSystemBind(String relativePath,
}

public Integer getProcessUid(String processName) throws IOException, InterruptedException, ProcessNotFoundException {
ExecResult result = executeShellCommand("stat -c '%u' /proc/$(ps axf | grep '"+ processName +"' | grep -v grep | awk -v def=\"not-found\" '{ print $1 } END { if(NR==0) {print def} }')");
ExecResult result = executeShellCommand("stat -c '%u' /proc/$(ps axf | grep '"+ processName +"$' | grep -v grep | awk -v def=\"not-found\" '{ print $1 } END { if(NR==0) {print def} }')");

if(result.getExitCode() != 0) {
throw new ProcessNotFoundException(processName);
Expand All @@ -51,7 +51,7 @@ public Integer getProcessUid(String processName) throws IOException, Interrupted
}

public Integer getProcessGid(String processName) throws IOException, InterruptedException, ProcessNotFoundException {
ExecResult result = executeShellCommand("stat -c '%g' /proc/$(ps axf | grep '"+ processName +"' | grep -v grep | awk -v def=\"not-found\" '{ print $1 } END { if(NR==0) {print def} }')");
ExecResult result = executeShellCommand("stat -c '%g' /proc/$(ps axf | grep '"+ processName +"$' | grep -v grep | awk -v def=\"not-found\" '{ print $1 } END { if(NR==0) {print def} }')");

if(result.getExitCode() != 0) {
throw new ProcessNotFoundException(processName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ public void throwGivenNotExistingProcess() throws IOException, InterruptedExcept

_container.getProcessGid("not-existing");
}

@Test
public void returnResultWhenProcessNameSubstringOfAnotherProcess() throws IOException, InterruptedException, ProcessNotFoundException {
_container = new GenericContainerEx<>("alpine").withCommand("ash", "-c", "sleep 1000");
_container.start();

_container.execInContainer("ash", "-c", "sleep 100 &");

int gid = _container.getProcessGid("sleep 100");

assertEquals(0, gid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ public void throwGivenNotExistingProcess() throws IOException, InterruptedExcept

_container.getProcessUid("not-existing");
}

@Test
public void returnResultWhenProcessNameSubstringOfAnotherProcess() throws IOException, InterruptedException, ProcessNotFoundException {
_container = new GenericContainerEx<>("alpine").withCommand("ash", "-c", "sleep 1000");
_container.start();

_container.execInContainer("ash", "-c", "sleep 100 &");

int uid = _container.getProcessUid("sleep 100");

assertEquals(0, uid);
}
}

0 comments on commit 7ca851c

Please sign in to comment.