v86d: add support for ESBX buffers
[v86d.git] / main.c
bloba7300a2d42a0c17067e78854f07155c58f5705c3
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <time.h>
7 #include <fcntl.h>
9 #include <sys/socket.h>
10 #include <sys/poll.h>
12 #include <linux/netlink.h>
13 #include <linux/rtnetlink.h>
15 #include <arpa/inet.h>
17 #include "v86.h"
19 static int need_exit;
20 static __u32 seq;
22 static int netlink_send(int s, struct cn_msg *msg)
24 struct nlmsghdr *nlh;
25 unsigned int size;
26 int err;
27 char buf[CONNECTOR_MAX_MSG_SIZE];
28 struct cn_msg *m;
30 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
32 nlh = (struct nlmsghdr *)buf;
33 nlh->nlmsg_seq = seq++;
34 nlh->nlmsg_pid = getpid();
35 nlh->nlmsg_type = NLMSG_DONE;
36 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
37 nlh->nlmsg_flags = 0;
39 m = NLMSG_DATA(nlh);
40 memcpy(m, msg, sizeof(*m) + msg->len);
42 err = send(s, nlh, size, 0);
43 if (err == -1)
44 ulog("Failed to send: %s [%d].\n", strerror(errno), errno);
46 return err;
49 int req_exec(int s, struct cn_msg *msg)
51 struct uvesafb_task *tsk = (struct uvesafb_task*)(msg + 1);
52 u8 *buf = (u8*)tsk + sizeof(struct uvesafb_task);
53 int i;
55 v86_task(tsk, buf);
56 netlink_send(s, msg);
58 return 0;
62 int main(int argc, char *argv[])
64 int s;
65 char buf[CONNECTOR_MAX_MSG_SIZE];
66 int len, i;
67 struct nlmsghdr *reply;
68 struct sockaddr_nl l_local;
69 struct cn_msg *data;
70 time_t tm;
71 struct pollfd pfd;
73 FILE *fout;
75 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
76 if (s == -1) {
77 perror("socket");
78 return -1;
81 l_local.nl_family = AF_NETLINK;
82 l_local.nl_groups = 1 << (CN_IDX_UVESAFB-1); /* bitmask of requested groups */
83 l_local.nl_pid = 0;
85 if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
86 perror("bind");
87 close(s);
88 return -1;
91 i = fork();
92 if (i) {
93 exit(0);
96 setsid();
97 chdir("/");
99 openlog("v86d", 0, LOG_KERN);
101 if (v86_init())
102 return -1;
104 ulog("it's alive %d!\n", s);
105 memset(buf, 0, sizeof(buf));
107 /* data = (struct cn_msg *)buf;
108 data->id.idx = CN_IDX_UVESAFB;
109 data->id.val = CN_VAL_UVESAFB;
110 data->seq = seq++;
111 data->ack = 0;
112 data->len = 0;
113 netlink_send(s, data);
115 pfd.fd = s;
117 while (!need_exit) {
118 pfd.events = POLLIN;
119 pfd.revents = 0;
120 switch (poll(&pfd, 1, -1)) {
121 case 0:
122 need_exit = 1;
123 continue;
124 case -1:
125 if (errno != EINTR) {
126 need_exit = 1;
127 break;
129 continue;
132 memset(buf, 0, sizeof(buf));
133 len = recv(s, buf, sizeof(buf), 0);
134 if (len == -1) {
135 perror("recv buf");
136 close(s);
137 return -1;
140 reply = (struct nlmsghdr *)buf;
142 switch (reply->nlmsg_type) {
143 case NLMSG_ERROR:
144 ulog("Error message received.\n");
145 break;
147 case NLMSG_DONE:
148 data = (struct cn_msg *)NLMSG_DATA(reply);
149 req_exec(s, data);
150 break;
151 default:
152 break;
156 closelog();
157 close(s);
158 return 0;