From 2ee11a5eac27ac16f5a09469272184bc6126f86b Mon Sep 17 00:00:00 2001 From: Narvin Singh Date: Sun, 3 Jan 2021 01:10:16 -0500 Subject: [PATCH 1/3] 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. --- avds | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/avds b/avds index de66324..15c9281 100755 --- a/avds +++ b/avds @@ -5,7 +5,8 @@ USAGE: avds [] [] 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 From 1bf05d0972b043ef4e65c812bcbc4c11b2d6cbdb Mon Sep 17 00:00:00 2001 From: Narvin Singh Date: Mon, 25 Oct 2021 22:29:52 -0400 Subject: [PATCH 2/3] Feat: Return a PID in edge cases - Return the first ancestor PID if root_name is not found. - Return the process' own PID if its own name is root_name. --- rpid | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/rpid b/rpid index b1f2b71..c957f84 100755 --- a/rpid +++ b/rpid @@ -21,6 +21,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 @@ -51,13 +61,19 @@ get_info() { 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="${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:]]}" + 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}" From d19eaeb6e9ef855c6a7881eb58aef79ab5806c42 Mon Sep 17 00:00:00 2001 From: Narvin Singh Date: Mon, 25 Oct 2021 22:43:46 -0400 Subject: [PATCH 3/3] Refactor: DRY --- avdd | 6 ++++-- avds | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/avdd b/avdd index 92514f6..d8770ec 100755 --- a/avdd +++ b/avdd @@ -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}}" diff --git a/avds b/avds index 15c9281..a562b82 100755 --- a/avds +++ b/avds @@ -75,7 +75,7 @@ if [[ "${mod_list}" =~ ^\>\>\*\ ]]; then mod_list="${mod_list:4}" is_request_all=1 else - fifo=/tmp/avdd-fifo-"$("$(dirname "$0")"/rpid $$)" + fifo="/tmp/${DAEMON}-fifo-$("$(dirname "$0")/rpid" $$)" fi when="${2:-0}"