108 lines
3.0 KiB
C++
108 lines
3.0 KiB
C++
/*
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include <atomic>
|
|
#include <errno.h>
|
|
#include <log/log.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <fcntl.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
//#include <linux/vm_sockets.h>
|
|
#include <qemu_pipe_bp.h>
|
|
|
|
#include <sys/cdefs.h>
|
|
#include <sys/mman.h>
|
|
#include <pthread.h> /* for pthread_once() */
|
|
#include <stdlib.h>
|
|
#include <sys/un.h>
|
|
|
|
#ifndef D
|
|
# define D(...) do{}while(0)
|
|
#endif
|
|
extern "C" {
|
|
|
|
int qemu_pipe_open_ns(const char* ns, const char* pipeName, int flags) {
|
|
ALOGD("%s:%d coming....device:%s",__FUNCTION__ ,__LINE__,pipeName);
|
|
if (pipeName == NULL || pipeName[0] == '\0') {
|
|
errno = EINVAL;
|
|
return -1;
|
|
}
|
|
int fd = -1, ret;
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
struct sockaddr_un addr;
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
const char sun_path[] = "abstractSocketVmhalbox";
|
|
int sun_path_size = offsetof(struct sockaddr_un, sun_path) + sizeof(sun_path);
|
|
strncpy(addr.sun_path + 1, sun_path, strlen(sun_path));
|
|
|
|
if (connect(fd, (struct sockaddr*) &addr, sun_path_size) < 0) {
|
|
close(fd);
|
|
fd = -1;
|
|
}
|
|
|
|
if (fd < 0) {
|
|
D("%s: Could not open %s size %d error: %s", __FUNCTION__, sun_path, sun_path_size, strerror(errno));
|
|
//errno = ENOSYS;
|
|
return -1;
|
|
}
|
|
|
|
char buf[256];
|
|
int bufLen;
|
|
if (ns) {
|
|
bufLen = snprintf(buf, sizeof(buf), "pipe:%s:%s", ns, pipeName);
|
|
} else {
|
|
bufLen = snprintf(buf, sizeof(buf), "pipe:%s", pipeName);
|
|
}
|
|
|
|
if (!qemu_pipe_write_fully(fd, buf, bufLen + 1)) {
|
|
ALOGE("%s:%d: Could not connect to the '%s' service: %s",
|
|
__func__, __LINE__, buf, strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
ret = TEMP_FAILURE_RETRY(read(fd, buf, 2)); // 读取返回值
|
|
if (ret != 2 || buf[0] != 'O' || buf[1] != 'K') {
|
|
D("%s: Could not connect to %s pipe service: %s", __FUNCTION__, pipeName, strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return fd;
|
|
}
|
|
|
|
int qemu_pipe_open(const char* pipeName) {
|
|
return qemu_pipe_open_ns(NULL, pipeName, O_RDWR | O_NONBLOCK);
|
|
}
|
|
|
|
void qemu_pipe_close(int pipe) {
|
|
close(pipe);
|
|
}
|
|
|
|
int qemu_pipe_read(int pipe, void* buffer, int size) {
|
|
return read(pipe, buffer, size);
|
|
}
|
|
|
|
int qemu_pipe_write(int pipe, const void* buffer, int size) {
|
|
return write(pipe, buffer, size);
|
|
}
|
|
|
|
} // extern "C"
|