MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/clear_orphaned_virtfs_mounts
#!/usr/bin/perl
# cpanel - clear_orphaned_virtfs_mounts           Copyright(c) 2010 cPanel, Inc.
#                                                           All Rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel'; }

use strict;
use warnings;
use Cpanel::Config::LoadUserDomains ();
use Cpanel::CPAN::Getopt::Param       ();
use Cpanel::Filesys::Virtfs         ();
use Cpanel::AcctUtils               ();

my $prm = Cpanel::CPAN::Getopt::Param->new(
    {
        'help_coderef' => sub {
            print <<"END_USAGE";
Unmount any virtfs mounts whose users no longer exist or whose shell is not currently jailshell
  $0 --help       - this screen
  $0 --errorsonly - Do not have any output unless there are errors
  $0 --clearall   - Unmount all virtfs mounts regardless of user's jailshell status

END_USAGE
            exit;
        },

        # 'actions' => [
        #     [ 'usage', 1 ],                                    # help alias
        #     [ 'version', sub { print "$0 v0.1\n"; exit; } ],   #  $0 --version    - print version informtaion and exit
        # ],
    }
);

my %user_map = %{ Cpanel::Config::LoadUserDomains::loaduserdomains( undef, 0, 1 ) };
my $errorsonly = $prm->get_param('errorsonly') ? 1 : 0;
my $clear_all  = $prm->get_param('clearall')   ? 1 : 0;
my %processed_users;

for my $mount ( Cpanel::Filesys::Virtfs::get_virtfs_mounts() ) {
    my $username = Cpanel::Filesys::Virtfs::get_username_from_virtfs_mount_string($mount);
    next if exists $processed_users{$username};

    print "-- Begin user '$username' --\n" unless $errorsonly;

    my $umount = 0;
    if ( !exists $user_map{$username} ) {
        print "User no longer exists, cleaning orphan...\n" unless $errorsonly;
        $umount++;
    }
    elsif ( $clear_all || ( Cpanel::AcctUtils::getshell($username) ne Cpanel::Filesys::Virtfs::get_jailshell_path() ) ) {
        print "User no longer has jailshell, cleaning orphan...\n" unless ( $errorsonly || $clear_all );
        $umount++;
    }
    else {
        print "No action needed\n" unless $errorsonly;
    }

    if ($umount) {
        print "Cleaning virtfs mounts (if any)\n" unless $errorsonly;
        my ( $rc, @errors ) = Cpanel::Filesys::Virtfs::clean_user_virtfs($username);
        if ($rc) {
            print "Done\n" unless $errorsonly;
            $processed_users{$username}++;
        }
        else {
            print "Failed\n" . join( "\t", @errors ) . "\n";
        }
    }
    else {
        $processed_users{$username}++;
    }

    print "-- End user '$username' -- \n\n" unless $errorsonly;
}