Path: blob/master/test/TestConfig/scripts/tools/sysvcleanup.pl
6004 views
#!/usr/bin/perl1##############################################################################2# Copyright (c) 2016, 2019 IBM Corp. and others3#4# This program and the accompanying materials are made available under5# the terms of the Eclipse Public License 2.0 which accompanies this6# distribution and is available at https://www.eclipse.org/legal/epl-2.0/7# or the Apache License, Version 2.0 which accompanies this distribution and8# is available at https://www.apache.org/licenses/LICENSE-2.0.9#10# This Source Code may also be made available under the following11# Secondary Licenses when the conditions for such availability set12# forth in the Eclipse Public License, v. 2.0 are satisfied: GNU13# General Public License, version 2 with the GNU Classpath14# Exception [1] and GNU General Public License, version 2 with the15# OpenJDK Assembly Exception [2].16#17# [1] https://www.gnu.org/software/classpath/license.html18# [2] http://openjdk.java.net/legal/assembly-exception.html19#20# 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-exception21##############################################################################2223use strict;2425#26# This script cleans up SysV memory and semaphores.27#2829my $numargs = @ARGV;30if ($numargs > 2) {31print("Error: incorrect usage!\n");32print("\tscriptname.pl userid zos\n");33print("\t\tuserid: user you wish to clean up SysV objects for\n");34print("\t\tzos: set this if your cleaning up on zos\n");35}3637#38# The user we are cleaning up ipcs for ...39#4041my $cleanupuser = "j9build";42# a1 is for Attach API, ad is for shared classes, 0a is for pltest (and for cloud at one time)43my $semaphoreProjid = "(0xad|0xa1|0x41|0x42|0x43|0x44|0x45|0x46|0x47|0x48|0x49|0x4a|0x4b|0x4c|0x4d|0x4e|0x4f|0x50|0x51|0x52|0x53|0x54|".44"0x81|0x81|0x82|0x83|0x84|0x85|0x86|0x87|0x88|0x89|0x8a|0x8b|0x8c|0x8d|0x8e|0x8f|0x90|0x91|0x92|0x93|0x94|0x0a)";4546my $memoryProjid = "(0xde|0x21|0x22|0x23|0x24|0x25|0x26|0x27|0x28|0x29|0x2a|0x2b|0x2c|0x2d|0x2e|0x2f|0x30|0x31|0x32|0x33|0x34|".47"0x61|0x61|0x62|0x63|0x64|0x65|0x66|0x67|0x68|0x69|0x6a|0x6b|0x6c|0x6d|0x6e|0x6f|0x70|0x71|0x72|0x73|0x74)";4849my $keyindex = 0;50my $idindex = 1;51my $ownerindex = 2;5253my @stdoutvar = ();54my $iter = "";55my $foundSemaphoreHeader = 0;56my $foundMemoryHeader = 0;5758my $memsremoved=0;59my $semsremoved=0;6061if ($numargs>0) {62$cleanupuser=$ARGV[0];63}64if (($numargs>1) && ($ARGV[1] =~ /zos/))65{66$keyindex=2;67$idindex=1;68$ownerindex=4;69}7071if (($numargs>1) && ($ARGV[1] =~ /aix/))72{73$keyindex=2;74$idindex=1;75$ownerindex=4;76}777879printf("Start SysV clean up for user:".$cleanupuser." ".$ARGV[1]."\n");8081#82# 1.) LIST MEMORY:83# - Call ipcs and store stdout in a local variable.84# - If the command fails we exit ASAP85#86@stdoutvar = `ipcs -m` or die("Error: could not execute ipcs!");8788#89# 2.) CLEAN UP SYSV MEMORY90# Loop through stdout and perform clean up of semaphores owned by 'user'91#92$iter = "";93$foundSemaphoreHeader = 0;94$foundMemoryHeader = 0;95foreach $iter (@stdoutvar) {9697if ($iter =~ /Shared Memory/) {98$foundMemoryHeader=1;99}100if (($iter =~ /$memoryProjid/) && ($foundMemoryHeader==1)) {101#remove any sequence of whitespace and insert a :102$iter =~s/[\s]+/:/g;103104#split the string around :105my @vals = split(/:/,$iter);106107#we expect 3 entries in this row ... the 1st three are kei,id,owner ...108#the rest are don't cares ...109if (@vals<3) {110die("ipcs did not return expected output");111}112113my $key = $vals[$keyindex];114my $id = $vals[$idindex];115my $owner = $vals[$ownerindex];116if (($owner =~ /$cleanupuser/) || ($cleanupuser =~ /all/)) {117my $cleanmem = "ipcrm -m $id";118my $out = `$cleanmem`;119print($out);120$memsremoved = $memsremoved + 1;121}122}123}124125#126# 3.) LIST SEMAPHORES:127# - Call ipcs and store stdout in a local variable.128# - If the command fails we exit ASAP129#130@stdoutvar = `ipcs -s` or die("Error: could not execute ipcs!");131132#133# 4.) CLEAN UP SYSV MEMORY134# Loop through stdout and perform clean up of semaphores owned by 'user'135#136$iter = "";137$foundSemaphoreHeader = 0;138$foundMemoryHeader = 0;139foreach $iter (@stdoutvar) {140141if ($iter =~ /Semaphore/) {142$foundSemaphoreHeader=1;143}144if (($iter =~ /$semaphoreProjid/) && ($foundSemaphoreHeader==1)) {145#remove any sequence of whitespace and insert a :146$iter =~s/[\s]+/:/g;147148#split the string around :149my @vals = split(/:/,$iter);150151#we expect 3 entries in this row ... the 1st three are kei,id,owner ...152#the rest are don't cares ...153if (@vals<3) {154die("ipcs did not return expected output");155}156my $key = $vals[$keyindex];157my $id = $vals[$idindex];158my $owner = $vals[$ownerindex];159if (($owner =~ /$cleanupuser/) || ($cleanupuser =~ /all/)) {160my $cleansem = "ipcrm -s $id";161my $out = `$cleansem`;162print($out);163$semsremoved = $semsremoved + 1;164}165}166}167168printf("Clean up is finished:\n");169printf("\tShared Memory Segments Removed:".$memsremoved."\n");170printf("\tShared Semaphores Removed:".$semsremoved."\n");1711720;173174175176