set TEST_NAME "pr13158"
if {![installtest_p]} { untested $TEST_NAME; return }

# This is a section of the original rename_module.exp test that caused
# the problem of a systemtap module sticking around to be found.  That
# test case bug found a real problem  - that staprun didn't handle
# SIGINT.

########## Inserting 2 Identical Modules: Staprun without -R #################
# Should give an error when inserting 2 identical modules 
# when calling staprun directly without using -R

set compile { exec stap -e {probe begin{printf("Hello\n");}} -p4 }
set module_path [eval $compile] 
set module_name [file rootname [file tail $module_path]]

spawn staprun $module_path
set firstid $spawn_id

# Make sure we get the output from the 1st copy of the module before
# trying to load the 2nd copy of the module.
set test "$TEST_NAME: Inserting 1st module"
set output_seen 0
expect {
    -timeout 30
    "Hello\r\n" { set output_seen 1 }
    timeout { fail "$test (timeout)" }
}
if {$output_seen} {
    pass "$test"
} else {
    fail "$test"
    return
}

set test "$TEST_NAME: Inserting 2 Identical Modules: Staprun without -R"
set failed 0

spawn staprun $module_path
set secondid $spawn_id

# Make sure it fails
expect {
    -timeout 30
    -re {^ERROR: Couldn't insert module .*: File exists\r\n} {
	exp_continue
    }
    -re {^ERROR: Rerun with staprun option '\-R' to rename this module.\r\n} {
	exp_continue
    }
    -re {^[^\r\n]*\r\n} {
	set failed 1		
    }
    timeout { fail "$test (timeout)" }
}

# Kill both of the hanging processes
set firstpid [exp_pid -i $firstid]
set secondpid [exp_pid -i $secondid]
kill -INT $firstpid
kill -INT $secondpid
catch { close -i $firstid }; catch { wait -i $firstid };
catch { close -i $secondid }; catch { wait -i $secondid }
if {$failed} {
    fail "$test"
} else {
    pass "$test"
}

# Is the module still loaded?
if {[catch { exec lsmod | grep -q $module_name }]} {
    pass "$TEST_NAME: module was correctly removed"
} else {
    fail "$TEST_NAME: module is still loaded!"
    catch {exec staprun -d $module_name}
}
