Skip to content

Commit 0c7495f

Browse files
committed
Allow setting unix socket mode
Signed-off-by: Marco Rebhan <[email protected]>
1 parent 02bae49 commit 0c7495f

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

crates/cli/src/server.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
// Please see LICENSE in the repository root for full details.
66

77
use std::{
8+
fs,
89
future::ready,
910
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpListener, ToSocketAddrs},
10-
os::unix::net::UnixListener,
11+
os::unix::{fs::PermissionsExt, net::UnixListener},
1112
};
1213

1314
use anyhow::Context;
@@ -329,8 +330,20 @@ pub fn build_listeners(
329330
listener.try_into()?
330331
}
331332

332-
HttpBindConfig::Unix { socket } => {
333+
HttpBindConfig::Unix { socket, mode } => {
333334
let listener = UnixListener::bind(socket).context("could not bind socket")?;
335+
336+
if let Some(mode) = mode {
337+
let mut permissions = fs::metadata(socket)
338+
.context("could not read socket metadata")?
339+
.permissions();
340+
let mode = u32::from_str_radix(mode, 8)
341+
.with_context(|| format!("could not parse mode: {}", mode))?;
342+
permissions.set_mode(mode);
343+
fs::set_permissions(socket, permissions)
344+
.context("could not set socket permissions")?;
345+
}
346+
334347
listener.try_into()?
335348
}
336349

crates/config/src/sections/http.rs

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ pub enum BindConfig {
124124
/// Path to the socket
125125
#[schemars(with = "String")]
126126
socket: Utf8PathBuf,
127+
128+
/// Socket file mode
129+
mode: Option<String>,
127130
},
128131

129132
/// Accept connections on file descriptors passed by the parent process.

docs/config.schema.json

+4
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,10 @@
889889
"socket": {
890890
"description": "Path to the socket",
891891
"type": "string"
892+
},
893+
"mode": {
894+
"description": "Socket file mode",
895+
"type": "string"
892896
}
893897
}
894898
},

docs/reference/configuration.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ http:
5858

5959
# Third option: listen on the given UNIX socket
6060
- socket: /tmp/mas.sock
61+
mode: "660" # optional
6162

6263
# Fourth option: grab an already open file descriptor given by the parent process
6364
# This is useful when using systemd socket activation

0 commit comments

Comments
 (0)