From: Jacob Lifshay Date: Wed, 20 Apr 2022 05:33:15 +0000 (-0700) Subject: set up for use by systemd X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=d6a59e33c7c197d0c61abe7f4ae5666c95535b89;p=utils.git set up for use by systemd --- diff --git a/.gitignore b/.gitignore index 4196ccb..a34da97 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .gitlab-runner-ccache +.vscode diff --git a/program-fpga.py b/program-fpga.py index 0334601..52f8cae 100755 --- a/program-fpga.py +++ b/program-fpga.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -import io import pty import socket import socketserver @@ -79,16 +78,25 @@ class RequestHandler(socketserver.BaseRequestHandler): send_chunk(self.request, str(status).encode()) -if sys.argv[1:] == ["--daemon"]: - try: - os.unlink(SOCKET_PATH) - except FileNotFoundError: - pass +if sys.argv[1] == "--daemon": + if len(sys.argv) > 2: + assert len(sys.argv) == 3, "invalid command line" + assert sys.argv[2].isdigit(), "invalid listening fd" + listen_socket = socket.socket(fileno=int(sys.argv[2])) + else: + listen_socket = None + try: + os.unlink(SOCKET_PATH) + except FileNotFoundError: + pass fd = os.open("/dev/null", os.O_RDONLY) os.dup2(fd, sys.stdin.fileno()) os.close(fd) - with socketserver.UnixStreamServer(SOCKET_PATH, RequestHandler) as server: - server.allow_reuse_address = True + with socketserver.UnixStreamServer( + SOCKET_PATH, RequestHandler, + bind_and_activate=listen_socket is None) as server: + if listen_socket is not None: + server.socket = listen_socket server.serve_forever() else: assert len(sys.argv) == 4, "invalid command line" diff --git a/program-fpga.service b/program-fpga.service new file mode 100644 index 0000000..ff140a6 --- /dev/null +++ b/program-fpga.service @@ -0,0 +1,13 @@ +[Unit] +Description=Service for Programming the FPGA +Requires=program-fpga.socket + +[Service] +Type=simple +ExecStart=bash -ic 'exec program-fpga.py --daemon 3' +WorkingDirectory=%S/program-fpga +StateDirectory=program-fpga + +[Install] +Also=program-fpga.socket +WantedBy=multi-user.target \ No newline at end of file diff --git a/program-fpga.socket b/program-fpga.socket new file mode 100644 index 0000000..ca1950b --- /dev/null +++ b/program-fpga.socket @@ -0,0 +1,8 @@ +[Unit] +Description=Socket for Programming the FPGA + +[Socket] +ListenStream=%t/program-fpga.sock + +[Install] +WantedBy=sockets.target