Skip to content

Commit 74e745c

Browse files
committed
check the path of /proc/1/exe if "init" can't be found
Signed-off-by: Zen <[email protected]>
1 parent 322bff4 commit 74e745c

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/ugrd/base/base.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "7.1.0"
2+
__version__ = "7.2.0"
33

44
from pathlib import Path
55
from shutil import which
@@ -36,12 +36,26 @@ def _process_loglevel(self, loglevel: int) -> None:
3636
@contains("autodetect_init", log_level=30)
3737
@unset("init_target", "init_target is already set, skipping autodetection.", log_level=30)
3838
def autodetect_init(self) -> None:
39-
"""Autodetects the init_target."""
39+
"""Autodetects the init_target.
40+
Attempts to find the "init" binary using which,
41+
if this fails, read /proc/1/exe to find the init binary.
42+
"""
4043
if init := which("init"):
4144
self.logger.info("Detected init at: %s", c_(init, "cyan", bright=True))
4245
self["init_target"] = init
43-
else:
44-
raise AutodetectError("init_target is not specified and could not be detected.")
46+
return
47+
48+
self.logger.info(f"No init found in PATH, checking {c_('/proc/1/exe', 'green')}")
49+
try:
50+
init = Path("/proc/1/exe").readlink()
51+
init = init.resolve() # Resolve the symlink to get the actual path
52+
self.logger.info("Detected from process 1: %s", c_(init, "cyan", bright=True))
53+
self["init_target"] = init
54+
return
55+
except PermissionError:
56+
self.logger.eror("Unable to read /proc/1/exe, permission denied.")
57+
58+
raise AutodetectError("init_target is not specified and could not be detected.")
4559

4660

4761
@unset("shebang", "shebang is already set.", log_level=10)

0 commit comments

Comments
 (0)