#!/bin/bash
###############################################################################
##
## Copyright (c) Kerlabs, 2009
##
## This program is free software;  you can redistribute it and#or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
## for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program;  if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
###############################################################################
#
# Description:  Test program for Kerrighed checkpoint/restart of POSIX SHM
#
# Author:       Matthieu Fertré, matthieu.fertre@kerlabs.com
#

source `dirname $0`/lib_cr.sh
source `dirname $0`/lib_cr_ipc.sh

description="Checkpoint/Restart of POSIX SHM object (no C/R of process)"

cr_posix_shm01()
{
    TCID="$FUNCNAME"               # Identifier of this testcase.
    TST_COUNT=$[$TST_COUNT+1]      # Test case number.

    local shmname="${FUNCNAME}_$$"
    local written_msg="$RANDOM"

    #
    # WARNING: this test must be run without DISTANT_FORK
    #
    local oldincap=`krgcapset -s - | grep "Inheritable Effective" | cut -d: -f2`
    local oldefcap=`krgcapset -s - | grep "^Effective" | cut -d: -f2`
    krgcapset -d -DISTANT_FORK
    krgcapset -e -DISTANT_FORK

    # create the shm and get its identifier
    create_posix_shm $shmname $written_msg || return $?

    # dump it
    dump_posix_shm $shmname /var/chkpt/shm_${shmname}_v1.bin || return $?

    # check the value is still ok
    check_posix_shm_value $shmname "$written_msg" || return $?

    # update the value
    write_posix_shm_value $shmname "$RANDOM" || return $?

    # delete it
    delete_posix_shm $shmname || return $?

    # restore it
    restore_posix_shm $shmname /var/chkpt/shm_${shmname}_v1.bin || return $?

    check_posix_shm_value $shmname "$written_msg" || return $?

    written_msg="$RANDOM"
    write_posix_shm_value $shmname "$written_msg" || return $?

    check_posix_shm_value $shmname "$written_msg" || return $?

    dump_posix_shm $shmname /var/chkpt/shm_${shmname}_v2.bin || return $?

    written_msg="$RANDOM"
    write_posix_shm_value $shmname "$written_msg" || return $?

    dump_posix_shm $shmname /var/chkpt/shm_${shmname}_v3.bin || return $?

    delete_posix_shm $shmname || return $?

    restore_posix_shm $shmname /var/chkpt/shm_${shmname}_v3.bin || return $?

    check_posix_shm_value $shmname "$written_msg" || return $?

    delete_posix_shm $shmname || return $?

    ret=$?

    #
    # WARNING: this test was run without DISTANT_FORK
    #
    krgcapset -d $oldincap
    krgcapset -e $oldefcap

    print_success $ret
    return $ret
}

CR_setup $@ || exit $?

cr_posix_shm01 || exit $?
