Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 1 | /* |
Matthew Maurer | e251fa3 | 2020-06-15 14:08:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 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 | */ |
| 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 Salyzyn | cfd5b08 | 2016-10-17 14:28:00 -0700 | [diff] [blame] | 25 | #include <sys/ioctl.h> |
| 26 | #include <unistd.h> |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 27 | |
Mark Salyzyn | 30f991f | 2017-01-10 13:19:54 -0800 | [diff] [blame] | 28 | #include <log/log.h> |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 29 | |
Matthew Maurer | e251fa3 | 2020-06-15 14:08:55 -0700 | [diff] [blame] | 30 | #include <trusty/ipc.h> |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 31 | |
Tri Vo | b47dbe7 | 2020-11-17 19:39:36 -0800 | [diff] [blame] | 32 | int tipc_connect(const char* dev_name, const char* srv_name) { |
| 33 | int fd; |
| 34 | int rc; |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 35 | |
Tri Vo | 27b0b3f | 2020-11-17 19:41:57 -0800 | [diff] [blame] | 36 | fd = TEMP_FAILURE_RETRY(open(dev_name, O_RDWR)); |
Tri Vo | b47dbe7 | 2020-11-17 19:39:36 -0800 | [diff] [blame] | 37 | if (fd < 0) { |
| 38 | rc = -errno; |
| 39 | ALOGE("%s: cannot open tipc device \"%s\": %s\n", __func__, dev_name, strerror(errno)); |
| 40 | return rc < 0 ? rc : -1; |
| 41 | } |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 42 | |
Tri Vo | 27b0b3f | 2020-11-17 19:41:57 -0800 | [diff] [blame] | 43 | rc = TEMP_FAILURE_RETRY(ioctl(fd, TIPC_IOC_CONNECT, srv_name)); |
Tri Vo | b47dbe7 | 2020-11-17 19:39:36 -0800 | [diff] [blame] | 44 | if (rc < 0) { |
| 45 | rc = -errno; |
| 46 | ALOGE("%s: can't connect to tipc service \"%s\" (err=%d)\n", __func__, srv_name, errno); |
| 47 | close(fd); |
| 48 | return rc < 0 ? rc : -1; |
| 49 | } |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 50 | |
Tri Vo | b47dbe7 | 2020-11-17 19:39:36 -0800 | [diff] [blame] | 51 | ALOGV("%s: connected to \"%s\" fd %d\n", __func__, srv_name, fd); |
| 52 | return fd; |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Matthew Maurer | e251fa3 | 2020-06-15 14:08:55 -0700 | [diff] [blame] | 55 | ssize_t tipc_send(int fd, const struct iovec* iov, int iovcnt, struct trusty_shm* shms, |
| 56 | int shmcnt) { |
| 57 | struct tipc_send_msg_req req; |
| 58 | req.iov = (__u64)iov; |
| 59 | req.iov_cnt = (__u64)iovcnt; |
| 60 | req.shm = (__u64)shms; |
| 61 | req.shm_cnt = (__u64)shmcnt; |
| 62 | |
Tri Vo | 27b0b3f | 2020-11-17 19:41:57 -0800 | [diff] [blame] | 63 | int rc = TEMP_FAILURE_RETRY(ioctl(fd, TIPC_IOC_SEND_MSG, &req)); |
Matthew Maurer | e251fa3 | 2020-06-15 14:08:55 -0700 | [diff] [blame] | 64 | if (rc < 0) { |
| 65 | ALOGE("%s: failed to send message (err=%d)\n", __func__, rc); |
| 66 | } |
| 67 | |
| 68 | return rc; |
| 69 | } |
| 70 | |
Tri Vo | b47dbe7 | 2020-11-17 19:39:36 -0800 | [diff] [blame] | 71 | void tipc_close(int fd) { |
| 72 | close(fd); |
Michael Ryleev | 0a72ad9 | 2015-07-22 18:21:10 -0700 | [diff] [blame] | 73 | } |