|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# Copyright (c) 2024 Red Hat, Inc. |
| 4 | +# Author: Oldřich Jedlička <oldium.pro@gmail.com> |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | + |
| 20 | +start_tcsd() { |
| 21 | + local temp_dir |
| 22 | + local fifo_file |
| 23 | + local output_file |
| 24 | + local sleep_pid |
| 25 | + local tcsd_pid |
| 26 | + local ret |
| 27 | + |
| 28 | + [ -s /run/tcsd.pid ] && return 0 |
| 29 | + |
| 30 | + if ! ip link show up dev lo | grep -qw UP; then |
| 31 | + ip link set dev lo up && echo "lo" > /tmp/tcsd.if || : |
| 32 | + if ! ip link show up dev lo | grep -qw UP; then |
| 33 | + echo "Unable to set-up loopback network device" |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + fi |
| 37 | + |
| 38 | + if ! temp_dir="$(mktemp -d)"; then |
| 39 | + echo "Unable to create temporary directory" |
| 40 | + return 1 |
| 41 | + fi |
| 42 | + |
| 43 | + fifo_file="$temp_dir/fifo" |
| 44 | + output_file="$temp_dir/output" |
| 45 | + |
| 46 | + # If we have udev, let the initialization on udev |
| 47 | + if ! [ -f /lib/udev/rules.d/60-tpm-udev.rules ]; then |
| 48 | + chown tss: /dev/tpm0 |
| 49 | + chmod 660 /dev/tpm0 |
| 50 | + fi |
| 51 | + |
| 52 | + mkfifo "$fifo_file" |
| 53 | + |
| 54 | + # Start timeout to finish TCSD startup |
| 55 | + sleep 10 & |
| 56 | + sleep_pid=$! |
| 57 | + |
| 58 | + # The following loop ends when output side of FIFO closes (i.e. TCSD ends) |
| 59 | + { while IFS= read -r LINE; do |
| 60 | + echo "$LINE" |
| 61 | + case "$LINE" in |
| 62 | + *"TCSD up and running"*) |
| 63 | + kill $sleep_pid 2>/dev/null |
| 64 | + ;; |
| 65 | + esac |
| 66 | + done < $fifo_file && kill $sleep_pid; } >> "$output_file" 2>&1 & |
| 67 | + |
| 68 | + # TCSD in background mode logs into syslogd, so we would not have any logs |
| 69 | + # available for debugging, so start TCSD in foreground mode, but as a |
| 70 | + # background job. Unfortunatelly the redirected output to pipe is |
| 71 | + # block-buffered (see `man 3 setbuf`), so in order to see any output we |
| 72 | + # need to set it to line-buffered with stdbuf tool |
| 73 | + stdbuf -oL tcsd -f >$fifo_file 2>&1 & |
| 74 | + tcsd_pid=$! |
| 75 | + |
| 76 | + wait $sleep_pid 2>/dev/null |
| 77 | + |
| 78 | + if ps -A -o pid | awk -v pid="$tcsd_pid" '$1==pid {found=1} END {exit !found}'; then |
| 79 | + ret=0 |
| 80 | + echo $tcsd_pid > /run/tcsd.pid |
| 81 | + else |
| 82 | + ret=1 |
| 83 | + [ -s "$output_file" ] && cat "$output_file" |
| 84 | + fi |
| 85 | + |
| 86 | + rm -rf "$temp_dir" |
| 87 | + |
| 88 | + return $ret |
| 89 | +} |
| 90 | + |
| 91 | +stop_tcsd() { |
| 92 | + [ -s /run/tcsd.pid ] && { |
| 93 | + pid=$(cat /run/tcsd.pid) |
| 94 | + kill $pid >/dev/null 2>&1 || : |
| 95 | + rm -f /run/tcsd.pid |
| 96 | + } |
| 97 | + |
| 98 | + [ -s /tmp/tcsd.if ] && { |
| 99 | + ip link set dev lo down || : |
| 100 | + ip addr flush dev lo || : |
| 101 | + rm -f /tmp/tcsd.if |
| 102 | + } |
| 103 | +} |
0 commit comments