blob: ad4d8cd546e14dc6262602c5da81ff9514338712 [file] [log] [blame]
Michael Ryleev0a72ad92015-07-22 18:21:10 -07001/*
Matthew Maurere251fa32020-06-15 14:08:55 -07002 * Copyright (C) 2020 The Android Open Source Project
Michael Ryleev0a72ad92015-07-22 18:21:10 -07003 *
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 */
16
17#define LOG_TAG "libtrusty"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <stdbool.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070025#include <sys/ioctl.h>
26#include <unistd.h>
Michael Ryleev0a72ad92015-07-22 18:21:10 -070027
Mark Salyzyn30f991f2017-01-10 13:19:54 -080028#include <log/log.h>
Michael Ryleev0a72ad92015-07-22 18:21:10 -070029
Matthew Maurere251fa32020-06-15 14:08:55 -070030#include <trusty/ipc.h>
Michael Ryleev0a72ad92015-07-22 18:21:10 -070031
32int tipc_connect(const char *dev_name, const char *srv_name)
33{
34 int fd;
35 int rc;
36
37 fd = open(dev_name, O_RDWR);
38 if (fd < 0) {
39 rc = -errno;
40 ALOGE("%s: cannot open tipc device \"%s\": %s\n",
41 __func__, dev_name, strerror(errno));
42 return rc < 0 ? rc : -1;
43 }
44
45 rc = ioctl(fd, TIPC_IOC_CONNECT, srv_name);
46 if (rc < 0) {
47 rc = -errno;
48 ALOGE("%s: can't connect to tipc service \"%s\" (err=%d)\n",
49 __func__, srv_name, errno);
50 close(fd);
51 return rc < 0 ? rc : -1;
52 }
53
54 ALOGV("%s: connected to \"%s\" fd %d\n", __func__, srv_name, fd);
55 return fd;
56}
57
Matthew Maurere251fa32020-06-15 14:08:55 -070058ssize_t tipc_send(int fd, const struct iovec* iov, int iovcnt, struct trusty_shm* shms,
59 int shmcnt) {
60 struct tipc_send_msg_req req;
61 req.iov = (__u64)iov;
62 req.iov_cnt = (__u64)iovcnt;
63 req.shm = (__u64)shms;
64 req.shm_cnt = (__u64)shmcnt;
65
66 int rc = ioctl(fd, TIPC_IOC_SEND_MSG, &req);
67 if (rc < 0) {
68 ALOGE("%s: failed to send message (err=%d)\n", __func__, rc);
69 }
70
71 return rc;
72}
73
Michael Ryleev0a72ad92015-07-22 18:21:10 -070074void tipc_close(int fd)
75{
76 close(fd);
77}