-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.rs
More file actions
227 lines (199 loc) · 5.72 KB
/
Copy pathserver.rs
File metadata and controls
227 lines (199 loc) · 5.72 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright 2023 Oxide Computer Company
*/
use std::{io::ErrorKind, os::unix::prelude::PermissionsExt, sync::Arc};
use anyhow::{bail, Result};
use bytes::{Buf, BufMut, BytesMut};
use tokio::{
io::Interest,
net::{UnixListener, UnixStream},
sync::{
mpsc::{channel, Receiver, Sender},
Mutex, Notify,
},
};
use super::protocol::{Decoder, Message, Payload, PayloadReq, PayloadRes};
use crate::InstallLocation;
#[derive(Debug)]
pub struct Request {
id: u64,
payload: PayloadReq,
conn: Arc<Connection>,
}
impl Request {
pub async fn reply(self, resp: PayloadRes) {
let m = Message { id: self.id, payload: Payload::Resp(resp) }
.pack()
.unwrap();
/*
* Put the serialised message on the write queue for the socket from
* whence it came and wake the I/O task:
*/
let mut ci = self.conn.inner.lock().await;
ci.writeq.put_slice(&m);
self.conn.notify.notify_one();
}
pub fn payload(&self) -> &PayloadReq {
&self.payload
}
}
pub fn listen() -> Result<Receiver<Request>> {
let loc = InstallLocation::detect()?;
/*
* Create the UNIX socket that the control program will use to contact the
* agent.
*/
std::fs::remove_file(loc.control_sock()).ok();
let ul = UnixListener::bind(loc.control_sock())?;
/*
* Allow everyone to connect:
*/
let mut perm = std::fs::metadata(loc.control_sock())?.permissions();
perm.set_mode(0o777);
std::fs::set_permissions(loc.control_sock(), perm)?;
/*
* Create channel to hand requests back to the main loop.
*/
let (tx, rx) = channel::<Request>(64);
tokio::spawn(async move {
loop {
match ul.accept().await {
Ok((stream, _addr)) => {
/*
* Create new client connection.
*/
let conn = Connection::new();
let tx = tx.clone();
tokio::spawn(async move {
handle_client(conn, stream, tx).await
});
}
Err(e) => println!("CONTROL ERROR: accept: {e}"),
}
}
});
Ok(rx)
}
#[derive(Debug)]
enum ClientState {
Running,
}
#[derive(Debug)]
struct ConnectionInner {
writeq: BytesMut,
state: ClientState,
decoder: Decoder,
}
#[derive(Debug)]
struct Connection {
notify: Notify,
inner: Mutex<ConnectionInner>,
}
impl Connection {
fn new() -> Arc<Connection> {
Arc::new(Connection {
notify: Notify::new(),
inner: Mutex::new(ConnectionInner {
writeq: Default::default(),
state: ClientState::Running,
decoder: Decoder::new(),
}),
})
}
}
async fn handle_client(
conn: Arc<Connection>,
us: UnixStream,
tx: Sender<Request>,
) {
/*
* This loop runs until a client connection is terminated, on purpose or
* otherwise:
*/
loop {
match handle_client_turn(&conn, &us, &tx).await {
Ok(fin) => {
if fin {
break;
}
}
Err(e) => {
println!("client error (closing connection): {:?}", e);
break;
}
};
tokio::task::yield_now().await;
}
}
async fn handle_client_turn(
conn: &Arc<Connection>,
us: &UnixStream,
tx: &Sender<Request>,
) -> Result<bool> {
/*
* Create our notified listener prior to checking if the writeq is
* empty:
*/
let notified = conn.notify.notified();
let dowrite = !conn.inner.lock().await.writeq.is_empty();
let mut i = Interest::READABLE;
if dowrite {
i = i.add(Interest::WRITABLE);
}
/*
* Wait for incoming data, or for the socket to be ready to send
* outbound data from our queue.
*/
let r = tokio::select! {
_ = notified => {
/*
* If we have been notified, there was probably an outside
* change to the write queue and we should go for another turn.
*/
return Ok(false);
}
r = us.ready(i) => r,
}?;
let mut ci = conn.inner.lock().await;
if r.is_writable() && !ci.writeq.is_empty() {
match us.try_write(&ci.writeq) {
Ok(n) => ci.writeq.advance(n),
Err(e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!("write error: {:?}", e),
}
}
if r.is_readable() {
let mut data = vec![0; 1024];
match us.try_read(&mut data) {
Ok(0) => ci.decoder.ingest_eof(),
Ok(n) => ci.decoder.ingest_bytes(&data[0..n]),
Err(e) if e.kind() == ErrorKind::WouldBlock => (),
Err(e) => bail!("read error: {:?}", e),
}
}
/*
* Process messages we have read from the connection, if any:
*/
while let Some(msg) = ci.decoder.take()? {
match ci.state {
ClientState::Running => match &msg.payload {
Payload::Req(request) => {
/*
* These are requests from the control program. Pass them
* on to the main loop.
*/
let req = Request {
id: msg.id,
payload: request.clone(),
conn: Arc::clone(conn),
};
tx.send(req).await.unwrap();
}
Payload::Resp(resp) => {
bail!("received response instead of request: {resp:?}");
}
},
}
}
Ok(ci.decoder.ended())
}