#!/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
#               Semaphore arrays 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 sem object with C/R of processus"

TESTCMD="ipcsem-tool"

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

    # create the sem and get its identifier
    local sempath=`mktemp -d`
    local semvalue="-1"

    create_sem $sempath 5 || return $?

    # init with random value
    local i=0
    while [ $i -lt 5 ]; do
	ipcsem-tool -a$i:$(($RANDOM % 10)) -i $SEMID -q
	i=$(($i+1))
    done

    save_sem_value $SEMID $sempath || return $?
    semvalue=$SEMVALUE

    TESTCMD_OPTIONS="-q -L -i $SEMID"

    runcommand +CHECKPOINTABLE 2 nosync || return $?

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

    freeze_process $PID $TESTCMD || return $?

    checkpoint_frozen_process $PID $TESTCMD || return $?

    dump_sem $SEMID $sempath $sempath/sem_v1.bin || return $?

    unfreeze_process $PID $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    check_sem_value $SEMID $sempath "$semvalue" || return $?

    restart_process $PID 1 $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    check_sem_value_has_changed $SEMID $sempath "$semvalue" || return $?

    delete_sem $SEMID $sempath || return $?

    restart_process_must_fail $PID 1 $TESTCMD || return $?

    restore_sem $SEMID $sempath $sempath/sem_v1.bin || return $?

    restart_process $PID 1 $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    check_sem_value $SEMID $sempath "$semvalue" || return $?

    delete_sem $SEMID $sempath || return $?

    ret=$?

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

    print_success $ret
    return $ret
}

CR_setup $@ || exit $?

cr_ipc_sem02 || exit $?
