forked from krnlyng/sfdroid_renderer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsfdroid_funcs.cpp
122 lines (100 loc) · 4.01 KB
/
sfdroid_funcs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* this file is part of sfdroid
* Copyright (C) 2015, Franz-Josef Haider <[email protected]>
* based on harmattandroid by Thomas Perl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "sfdroid_defs.h"
#include <sys/socket.h>
#include <cstdlib>
#include <iostream>
using namespace std;
int recv_native_handle(int fd, native_handle_t **handle, struct buffer_info_t *info)
{
struct msghdr socket_message;
struct iovec io_vector[1];
struct cmsghdr *control_message = NULL;
unsigned int buffer_size = sizeof(struct buffer_info_t) + sizeof(native_handle_t) + sizeof(int)*(MAX_NUM_FDS + MAX_NUM_INTS);
unsigned int handle_size = sizeof(native_handle_t) + sizeof(int)*(MAX_NUM_FDS + MAX_NUM_INTS);
char message_buffer[buffer_size];
char ancillary_buffer[CMSG_SPACE(sizeof(int) * MAX_NUM_FDS)];
*handle = (native_handle_t*)malloc(handle_size);
if(!(*handle)) return -1;
memset(&socket_message, 0, sizeof(struct msghdr));
memset(ancillary_buffer, 0, CMSG_SPACE(sizeof(int) * MAX_NUM_FDS));
io_vector[0].iov_base = message_buffer;
io_vector[0].iov_len = buffer_size;
socket_message.msg_iov = io_vector;
socket_message.msg_iovlen = 1;
socket_message.msg_control = ancillary_buffer;
socket_message.msg_controllen = CMSG_SPACE(sizeof(int) * MAX_NUM_FDS);
control_message = CMSG_FIRSTHDR(&socket_message);
control_message->cmsg_len = socket_message.msg_controllen;
control_message->cmsg_level = SOL_SOCKET;
control_message->cmsg_type = SCM_RIGHTS;
if(recvmsg(fd, &socket_message, MSG_CMSG_CLOEXEC) < 0)
{
if(errno != ETIMEDOUT && errno != EAGAIN) cerr << "recvmsg failed: " << strerror(errno) << endl;
free(*handle);
*handle = NULL;
return -1;
}
memcpy(info, message_buffer, sizeof(struct buffer_info_t));
memcpy(*handle, message_buffer + sizeof(struct buffer_info_t), sizeof(native_handle_t));
if((*handle)->numFds > MAX_NUM_FDS)
{
cerr << "too less space reserved for fds: " << (*handle)->numFds << " > " << MAX_NUM_FDS << endl;
free(*handle);
*handle = NULL;
return -1;
}
if((*handle)->numInts > MAX_NUM_INTS)
{
cerr << "too less space reserved for ints: " << (*handle)->numInts << " > " << MAX_NUM_INTS << endl;
free(*handle);
*handle = NULL;
return -1;
}
*handle = (native_handle_t*)realloc(*handle, sizeof(native_handle_t) + sizeof(int)*((*handle)->numFds + (*handle)->numInts));
memcpy((char*)*handle + sizeof(native_handle_t), message_buffer + sizeof(struct buffer_info_t) + sizeof(native_handle_t), sizeof(int)*((*handle)->numFds + (*handle)->numInts));
if(socket_message.msg_flags & MSG_CTRUNC)
{
cerr << "not enough space in the ancillary buffer" << endl;
free(*handle);
*handle = NULL;
return -1;
}
for(int i=0;i<(*handle)->numFds;i++)
{
((native_handle_t*)(*handle))->data[i] = ((int*)CMSG_DATA(control_message))[i];
}
return 0;
}
int send_status(int fd, int failed)
{
char message_buffer[3];
if(failed) memcpy(message_buffer, "FA", sizeof(message_buffer));
else memcpy(message_buffer, "OK", sizeof(message_buffer));
return send(fd, message_buffer, sizeof(message_buffer), MSG_NOSIGNAL);
}
void free_handle(native_handle_t *handle)
{
for(int i=0;i<handle->numFds;i++)
{
close(handle->data[i]);
}
free((void*)handle);
}