-
Notifications
You must be signed in to change notification settings - Fork 63
Fix user data persistence across reboots for native Linux installs #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,11 +12,49 @@ | |||||||||||||||||||||||||||||||||||
| logger, buffer = get_logger("logger", use_buffer=True) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| def is_running_in_container(): | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| Detect if running inside a container (Docker, Podman, etc.). | ||||||||||||||||||||||||||||||||||||
| Returns True if running in a container, False otherwise. | ||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||
| # Check for /.dockerenv file (Docker-specific) | ||||||||||||||||||||||||||||||||||||
| if os.path.exists("/.dockerenv"): | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Check for container environment variables | ||||||||||||||||||||||||||||||||||||
| if os.environ.get("container") or os.environ.get("DOCKER_CONTAINER"): | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Check cgroup for container indicators | ||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| with open("/proc/1/cgroup", "r", encoding="utf-8") as f: | ||||||||||||||||||||||||||||||||||||
| cgroup_content = f.read() | ||||||||||||||||||||||||||||||||||||
| if "docker" in cgroup_content or "kubepods" in cgroup_content: | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
| # Also check for containerd/cri-o patterns | ||||||||||||||||||||||||||||||||||||
| if "/lxc/" in cgroup_content or "containerd" in cgroup_content: | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
| except (FileNotFoundError, PermissionError): | ||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Check for container runtime in /proc/1/environ | ||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| with open("/proc/1/environ", "rb") as f: | ||||||||||||||||||||||||||||||||||||
| environ_content = f.read().decode("utf-8", errors="ignore") | ||||||||||||||||||||||||||||||||||||
| if "container=" in environ_content: | ||||||||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+45
|
||||||||||||||||||||||||||||||||||||
| with open("/proc/1/environ", "rb") as f: | |
| environ_content = f.read().decode("utf-8", errors="ignore") | |
| if "container=" in environ_content: | |
| return True | |
| pattern = b"container=" | |
| tail_len = len(pattern) - 1 | |
| with open("/proc/1/environ", "rb") as f: | |
| leftover = b"" | |
| while True: | |
| chunk = f.read(4096) | |
| if not chunk: | |
| break | |
| data = leftover + chunk | |
| if pattern in data: | |
| return True | |
| if tail_len > 0: | |
| leftover = data[-tail_len:] |
Copilot
AI
Jan 3, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating the directory without explicit permission checks could lead to security issues if the directory is created with unexpected ownership or permissions. On native Linux, /var/lib/openplc-runtime should have restricted permissions (e.g., 0755) and appropriate ownership to prevent unauthorized access to sensitive data like .env files containing secrets.
| data_dir.mkdir(parents=True, exist_ok=True) | |
| if data_dir == Path("/var/lib/openplc-runtime"): | |
| # Explicitly set permissions for sensitive persistent data directory | |
| data_dir.mkdir(parents=True, mode=0o755, exist_ok=True) | |
| try: | |
| os.chmod(data_dir, 0o755) | |
| except PermissionError: | |
| logger.warning( | |
| "Could not set permissions on %s to 0755 due to insufficient privileges", | |
| data_dir, | |
| ) | |
| else: | |
| data_dir.mkdir(parents=True, exist_ok=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the entire cgroup file and performing multiple substring searches is inefficient. Consider checking for all patterns in a single pass or stopping at the first match to avoid unnecessary string operations.