Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/runtimemxbeanTests/getPidTest.pl
6004 views
1
#!/usr/bin/perl
2
3
##############################################################################
4
# Copyright (c) 2017, 2020 IBM Corp. and others
5
#
6
# This program and the accompanying materials are made available under
7
# the terms of the Eclipse Public License 2.0 which accompanies this
8
# distribution and is available at https://www.eclipse.org/legal/epl-2.0/
9
# or the Apache License, Version 2.0 which accompanies this distribution and
10
# is available at https://www.apache.org/licenses/LICENSE-2.0.
11
#
12
# This Source Code may also be made available under the following
13
# Secondary Licenses when the conditions for such availability set
14
# forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
15
# General Public License, version 2 with the GNU Classpath
16
# Exception [1] and GNU General Public License, version 2 with the
17
# OpenJDK Assembly Exception [2].
18
#
19
# [1] https://www.gnu.org/software/classpath/license.html
20
# [2] http://openjdk.java.net/legal/assembly-exception.html
21
#
22
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
23
##############################################################################
24
25
use strict;
26
use warnings;
27
use IPC::Open3;
28
use IO::Select;
29
30
my $javaCmd = join(' ', @ARGV);
31
32
my ($in, $out, $err);
33
34
my $perlPid;
35
36
my $javaPid;
37
38
if ($^O eq 'cygwin') {
39
open3($in, $out, $err, $javaCmd);
40
$javaPid = <$out>;
41
# Command gets the list of PIDs of the Java processes system is running
42
$perlPid = `wmic process where "name='java.exe'" get ProcessID`;
43
print $in "getPid finished";
44
# String trim both sides of javaPid and perlPid for the index check
45
$javaPid =~ s/^\s+|\s+$//g;
46
$perlPid =~ s/^\s+|\s+$//g;
47
# After trimming, check if javaPid is in the list of PIDs system returns
48
# index() would return an index if it is in the list, return -1 otherwise
49
# (PASS if not -1)
50
if (index($perlPid, $javaPid) != -1) {
51
$perlPid = $javaPid;
52
}
53
} else {
54
$perlPid = open3($in, $out, $err, $javaCmd);
55
my $s = IO::Select->new();
56
$s->add($out);
57
$javaPid = 0;
58
while (my @ready = $s->can_read(5)) { # set timeout of 5 seconds
59
my $buf = '';
60
$! = 0;
61
my $read = sysread($ready[0], $buf, 1024);
62
if (defined($read)) {
63
if ($buf =~ /^\d+$/) {
64
$javaPid = $buf;
65
last;
66
}
67
} else {
68
print "Error in reading output of the Java process\n";
69
last;
70
}
71
}
72
print $in "getPid finished";
73
}
74
75
if ($perlPid == $javaPid) {
76
print "PASS: RuntimeMXBean.getPid() returned correct PID.";
77
}
78
else {
79
print "FAIL: RuntimeMXBean.getPID() returned ${javaPid} instead of ${perlPid}";
80
}
81
82