#!/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 SYSV IPC SHM
#               with C/R of processus
#
# Author:       Matthieu Fertré, matthieu.fertre@kerlabs.com
#

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

description="Checkpoint/Restart of SYSV IPC SHM object with C/R of processus"

TESTCMD="ipcshm-tool"

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

    # create the shm and get its identifier
    local written_msg="$RANDOM"
    local shmpath=`mktemp -d`

    create_sysv_shm $shmpath $written_msg || return $?

    TESTCMD_OPTIONS="-r10000 -q -i $SHMID"

    runcommand +CHECKPOINTABLE 1 nosync || return $?

    wait_other_instances $FUNCNAME 'ipcs -m|grep "^0x"|wc -l' || return $?

    freeze_process $PID $TESTCMD || return $?

    checkpoint_frozen_process $PID $TESTCMD || return $?

    dump_sysv_shm $SHMID $shmpath $shmpath/shm_v1.bin || return $?

    unfreeze_process $PID $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    restart_process $PID 1 $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    delete_sysv_shm $SHMID $shmpath || return $?

    restart_process_must_fail $PID 1 $TESTCMD || return $?

    restore_sysv_shm $SHMID $shmpath $shmpath/shm_v1.bin || return $?

    restart_process $PID 1 $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    delete_sysv_shm $SHMID $shmpath || return $?

    ret=$?

    # thanks to NFS, even rm -rf may fail
    rm -rf $shmpath 2> /dev/null

    print_success $ret
    return $ret
}

CR_setup $@ || exit $?

cr_ipc_shm02 || exit $?
