Initial commit
[v86d.git] / main.c
blob843f079c2abe1463948e64a36dda870c56411699
1 #include <asm/types.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <sys/poll.h>
7 #include <linux/netlink.h>
8 #include <linux/rtnetlink.h>
10 #include <arpa/inet.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <time.h>
18 #include <fcntl.h>
20 #include <linux/connector.h>
21 #include <asm/vm86.h>
23 #define u8 __u8
24 #define u16 __u16
25 #define u32 __u32
27 struct completion;
29 #include "kernel/uvesafb.h"
31 //#define ulog(args...) do {} while (0)
32 #define ulog(args...) fprintf(stdout, ##args)
34 static int need_exit;
35 static __u32 seq;
37 static int netlink_send(int s, struct cn_msg *msg)
39 struct nlmsghdr *nlh;
40 unsigned int size;
41 int err;
42 char buf[1024];
43 struct cn_msg *m;
45 size = NLMSG_SPACE(sizeof(struct cn_msg) + msg->len);
47 nlh = (struct nlmsghdr *)buf;
48 nlh->nlmsg_seq = seq++;
49 nlh->nlmsg_pid = getpid();
50 nlh->nlmsg_type = NLMSG_DONE;
51 nlh->nlmsg_len = NLMSG_LENGTH(size - sizeof(*nlh));
52 nlh->nlmsg_flags = 0;
54 m = NLMSG_DATA(nlh);
55 memcpy(m, msg, sizeof(*m) + msg->len);
57 err = send(s, nlh, size, 0);
58 if (err == -1)
59 ulog("Failed to send: %s [%d].\n", strerror(errno), errno);
61 return err;
64 int req_exec(int s, struct cn_msg *msg)
66 struct uvesafb_task *tsk = (struct uvesafb_task*)(msg + 1);
67 int i;
69 // for (i = 0; i < msg->len + sizeof(struct cn_msg); i++) {
70 // ulog("%x ", blah[i]);
71 // }
73 ulog("got %d | %d\n", tsk->flags, msg->len);
75 if (tsk->flags & TF_VBEIB) {
76 ulog("%d\n", s);
77 netlink_send(s, msg);
79 return 0;
83 int main(int argc, char *argv[])
85 int s;
86 char buf[1024];
87 int len, i;
88 struct nlmsghdr *reply;
89 struct sockaddr_nl l_local;
90 struct cn_msg *data;
91 time_t tm;
92 struct pollfd pfd;
94 i = open("/dev/tty1", O_RDWR);
95 dup2(i, 0);
96 dup2(i, 1);
97 dup2(i, 2);
99 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
100 if (s == -1) {
101 perror("socket");
102 return -1;
105 l_local.nl_family = AF_NETLINK;
106 l_local.nl_groups = 1 << (CN_IDX_UVESAFB-1); /* bitmask of requested groups */
107 l_local.nl_pid = 0;
109 if (bind(s, (struct sockaddr *)&l_local, sizeof(struct sockaddr_nl)) == -1) {
110 perror("bind");
111 close(s);
112 return -1;
115 i = fork();
116 if (i) {
117 exit(0);
120 setsid();
121 chdir("/");
123 ulog("it's alive %d!\n", s);
125 memset(buf, 0, sizeof(buf));
127 /* data = (struct cn_msg *)buf;
128 data->id.idx = CN_IDX_UVESAFB;
129 data->id.val = CN_VAL_UVESAFB;
130 data->seq = seq++;
131 data->ack = 0;
132 data->len = 0;
133 netlink_send(s, data);
135 pfd.fd = s;
137 while (!need_exit) {
138 pfd.events = POLLIN;
139 pfd.revents = 0;
140 switch (poll(&pfd, 1, -1)) {
141 case 0:
142 need_exit = 1;
143 continue;
144 case -1:
145 if (errno != EINTR) {
146 need_exit = 1;
147 break;
149 continue;
152 memset(buf, 0, sizeof(buf));
153 len = recv(s, buf, sizeof(buf), 0);
154 if (len == -1) {
155 perror("recv buf");
156 close(s);
157 return -1;
160 reply = (struct nlmsghdr *)buf;
162 switch (reply->nlmsg_type) {
163 case NLMSG_ERROR:
164 ulog("Error message received.\n");
165 break;
167 case NLMSG_DONE:
168 data = (struct cn_msg *)NLMSG_DATA(reply);
169 req_exec(s, data);
170 break;
171 default:
172 break;
176 close(s);
177 return 0;