Merge: feat-request-all

This commit is contained in:
Narvin Singh 2021-10-25 22:53:41 -04:00
commit 8d02ac14c9
3 changed files with 54 additions and 22 deletions

6
avdd
View File

@ -43,8 +43,10 @@ DEFAULT_PRE=' '
DEFAULT_SEP_L='| '
DEFAULT_SEP_R=' '
DEFAULT_SUF=' '
MOD_DIR="$(dirname "$0")"/mod
FIFO=/tmp/avdd-fifo-"$("$(dirname "$0")"/rpid)"
DIR="$(dirname "$0")"
MOD_DIR="${DIR}/mod"
DAEMON="$(basename "$0")"
FIFO="/tmp/${DAEMON}-fifo-$("${DIR}/rpid" $$)"
mod_list="${1-${DEFAULT_MOD_LIST}}"

36
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/${DAEMON}-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
# 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}"
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
done

30
rpid
View File

@ -22,6 +22,16 @@ EXAMPLES:
process.
rpid
Get the PID of the first ancestor of the current process, if there
is no ancestor named "not_a_process."
rpid $$ not_a_process
Get the PID of the current process itself, if it is named
"current_process."
rpid $$ current_process
'
# Validate the arguments
@ -40,25 +50,19 @@ fi
root_name="${2:-login}"
get_info() {
local -ri current_pid="$1"
if [[ ! -r "/proc/${current_pid}/status" ]]; then exit 1; fi
prev_pid="${pid}"
current_pid="${pid}"
while [[ "${name}" != "${root_name}" && -r "/proc/${current_pid}/status" ]]; do
mapfile info < \
<(grep --null -E -m 2 '^(Name|PPid):' "/proc/${current_pid}/status" \
| sort | cut -f 2)
name="${info[0]##[[:space:]]}"
name="${name%%[[:space:]]}"
ppid="${info[1]##[[:space:]]}"
ppid="${ppid%%[[:space:]]}"
}
next_pid="${pid}"
while [[ "${name}" != "${root_name}" && "${ppid}" -ne 1 ]]; do
get_info "${next_pid}";
name_pid="${next_pid}"
next_pid="${ppid}"
prev_pid="${current_pid}"
current_pid="${info[1]##[[:space:]]}"
current_pid="${current_pid%%[[:space:]]}"
done
printf '%s\n' "${prev_pid}"
if [[ "${name}" != "${root_name}" ]]; then exit 1; fi
printf '%s\n' "${name_pid}"