archivist_lib/logs/servers/
log_freeze.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::logs::error::LogsError;
6use fidl::endpoints::{ControlHandle, DiscoverableProtocolMarker};
7use fidl_fuchsia_diagnostics_system as ftarget;
8use futures::StreamExt;
9use log::warn;
10use zx::EventPair;
11
12pub struct LogFreezeServer {
13    freeze_token: EventPair,
14}
15
16impl LogFreezeServer {
17    pub fn new(freeze_token: EventPair) -> Self {
18        Self { freeze_token }
19    }
20
21    /// Handles a single FIDL request to freeze the serial log stream.
22    pub async fn wait_for_client_freeze_request(
23        self,
24        stream: ftarget::SerialLogControlRequestStream,
25    ) {
26        if let Err(e) = Self::handle_requests(stream, self.freeze_token).await {
27            warn!("error handling Log requests: {}", e);
28        }
29    }
30
31    /// Actually handle the FIDL request. This handles only a single request, then exits.
32    async fn handle_requests(
33        mut stream: ftarget::SerialLogControlRequestStream,
34        freeze_token: EventPair,
35    ) -> Result<(), LogsError> {
36        while let Some(request) = stream.next().await {
37            let request = request.map_err(|source| LogsError::HandlingRequests {
38                protocol: ftarget::SerialLogControlMarker::PROTOCOL_NAME,
39                source,
40            })?;
41
42            match request {
43                fidl_fuchsia_diagnostics_system::SerialLogControlRequest::FreezeSerialForwarding { responder } => {
44                    responder.send(freeze_token)?;
45                    return Ok(());
46                },
47                ftarget::SerialLogControlRequest::_UnknownMethod {
48                                            ordinal,
49                                            method_type,
50                                            control_handle,
51                                            ..
52                                        } => {
53                                            warn!(ordinal, method_type:?; "Unknown request. Closing connection");
54                                            control_handle.shutdown_with_epitaph(zx::Status::UNAVAILABLE);
55                                        }
56            }
57        }
58        Ok(())
59    }
60}