0008-java.base-Enable-java.lang.Process-on-serenity.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Andrew Kaster <andrewdkaster@gmail.com>
  3. Date: Sun, 12 Jun 2022 23:15:17 -0600
  4. Subject: [PATCH] java.base: Enable java.lang.Process on serenity
  5. ---
  6. make/modules/java.base/Launcher.gmk | 2 +-
  7. make/modules/java.base/lib/CoreLibraries.gmk | 3 +
  8. .../libjava/ProcessHandleImpl_serenity.cpp | 164 ++++++++++++++++++
  9. .../unix/classes/java/lang/ProcessImpl.java | 7 +-
  10. 4 files changed, 174 insertions(+), 2 deletions(-)
  11. create mode 100644 src/java.base/serenity/native/libjava/ProcessHandleImpl_serenity.cpp
  12. diff --git a/make/modules/java.base/Launcher.gmk b/make/modules/java.base/Launcher.gmk
  13. index 700ddefda49e891ac1a2cfd8602fb8a9409ad1d4..78c884dae8271aea4431976823a0f18506cab4b4 100644
  14. --- a/make/modules/java.base/Launcher.gmk
  15. +++ b/make/modules/java.base/Launcher.gmk
  16. @@ -73,7 +73,7 @@ endif
  17. ################################################################################
  18. -ifeq ($(call isTargetOs, macosx aix linux), true)
  19. +ifeq ($(call isTargetOs, macosx aix linux serenity), true)
  20. $(eval $(call SetupJdkExecutable, BUILD_JSPAWNHELPER, \
  21. NAME := jspawnhelper, \
  22. SRC := $(TOPDIR)/src/$(MODULE)/unix/native/jspawnhelper, \
  23. diff --git a/make/modules/java.base/lib/CoreLibraries.gmk b/make/modules/java.base/lib/CoreLibraries.gmk
  24. index 0a61d009f34a4e73ace746d0cc6068fe2852e832..7867a3095dbe3d76c8db6d7d1948ae0f05d41c63 100644
  25. --- a/make/modules/java.base/lib/CoreLibraries.gmk
  26. +++ b/make/modules/java.base/lib/CoreLibraries.gmk
  27. @@ -90,6 +90,8 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \
  28. OPTIMIZATION := HIGH, \
  29. CFLAGS := $(CFLAGS_JDKLIB) \
  30. $(LIBJAVA_CFLAGS), \
  31. + CXXFLAGS := $(CXXFLAGS_JDKLIB) \
  32. + $(LIBJAVA_CXXFLAGS), \
  33. jdk_util.c_CFLAGS := $(VERSION_CFLAGS), \
  34. EXTRA_HEADER_DIRS := libfdlibm, \
  35. WARNINGS_AS_ERRORS_xlc := false, \
  36. @@ -102,6 +104,7 @@ $(eval $(call SetupJdkLibrary, BUILD_LIBJAVA, \
  37. LIBS_unix := -ljvm, \
  38. LIBS_linux := $(LIBDL), \
  39. LIBS_aix := $(LIBDL) $(LIBM),\
  40. + LIBS_serenity := $(LIBDL) -lcore, \
  41. LIBS_macosx := -framework CoreFoundation \
  42. -framework Foundation \
  43. -framework SystemConfiguration, \
  44. diff --git a/src/java.base/serenity/native/libjava/ProcessHandleImpl_serenity.cpp b/src/java.base/serenity/native/libjava/ProcessHandleImpl_serenity.cpp
  45. new file mode 100644
  46. index 0000000000000000000000000000000000000000..cc0c08cb85a682d66a00f6b48ad2871f83b5e719
  47. --- /dev/null
  48. +++ b/src/java.base/serenity/native/libjava/ProcessHandleImpl_serenity.cpp
  49. @@ -0,0 +1,166 @@
  50. +/*
  51. + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
  52. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  53. + *
  54. + * This code is free software; you can redistribute it and/or modify it
  55. + * under the terms of the GNU General Public License version 2 only, as
  56. + * published by the Free Software Foundation. Oracle designates this
  57. + * particular file as subject to the "Classpath" exception as provided
  58. + * by Oracle in the LICENSE file that accompanied this code.
  59. + *
  60. + * This code is distributed in the hope that it will be useful, but WITHOUT
  61. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  62. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  63. + * version 2 for more details (a copy is included in the LICENSE file that
  64. + * accompanied this code).
  65. + *
  66. + * You should have received a copy of the GNU General Public License version
  67. + * 2 along with this work; if not, write to the Free Software Foundation,
  68. + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  69. + *
  70. + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  71. + * or visit www.oracle.com if you need additional information or have any
  72. + * questions.
  73. + */
  74. +
  75. +#define AK_DONT_REPLACE_STD
  76. +
  77. +#include "jvm.h"
  78. +#include "jni.h"
  79. +#include "jni_util.h"
  80. +#include "java_lang_String.h"
  81. +
  82. +extern "C" {
  83. +#include "ProcessHandleImpl_unix.h"
  84. +}
  85. +
  86. +#include <AK/JsonArray.h>
  87. +#include <LibCore/DeprecatedFile.h>
  88. +#include <LibCore/File.h>
  89. +#include <LibCore/ProcessStatisticsReader.h>
  90. +#include <LibCore/Stream.h>
  91. +#include <stdio.h>
  92. +#include <string.h>
  93. +
  94. +/*
  95. + * Implementation of native ProcessHandleImpl functions for SERENITY.
  96. + * See ProcessHandleImpl_unix.c for more details.
  97. + */
  98. +
  99. +#define JAVA_TRY(expression, message) \
  100. + ({ \
  101. + auto _temporary_result = (expression); \
  102. + if (_temporary_result.is_error()) [[unlikely]] \
  103. + return throwSerenityError(env, _temporary_result.release_error(), (message)); \
  104. + _temporary_result.release_value(); \
  105. + })
  106. +
  107. +
  108. +static RefPtr<Core::DeprecatedFile> proc_all;
  109. +
  110. +extern "C" {
  111. +void os_initNative(JNIEnv *env, jclass clazz) {
  112. + proc_all = MUST(Core::DeprecatedFile::open("/sys/kernel/processes", Core::OpenMode::ReadOnly));
  113. +}
  114. +
  115. +jint os_getChildren(JNIEnv *env, jlong jpid, jlongArray jarray,
  116. + jlongArray jparentArray, jlongArray jstimesArray) {
  117. + return unix_getChildren(env, jpid, jarray, jparentArray, jstimesArray);
  118. +}
  119. +
  120. +pid_t os_getParentPidAndTimings(JNIEnv *env, pid_t pid, jlong *total, jlong *start) {
  121. + auto maybe_stats = Core::ProcessStatisticsReader::get_all(proc_all);
  122. + if (maybe_stats.is_error()) {
  123. + JNU_ThrowByNameWithLastError(env,
  124. + "java/lang/RuntimeException", "ProcessStatisticsReader::get_all failed");
  125. + return -1;
  126. + }
  127. + auto stats = maybe_stats.release_value();
  128. + auto proc_it = find_if(stats.processes.begin(), stats.processes.end(), [pid](auto& proc_stats) {
  129. + return proc_stats.pid == pid;
  130. + });
  131. + if (proc_it == stats.processes.end()) {
  132. + JNU_ThrowByNameWithLastError(env,
  133. + "java/lang/RuntimeException", "Selected pid does not exist");
  134. + return -1;
  135. + }
  136. + auto& proc = *proc_it;
  137. +
  138. + for (auto& thread : proc.threads) {
  139. + *total += thread.time_user + thread.time_kernel;
  140. + }
  141. +
  142. + *start = 0; // FIXME: When did thread start? not reported in /sys/kernel/processes
  143. +
  144. + return proc.ppid;
  145. +}
  146. +
  147. +
  148. +static void throwSerenityError(JNIEnv* env, Error const& e, StringView msg) {
  149. + char err_buf[256];
  150. + if (e.is_errno())
  151. + getErrorString(e.code(), err_buf, sizeof(err_buf));
  152. + else
  153. + strncpy(err_buf, e.string_literal().characters_without_null_termination(), sizeof(err_buf) - 1);
  154. + jstring s = JNU_NewStringPlatform(env, err_buf);
  155. + if (s != NULL) {
  156. + jobject x = JNU_NewObjectByName(env, "java/lang/RuntimeException",
  157. + "(Ljava/lang/String;)V", s);
  158. + if (x != NULL) {
  159. + env->Throw((jthrowable)x);
  160. + }
  161. + }
  162. + if (!env->ExceptionOccurred()) {
  163. + JNU_ThrowByName(env, "java/lang/RuntimeException", msg.characters_without_null_termination());
  164. + }
  165. +}
  166. +
  167. +void os_getCmdlineAndUserInfo(JNIEnv *env, jobject jinfo, pid_t pid) {
  168. + auto maybe_stats = Core::ProcessStatisticsReader::get_all(proc_all);
  169. + if (maybe_stats.is_error()) {
  170. + JNU_ThrowByNameWithLastError(env,
  171. + "java/lang/RuntimeException", "ProcessStatisticsReader::get_all failed");
  172. + return;
  173. + }
  174. +
  175. + auto stats = maybe_stats.release_value();
  176. + auto proc_it = find_if(stats.processes.begin(), stats.processes.end(), [pid](auto& proc_stats) {
  177. + return proc_stats.pid == pid;
  178. + });
  179. + if (proc_it == stats.processes.end()) {
  180. + JNU_ThrowByNameWithLastError(env,
  181. + "java/lang/RuntimeException", "Selected pid does not exist");
  182. + return;
  183. + }
  184. + auto& proc = *proc_it;
  185. +
  186. + unix_getUserInfo(env, jinfo, proc.pid);
  187. + JNU_CHECK_EXCEPTION(env);
  188. +
  189. + auto cmdline_file = JAVA_TRY(Core::File::open(DeprecatedString::formatted("/proc/{}/cmdline", pid), Core::File::OpenMode::Read), "Unable to open /proc/pid/cmdline"sv);
  190. + auto contents = JAVA_TRY(cmdline_file->read_until_eof(), "Unable to read /proc/pid/cmdline"sv);
  191. + auto cmdline = JAVA_TRY(JsonValue::from_string(contents), "Invalid JSON in /proc/pid/cmdline"sv);
  192. +
  193. + if (!cmdline.is_array())
  194. + return throwSerenityError(env, Error::from_string_literal("Not an array"), "Unexpected JSON in /proc/pid/cmdline"sv);
  195. +
  196. + jstring cmdexe = JNU_NewStringPlatform(env, cmdline.as_array()[0].as_string().characters());
  197. + env->ExceptionClear(); // unconditionally clear any exception
  198. + env->SetObjectField(jinfo, ProcessHandleImpl_Info_commandID, cmdexe);
  199. +
  200. + int arr_size = cmdline.as_array().size();
  201. + jclass string_clazz = JNU_ClassString(env);
  202. + CHECK_NULL(string_clazz);
  203. + jobjectArray java_cmdline = env->NewObjectArray(arr_size, string_clazz, NULL);
  204. + CHECK_NULL(java_cmdline);
  205. + jstring elem = NULL;
  206. + for (int i = 0; i < arr_size; ++i) {
  207. + elem = JNU_NewStringPlatform(env, cmdline.as_array()[i].as_string().characters());
  208. + CHECK_NULL(elem);
  209. + env->SetObjectArrayElement(java_cmdline, i, elem);
  210. + JNU_CHECK_EXCEPTION(env);
  211. + }
  212. + env->SetObjectField(jinfo, ProcessHandleImpl_Info_argumentsID, java_cmdline);
  213. + JNU_CHECK_EXCEPTION(env);
  214. +}
  215. +}
  216. diff --git a/src/java.base/unix/classes/java/lang/ProcessImpl.java b/src/java.base/unix/classes/java/lang/ProcessImpl.java
  217. index 2bf36f8f136794af4030e12c64026ab217696959..317bbf158a2032cc23ef3b73528a9e9c801612cf 100644
  218. --- a/src/java.base/unix/classes/java/lang/ProcessImpl.java
  219. +++ b/src/java.base/unix/classes/java/lang/ProcessImpl.java
  220. @@ -89,7 +89,9 @@ final class ProcessImpl extends Process {
  221. BSD(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK),
  222. - AIX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK);
  223. + AIX(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK),
  224. +
  225. + SERENITY(LaunchMechanism.POSIX_SPAWN, LaunchMechanism.FORK);
  226. final LaunchMechanism defaultLaunchMechanism;
  227. final Set<LaunchMechanism> validLaunchMechanisms;
  228. @@ -135,6 +137,7 @@ final class ProcessImpl extends Process {
  229. if (osName.equals("Linux")) { return LINUX; }
  230. if (osName.contains("OS X")) { return BSD; }
  231. if (osName.equals("AIX")) { return AIX; }
  232. + if (osName.equals("SerenityOS")) { return SERENITY; }
  233. throw new Error(osName + " is not a supported OS platform.");
  234. }
  235. @@ -348,6 +351,7 @@ final class ProcessImpl extends Process {
  236. switch (platform) {
  237. case LINUX:
  238. case BSD:
  239. + case SERENITY:
  240. stdin = (fds[0] == -1) ?
  241. ProcessBuilder.NullOutputStream.INSTANCE :
  242. new ProcessPipeOutputStream(fds[0]);
  243. @@ -467,6 +471,7 @@ final class ProcessImpl extends Process {
  244. case LINUX:
  245. case BSD:
  246. case AIX:
  247. + case SERENITY:
  248. // There is a risk that pid will be recycled, causing us to
  249. // kill the wrong process! So we only terminate processes
  250. // that appear to still be running. Even with this check,