Skip to content

Commit 5b5bcaf

Browse files
committed
Minor code normalizations.
1 parent 1897879 commit 5b5bcaf

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java

+27-43
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,7 @@ protected ForOpenJ9(Socket socket) {
16621662
public static VirtualMachine attach(String processId) throws IOException {
16631663
return attach(processId, 5000, Platform.isWindows()
16641664
? new Dispatcher.ForJnaWindowsEnvironment()
1665-
: new Dispatcher.ForJnaPosixEnvironment(15, 100, TimeUnit.MILLISECONDS), ignoreUser);
1665+
: new Dispatcher.ForJnaPosixEnvironment(15, 100, TimeUnit.MILLISECONDS));
16661666
}
16671667

16681668
/**
@@ -1692,7 +1692,7 @@ public static VirtualMachine attach(String processId, int timeout, Dispatcher di
16921692
}
16931693
virtualMachines = new ArrayList<Properties>();
16941694
for (File aVmFolder : vmFolder) {
1695-
if (aVmFolder.isDirectory() && isFileOwnedByUid(dispatcher, aVmFolder, userId)) {
1695+
if (aVmFolder.isDirectory() && (userId == 0L || dispatcher.getOwnerIdOf(aVmFolder) == userId)) {
16961696
File attachInfo = new File(aVmFolder, "attachInfo");
16971697
if (attachInfo.isFile()) {
16981698
Properties virtualMachine = new Properties();
@@ -1703,7 +1703,12 @@ public static VirtualMachine attach(String processId, int timeout, Dispatcher di
17031703
inputStream.close();
17041704
}
17051705
int targetProcessId = Integer.parseInt(virtualMachine.getProperty("processId"));
1706-
long targetUserId = getUserId(virtualMachine);
1706+
long targetUserId;
1707+
try {
1708+
targetUserId = Long.parseLong(virtualMachine.getProperty("userUid"));
1709+
} catch (NumberFormatException ignored) {
1710+
targetUserId = 0L;
1711+
}
17071712
if (userId != 0L && targetUserId == 0L) {
17081713
targetUserId = dispatcher.getOwnerIdOf(attachInfo);
17091714
}
@@ -1750,13 +1755,18 @@ public static VirtualMachine attach(String processId, int timeout, Dispatcher di
17501755
key = Long.toHexString(SECURE_RANDOM.nextLong());
17511756
}
17521757
File reply = new File(receiver, "replyInfo");
1753-
long targetUserId = getUserId(target);
1758+
long targetUserId;
1759+
try {
1760+
targetUserId = Long.parseLong(target.getProperty("userUid"));
1761+
} catch (NumberFormatException ignored) {
1762+
targetUserId = 0L;
1763+
}
17541764
try {
17551765
if (reply.createNewFile()) {
17561766
dispatcher.setPermissions(reply, 0600);
17571767
}
1758-
if (0 == userId && 0 != targetUserId) {
1759-
dispatcher.chownFileToTargetUid(reply, targetUserId);
1768+
if (userId == 0L && targetUserId != 0L) {
1769+
dispatcher.chownFileToUser(reply, targetUserId);
17601770
}
17611771
FileOutputStream outputStream = new FileOutputStream(reply);
17621772
try {
@@ -1839,31 +1849,6 @@ public static VirtualMachine attach(String processId, int timeout, Dispatcher di
18391849
}
18401850
}
18411851

1842-
/**
1843-
* Returns the userUid present in the virtualMachine properties file
1844-
* @param virtualMachine Properties of the J9 attachInfo file
1845-
* @return the userUid if it can be parsed, <code>0L</code> otherwise.
1846-
*/
1847-
private static long getUserId(Properties virtualMachine) {
1848-
long targetUserId;
1849-
try {
1850-
targetUserId = Long.parseLong(virtualMachine.getProperty("userUid"));
1851-
} catch (NumberFormatException ignored) {
1852-
targetUserId = 0L;
1853-
}
1854-
return targetUserId;
1855-
}
1856-
1857-
/**
1858-
* Check if the file is owned by the UID. Note that UID 0 "owns" all files.
1859-
* @param aVmFolder File or directory
1860-
* @param userId user UID.
1861-
* @return true if the uid owns the file or uid == 0.
1862-
*/
1863-
private static boolean isFileOwnedByUid(Dispatcher dispatcher, File aVmFolder, long userId) {
1864-
return 0 == userId || dispatcher.getOwnerIdOf(aVmFolder) == userId;
1865-
}
1866-
18671852
/**
18681853
* {@inheritDoc}
18691854
*/
@@ -2075,11 +2060,12 @@ public interface Dispatcher {
20752060
void decrementSemaphore(File directory, String name, boolean global, int count);
20762061

20772062
/**
2078-
* change the ownership of a file. Can be called only if this process is owned by root.
2079-
* @param path path to the file
2080-
* @param targetUserId effective userid
2063+
* Changes the ownership of a file. Can be called only if this process is owned by root.
2064+
*
2065+
* @param file The path of the file to change ownership of.
2066+
* @param userId The user that should own the file.
20812067
*/
2082-
void chownFileToTargetUid(File path, long targetUserId);
2068+
void chownFileToUser(File file, long userId);
20832069

20842070
/**
20852071
* A connector implementation for a POSIX environment using JNA.
@@ -2221,9 +2207,8 @@ public void decrementSemaphore(File directory, String name, boolean global, int
22212207
/**
22222208
* {@inheritDoc}
22232209
*/
2224-
@Override
2225-
public void chownFileToTargetUid(File file, long targetUserId) {
2226-
library.chown(file.getAbsolutePath(), targetUserId);
2210+
public void chownFileToUser(File file, long userId) {
2211+
library.chown(file.getAbsolutePath(), userId);
22272212
}
22282213

22292214
/**
@@ -2334,12 +2319,12 @@ protected interface PosixLibrary extends Library {
23342319
/**
23352320
* Runs the {@code chown} command.
23362321
*
2337-
* @param path The file path.
2338-
* @param uid The userid to set.
2322+
* @param path The file path.
2323+
* @param userId The user id to set.
23392324
* @return The return code.
23402325
* @throws LastErrorException If an error occurred.
23412326
*/
2342-
int chown(String path, long uid) throws LastErrorException;
2327+
int chown(String path, long userId) throws LastErrorException;
23432328

23442329
/**
23452330
* Runs the {@code ftok} command.
@@ -2527,8 +2512,7 @@ public void decrementSemaphore(File directory, String name, boolean global, int
25272512
/**
25282513
* {@inheritDoc}
25292514
*/
2530-
@Override
2531-
public void chownFileToTargetUid(File path, long targetUserId) {
2515+
public void chownFileToUser(File file, long userId) {
25322516
/* do nothing */
25332517
}
25342518

0 commit comments

Comments
 (0)