-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f9394f
commit f1857d3
Showing
30 changed files
with
1,054 additions
and
562 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
tags: | ||
- zeusd-v* | ||
|
||
jobs: | ||
cargo-publish: | ||
if: github.repository_owner == 'ml-energy' | ||
runs-on: ubuntu-latest | ||
env: | ||
CARGO_TERM_COLOR: always | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
with: | ||
sparse-checkout: zeusd | ||
- name: Publish to crates.io | ||
uses: katyo/publish-crates@v2 | ||
with: | ||
path: zeusd | ||
registry-token: ${{ secrets.CRATES_IO_TOKEN }} | ||
check-repo: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Check format, lint, and test for Zeusd | ||
name: (Zeusd) Check format, lint, and test | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""Common utilities for device management.""" | ||
|
||
from __future__ import annotations | ||
|
||
import os | ||
import ctypes | ||
from functools import lru_cache | ||
|
||
from zeus.utils.logging import get_logger | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
@lru_cache(maxsize=1) | ||
def has_sys_admin() -> bool: | ||
"""Check if the current process has `SYS_ADMIN` capabilities.""" | ||
# First try to read procfs. | ||
try: | ||
with open("/proc/self/status") as f: | ||
for line in f: | ||
if line.startswith("CapEff"): | ||
bitmask = int(line.strip().split()[1], 16) | ||
has = bool(bitmask & (1 << 21)) | ||
logger.info( | ||
"Read security capabilities from /proc/self/status -- SYS_ADMIN: %s", | ||
has, | ||
) | ||
return has | ||
except Exception: | ||
logger.info("Failed to read capabilities from /proc/self/status", exc_info=True) | ||
|
||
# If that fails, try to use the capget syscall. | ||
class CapHeader(ctypes.Structure): | ||
_fields_ = [("version", ctypes.c_uint32), ("pid", ctypes.c_int)] | ||
|
||
class CapData(ctypes.Structure): | ||
_fields_ = [ | ||
("effective", ctypes.c_uint32), | ||
("permitted", ctypes.c_uint32), | ||
("inheritable", ctypes.c_uint32), | ||
] | ||
|
||
# Attempt to load libc and set up capget | ||
try: | ||
libc = ctypes.CDLL("libc.so.6") | ||
capget = libc.capget | ||
capget.argtypes = [ctypes.POINTER(CapHeader), ctypes.POINTER(CapData)] | ||
capget.restype = ctypes.c_int | ||
except Exception: | ||
logger.info("Failed to load libc.so.6", exc_info=True) | ||
return False | ||
|
||
# Initialize the header and data structures | ||
header = CapHeader(version=0x20080522, pid=0) # Use the current process | ||
data = CapData() | ||
|
||
# Call capget and check for errors | ||
if capget(ctypes.byref(header), ctypes.byref(data)) != 0: | ||
errno = ctypes.get_errno() | ||
logger.info( | ||
"capget failed with error: %s (errno %s)", os.strerror(errno), errno | ||
) | ||
return False | ||
|
||
bitmask = data.effective | ||
has = bool(bitmask & (1 << 21)) | ||
logger.info("Read security capabilities from capget -- SYS_ADMIN: %s", has) | ||
return has |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.