Support forking of service.

master
Marko Semet 2020-04-16 14:13:13 +02:00
parent d52c74edc4
commit 5debffbd58
5 changed files with 16 additions and 8 deletions

View File

@ -16,7 +16,9 @@ def main(args):
parser_remote_delete = sub_parser.add_parser("remote-delete", help="Delete remote.")
parser_system_server = sub_parser.add_parser("system-server", help="Run system service (root required).")
parser_system_server.add_argument("--fork", action="store_const", const=True, default=False, help="Makes a deamon through forking.")
parser_user_server = sub_parser.add_parser("user-server", help="Run user service.")
parser_user_server.add_argument("--fork", action="store_const", const=True, default=False, help="Makes a deamon through forking.")
# Arguments
result = parser.parse_args(args)
@ -28,9 +30,12 @@ def main(args):
raise RuntimeError("System service has to run as root.")
# Run deamon
asyncio.run(sys_service.rpc.run_deamon()) # TODO: Change default path
if result.fork:
if os.fork() != 0:
return # Stop as parent process
asyncio.run(sys_service.rpc.run_deamon(fork=result.fork)) # TODO: Change default path
elif result.action == "user-server":
asyncio.run(user_service.rpc.run_deamon()) # TODO: Change default path of user and system socket
asyncio.run(user_service.rpc.run_deamon(fork=result.fork)) # TODO: Change default path of user and system socket
# Client actions

View File

@ -15,5 +15,5 @@ async def callback_func(data, uid):
raise NotImplementedError()
async def run_deamon(path:str=defaults.DEFAULT_PATH):
await utils.run_access_socket(path, callback_func)
async def run_deamon(path:str=defaults.DEFAULT_PATH, fork:bool=False):
await utils.run_access_socket(path, callback_func, fork=fork)

View File

@ -39,7 +39,7 @@ def gen_callback_func(master:BackupManager):
return callback_func
async def run_deamon(user_path:str=None, sys_path:str=defaults.DEFAULT_PATH):
async def run_deamon(user_path:str=None, fork:bool=False, sys_path:str=defaults.DEFAULT_PATH):
# Find path
if user_path is None:
user_path = defaults.USER_PATH
@ -55,4 +55,4 @@ async def run_deamon(user_path:str=None, sys_path:str=defaults.DEFAULT_PATH):
timer_task = asyncio.create_task(config.Timer().run(gen_create_backup_func(backup_manager)))
# Start serving
await utils.run_access_socket(user_path, gen_callback_func(backup_manager))
await utils.run_access_socket(user_path, gen_callback_func(backup_manager), fork=fork)

View File

@ -21,7 +21,7 @@ async def get_user_home(name:str):
return proc_data[0].decode().split(":")[6]
async def run_access_socket(path:str, async_callback):
async def run_access_socket(path:str, async_callback, fork:bool):
async def run_func(read, write):
# Get user id
sock = write.get_extra_info("socket")
@ -33,6 +33,9 @@ async def run_access_socket(path:str, async_callback):
await async_callback(read, write, uid)
server = await asyncio.start_unix_server(run_func, path=path)
os.chmod(path, 0o666)
if fork:
if os.fork() != 0:
exit(0)
await server.serve_forever()

View File

@ -13,5 +13,5 @@ setup(name="home-backup",
author_email="marko@marko10-000.de",
#url="", TODO: Make a project site
packages=find_packages(),
install_requires=["systemd==0.16.1", "aiofile==1.5.2"]
install_requires=["aiofile==1.5.2"]
)