blob: 792ca62fc67984d7c0e8f1165e5dd0524d355461 [file] [log] [blame]
Jeremy Kerr9326d772016-03-17 17:15:02 +08001/**
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 Kerr2bd05182016-03-10 16:59:43 +080016
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103017#include "console-server.h"
18
19#include <errno.h>
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093020#include <limits.h>
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103021#include <stdio.h>
22#include <stdlib.h>
23#include <sys/socket.h>
24#include <sys/un.h>
Jeremy Kerr0cff6522016-03-18 09:57:01 +080025#include <sys/types.h>
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093026#include <unistd.h>
Jeremy Kerr0cff6522016-03-18 09:57:01 +080027
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103028#define CONSOLE_SOCKET_PREFIX "obmc-console"
Jeremy Kerr2bd05182016-03-10 16:59:43 +080029
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103030ssize_t console_socket_path(struct sockaddr_un *addr, const char *id)
31{
32 char *sun_path;
33 ssize_t rc;
34
35 sun_path = (char *)addr + sizeof(*addr) - sizeof(addr->sun_path);
36
37 if (id) {
38 rc = snprintf(sun_path + 1, sizeof(addr->sun_path) - 1,
39 CONSOLE_SOCKET_PREFIX ".%s", id);
40 } else {
41 rc = snprintf(sun_path + 1, sizeof(addr->sun_path) - 1,
42 CONSOLE_SOCKET_PREFIX);
43 }
44
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093045 if (rc < 0) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103046 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093047 }
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103048
Andrew Jeffery8f548f62023-04-18 11:48:49 +093049 if ((size_t)rc > (sizeof(addr->sun_path) - 1)) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103050 errno = 0;
51 return -1;
52 }
53
54 sun_path[0] = '\0';
55
56 return rc + 1 /* Capture NUL prefix */;
57}
58
59ssize_t console_socket_path_readable(const struct sockaddr_un *addr,
60 size_t addrlen, socket_path_t path)
61{
62 const char *src = (const char *)addr;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093063 size_t len;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103064
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093065 if (addrlen > SSIZE_MAX) {
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093066 return -EINVAL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093067 }
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093068
69 len = addrlen - sizeof(addr->sun_family) - 1;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103070 memcpy(path, src + sizeof(addr->sun_family) + 1, len);
71 path[len] = '\0';
72
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093073 return (ssize_t)len; /* strlen() style */
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103074}