forked from shugaoye/platform_external_v86d
-
Notifications
You must be signed in to change notification settings - Fork 2
/
v86.c
158 lines (124 loc) · 2.7 KB
/
v86.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include "v86.h"
static int need_exit;
static __u32 seq;
static int netlink_send(int s, struct cn_msg *msg)
{
struct nlmsghdr *nlh;
unsigned int size;
int err;
char buf[CONNECTOR_MAX_MSG_SIZE];
struct cn_msg *m;
size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
nlh = (struct nlmsghdr *)buf;
nlh->nlmsg_seq = seq++;
nlh->nlmsg_pid = getpid();
nlh->nlmsg_type = NLMSG_DONE;
nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
nlh->nlmsg_flags = 0;
m = NLMSG_DATA(nlh);
memcpy(m, msg, sizeof(*m) + msg->len);
err = send(s, nlh, size, 0);
if (err == -1)
ulog(LOG_ERR, "Failed to send: %s [%d].\n", strerror(errno), errno);
return err;
}
int req_exec(int s, struct cn_msg *msg)
{
struct uvesafb_task *tsk = (struct uvesafb_task*)(msg + 1);
u8 *buf = (u8*)tsk + sizeof(struct uvesafb_task);
if (tsk->flags & TF_EXIT)
return 1;
if (v86_task(tsk, buf))
return 2;
netlink_send(s, msg);
return 0;
}
int main(int argc, char *argv[])
{
char buf[CONNECTOR_MAX_MSG_SIZE];
int len, i, err = 0, s;
struct nlmsghdr *reply;
struct sockaddr_nl l_local;
struct cn_msg *data;
struct pollfd pfd;
s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
if (s == -1) {
perror("socket");
return -1;
}
l_local.nl_family = AF_NETLINK;
l_local.nl_groups = 1 << (CN_IDX_V86D-1); /* bitmask of requested groups */
l_local.nl_pid = 0;
if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
perror("bind");
close(s);
return -1;
}
i = fork();
if (i) {
exit(0);
}
setsid();
chdir("/");
openlog("v86d", 0, LOG_KERN);
if (v86_init())
return -1;
memset(buf, 0, sizeof(buf));
pfd.fd = s;
while (!need_exit) {
pfd.events = POLLIN;
pfd.revents = 0;
switch (poll(&pfd, 1, -1)) {
case 0:
need_exit = 1;
continue;
case -1:
if (errno != EINTR) {
need_exit = 1;
break;
}
continue;
}
memset(buf, 0, sizeof(buf));
len = recv(s, buf, sizeof(buf), 0);
if (len == -1) {
perror("recv buf");
err = -1;
goto out;
}
reply = (struct nlmsghdr *)buf;
/* Ignore requests coming from outside the kernel. */
if (reply->nlmsg_pid != 0) {
continue;
}
switch (reply->nlmsg_type) {
case NLMSG_ERROR:
ulog(LOG_ERR, "Error message received.\n");
break;
case NLMSG_DONE:
data = (struct cn_msg *)NLMSG_DATA(reply);
if (req_exec(s, data))
goto out;
break;
default:
break;
}
}
out:
v86_cleanup();
closelog();
close(s);
return err;
}