|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | //! Handler for Unix domain sockets |
4 | 4 | use super::{Connect, ReadWrite}; |
5 | | -use crate::error::{ClientErrorKind, Result}; |
| 5 | +use crate::error::{ClientErrorKind, Error, Result}; |
| 6 | +use std::os::unix::fs::FileTypeExt; |
6 | 7 | use std::os::unix::net::UnixStream; |
7 | 8 | use std::path::PathBuf; |
8 | 9 | use std::time::Duration; |
9 | 10 |
|
10 | | -const DEFAULT_SOCKET_PATH: &str = "/run/parsec/parsec.sock"; |
11 | | -const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60); |
| 11 | +/// Default socket path used by the service. |
| 12 | +pub const DEFAULT_SOCKET_PATH: &str = "/run/parsec/parsec.sock"; |
12 | 13 |
|
13 | 14 | /// IPC handler for Unix domain sockets |
14 | 15 | #[derive(Debug, Clone)] |
@@ -40,16 +41,25 @@ impl Connect for Handler { |
40 | 41 |
|
41 | 42 | impl Handler { |
42 | 43 | /// Create new client using given socket path and timeout duration |
43 | | - pub fn new(path: PathBuf, timeout: Option<Duration>) -> Self { |
44 | | - Handler { path, timeout } |
| 44 | + pub fn new(path: PathBuf, timeout: Option<Duration>) -> Result<Self> { |
| 45 | + if path.exists() |
| 46 | + && std::fs::metadata(&path) |
| 47 | + .map_err(|_| Error::Client(ClientErrorKind::InvalidSocketAddress))? |
| 48 | + .file_type() |
| 49 | + .is_socket() |
| 50 | + { |
| 51 | + Ok(Handler { path, timeout }) |
| 52 | + } else { |
| 53 | + Err(Error::Client(ClientErrorKind::InvalidSocketAddress)) |
| 54 | + } |
45 | 55 | } |
46 | 56 | } |
47 | 57 |
|
48 | 58 | impl Default for Handler { |
49 | 59 | fn default() -> Self { |
50 | 60 | Handler { |
51 | 61 | path: DEFAULT_SOCKET_PATH.into(), |
52 | | - timeout: Some(DEFAULT_TIMEOUT), |
| 62 | + timeout: Some(super::DEFAULT_TIMEOUT), |
53 | 63 | } |
54 | 64 | } |
55 | 65 | } |
0 commit comments