From 15d11beb8369075c7331edcf2f2b912559d862cf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 15:48:30 -0800 Subject: [PATCH] Print: a second scheduler on a loopback port supplies its own paper sizes A `CUPS_SERVER` or `ServerName` of `localhost`, `127.0.0.1` or `::1` counts as this machine's scheduler only with no port or port 631. On another port (a second CUPS instance) the dialog no longer reads `/etc/cups/ppd`, whose queues belong to the default scheduler, and asks `lpoptions` and IPP instead. When the named server is one of CUPS's own domain sockets and neither the PPD nor `lpoptions` lists sizes, the IPP lookup asks `localhost` on the IPP default port, as it already does when no server is named. `ipptool` rejects a URI whose host is a socket path (`httpSeparateURI` in CUPS's `cups/http-support.c`), so another socket path is not asked. --- apps/vectorcraft/src/printing.rs | 61 +++++++++++++++++++++++++------- docs/mcp.md | 5 +-- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/apps/vectorcraft/src/printing.rs b/apps/vectorcraft/src/printing.rs index f77d74610c..4eb39067a0 100644 --- a/apps/vectorcraft/src/printing.rs +++ b/apps/vectorcraft/src/printing.rs @@ -199,11 +199,7 @@ mod system { } // A queue whose server has no PPD either: its IPP `media-supported` attribute, from the scheduler CUPS uses. let port = vectorcraft_ui_egui::print::ipp_port(std::env::var("IPP_PORT").ok().as_deref()); - // With no server named, CUPS uses its domain socket when it exists, and `localhost` on `IPP_PORT` otherwise. - let server = named.or_else(|| { - let sockets = ["/private/var/run/cupsd", "/run/cups/cups.sock", "/var/run/cups/cups.sock"]; - (!sockets.iter().any(|s| std::path::Path::new(s).exists())).then(|| "localhost".to_string()) - }); + let server = ipp_server(named, |s| std::path::Path::new(s).exists()); let Some(uri) = vectorcraft_ui_egui::print::ipp_queue_uri(server.as_deref(), port, printer) else { return papers }; use std::io::Write as _; use std::sync::atomic::{AtomicU64, Ordering}; @@ -235,6 +231,24 @@ mod system { output.map(|out| vectorcraft_ui_egui::print::ipp_papers(&String::from_utf8_lossy(&out))).unwrap_or_default() } } + /// CUPS's own domain sockets, where the local scheduler listens next to `localhost:631`. + #[cfg(not(windows))] + const CUPS_SOCKETS: [&str; 3] = ["/private/var/run/cupsd", "/run/cups/cups.sock", "/var/run/cups/cups.sock"]; + /// The server for the IPP lookup, given the one `cups_server` names and whether a path exists. With no server + /// named, CUPS uses its domain socket when it exists (`None`: [`ipp_queue_uri`] asks `localhost` on the IPP default + /// port), and `localhost` on `IPP_PORT` otherwise. A named domain socket that is one of CUPS's own is the same local + /// scheduler; any other socket path stays as named, so the lookup is skipped rather than asking a scheduler the job + /// does not go to. + /// + /// [`ipp_queue_uri`]: vectorcraft_ui_egui::print::ipp_queue_uri + #[cfg(not(windows))] + pub(super) fn ipp_server(named: Option, exists: impl Fn(&str) -> bool) -> Option { + match named { + Some(server) if CUPS_SOCKETS.contains(&server.trim()) => None, + Some(server) => Some(server), + None => (!CUPS_SOCKETS.iter().any(|s| exists(s))).then(|| "localhost".to_string()), + } + } /// The scheduler CUPS clients use: `CUPS_SERVER`, else the `ServerName` of `~/.cups/client.conf` or /// `/etc/cups/client.conf`; `None` for the local default. macOS does not read `ServerName` /// (https://openprinting.github.io/cups/doc/man-client.conf.html), so there only `CUPS_SERVER` counts. @@ -255,7 +269,8 @@ mod system { }) } /// Whether a scheduler named by `cups_server` runs on this machine: none named, a domain socket path, - /// or `localhost`, `127.0.0.1` or `::1`, each with an optional `:port` and `/version=` suffix. + /// or `localhost`, `127.0.0.1` or `::1` with no port or port 631, each with an optional `/version=` suffix. A loopback + /// name on another port is a second scheduler, whose queues the local PPD folder does not describe. #[cfg(not(windows))] pub(super) fn scheduler_is_local(server: Option<&str>) -> bool { let Some(server) = server.map(str::trim) else { return true }; @@ -264,14 +279,15 @@ mod system { } // A network name may end in `/version=1.1`. let server = server.split_once('/').map_or(server, |(server, _)| server); - let host = if let Some(rest) = server.strip_prefix('[') { - rest.split_once(']').map_or(rest, |(host, _)| host) + let (host, port) = if let Some(rest) = server.strip_prefix('[') { + rest.split_once(']').map_or((rest, None), |(host, after)| (host, after.strip_prefix(':'))) } else if server.matches(':').count() == 1 { - server.split_once(':').map_or(server, |(host, _)| host) + server.split_once(':').map_or((server, None), |(host, port)| (host, Some(port))) } else { - server + (server, None) }; - host.eq_ignore_ascii_case("localhost") || host == "127.0.0.1" || host == "::1" + let loopback = host.eq_ignore_ascii_case("localhost") || host == "127.0.0.1" || host == "::1"; + loopback && port.is_none_or(|port| port.trim() == "631") } /// The last `ServerName` in a `client.conf`. CUPS (`cupsFileGetConf`) looks only at the first `#` on a /// line: unescaped, it starts a comment that runs to the end of the line; as `\#`, the backslash is @@ -588,7 +604,19 @@ mod turn_tests { #[cfg(all(test, not(windows)))] mod client_conf_tests { - use super::system::{client_conf_server_name, scheduler_is_local}; + use super::system::{client_conf_server_name, ipp_server, scheduler_is_local}; + + #[test] + fn the_ipp_lookup_asks_a_socket_scheduler_only_when_it_is_cups_own() { + let none = |_: &str| false; + let socket = |s: &str| s == "/run/cups/cups.sock"; + assert_eq!(ipp_server(Some("/run/cups/cups.sock".into()), none), None, "CUPS's socket is asked as localhost:631"); + assert_eq!(ipp_server(Some(" /var/run/cups/cups.sock ".into()), none), None); + assert_eq!(ipp_server(Some("/tmp/other.sock".into()), socket).as_deref(), Some("/tmp/other.sock"), "another socket is not asked"); + assert_eq!(ipp_server(Some("localhost:8631".into()), socket).as_deref(), Some("localhost:8631")); + assert_eq!(ipp_server(None, socket), None); + assert_eq!(ipp_server(None, none).as_deref(), Some("localhost")); + } #[test] fn only_a_scheduler_on_this_machine_counts_as_local() { @@ -597,10 +625,13 @@ mod client_conf_tests { Some("/run/cups/cups.sock"), Some("localhost"), Some("LocalHost:631"), - Some("127.0.0.1:8631"), + Some("localhost:631"), + Some("127.0.0.1:631"), Some("::1"), Some("[::1]:631"), + Some("[::1]"), Some("localhost/version=1.1"), + Some("localhost:631/version=1.1"), ] { assert!(scheduler_is_local(local), "{local:?}"); } @@ -612,6 +643,10 @@ mod client_conf_tests { "2001:db8::1", "localhost.example.com", "print.example.com:631/version=1.1", + "localhost:8631", + "127.0.0.1:8631", + "[::1]:8631", + "localhost:8631/version=1.1", ] { assert!(!scheduler_is_local(Some(remote)), "{remote}"); } diff --git a/docs/mcp.md b/docs/mcp.md index 8a0b9f86ee..250806a975 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -13344,10 +13344,11 @@ driver's keyword for the paper (`Letter`, `CR80`, `iso_a4_210x297mm`), which the `media` on macOS and Linux. `paper` is an error on Windows, with file output, with `directPdf`, or when the value is not such a keyword. The Print dialog sends the keyword of the Size chosen when the printer's driver lists that paper. A printer whose PPD is not on this machine (a driverless or remote queue), or any printer when `CUPS_SERVER` or `ServerName` names a scheduler other than a domain socket, -`localhost`, `127.0.0.1` or `::1`, lists the paper sizes `lpoptions -l` reports for it, and when its server has no PPD either, the sizes +`localhost`, `127.0.0.1` or `::1` (each with no port or port 631), lists the paper sizes `lpoptions -l` reports for it, and when its server has no PPD either, the sizes of its IPP `media-supported` attribute. That lookup asks the server named by `CUPS_SERVER` or, on Linux, `client.conf`'s `ServerName`; a server named without a port, or `localhost` when no server is named and CUPS has no domain socket, is -asked on the port in `IPP_PORT` when it is set, and a port in the server name takes precedence. +asked on the port in `IPP_PORT` when it is set, and a port in the server name takes precedence. CUPS's own domain socket named as the +server is asked as `localhost` on port 631; another socket path is not asked. In the desktop Print dialog, installed printers appear once the background lookup has returned and the dialog has applied the list. `ui.dialog.set` accepts a named `printer` once it appears in that list. PDF File is available at once. The dialog shows and announces printer discovery. Physical Print is disabled until the list is applied; PDF File and Done remain available.