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 | |
| 19 | #include <errno.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 26 | #define CONSOLE_SOCKET_PREFIX "obmc-console" |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 27 | |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 28 | ssize_t console_socket_path(struct sockaddr_un *addr, const char *id) |
| 29 | { |
| 30 | char *sun_path; |
| 31 | ssize_t rc; |
| 32 | |
| 33 | sun_path = (char *)addr + sizeof(*addr) - sizeof(addr->sun_path); |
| 34 | |
| 35 | if (id) { |
| 36 | rc = snprintf(sun_path + 1, sizeof(addr->sun_path) - 1, |
| 37 | CONSOLE_SOCKET_PREFIX ".%s", id); |
| 38 | } else { |
| 39 | rc = snprintf(sun_path + 1, sizeof(addr->sun_path) - 1, |
| 40 | CONSOLE_SOCKET_PREFIX); |
| 41 | } |
| 42 | |
| 43 | if (rc < 0) |
| 44 | return rc; |
| 45 | |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame^] | 46 | if ((size_t)rc > (sizeof(addr->sun_path) - 1)) { |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 47 | errno = 0; |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | sun_path[0] = '\0'; |
| 52 | |
| 53 | return rc + 1 /* Capture NUL prefix */; |
| 54 | } |
| 55 | |
| 56 | ssize_t console_socket_path_readable(const struct sockaddr_un *addr, |
| 57 | size_t addrlen, socket_path_t path) |
| 58 | { |
| 59 | const char *src = (const char *)addr; |
| 60 | const size_t len = addrlen - sizeof(addr->sun_family) - 1; |
| 61 | |
| 62 | memcpy(path, src + sizeof(addr->sun_family) + 1, len); |
| 63 | path[len] = '\0'; |
| 64 | |
| 65 | return len; /* strlen() style */ |
| 66 | } |