set up for use by systemd
authorJacob Lifshay <programmerjake@gmail.com>
Wed, 20 Apr 2022 05:33:15 +0000 (22:33 -0700)
committerJacob Lifshay <programmerjake@gmail.com>
Wed, 20 Apr 2022 05:33:15 +0000 (22:33 -0700)
.gitignore
program-fpga.py
program-fpga.service [new file with mode: 0644]
program-fpga.socket [new file with mode: 0644]

index 4196ccb143d9147a0342ab19ffc2ef6fde5fad3f..a34da978334d33321e5c7c9e803b70dce9f9018a 100644 (file)
@@ -1 +1,2 @@
 .gitlab-runner-ccache
+.vscode
index 0334601a0388df9cc66a380e294eb542c44491ff..52f8caea0996568ff79c7feb83becebee1f3202d 100755 (executable)
@@ -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 (file)
index 0000000..ff140a6
--- /dev/null
@@ -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 (file)
index 0000000..ca1950b
--- /dev/null
@@ -0,0 +1,8 @@
+[Unit]
+Description=Socket for Programming the FPGA
+
+[Socket]
+ListenStream=%t/program-fpga.sock
+
+[Install]
+WantedBy=sockets.target