@@ -3055,6 +3055,87 @@ check_systemd_services() {
30553055 return 0
30563056}
30573057
3058+ # Check whether a systemd service/unit exists on the target.
3059+ systemd_service_exists () {
3060+ svc=" $1 "
3061+
3062+ if ! command -v systemctl > /dev/null 2>&1 ; then
3063+ return 1
3064+ fi
3065+
3066+ systemctl cat " $svc " > /dev/null 2>&1
3067+ }
3068+
3069+ # Check whether a systemd service/unit is currently active.
3070+ systemd_service_is_active () {
3071+ svc=" $1 "
3072+
3073+ if ! command -v systemctl > /dev/null 2>&1 ; then
3074+ return 1
3075+ fi
3076+
3077+ systemctl is-active --quiet " $svc "
3078+ }
3079+
3080+ # Start a systemd service/unit quietly.
3081+ systemd_service_start_safe () {
3082+ svc=" $1 "
3083+
3084+ if ! command -v systemctl > /dev/null 2>&1 ; then
3085+ return 1
3086+ fi
3087+
3088+ systemctl start " $svc " > /dev/null 2>&1
3089+ }
3090+
3091+ # Stop a systemd service/unit quietly.
3092+ systemd_service_stop_safe () {
3093+ svc=" $1 "
3094+
3095+ if ! command -v systemctl > /dev/null 2>&1 ; then
3096+ return 1
3097+ fi
3098+
3099+ systemctl stop " $svc " > /dev/null 2>&1
3100+ }
3101+
3102+ # Log status-only output for a systemd service/unit to the given logfile.
3103+ systemd_service_status_log () {
3104+ label=" $1 "
3105+ logfile=" $2 "
3106+ svc=" $3 "
3107+
3108+ if ! command -v systemctl > /dev/null 2>&1 ; then
3109+ return 1
3110+ fi
3111+
3112+ {
3113+ echo " ===== $label ====="
3114+ systemctl --no-pager --full --lines=0 status " $svc " 2>&1
3115+ echo " =============================="
3116+ } | tee -a " $logfile "
3117+ }
3118+
3119+ # Log stdout journal entries for a systemd service/unit since a given timestamp.
3120+ systemd_service_stdout_since () {
3121+ label=" $1 "
3122+ logfile=" $2 "
3123+ since_ts=" $3 "
3124+ svc=" $4 "
3125+
3126+ if ! command -v journalctl > /dev/null 2>&1 ; then
3127+ return 1
3128+ fi
3129+
3130+ {
3131+ echo " ===== $label ====="
3132+ journalctl --no-pager -q \
3133+ _SYSTEMD_UNIT=" $svc " \
3134+ _TRANSPORT=stdout \
3135+ --since " $since_ts " 2>&1
3136+ echo " =============================="
3137+ } | tee -a " $logfile "
3138+ }
30583139# Ensure udhcpc default.script exists, create if missing
30593140ensure_udhcpc_script () {
30603141 udhcpc_dir=" /usr/share/udhcpc"
@@ -4558,3 +4639,51 @@ get_pid() {
45584639 log_info " Process '$process_name ' not found."
45594640 return 1
45604641}
4642+
4643+ # Returns the size of the given file in bytes using stat, with fallbacks for portability across systems.
4644+ # Prints 0 and returns failure if the file does not exist or the size cannot be determined reliably.
4645+ file_size_bytes () {
4646+ file_path=" $1 "
4647+ size=" "
4648+
4649+ [ -f " $file_path " ] || {
4650+ printf ' %s\n' " 0"
4651+ return 1
4652+ }
4653+
4654+ # Prefer GNU stat first.
4655+ size=" $( stat -c %s " $file_path " 2> /dev/null || true) "
4656+ case " $size " in
4657+ ' ' |* [!0-9]* )
4658+ size=" "
4659+ ;;
4660+ esac
4661+
4662+ # Fall back to BSD stat only if GNU form did not work.
4663+ if [ -z " $size " ]; then
4664+ size=" $( stat -f %z " $file_path " 2> /dev/null || true) "
4665+ case " $size " in
4666+ ' ' |* [!0-9]* )
4667+ size=" "
4668+ ;;
4669+ esac
4670+ fi
4671+
4672+ # Final portable fallback.
4673+ if [ -z " $size " ]; then
4674+ size=" $( wc -c < " $file_path " 2> /dev/null | awk ' {print $1}' ) "
4675+ case " $size " in
4676+ ' ' |* [!0-9]* )
4677+ size=" "
4678+ ;;
4679+ esac
4680+ fi
4681+
4682+ if [ -z " $size " ]; then
4683+ printf ' %s\n' " 0"
4684+ return 1
4685+ fi
4686+
4687+ printf ' %s\n' " $size "
4688+ return 0
4689+ }
0 commit comments