| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | set -e -o pipefail |
| 4 | |
| 5 | . $builddir/tests/test_common.sh |
| 6 | probecheck "process58" || exit 255 |
| 7 | |
| 8 | # This test depends on the ablility of the init process to reap zombies, |
| 9 | # so we are going to skip it if the PID 1 is not Systemd |
| 10 | psscip-shell system . . ps. 1 | grepscip-shell system . . grep. -q "systemd" || { |
| 11 | echoscip-shell system . . echo. "PID 1 is not Systemd, skipping" && exit 255 |
| 12 | } |
| 13 | |
| 14 | function clean_processesscip-shell shell shell-project 0.0.0 clean_processes(). { |
| 15 | # Processes are in stopped state. SIGCONT cause their exiting |
| 16 | [scip-shell system . . `[`. -n "${ZOMBIE_PPID}" ] && killscip-shell system . . kill. -SIGCONT "${ZOMBIE_PPID}" |
| 17 | [scip-shell system . . `[`. -n "${PID}" ] && killscip-shell system . . kill. -SIGCONT "${PID}" |
| 18 | [scip-shell system . . `[`. -n "${ESCAPED_PID}" ] && killscip-shell system . . kill. -SIGCONT "${ESCAPED_PID}" |
| 19 | } |
| 20 | trap clean_processes EXIT |
| 21 | |
| 22 | PROC="$srcdir/stopped_process.sh" # the process go to stopped state after start |
| 23 | |
| 24 | # Parse "ps" and return command line of process with $PID |
| 25 | function get_process_cmdlinescip-shell shell shell-project 0.0.0 get_process_cmdline().() { |
| 26 | local PID="$1" |
| 27 | psscip-shell system . . ps. opid,cmd | grepscip-shell system . . grep. -E "^\s*$PID\s+" | sedscip-shell system . . sed. -E 's/^\s*'$PID'\s+(.*)$/\1/' |
| 28 | return 0 |
| 29 | } |
| 30 | |
| 31 | # Wait for process start |
| 32 | # We can scan processes before process's exec() and get wrong command_line |
| 33 | function wait_for_processscip-shell shell shell-project 0.0.0 wait_for_process().() { |
| 34 | local PID="$1" |
| 35 | local PROC_NAME="$2" |
| 36 | if [[ -z "$PROC_NAME" ]]; then |
| 37 | PROC_NAME="$PROC" |
| 38 | fi |
| 39 | for i in $(seqscip-shell system . . seq. 1 100); # wait max 100 * 300ms |
| 40 | do |
| 41 | PROCESS_CMDLINE=$(get_process_cmdlinescip-shell shell shell-project 0.0.0 get_process_cmdline(). "$PID") |
| 42 | [[ "${PROCESS_CMDLINE}" == *${PROC_NAME}* ]] && break |
| 43 | sleepscip-shell system . . sleep. 0.3s |
| 44 | done |
| 45 | } |
| 46 | |
| 47 | # Actively wait for zombie process with specified PPID and return its PID |
| 48 | # Wait max 100 x 300ms |
| 49 | function get_zombie_pid_from_ppidscip-shell shell shell-project 0.0.0 get_zombie_pid_from_ppid().() { |
| 50 | local PARENT_PID="$1" |
| 51 | for i in $(seqscip-shell system . . seq. 1 100); |
| 52 | do |
| 53 | ZOMBIE_PID=$(psscip-shell system . . ps. -ostate,pid --ppid "${PARENT_PID}" | grepscip-shell system . . grep. "^Z" | sedscip-shell system . . sed. -E 's/^.\s*?([[:digit:]]+).*$/\1/') |
| 54 | [scip-shell system . . `[`. "${ZOMBIE_PID}" != "" ] && break; |
| 55 | sleepscip-shell system . . sleep. 0.3s |
| 56 | done |
| 57 | if [scip-shell system . . `[`. "x${ZOMBIE_PID}" == "x" ]; then |
| 58 | echoscip-shell system . . echo. "Debug: The ZOMBIE_PID was not found!" >&2 |
| 59 | psscip-shell system . . ps. -ostate,pid --ppid "${PARENT_PID}" >&2 |
| 60 | fi |
| 61 | echoscip-shell system . . echo. ${ZOMBIE_PID} |
| 62 | } |
| 63 | |
| 64 | function get_command_line_node_textscip-shell shell shell-project 0.0.0 get_command_line_node_text().() { |
| 65 | local PID="$1" |
| 66 | local item='/oval_results/results/system/oval_system_characteristics/system_data/unix-sys:process58_item[@status="exists"]/unix-sys:pid[text()="'${PID}'"]' |
| 67 | assert_exists 1 "${item}" |
| 68 | $XPATH $result "${item}/../unix-sys:command_line/text()" 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 69 | } |
| 70 | |
| 71 | function assert_match_command_linescip-shell shell shell-project 0.0.0 assert_match_command_line().() { |
| 72 | local PID="$1" |
| 73 | local EXPECTED_COMMAND_LINE="$2" |
| 74 | |
| 75 | # get text from element and fix xpath bug |
| 76 | cmdline=$(get_command_line_node_textscip-shell shell shell-project 0.0.0 get_command_line_node_text(). "$PID" | sedscip-shell system . . sed. 's|<|<|g' | sedscip-shell system . . sed. 's|>|>|g') |
| 77 | |
| 78 | echoscip-shell system . . echo. "$cmdline" | grepscip-shell system . . grep. -qE "${EXPECTED_COMMAND_LINE}" || { # compare commands |
| 79 | echoscip-shell system . . echo. "Failed: expected command-line of process with PID=${PID} is '${EXPECTED_COMMAND_LINE}' but real is: '$cmdline'" |
| 80 | exit 1 |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | name=$(basenamescip-shell system . . basename. $0 .sh) |
| 85 | result=$(mktempscip-shell system . . mktemp. ${name}.out.XXXXXX) |
| 86 | echoscip-shell system . . echo. "result file: $result" |
| 87 | stderr=$(mktempscip-shell system . . mktemp. ${name}.err.XXXXXX) |
| 88 | echoscip-shell system . . echo. "stderr file: $stderr" |
| 89 | |
| 90 | ######################################################################## |
| 91 | ### Run monitored processes: |
| 92 | ######################################################################## |
| 93 | # Run process with simple params |
| 94 | "${PROC}" param1 param2 param3 & |
| 95 | PID=$! |
| 96 | [scip-shell system . . `[`. -n "${PID}" ] |
| 97 | # "/usr/bin/env bash ./stopped_process.sh param1 param2 param3" |
| 98 | CMDLINE_REGEX=$(printfscip-shell system . . printf. "%s" "bash.*stopped_process.sh.*param1.param2.param3") |
| 99 | |
| 100 | # Run zombie process (without full cmdline) |
| 101 | ( SHELL_PID=$BASHPID && ( killscip-shell system . . kill. -STOP $SHELL_PID && sleepscip-shell system . . sleep. 1 ) ) & |
| 102 | ZOMBIE_PPID=$! |
| 103 | ZOMBIE_PID=$(get_zombie_pid_from_ppidscip-shell shell shell-project 0.0.0 get_zombie_pid_from_ppid(). ${ZOMBIE_PPID}) |
| 104 | [scip-shell system . . `[`. -n "${ZOMBIE_PPID}" ] |
| 105 | # "[command_line_extra.sh] <defunct>" |
| 106 | ZOMBIE_CMDLINE_REGEX='\[(bash|sleep)]\ <defunct>' |
| 107 | |
| 108 | # Run process with special characters in parameters |
| 109 | "${PROC}" escaped "$(echoscip-shell system . . echo. -ne "\e\n\E[1;33m") \\\n\e" & |
| 110 | ESCAPED_PID=$! |
| 111 | [scip-shell system . . `[`. -n "${ESCAPED_PID}" ] |
| 112 | # "/usr/bin/env bash ./stopped_process.sh escaped . .[1;33m \\n\e" |
| 113 | ESCAPED_CMDLINE_REGEX=$(printfscip-shell system . . printf. "%s" "bash.*stopped_process.sh.*escaped.?.?*\n.\e") |
| 114 | |
| 115 | ######################################################################## |
| 116 | ### Wait for start of all processes (all processes have done exec()) |
| 117 | ######################################################################## |
| 118 | wait_for_processscip-shell shell shell-project 0.0.0 wait_for_process(). $PID |
| 119 | wait_for_processscip-shell shell shell-project 0.0.0 wait_for_process(). ${ZOMBIE_PID} "+(bash|sleep)" |
| 120 | wait_for_processscip-shell shell shell-project 0.0.0 wait_for_process(). ${ESCAPED_PID} |
| 121 | |
| 122 | ######################################################################## |
| 123 | ### Run evaluation: |
| 124 | ######################################################################## |
| 125 | echoscip-shell system . . echo. "Eval:" |
| 126 | $OSCAP oval eval --results $result $srcdir/$name.oval.xml 2> $stderr |
| 127 | [scip-shell system . . `[`. ! -s $stderr ] |
| 128 | rmscip-shell system . . rm. $stderr |
| 129 | |
| 130 | ######################################################################## |
| 131 | ### Check results: |
| 132 | ######################################################################## |
| 133 | [scip-shell system . . `[`. -s $result ] |
| 134 | assert_match_command_linescip-shell shell shell-project 0.0.0 assert_match_command_line(). "${PID}" "${CMDLINE_REGEX}" |
| 135 | assert_match_command_linescip-shell shell shell-project 0.0.0 assert_match_command_line(). "${ZOMBIE_PID}" "${ZOMBIE_CMDLINE_REGEX}" |
| 136 | assert_match_command_linescip-shell shell shell-project 0.0.0 assert_match_command_line(). "${ESCAPED_PID}" "${ESCAPED_CMDLINE_REGEX}" |
| 137 | |
| 138 | rmscip-shell system . . rm. $result |