Path: blob/master/test/functional/cmdLineTests/runtimemxbeanTests/getPidTest.pl
6004 views
#!/usr/bin/perl12##############################################################################3# Copyright (c) 2017, 2020 IBM Corp. and others4#5# This program and the accompanying materials are made available under6# the terms of the Eclipse Public License 2.0 which accompanies this7# distribution and is available at https://www.eclipse.org/legal/epl-2.0/8# or the Apache License, Version 2.0 which accompanies this distribution and9# is available at https://www.apache.org/licenses/LICENSE-2.0.10#11# This Source Code may also be made available under the following12# Secondary Licenses when the conditions for such availability set13# forth in the Eclipse Public License, v. 2.0 are satisfied: GNU14# General Public License, version 2 with the GNU Classpath15# Exception [1] and GNU General Public License, version 2 with the16# OpenJDK Assembly Exception [2].17#18# [1] https://www.gnu.org/software/classpath/license.html19# [2] http://openjdk.java.net/legal/assembly-exception.html20#21# 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-exception22##############################################################################2324use strict;25use warnings;26use IPC::Open3;27use IO::Select;2829my $javaCmd = join(' ', @ARGV);3031my ($in, $out, $err);3233my $perlPid;3435my $javaPid;3637if ($^O eq 'cygwin') {38open3($in, $out, $err, $javaCmd);39$javaPid = <$out>;40# Command gets the list of PIDs of the Java processes system is running41$perlPid = `wmic process where "name='java.exe'" get ProcessID`;42print $in "getPid finished";43# String trim both sides of javaPid and perlPid for the index check44$javaPid =~ s/^\s+|\s+$//g;45$perlPid =~ s/^\s+|\s+$//g;46# After trimming, check if javaPid is in the list of PIDs system returns47# index() would return an index if it is in the list, return -1 otherwise48# (PASS if not -1)49if (index($perlPid, $javaPid) != -1) {50$perlPid = $javaPid;51}52} else {53$perlPid = open3($in, $out, $err, $javaCmd);54my $s = IO::Select->new();55$s->add($out);56$javaPid = 0;57while (my @ready = $s->can_read(5)) { # set timeout of 5 seconds58my $buf = '';59$! = 0;60my $read = sysread($ready[0], $buf, 1024);61if (defined($read)) {62if ($buf =~ /^\d+$/) {63$javaPid = $buf;64last;65}66} else {67print "Error in reading output of the Java process\n";68last;69}70}71print $in "getPid finished";72}7374if ($perlPid == $javaPid) {75print "PASS: RuntimeMXBean.getPid() returned correct PID.";76}77else {78print "FAIL: RuntimeMXBean.getPID() returned ${javaPid} instead of ${perlPid}";79}808182