MOON
Server: Apache
System: Linux server30c.hostingraja.org 3.10.0-962.3.2.lve1.5.63.el7.x86_64 #1 SMP Fri Oct 8 12:03:35 UTC 2021 x86_64
User: jibhires (1887)
PHP: 8.1.30
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open, allow_url_fopen, symlink, escapeshellcmd, pcntl_exec
Upload Files
File: //opt/imunify360/venv/lib64/python3.11/site-packages/im360/plugins/sensor/unix_socket.py
import os
import socket
from contextlib import suppress

from defence360agent.utils import run_with_umask


def create_unix_socket(
    path: str, dir_mode: int, sock_mode: int, uid: int = 0, gid: int = 0
):
    """Creates a unix socket at specified path. Returns bound socket object.

    Directories in path are automatically created with mode dir_mode.
    Socket file mode is set to sock_mode. Socket ownership is set to uid
    and gid (defaults to 0 - root)"""
    with run_with_umask(0):
        os.makedirs(os.path.dirname(path), mode=dir_mode, exist_ok=True)
    with suppress(FileNotFoundError):
        os.unlink(path)
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(path)
    if uid != 0 or gid != 0:
        os.chown(path, uid, gid)
    os.chmod(path, sock_mode)
    return sock