예제 #1
0
def test_uid_and_gid_without_permission():
    nobody = getpwnam('nobody')
    daemon = Daemon(worker=lambda: None, uid=nobody.pw_uid, gid=nobody.pw_gid)
    with pytest.raises(DaemonError) as exc_info:
        daemon.do_action('start')
    assert ('Unable to setuid or setgid '
            '([Errno 1] Operation not permitted') in str(exc_info.value)
예제 #2
0
def test_uid_and_gid_without_permission():
    nobody = getpwnam('nobody')
    daemon = Daemon(worker=lambda: None, uid=nobody.pw_uid, gid=nobody.pw_gid)
    with pytest.raises(DaemonError) as excinfo:
        daemon.do_action('start')
    assert ('Unable to setuid or setgid '
            '([Errno 1] Operation not permitted') in str(excinfo.value)
예제 #3
0
def daemonize(
    action: str,
    name: str,
    command: str | None = None,
    log_file: str | None = None,
):  # pragma: no cover
    """Executes a command as a daemon.

    Runs a COMMAND as a detached daemon assigning it a given NAME. The daemon can
    be stopped by calling daemonize stop NAME or its status checked with
    daemonize status NAME.

    If --log-file is used, a rotating log file with the STDOUT and STDERR of the
    worker subprocess will be generated. If STATUS is "debug", runs the worker
    subprocess without detaching.

    """

    def worker():

        if command is None:
            return

        if log_file:

            path = os.path.realpath(os.path.expanduser(os.path.expandvars(log_file)))
            if os.path.exists(path):
                date = datetime.now()
                suffix = date.strftime(".%Y-%m-%d_%H:%M:%S")
                move(path, path + suffix)

            os.makedirs(os.path.dirname(path), exist_ok=True)

            f = open(log_file, "w")
            sys.stdout = f
            sys.stderr = f

        subprocess.run(
            " ".join(command),
            shell=True,
            capture_output=False,
            cwd=os.getcwd(),
        )

    pid_file = f"/var/tmp/{name}.pid"

    if action == "debug":
        log_file = None
        worker()
    else:
        daemon = Daemon(
            worker=worker,
            pid_file=pid_file,
            work_dir=os.getcwd(),
        )
        daemon.do_action(action)
예제 #4
0
def start(ctx, debug):
    click.secho('PKNS Server Address: ', nl=False)
    click.secho(
        f"{ctx.obj['WORKER'].ip_address}:{ctx.obj['WORKER'].port}",
        fg='green'
    )
    daemon = Daemon('PKNS Server', worker=ctx.obj['WORKER'].serve_endless,
                    detach=(not debug), pidfile=os.path.abspath(
                        os.environ['HOME']+"/.pkns/PKNS.pid"),
                    work_dir=os.path.abspath(os.environ['HOME']),
                    stdout_file=os.path.abspath(
                        os.environ['HOME'] + "/.pkns/PKNS.log"),
                    stderr_file=os.path.abspath(
                        os.environ['HOME'] + "/.pkns/PKNS_error.log"),
                    uid=os.getuid(), gid=os.getgid())
    daemon.do_action('start')
예제 #5
0
def test_no_worker():
    daemon = Daemon()
    with pytest.raises(DaemonError):
        daemon.do_action("start")
예제 #6
0
def test_chrootdir_without_permission():
    daemon = Daemon(worker=lambda: None, chroot_dir=os.getcwd())
    with pytest.raises(DaemonError) as exc_info:
        daemon.do_action('start')
    assert ('Unable to change root directory '
            '([Errno 1] Operation not permitted') in str(exc_info.value)
예제 #7
0
def test_no_args_or_worker():
    daemon = Daemon()
    assert daemon.name == posixpath.basename(sys.argv[0])
    with pytest.raises(DaemonError):
        daemon.do_action('start')
예제 #8
0
def test_chrootdir_without_permission():
    daemon = Daemon(worker=lambda: None, chrootdir=os.getcwd())
    with pytest.raises(DaemonError) as excinfo:
        daemon.do_action('start')
    assert ('Unable to change root directory '
            '([Errno 1] Operation not permitted') in str(excinfo.value)
예제 #9
0
def test_no_worker():
    daemon = Daemon()
    with pytest.raises(DaemonError):
        daemon.do_action('start')