| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include <errno.h> |
| 4 | #include <mos/syscall/usermode.h> |
| 5 | #include <pthread.h> |
| 6 | #include <spawn.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | static fd_t server_fd; |
| 13 | static const char data[44] = "The quick brown fox jumps over the lazy dog" ; |
| 14 | |
| 15 | static void *thread_main(void *arg) |
| 16 | { |
| 17 | const fd_t client_fd = (fd_t) (long) arg; |
| 18 | |
| 19 | char buf[150]; |
| 20 | const size_t bufsize = sprintf(buffer: buf, format: "welcome to the server, client fd %d. '%s'!" , client_fd, data); |
| 21 | |
| 22 | write(fd: client_fd, buffer: &bufsize, size: sizeof(bufsize)); |
| 23 | const size_t written = write(fd: client_fd, buffer: buf, size: bufsize); |
| 24 | if (written != bufsize) |
| 25 | puts(string: "server: failed to write to ipc channel" ); |
| 26 | |
| 27 | size_t readbufsize = 0; |
| 28 | const size_t readn = read(fd: client_fd, buffer: &readbufsize, size: sizeof(readbufsize)); |
| 29 | |
| 30 | if (readn != sizeof(readbufsize)) |
| 31 | { |
| 32 | puts(string: "server: failed to read from ipc channel" ); |
| 33 | return NULL; |
| 34 | } |
| 35 | |
| 36 | char *readbuf = malloc(size: readbufsize); |
| 37 | const size_t read_size = read(fd: client_fd, buffer: readbuf, size: readbufsize); |
| 38 | if (read_size != readbufsize) |
| 39 | { |
| 40 | puts(string: "server: failed to read from ipc channel" ); |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | printf(format: "server: received '%.*s' from client %d\n" , (int) read_size, readbuf, client_fd); |
| 45 | |
| 46 | close(fd: client_fd); |
| 47 | close(fd: server_fd); |
| 48 | |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | int main(int argc, char **argv) |
| 53 | { |
| 54 | if (argc != 2) |
| 55 | { |
| 56 | printf(format: "usage: %s <ping-pong-ipc-channel>\n" , argv[0]); |
| 57 | puts(string: "starts a server that accepts connections on the given ipc name." ); |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | const char *ipc_name = argv[1]; |
| 62 | printf(format: "server: ipc-name='%s'\n" , ipc_name); |
| 63 | |
| 64 | const char *pong_path = "/initrd/tests/ipc-pong" ; |
| 65 | const char *const pond_argv[] = { pong_path, ipc_name, NULL }; |
| 66 | |
| 67 | pid_t p; |
| 68 | posix_spawn(pid: &p, path: pong_path, NULL, NULL, argv: (char *const *) pond_argv, envp: environ); |
| 69 | |
| 70 | server_fd = syscall_ipc_create(name: ipc_name, max_pending_connections: 32); |
| 71 | if (server_fd < 0) |
| 72 | { |
| 73 | puts(string: "failed to open ipc channel" ); |
| 74 | return -1; |
| 75 | } |
| 76 | |
| 77 | while (true) |
| 78 | { |
| 79 | const fd_t client_fd = syscall_ipc_accept(fd: server_fd); |
| 80 | if (client_fd == -ECONNABORTED) |
| 81 | { |
| 82 | puts(string: "Server: server closed" ); |
| 83 | return 0; |
| 84 | } |
| 85 | else if (client_fd < 0) |
| 86 | { |
| 87 | puts(string: "failed to accept ipc channel" ); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | printf(format: "server: accepted fd %d\n" , client_fd); |
| 92 | |
| 93 | pthread_t thread = { 0 }; |
| 94 | pthread_create(thrd: &thread, NULL, fn: thread_main, arg: (void *) (long) client_fd); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |