HEX
Server: Apache
System: Linux www3.pit.tblive.com 5.14.0-687.38.1.el9_8.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 12 17:19:12 EDT 2026 x86_64
User: awaldron (1020)
PHP: 8.1.34
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/imunify360/venv/versions/imunify-core-8.12.0-2/defence360agent/subsys/systemd_notifier.py
"""Notify systemd about process state"""
import logging
import os
import socket

from defence360agent.contracts.config import ANTIVIRUS_MODE


logger = logging.getLogger(__name__)

_notify_socket_addr = None
_socket_detached = False


class AgentState(object):
    """Allowed agent state for notifying systemd."""

    READY = "READY=1"
    STARTING = "STATUS=Starting main process"
    MIGRATING = "STATUS=Applying database migrations"
    DAEMONIZED = "STATUS=Demonized"


def _take_notify_socket():
    # Capture $NOTIFY_SOCKET once and drop it from the environment, so child
    # processes (systemctl and other libsystemd-aware tools) do not inherit it
    # and emit sd_notify datagrams systemd cannot attribute to this unit.
    global _notify_socket_addr, _socket_detached
    if not _socket_detached:
        _notify_socket_addr = os.environ.pop("NOTIFY_SOCKET", None)
        _socket_detached = True
    return _notify_socket_addr


def notify(state):
    """
    Send notification to systemd, allowed formats described here
    https://www.freedesktop.org/software/systemd/man/sd_notify.html

    For example:

        notify("STATUS=Almost ready")

    """
    if ANTIVIRUS_MODE:
        return

    addr = _take_notify_socket()
    if not addr:
        return

    # systemd uses the abstract socket namespace when the path begins with '@'.
    connect_addr = "\0" + addr[1:] if addr.startswith("@") else addr
    try:
        with socket.socket(
            socket.AF_UNIX, socket.SOCK_DGRAM | socket.SOCK_CLOEXEC
        ) as sock:
            sock.connect(connect_addr)
            sock.sendall(state.encode())
    except OSError as e:
        logger.exception(
            "some problem has occurred during notifying of systemd: %s",
            e,
        )