Make internal functions readonly
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
		
							
								
								
									
										89
									
								
								ssm
									
									
									
									
									
								
							
							
						
						
									
										89
									
								
								ssm
									
									
									
									
									
								
							| @@ -11,7 +11,7 @@ default() { | ||||
| 			_p+=( "$v" ) | ||||
| 		done | ||||
| 	} | ||||
| } | ||||
| }; readonly -f default | ||||
|  | ||||
| ## Die. Why not? | ||||
| die() { | ||||
| @@ -19,7 +19,7 @@ die() { | ||||
|  | ||||
| 	[[ "$2" ]] && printf '%s\n' "$2" | ||||
| 	exit "$code" | ||||
| } | ||||
| }; readonly -f die | ||||
|  | ||||
| ## Run the command and wait for it to die | ||||
| svc() { | ||||
| @@ -39,7 +39,7 @@ svc() { | ||||
| 	wait "$job_pid" | ||||
|  | ||||
| 	svc::cleanup | ||||
| } | ||||
| }; readonly -f svc | ||||
|  | ||||
| ## Respawn | ||||
| respawn() { | ||||
| @@ -76,12 +76,11 @@ respawn() { | ||||
| 			wait "$job_pid" | ||||
| 		done | ||||
| 	done | ||||
| } | ||||
| }; readonly -f respawn | ||||
|  | ||||
| ## Run a command with its output discarded | ||||
| nullexec() { | ||||
| 	"$@" &>/dev/null | ||||
| } | ||||
| nullexec() { "$@" &>/dev/null; } | ||||
| readonly -f nullexec | ||||
|  | ||||
| ## Wait for a pid to die | ||||
| pid_wait() { | ||||
| @@ -94,7 +93,7 @@ pid_wait() { | ||||
| 	done | ||||
|  | ||||
| 	return 0 | ||||
| } | ||||
| }; readonly -f pid_wait | ||||
|  | ||||
| ## See if NAME is a function | ||||
| is_function() { | ||||
| @@ -107,7 +106,7 @@ is_function() { | ||||
| 	fi | ||||
|  | ||||
| 	return 1 | ||||
| } | ||||
| }; readonly -f is_function | ||||
|  | ||||
| ## Simple timer | ||||
| timer() { | ||||
| @@ -121,15 +120,15 @@ timer() { | ||||
| 	done | ||||
|  | ||||
| 	return 0 | ||||
| } | ||||
| }; readonly -f timer | ||||
|  | ||||
| ## Is a service ready? | ||||
| is_ready() [[ -f "$service_ready_flag" ]] | ||||
| readonly -f is_ready | ||||
|  | ||||
| ## Wait for this service to get ready | ||||
| wait_ready() { | ||||
| 	timer "$service_ready_timeout" is_ready | ||||
| } | ||||
| wait_ready() { timer "$service_ready_timeout" is_ready; } | ||||
| readonly -f wait_ready | ||||
|  | ||||
| ## Depend on other services to be started | ||||
| depend() { | ||||
| @@ -143,7 +142,7 @@ depend() { | ||||
| 			} | ||||
| 		fi | ||||
| 	done | ||||
| } | ||||
| }; readonly -f depend | ||||
|  | ||||
| ## Create tmpfiles | ||||
| mktmpfiles() { | ||||
| @@ -169,7 +168,7 @@ mktmpfiles() { | ||||
| 			esac | ||||
| 		fi | ||||
| 	done | ||||
| } | ||||
| }; readonly -f mktmpfiles | ||||
|  | ||||
| ## Depend on other services to be ready | ||||
| depend_ready() { | ||||
| @@ -183,11 +182,27 @@ depend_ready() { | ||||
| 			return 1 | ||||
| 		} | ||||
| 	done | ||||
| } | ||||
| }; readonly -f depend_ready | ||||
|  | ||||
| # Super functions | ||||
| result() { | ||||
| 	declare rc=$1; shift | ||||
| 	declare -A msgs | ||||
|  | ||||
| 	while (( $# )); do | ||||
| 		[[ "$2" ]] || return 1 | ||||
|  | ||||
| 		msgs["$1"]="$2" | ||||
| 		shift 2 | ||||
| 	done | ||||
|  | ||||
| 	[[ "${msgs[$rc]}" ]] || msgs["$rc"]="Failed!" | ||||
|  | ||||
| 	printf '%s\n' "${msgs[$rc]}" | ||||
| }; readonly -f result | ||||
|  | ||||
| # Overloadable functions | ||||
| ## Start the service, write down the svc pid | ||||
| super_start() { | ||||
| start() { | ||||
| 	(( service_running )) && return 3 | ||||
|  | ||||
| 	rm -f "$service_stopped_flag" | ||||
| @@ -222,14 +237,9 @@ super_start() { | ||||
| 	return 0 | ||||
| } | ||||
|  | ||||
| # A separate function for oneshot services | ||||
| super_oneshot() { | ||||
| 	(( service_enabled )) && return 3 | ||||
| } | ||||
|  | ||||
| ## Reload the service | ||||
| ## Usually just sends HUP | ||||
| super_reload() { | ||||
| reload() { | ||||
| 	(( service_running )) || return 3 | ||||
|  | ||||
| 	if (( service_managed )); then | ||||
| @@ -242,7 +252,7 @@ super_reload() { | ||||
| ## Stop the service | ||||
| ## Returns: | ||||
| ##   3: Service is not running. | ||||
| super_stop() { | ||||
| stop() { | ||||
| 	if (( service_oneshot )); then | ||||
| 		(( service_enabled )) || return 3 | ||||
|  | ||||
| @@ -293,26 +303,7 @@ info() { | ||||
| 	printf "%12s: %s\n" "${_info_items[@]}" | ||||
| } | ||||
|  | ||||
| result() { | ||||
| 	declare rc=$1; shift | ||||
| 	declare -A msgs | ||||
|  | ||||
| 	while (( $# )); do | ||||
| 		[[ "$2" ]] || return 1 | ||||
|  | ||||
| 		msgs["$1"]="$2" | ||||
| 		shift 2 | ||||
| 	done | ||||
|  | ||||
| 	[[ "${msgs[$rc]}" ]] || msgs["$rc"]="Failed!" | ||||
|  | ||||
| 	printf '%s\n' "${msgs[$rc]}" | ||||
| } | ||||
|  | ||||
| # Overloadable functions | ||||
| start() { super_start; } | ||||
| stop() { super_stop; } | ||||
| reload() { super_reload; } | ||||
| # Restart just calls the script twice by default | ||||
| restart() { | ||||
| 	"$_self" "$service_name" stop | ||||
| 	"$_self" "$service_name" start | ||||
| @@ -335,7 +326,7 @@ qstatus() { nullexec status; } | ||||
| ## By default there is no ready check | ||||
| ready() { :; } | ||||
|  | ||||
| # Code | ||||
| # Main code | ||||
| main() { | ||||
| 	# Figure out our full path | ||||
| 	case "$0" in | ||||
| @@ -387,7 +378,7 @@ main() { | ||||
| 	# Common config path | ||||
| 	cfg_path+=( '/etc/ssm' ) | ||||
|  | ||||
| 	# Load custom functions | ||||
| 	# Load custom functions, reversing the PATH order | ||||
| 	for (( idx=${#cfg_path[@]}-1; idx>=0; idx-- )); do | ||||
| 		cfg_dir="${cfg_path[idx]}" | ||||
|  | ||||
| @@ -432,7 +423,7 @@ main() { | ||||
| 	done | ||||
|  | ||||
| 	# Get the service config | ||||
| 	source -- "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config" | ||||
| 	source "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config" | ||||
| 	 | ||||
| 	# Legacy | ||||
| 	[[ "$service_args" ]] && service_command=( "${service_command[@]}" "${service_args[@]}" ) | ||||
| @@ -547,6 +538,6 @@ main() { | ||||
| 			die 15 | ||||
| 		} | ||||
| 	fi | ||||
| } | ||||
| }; readonly -f main | ||||
|  | ||||
| main "$@" | ||||
|   | ||||
		Reference in New Issue
	
	Block a user