TERMINAL EXPLOIT V2.1

[LOCATION]: /opt/nydus/bin/

Folder Link Grabber

PREFIX: SUFFIX:

Mass File Creator

FILENAME: CONTENT:

Quick Actions

FILE:
NEW_ITEM:
#!/usr/bin/bash
# Clean orphaned PyInstaller _MEI* directories from /opt/nydus/tmp
# Only removes directories older than 2 hours with no active processes
#
# This script is designed to be called:
# - On service startup (via ExecStartPre)
# - Periodically via systemd timer (every 6 hours)
#
# Safety checks:
# - Only removes directories older than MAX_AGE_MINUTES
# - Checks if any process has files open in the directory
# - Checks if any process has the path in LD_LIBRARY_PATH
# - Logs all cleanup actions to syslog

set -e

TMP_DIR="${NYDUS_TMP_DIR:-/opt/nydus/tmp}"
MAX_AGE_MINUTES="${NYDUS_MEI_MAX_AGE_MINUTES:-120}"  # 2 hours default

# Exit if tmp directory doesn't exist
if [ ! -d "$TMP_DIR" ]; then
    exit 0
fi

# Check if a _MEI directory is in use by any process
# Returns 0 if in use, 1 if not in use
is_mei_in_use() {
    local mei_dir="$1"
    local mei_name
    mei_name=$(basename "$mei_dir")

    # Method 1: Check if any process has files open in this directory via /proc/*/fd
    for pid_dir in /proc/[0-9]*; do
        pid=$(basename "$pid_dir")
        fd_dir="$pid_dir/fd"
        if [ -d "$fd_dir" ]; then
            # Check if any file descriptor points to a file in this _MEI dir
            for fd in "$fd_dir"/*; do
                if [ -L "$fd" ]; then
                    target=$(readlink "$fd" 2>/dev/null) || continue
                    if [[ "$target" == "$mei_dir"* ]]; then
                        return 0  # In use
                    fi
                fi
            done
        fi
        # Method 2: Check if process cwd is in this _MEI dir
        if [ -L "$pid_dir/cwd" ]; then
            cwd=$(readlink "$pid_dir/cwd" 2>/dev/null) || continue
            if [[ "$cwd" == "$mei_dir"* ]]; then
                return 0  # In use
            fi
        fi
        # Method 3: Check if LD_LIBRARY_PATH contains this _MEI dir
        if [ -r "$pid_dir/environ" ]; then
            if tr '\0' '\n' < "$pid_dir/environ" 2>/dev/null | grep -q "LD_LIBRARY_PATH=.*$mei_name"; then
                return 0  # In use
            fi
        fi
    done 2>/dev/null

    return 1  # Not in use
}

# Find and clean stale _MEI* directories
find "$TMP_DIR" -maxdepth 1 -type d -name '_MEI*' -mmin +"$MAX_AGE_MINUTES" 2>/dev/null | while read -r dir; do
    # Check if any process is using this directory
    if ! is_mei_in_use "$dir"; then
        if rm -rf "$dir" 2>/dev/null; then
            logger -t nydus-cleanup "Cleaned orphaned PyInstaller dir: $dir"
        fi
    else
        logger -t nydus-cleanup "Skipping in-use PyInstaller dir: $dir"
    fi
done

exit 0
[ CLOSE ]