Skip to content

Commit 7da2a2d

Browse files
authored
cat: do not connect to unix domain socket and instead return an error (#9755)
* cat: do not connect to unix domain socket and instead return an error. fixed #9751 * added empty line to fr-FR.ftl * made NoSuchDeviceOrAddress error unix specific
1 parent 939ab03 commit 7da2a2d

File tree

4 files changed

+13
-41
lines changed

4 files changed

+13
-41
lines changed

src/uu/cat/locales/en-US.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ cat-error-unknown-filetype = unknown filetype: { $ft_debug }
1919
cat-error-is-directory = Is a directory
2020
cat-error-input-file-is-output-file = input file is output file
2121
cat-error-too-many-symbolic-links = Too many levels of symbolic links
22+
cat-error-no-such-device-or-address = No such device or address

src/uu/cat/locales/fr-FR.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ cat-error-unknown-filetype = type de fichier inconnu : { $ft_debug }
1919
cat-error-is-directory = Est un répertoire
2020
cat-error-input-file-is-output-file = le fichier d'entrée est le fichier de sortie
2121
cat-error-too-many-symbolic-links = Trop de niveaux de liens symboliques
22+
cat-error-no-such-device-or-address = Aucun appareil ou adresse de ce type

src/uu/cat/src/cat.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,10 @@ use memchr::memchr2;
1313
use std::ffi::OsString;
1414
use std::fs::{File, metadata};
1515
use std::io::{self, BufWriter, ErrorKind, IsTerminal, Read, Write};
16-
/// Unix domain socket support
17-
#[cfg(unix)]
18-
use std::net::Shutdown;
1916
#[cfg(unix)]
2017
use std::os::fd::AsFd;
2118
#[cfg(unix)]
2219
use std::os::unix::fs::FileTypeExt;
23-
#[cfg(unix)]
24-
use std::os::unix::net::UnixStream;
2520
use thiserror::Error;
2621
use uucore::display::Quotable;
2722
use uucore::error::UResult;
@@ -103,6 +98,9 @@ enum CatError {
10398
},
10499
#[error("{}", translate!("cat-error-is-directory"))]
105100
IsDirectory,
101+
#[cfg(unix)]
102+
#[error("{}", translate!("cat-error-no-such-device-or-address"))]
103+
NoSuchDeviceOrAddress,
106104
#[error("{}", translate!("cat-error-input-file-is-output-file"))]
107105
OutputIsInput,
108106
#[error("{}", translate!("cat-error-too-many-symbolic-links"))]
@@ -395,15 +393,7 @@ fn cat_path(path: &OsString, options: &OutputOptions, state: &mut OutputState) -
395393
}
396394
InputType::Directory => Err(CatError::IsDirectory),
397395
#[cfg(unix)]
398-
InputType::Socket => {
399-
let socket = UnixStream::connect(path)?;
400-
socket.shutdown(Shutdown::Write)?;
401-
let mut handle = InputHandle {
402-
reader: socket,
403-
is_interactive: false,
404-
};
405-
cat_handle(&mut handle, options, state)
406-
}
396+
InputType::Socket => Err(CatError::NoSuchDeviceOrAddress),
407397
_ => {
408398
let file = File::open(path)?;
409399
if is_unsafe_overwrite(&file, &io::stdout()) {

tests/by-util/test_cat.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -576,37 +576,17 @@ fn test_write_fast_fallthrough_uses_flush() {
576576

577577
#[test]
578578
#[cfg(unix)]
579-
#[ignore = ""]
580579
fn test_domain_socket() {
581-
use std::io::prelude::*;
582580
use std::os::unix::net::UnixListener;
583-
use std::sync::{Arc, Barrier};
584-
use std::thread;
585-
586-
let dir = tempfile::Builder::new()
587-
.prefix("unix_socket")
588-
.tempdir()
589-
.expect("failed to create dir");
590-
let socket_path = dir.path().join("sock");
591-
let listener = UnixListener::bind(&socket_path).expect("failed to create socket");
592-
593-
// use a barrier to ensure we don't run cat before the listener is setup
594-
let barrier = Arc::new(Barrier::new(2));
595-
let barrier2 = Arc::clone(&barrier);
596-
597-
let thread = thread::spawn(move || {
598-
let mut stream = listener.accept().expect("failed to accept connection").0;
599-
barrier2.wait();
600-
stream
601-
.write_all(b"a\tb")
602-
.expect("failed to write test data");
603-
});
604581

605-
let child = new_ucmd!().args(&[socket_path]).run_no_wait();
606-
barrier.wait();
607-
child.wait().unwrap().stdout_is("a\tb");
582+
let s = TestScenario::new(util_name!());
583+
let socket_path = s.fixtures.plus("sock");
584+
let _ = UnixListener::bind(&socket_path).expect("failed to create socket");
608585

609-
thread.join().unwrap();
586+
s.ucmd()
587+
.args(&[socket_path])
588+
.fails()
589+
.stderr_contains("No such device or address");
610590
}
611591

612592
#[test]

0 commit comments

Comments
 (0)