Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_setgid_invalid_group(_):
cfile = create_config(("group=testgroup",))
with pytest.raises(SystemExit) as exc:
Config(cfile).load()
setgid()
assert exc.value.code == 64
def test_setgid():
cfile = create_config(("group=abc",))
with mock.patch("grp.getgrnam") as mock_getgrnam, mock.patch(
"os.setgid"
) as mock_setgid:
Config(cfile).load()
setgid()
assert mock_getgrnam.called is True
assert mock_setgid.called is True
def test_setgid_same_group():
cfile = create_config(("",))
with mock.patch("os.setgid"):
Config(cfile).load()
assert setgid() is None
def test_setgid_no_perms():
cfile = create_config(("group=testgroup",))
with mock.patch(
"grp.getgrnam", side_effect=PermissionError
), pytest.raises(SystemExit) as exc:
Config(cfile).load()
setgid()
assert exc.value.code == 77
error occurs or :py:obj:`os.EX_OK` when the program
exits cleanly.
"""
args = parse_cmd_args(sys.argv[1:])
configure_logs(args)
logger = logging.getLogger("blackhole")
if args.test:
config_test(args)
try:
config = Config(args.config_file).load().test()
config.args = args
warn_options(config)
daemon = Daemon(config.pidfile)
supervisor = Supervisor()
pid_permissions()
setgid()
setuid()
except (ConfigException, DaemonException) as err:
logger.critical(err)
raise SystemExit(os.EX_USAGE)
except BlackholeRuntimeException as err:
logger.critical(err)
raise SystemExit(os.EX_NOPERM)
if args.background:
try:
daemon.daemonize()
except DaemonException as err:
supervisor.close_socks()
logger.critical(err)
raise SystemExit(os.EX_NOPERM)
try:
supervisor.run()
def setup_child(self):
"""Basic setup for the child process and starting it."""
setgid()
setuid()
asyncio.set_event_loop(None)
if setproctitle:
setproctitle.setproctitle("blackhole: worker")
process = Child(self.up_read, self.down_write, self.socks, self.idx)
process.start()