-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsuper_ctrl.c
More file actions
102 lines (81 loc) · 1.69 KB
/
super_ctrl.c
File metadata and controls
102 lines (81 loc) · 1.69 KB
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
#include <bits/socket.h>
#include <bits/socket/unix.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/signal.h>
#include <sys/sched.h>
#include <nlusctl.h>
#include <string.h>
#include <format.h>
#include <util.h>
#include "common.h"
#include "super.h"
int ctrlfd;
static char rxbuf[200];
static void shutdown_conn(struct conn* cn)
{
sys_shutdown(cn->fd, SHUT_RDWR);
}
void handle_conn(struct conn* cn)
{
int ret, fd = cn->fd;
struct urbuf ur = {
.buf = rxbuf,
.mptr = rxbuf,
.rptr = rxbuf,
.end = rxbuf + sizeof(rxbuf)
};
struct itimerval old, itv = {
.interval = { 0, 0 },
.value = { 1, 0 }
};
sys_setitimer(0, &itv, &old);
while(1) {
if((ret = uc_recv(fd, &ur, 0)) < 0)
break;
if((ret = dispatch_cmd(cn, ur.msg)) < 0)
break;
}
if(ret < 0 && ret != -EBADF && ret != -EAGAIN)
shutdown_conn(cn);
sys_setitimer(0, &old, NULL);
}
void accept_ctrl(int sfd)
{
int cfd;
struct sockaddr addr;
int addr_len = sizeof(addr);
struct conn *cn;
while((cfd = sys_accept(sfd, &addr, &addr_len)) > 0) {
if((cn = grab_conn_slot())) {
cn->fd = cfd;
} else {
sys_shutdown(cfd, SHUT_RDWR);
sys_close(cfd);
}
}
request(F_UPDATE_PFDS);
}
void setup_ctrl(void)
{
int fd;
struct sockaddr_un addr = {
.family = AF_UNIX,
.path = CONTROL
};
const int flags = SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
if((fd = sys_socket(AF_UNIX, flags, 0)) < 0)
return report("socket", "AF_UNIX", fd);
long ret;
char* name = addr.path;
ctrlfd = fd;
request(F_UPDATE_PFDS);
if((ret = sys_bind(fd, &addr, sizeof(addr))) < 0)
report("bind", name, ret);
else if((ret = sys_listen(fd, 1)))
report("listen", name, ret);
else
return;
ctrlfd = -1;
sys_close(fd);
}