blob: 38734408bb71edb09a84bceaac3e21c150adb1d9 [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
Andrew Jefferyae2460d2023-05-09 17:12:45 +093019#include <err.h>
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103020#include <errno.h>
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093021#include <limits.h>
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103022#include <stdio.h>
23#include <stdlib.h>
24#include <sys/socket.h>
25#include <sys/un.h>
Jeremy Kerr0cff6522016-03-18 09:57:01 +080026#include <sys/types.h>
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093027#include <unistd.h>
Jeremy Kerr0cff6522016-03-18 09:57:01 +080028
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103029#define CONSOLE_SOCKET_PREFIX "obmc-console"
Jeremy Kerr2bd05182016-03-10 16:59:43 +080030
Ninad Palsuleb14ca192023-03-31 09:49:00 -050031/* Build the socket path. */
32ssize_t console_socket_path(socket_path_t sun_path, const char *id)
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103033{
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103034 ssize_t rc;
35
Andrew Jefferyae2460d2023-05-09 17:12:45 +093036 if (!id) {
Ninad Palsule5ba20b52023-05-12 14:03:15 -050037 errno = EINVAL;
38 return -1;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103039 }
40
Andrew Jefferyae2460d2023-05-09 17:12:45 +093041 rc = snprintf(sun_path + 1, sizeof(socket_path_t) - 1,
42 CONSOLE_SOCKET_PREFIX ".%s", id);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093043 if (rc < 0) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103044 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093045 }
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103046
Ninad Palsuleb14ca192023-03-31 09:49:00 -050047 if ((size_t)rc > (sizeof(socket_path_t) - 1)) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103048 errno = 0;
49 return -1;
50 }
51
52 sun_path[0] = '\0';
53
54 return rc + 1 /* Capture NUL prefix */;
55}
56
57ssize_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 Jeffery5c359cc2023-04-18 22:50:07 +093061 size_t len;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103062
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093063 if (addrlen > SSIZE_MAX) {
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093064 return -EINVAL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093065 }
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093066
67 len = addrlen - sizeof(addr->sun_family) - 1;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103068 memcpy(path, src + sizeof(addr->sun_family) + 1, len);
69 path[len] = '\0';
70
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093071 return (ssize_t)len; /* strlen() style */
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103072}