aboutsummaryrefslogtreecommitdiffstats
path: root/service.sh
blob: 8cb77c1579854ffff3ba0411b1e11b6ff9178197 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/system/bin/sh

# magisk-fstab - Systemless fstab tool for magisk
# Copyright (C) 2025 Leonard Kugis <leonard@kug.is>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Do NOT assume where your module will be located.
# ALWAYS use $MODDIR if you need to know where this script and module is placed.
# This will make sure your module will still work if Magisk changes its mount point in the future
MODDIR=${0%/*}

export LOG_FILE="${MODDIR}/magisk-fstab.log"
LOG_MAX_LINES=1000

BOOTWAIT_MAX_COUNT=20
BOOTWAIT_COUNT_INTERVAL=15

CONF_MAIN="${MODDIR}/config/main.conf"
CONF_FSTAB="${MODDIR}/config/fstab.conf"

# --- single-run lock (prevents concurrent starts) ---------------------------
LOCKDIR="${MODDIR}/.runlock"
PIDFILE="${LOCKDIR}/pid"

acquire_lock() {
  # Versuche Lock-Verzeichnis atomar anzulegen
  if mkdir "${LOCKDIR}" 2>/dev/null; then
    # Lock bekommen
    echo "$$" > "${PIDFILE}"
    # Bei Exit Lock säubern
    trap 'rc=$?; rm -f "${PIDFILE}"; rmdir "${LOCKDIR}" 2>/dev/null; exit $rc' INT TERM EXIT
    return 0
  fi

  # Lock existiert -> prüfen, ob stale
  if [ -f "${PIDFILE}" ]; then
    oldpid="$(cat "${PIDFILE}" 2>/dev/null)"
    if [ -n "${oldpid}" ] && [ -d "/proc/${oldpid}" ]; then
      # Anderer Prozess läuft noch -> sauber beenden
      echo "Another instance already running (pid ${oldpid}). Exiting." >>"${LOG_FILE}" 2>&1
      return 1
    fi
  fi

  # Stale Lock entfernen und erneut versuchen
  echo "Stale lock detected, cleaning up..." >>"${LOG_FILE}" 2>&1
  rm -f "${PIDFILE}" 2>/dev/null
  rmdir "${LOCKDIR}" 2>/dev/null

  if mkdir "${LOCKDIR}" 2>/dev/null; then
    echo "$$" > "${PIDFILE}"
    trap 'rc=$?; rm -f "${PIDFILE}"; rmdir "${LOCKDIR}" 2>/dev/null; exit $rc' INT TERM EXIT
    return 0
  else
    echo "Failed to acquire lock after cleanup. Exiting." >>"${LOG_FILE}" 2>&1
    return 1
  fi
}

# Lock holen; wenn nicht möglich, still beenden
acquire_lock || exit 0
# ---------------------------------------------------------------------------

# Check if configs are readable
if [ ! -r "$CONF_MAIN" ]; then
  echo "File '$CONF_MAIN' not readable." >>"${LOG_FILE}" 2>&1
  exit 2
fi

if [ ! -r "$CONF_FSTAB" ]; then
  echo "File '$CONF_FSTAB' not readable." >>"${LOG_FILE}" 2>&1
  exit 2
fi

# Read main config
. "${CONF_MAIN}" >>"${LOG_FILE}" 2>&1

# wait for system boot to complete
bootwait_count=0
while [ "$(getprop sys.boot_completed)" != "1" ] && [ ${bootwait_count} -lt ${BOOTWAIT_MAX_COUNT} ]; do
  sleep ${BOOTWAIT_COUNT_INTERVAL}
  bootwait_count=$((bootwait_count + 1))
done
if [ ${bootwait_count} -ge ${BOOTWAIT_MAX_COUNT} ]; then
  echo "Boot wait timeout" >>"${LOG_FILE}" 2>&1
  exit 1
fi

# prevent log file from growing too large
tail -n "${LOG_MAX_LINES}" "${LOG_FILE}" >"${LOG_FILE}.tmp" 2>/dev/null
mv "${LOG_FILE}.tmp" "${LOG_FILE}" 2>/dev/null

echo "=== $(date) ===" >>"${LOG_FILE}" 2>&1

if [ -f "${CONF_FSTAB}" ]; then
  /system/bin/sh "${MODDIR}/fstab.sh" --su-options "${su_options}" --log-file "${LOG_FILE}" "${CONF_FSTAB}" &
else
  echo "${CONF_FSTAB} not found." >>"${LOG_FILE}" 2>&1
fi

wait
# Lock wird durch trap am Ende automatisch freigegeben