|
| 1 | +/* |
| 2 | + * crun - OCI runtime written in C |
| 3 | + * |
| 4 | + * Copyright (C) 2021 Giuseppe Scrivano <[email protected]> |
| 5 | + * crun is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * crun is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with crun. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#define _GNU_SOURCE |
| 20 | + |
| 21 | +#include <config.h> |
| 22 | +#include <stdarg.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <sys/un.h> |
| 25 | +#include <sys/socket.h> |
| 26 | +#include <errno.h> |
| 27 | +#include <unistd.h> |
| 28 | +#include <fcntl.h> |
| 29 | +#include <sys/ioctl.h> |
| 30 | +#include <termios.h> |
| 31 | +#include <stdio.h> |
| 32 | + |
| 33 | +#define error(status, errno, fmt, ...) do { \ |
| 34 | + if (errno) \ |
| 35 | + fprintf (stderr, "crun: " fmt, ##__VA_ARGS__); \ |
| 36 | + else \ |
| 37 | + fprintf (stderr, "crun: %s:" fmt, strerror (errno), ##__VA_ARGS__); \ |
| 38 | + if (status) \ |
| 39 | + exit (status); \ |
| 40 | + } while(0) |
| 41 | + |
| 42 | +static int |
| 43 | +open_unix_domain_socket (const char *path) |
| 44 | +{ |
| 45 | + struct sockaddr_un addr = {}; |
| 46 | + int ret; |
| 47 | + int fd = socket (AF_UNIX, SOCK_STREAM, 0); |
| 48 | + if (fd < 0) |
| 49 | + error (EXIT_FAILURE, errno, "error creating UNIX socket"); |
| 50 | + |
| 51 | + strcpy (addr.sun_path, path); |
| 52 | + addr.sun_family = AF_UNIX; |
| 53 | + ret = bind (fd, (struct sockaddr *) &addr, sizeof (addr)); |
| 54 | + if (ret < 0) |
| 55 | + error (EXIT_FAILURE, errno, "error binding UNIX socket"); |
| 56 | + |
| 57 | + ret = listen (fd, 1); |
| 58 | + if (ret < 0) |
| 59 | + error (EXIT_FAILURE, errno, "listen"); |
| 60 | + |
| 61 | + return fd; |
| 62 | +} |
| 63 | + |
| 64 | +static void |
| 65 | +print_payload (int from) |
| 66 | +{ |
| 67 | + int fd = -1; |
| 68 | + struct iovec iov[1]; |
| 69 | + struct msghdr msg = {}; |
| 70 | + char ctrl_buf[2048] = {}; |
| 71 | + char data[2048]; |
| 72 | + int ret; |
| 73 | + struct cmsghdr *cmsg; |
| 74 | + |
| 75 | + iov[0].iov_base = data; |
| 76 | + iov[0].iov_len = sizeof (data) - 1; |
| 77 | + |
| 78 | + msg.msg_name = NULL; |
| 79 | + msg.msg_namelen = 0; |
| 80 | + msg.msg_iov = iov; |
| 81 | + msg.msg_iovlen = 1; |
| 82 | + msg.msg_controllen = CMSG_SPACE (sizeof (int)); |
| 83 | + msg.msg_control = ctrl_buf; |
| 84 | + |
| 85 | + do |
| 86 | + ret = recvmsg (from, &msg, 0); |
| 87 | + while (ret < 0 && errno == EINTR); |
| 88 | + if (ret < 0) |
| 89 | + { |
| 90 | + error (0, errno, "recvmsg"); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + data[iov[0].iov_len] = '\0'; |
| 95 | + puts (data); |
| 96 | + |
| 97 | + cmsg = CMSG_FIRSTHDR (&msg); |
| 98 | + if (cmsg == NULL) |
| 99 | + { |
| 100 | + error (0, 0, "no msg received"); |
| 101 | + return; |
| 102 | + } |
| 103 | + memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd)); |
| 104 | + close (fd); |
| 105 | +} |
| 106 | + |
| 107 | +int |
| 108 | +main (int argc, char **argv) |
| 109 | +{ |
| 110 | + char buf[8192]; |
| 111 | + int ret, fd, socket; |
| 112 | + if (argc < 2) |
| 113 | + error (EXIT_FAILURE, 0, "usage %s PATH\n", argv[0]); |
| 114 | + |
| 115 | + unlink (argv[1]); |
| 116 | + |
| 117 | + socket = open_unix_domain_socket (argv[1]); |
| 118 | + while (1) |
| 119 | + { |
| 120 | + struct termios tset; |
| 121 | + int conn; |
| 122 | + |
| 123 | + do |
| 124 | + conn = accept (socket, NULL, NULL); |
| 125 | + while (conn < 0 && errno == EINTR); |
| 126 | + if (conn < 0) |
| 127 | + error (EXIT_FAILURE, errno, "accept"); |
| 128 | + |
| 129 | + print_payload (conn); |
| 130 | + } |
| 131 | + |
| 132 | + return 0; |
| 133 | +} |
0 commit comments