#
#
# osds_okaload.pm
# 
# Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
#
#
#    NAME
#      osds_okaload.pm - Linux OSD component of okaload.
#
#    DESCRIPTION
#      Purpose
#          start / stop previously installed OKA drivers.
#
#    NOTES
#      All user visible output should be done in the common code.
#      this will ensure a consistent look and feel across all platforms.
#
#

use strict;
package osds_okaload;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(
                  osds_check_okaadmin_changed
                  osds_verify_correct_oka_driver_version
                  osds_check_oka_loaded
                 );

use osds_okalib;
use okalib;
use File::Basename;

# fixed constants for use
use constant USM_SUCCESS      => 0;
use constant USM_FAIL         => 1;

#  osds_check_okaadmin_changed
#    We're called after the drivers are confirmed as installed.
#    look at the current udev permissions and see if the name of the
#    OKA admin group has changed. If so, we re-create the USM udev files
#    with the correct OKA admin name.

sub osds_check_okaadmin_changed
{
  my ($RH5);
  my ($udev_okaadmin);
  my ($okaadmin);
  my ($udev_perm_file);
  my ($ORACLE_HOME) = $ENV{ORACLE_HOME};

  # get the current system ASM admin group name
  $okaadmin = lib_get_oka_admin_name();

  # now get the ASM admin name recorded un the USM rules file
  $udev_perm_file = lib_osds_get_oka_RH5_udev_rules_file();

  # get the current udev ASM admin group name
  my($ret) = open (UDEV, "< $udev_perm_file");
  if (defined($ret))
  {
    while (<UDEV>)
    {
      # file format example:
      #      KERNEL=="oka",      GROUP="dba",  MODE="0664"
      my ($name, $group, $mode) = split /,/;
      if ($name eq "KERNEL==\"oka\"")
      {
        my (@tmp_array) = split(/=/, $group);
        $udev_okaadmin = $tmp_array[1];
        $udev_okaadmin =~ s/\"//g;
        last;
      }
    }
    close (UDEV);
  }

  # if the OKA admin name has not changed (typical case), we're done
  if (!defined($udev_okaadmin))
  {
     # this should not happen but.....
     $udev_okaadmin = "no udev file found";
  }
  if ($okaadmin eq $udev_okaadmin)
  {
    return USM_SUCCESS;
  }

  lib_osds_oka_create_udev($okaadmin);

  return USM_SUCCESS;
} # end osds_check_okaadmin_changed

# osds_verify_correct_oka_driver_version
#
# No longer needed. This code was only useful for RH4 where the driver version
# had to match the running kernel version.
#
sub osds_verify_correct_oka_driver_version
{
  return 1;
}

#  osds_check_oka_loaded
#    In order to check if our resource is healty we need to do some osds 
#    calls, here we do does checks and return 1 if module is online or 0 if
#    it is not.
sub osds_check_oka_loaded
{

  # find if the oracka module is loaded
  if (`lsmod | grep oracka` eq "") 
  {
    print("the oracka module is not loaded \n");
    return 0;
  }
  else
  {
    print("the oracka module is loaded \n");
    return 1;
  }

}

1;