Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 16 | |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 17 | #include "console-server.h" |
| 18 | |
Andrew Jeffery | ae2460d | 2023-05-09 17:12:45 +0930 | [diff] [blame] | 19 | #include <err.h> |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 20 | #include <errno.h> |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 21 | #include <limits.h> |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/un.h> |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 26 | #include <sys/types.h> |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 27 | #include <unistd.h> |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 28 | |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 29 | #define CONSOLE_SOCKET_PREFIX "obmc-console" |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 30 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 31 | /* Build the socket path. */ |
| 32 | ssize_t console_socket_path(socket_path_t sun_path, const char *id) |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 33 | { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 34 | ssize_t rc; |
| 35 | |
Andrew Jeffery | ae2460d | 2023-05-09 17:12:45 +0930 | [diff] [blame] | 36 | if (!id) { |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 37 | errno = EINVAL; |
| 38 | return -1; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 39 | } |
| 40 | |
Andrew Jeffery | ae2460d | 2023-05-09 17:12:45 +0930 | [diff] [blame] | 41 | rc = snprintf(sun_path + 1, sizeof(socket_path_t) - 1, |
| 42 | CONSOLE_SOCKET_PREFIX ".%s", id); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 43 | if (rc < 0) { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 44 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 45 | } |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 46 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 47 | if ((size_t)rc > (sizeof(socket_path_t) - 1)) { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 48 | errno = 0; |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | sun_path[0] = '\0'; |
| 53 | |
| 54 | return rc + 1 /* Capture NUL prefix */; |
| 55 | } |
| 56 | |
| 57 | ssize_t console_socket_path_readable(const struct sockaddr_un *addr, |
| 58 | size_t addrlen, socket_path_t path) |
| 59 | { |
| 60 | const char *src = (const char *)addr; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 61 | size_t len; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 62 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 63 | if (addrlen > SSIZE_MAX) { |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 64 | return -EINVAL; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 65 | } |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 66 | |
| 67 | len = addrlen - sizeof(addr->sun_family) - 1; |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 68 | memcpy(path, src + sizeof(addr->sun_family) + 1, len); |
| 69 | path[len] = '\0'; |
| 70 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 71 | return (ssize_t)len; /* strlen() style */ |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 72 | } |