Feat: Send request to all daemons

Write the requests to all named pipes if the mod list begins with
'>>* '. This feature will probably not be merged into the main branch
because we have to search for the fifo files before each send, and if
the daemon for this login session exits, the scheduler will continue
running, which may not be desirable.
This commit is contained in:
Narvin Singh 2021-01-03 01:10:16 -05:00
parent f2bdd47bec
commit 2ee11a5eac

40
avds
View File

@ -5,7 +5,8 @@ USAGE: avds <mod_list|-h|-[-]help> [<when>] [<repeat>]
mod_list
A comma or space separated list of modules to request that
the daemon execute.
the daemon execute. If mod_list begins this '>>* ', then
requests are sent to all running daemons.
when The integer number of milliseconds to wait before sending the
request, or one of the following values:
@ -31,6 +32,10 @@ EXAMPLES:
avds 'vol,bl'
Send the previous requests to all running daemons.
avds '>>* vol,bl'
If there are cpu and memory usage modules named 'cpu' and 'mem'
that update the cpu and memory usage statuses, send a requst to
update both of those statuses every 5 seconds.
@ -44,7 +49,6 @@ EXAMPLES:
avds 'bat,dt' m true
"
DAEMON=avdd
FIFO=/tmp/"${DAEMON}"-fifo-"$("$(dirname "$0")"/rpid $$)"
# Convert integer milliseconds to floating point seconds
ms_to_s () {
@ -65,6 +69,15 @@ if [[ "${mod_list}" =~ ^(-h|-(-)?help)$ ]]; then
exit 0
fi
# Check if we will be sending requests to all daemons, or set the named pipe
# for this login session
if [[ "${mod_list}" =~ ^\>\>\*\ ]]; then
mod_list="${mod_list:4}"
is_request_all=1
else
fifo=/tmp/avdd-fifo-"$("$(dirname "$0")"/rpid $$)"
fi
when="${2:-0}"
# Validate when
@ -103,11 +116,24 @@ while [[ "${first_run}" -eq 1 || -n "${repeat}" ]]; do
fi
fi
# Write each command to the pipe
if [[ ! -p "${FIFO}" ]]; then
printf 'The daemon %s is not running\n' "${DAEMON}" 1>&2
exit 1
# Write each command to the named pipe for this login session
if [[ ! -v is_request_all ]]; then
if [[ ! -p "${fifo}" ]]; then
printf 'The daemon %s is not running\n' "${DAEMON}" 1>&2
exit 1
fi
printf '%s\n' "${mod_list}" >> "${fifo}"
# Write each command to all the daemon named pipes
else
readarray -d '' fifos \
< <(find /tmp -maxdepth 1 -type p -name "${DAEMON}-fifo-*" -print0)
if [[ "${#fifos[@]}" -eq 0 ]]; then
printf 'There are no daemons running\n' 1>&2
exit 1
fi
for fifo in "${fifos[@]}"; do
printf '%s\n' "${mod_list}" >> "${fifo}"
done
fi
printf '%s\n' "${mod_list}" >> "${FIFO}"
done