|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
30 | 30 |
|
31 | 31 | #include "ProcessHandleImpl_unix.h"
|
32 | 32 |
|
33 |
| - |
| 33 | +#include <dirent.h> |
34 | 34 | #include <fcntl.h>
|
35 | 35 | #include <limits.h>
|
36 | 36 | #include <stdlib.h>
|
@@ -60,9 +60,131 @@ void os_initNative(JNIEnv *env, jclass clazz) {
|
60 | 60 | pageSize = sysconf(_SC_PAGESIZE);
|
61 | 61 | }
|
62 | 62 |
|
| 63 | +/* |
| 64 | + * Return pids of active processes, and optionally parent pids and |
| 65 | + * start times for each process. |
| 66 | + * For a specific non-zero pid, only the direct children are returned. |
| 67 | + * If the pid is zero, all active processes are returned. |
| 68 | + * Reads /proc and accumulates any process following the rules above. |
| 69 | + * The resulting pids are stored into an array of longs named jarray. |
| 70 | + * The number of pids is returned if they all fit. |
| 71 | + * If the parentArray is non-null, store also the parent pid. |
| 72 | + * In this case the parentArray must have the same length as the result pid array. |
| 73 | + * Of course in the case of a given non-zero pid all entries in the parentArray |
| 74 | + * will contain this pid, so this array does only make sense in the case of a given |
| 75 | + * zero pid. |
| 76 | + * If the jstimesArray is non-null, store also the start time of the pid. |
| 77 | + * In this case the jstimesArray must have the same length as the result pid array. |
| 78 | + * If the array(s) (is|are) too short, excess pids are not stored and |
| 79 | + * the desired length is returned. |
| 80 | + */ |
63 | 81 | jint os_getChildren(JNIEnv *env, jlong jpid, jlongArray jarray,
|
64 | 82 | jlongArray jparentArray, jlongArray jstimesArray) {
|
65 |
| - return unix_getChildren(env, jpid, jarray, jparentArray, jstimesArray); |
| 83 | + DIR* dir; |
| 84 | + struct dirent* ptr; |
| 85 | + pid_t pid = (pid_t) jpid; |
| 86 | + jlong* pids = NULL; |
| 87 | + jlong* ppids = NULL; |
| 88 | + jlong* stimes = NULL; |
| 89 | + jsize parentArraySize = 0; |
| 90 | + jsize arraySize = 0; |
| 91 | + jsize stimesSize = 0; |
| 92 | + jsize count = 0; |
| 93 | + |
| 94 | + arraySize = (*env)->GetArrayLength(env, jarray); |
| 95 | + JNU_CHECK_EXCEPTION_RETURN(env, -1); |
| 96 | + if (jparentArray != NULL) { |
| 97 | + parentArraySize = (*env)->GetArrayLength(env, jparentArray); |
| 98 | + JNU_CHECK_EXCEPTION_RETURN(env, -1); |
| 99 | + |
| 100 | + if (arraySize != parentArraySize) { |
| 101 | + JNU_ThrowIllegalArgumentException(env, "array sizes not equal"); |
| 102 | + return 0; |
| 103 | + } |
| 104 | + } |
| 105 | + if (jstimesArray != NULL) { |
| 106 | + stimesSize = (*env)->GetArrayLength(env, jstimesArray); |
| 107 | + JNU_CHECK_EXCEPTION_RETURN(env, -1); |
| 108 | + |
| 109 | + if (arraySize != stimesSize) { |
| 110 | + JNU_ThrowIllegalArgumentException(env, "array sizes not equal"); |
| 111 | + return 0; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * To locate the children we scan /proc looking for files that have a |
| 117 | + * position integer as a filename. |
| 118 | + */ |
| 119 | + if ((dir = opendir("/proc")) == NULL) { |
| 120 | + JNU_ThrowByNameWithMessageAndLastError(env, |
| 121 | + "java/lang/RuntimeException", "Unable to open /proc"); |
| 122 | + return -1; |
| 123 | + } |
| 124 | + |
| 125 | + do { // Block to break out of on Exception |
| 126 | + pids = (*env)->GetLongArrayElements(env, jarray, NULL); |
| 127 | + if (pids == NULL) { |
| 128 | + break; |
| 129 | + } |
| 130 | + if (jparentArray != NULL) { |
| 131 | + ppids = (*env)->GetLongArrayElements(env, jparentArray, NULL); |
| 132 | + if (ppids == NULL) { |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + if (jstimesArray != NULL) { |
| 137 | + stimes = (*env)->GetLongArrayElements(env, jstimesArray, NULL); |
| 138 | + if (stimes == NULL) { |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + while ((ptr = readdir(dir)) != NULL) { |
| 144 | + pid_t ppid = 0; |
| 145 | + jlong totalTime = 0L; |
| 146 | + jlong startTime = 0L; |
| 147 | + |
| 148 | + /* skip files that aren't numbers */ |
| 149 | + pid_t childpid = (pid_t) atoi(ptr->d_name); |
| 150 | + if ((int) childpid <= 0) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + |
| 154 | + // Get the parent pid, and start time |
| 155 | + ppid = os_getParentPidAndTimings(env, childpid, &totalTime, &startTime); |
| 156 | + if (ppid >= 0 && (pid == 0 || ppid == pid)) { |
| 157 | + if (count < arraySize) { |
| 158 | + // Only store if it fits |
| 159 | + pids[count] = (jlong) childpid; |
| 160 | + |
| 161 | + if (ppids != NULL) { |
| 162 | + // Store the parentPid |
| 163 | + ppids[count] = (jlong) ppid; |
| 164 | + } |
| 165 | + if (stimes != NULL) { |
| 166 | + // Store the process start time |
| 167 | + stimes[count] = startTime; |
| 168 | + } |
| 169 | + } |
| 170 | + count++; // Count to tabulate size needed |
| 171 | + } |
| 172 | + } |
| 173 | + } while (0); |
| 174 | + |
| 175 | + if (pids != NULL) { |
| 176 | + (*env)->ReleaseLongArrayElements(env, jarray, pids, 0); |
| 177 | + } |
| 178 | + if (ppids != NULL) { |
| 179 | + (*env)->ReleaseLongArrayElements(env, jparentArray, ppids, 0); |
| 180 | + } |
| 181 | + if (stimes != NULL) { |
| 182 | + (*env)->ReleaseLongArrayElements(env, jstimesArray, stimes, 0); |
| 183 | + } |
| 184 | + |
| 185 | + closedir(dir); |
| 186 | + // If more pids than array had size for; count will be greater than array size |
| 187 | + return count; |
66 | 188 | }
|
67 | 189 |
|
68 | 190 | /**
|
|
0 commit comments