xref: /freebsd/usr.sbin/syslogd/tests/syslogd_test_common.sh (revision 66a022a30dd968631c64b73e473dd16589592fad)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2021, 2023 The FreeBSD Foundation
5#
6# This software was developed by Mark Johnston under sponsorship from
7# the FreeBSD Foundation.
8#
9# This software was developed by Jake Freeland under sponsorship from
10# the FreeBSD Foundation.
11#
12
13readonly SYSLOGD_UDP_PORT="5140"
14readonly SYSLOGD_CONFIG="${PWD}/syslog.conf"
15readonly SYSLOGD_LOCAL_SOCKET="${PWD}/log.sock"
16readonly SYSLOGD_PIDFILE="${PWD}/syslogd.pid"
17readonly SYSLOGD_LOCAL_PRIVSOCKET="${PWD}/logpriv.sock"
18
19# Start a private syslogd instance.
20syslogd_start()
21{
22    local jail bind_addr conf_file pid_file socket privsocket
23    local opt next other_args
24
25    # Setup loopback so we can deliver messages to ourself.
26    atf_check ifconfig lo0 inet 127.0.0.1/16
27
28    OPTIND=1
29    while getopts ":b:f:j:P:p:S:" opt; do
30        case "${opt}" in
31        b)
32            bind_addr="${OPTARG}"
33            ;;
34        f)
35            conf_file="${OPTARG}"
36            ;;
37        j)
38            jail="jexec ${OPTARG}"
39            ;;
40        P)
41            pid_file="${OPTARG}"
42            ;;
43        p)
44            socket="${OPTARG}"
45            ;;
46        S)
47            privsocket="${OPTARG}"
48            ;;
49        ?)
50            opt="${OPTARG}"
51            next="$(eval echo \${${OPTIND}})"
52
53            case "${next}" in
54            -* | "")
55                other_args="${other_args} -${opt}"
56                shift $((OPTIND - 1))
57                ;;
58            *)
59                other_args="${other_args} -${opt} ${next}"
60                shift ${OPTIND}
61                ;;
62            esac
63
64            # Tell getopts to continue parsing.
65            OPTIND=1
66            ;;
67        :)
68            atf_fail "The -${OPTARG} flag requires an argument"
69            ;;
70        esac
71    done
72
73    $jail syslogd \
74        -b "${bind_addr:-":${SYSLOGD_UDP_PORT}"}" \
75        -C \
76        -d \
77        -f "${conf_file:-${SYSLOGD_CONFIG}}" \
78        -H \
79        -P "${pid_file:-${SYSLOGD_PIDFILE}}" \
80        -p "${socket:-${SYSLOGD_LOCAL_SOCKET}}" \
81        -S "${privsocket:-${SYSLOGD_LOCAL_PRIVSOCKET}}" \
82        ${other_args} \
83        &
84
85    # Give syslogd a bit of time to spin up.
86    while [ "$((i+=1))" -le 20 ]; do
87        [ -S "${socket:-${SYSLOGD_LOCAL_SOCKET}}" ] && return
88        sleep 0.1
89    done
90    atf_fail "timed out waiting for syslogd to start"
91}
92
93# Simple logger(1) wrapper.
94syslogd_log()
95{
96    atf_check -s exit:0 -o empty -e empty logger $*
97}
98
99# Make syslogd reload its configuration file.
100syslogd_reload()
101{
102    pkill -HUP -F "${1:-${SYSLOGD_PIDFILE}}"
103}
104
105# Stop a private syslogd instance.
106syslogd_stop()
107{
108    local pid_file="${1:-${SYSLOGD_PIDFILE}}"
109    local socket_file="${2:-${SYSLOGD_LOCAL_SOCKET}}"
110    local privsocket_file="${3:-${SYSLOGD_LOCAL_PRIVSOCKET}}"
111
112    pid=$(cat "${pid_file}")
113    if pkill -F "${pid_file}"; then
114        wait "${pid}"
115        rm -f "${pid_file}" "${socket_file}" "${privsocket_file}"
116    fi
117}
118