2 # SPDX-License-Identifier: GPL-2.0
5 # This file is part of Lustre, http://www.lustre.org/
7 # lustre/tests/socketserver
9 # Domain socket server, used to test that domain sockets
12 # Rewrite in Python: Timothy Day <timday@amazon.com>
20 # Define a function for logging messages
22 print(f"{sys.argv[0]} {os.getpid()}: {msg} at {os.popen('date').read().strip()}")
25 # Get the socket path from the command-line argument,
26 # or ask the user to input one
28 socket_path = sys.argv[1]
30 socket_path = input("Enter socket path: ")
33 # Create a Unix domain socket with a specified file path
34 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
36 # Bind the socket to the Unix domain socket file
37 sock.bind(socket_path)
39 # Listen for incoming connections on the socket
40 sock.listen(socket.SOMAXCONN)
42 # Fork a child process to handle the connection
47 logmsg(f"server started on {socket_path}")
51 logmsg(f"fork failed: {pid}")
54 # Accept an incoming connection
55 client_sock, _ = sock.accept()
57 logmsg(f"connection on {socket_path}")
59 # Send a message to the client and close
61 client_sock.send(b"This is a message from the server!\n")
64 except socket.error as e:
65 logmsg(f"Socket error: {e}")