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_L='| '
DEFAULT_SEP_R=' ' DEFAULT_SEP_R=' '
DEFAULT_SUF=' ' DEFAULT_SUF=' '
MOD_DIR="$(dirname "$0")"/mod DIR="$(dirname "$0")"
FIFO=/tmp/avdd-fifo-"$("$(dirname "$0")"/rpid)" MOD_DIR="${DIR}/mod"
DAEMON="$(basename "$0")"
FIFO="/tmp/${DAEMON}-fifo-$("${DIR}/rpid" $$)"
mod_list="${1-${DEFAULT_MOD_LIST}}" mod_list="${1-${DEFAULT_MOD_LIST}}"

40
avds
View File

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

30
rpid
View File

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