system_update_committer/
fidl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{anyhow, Context, Error};
use fidl_fuchsia_update::{CommitStatusProviderRequest, CommitStatusProviderRequestStream};
use fuchsia_component::server::{ServiceFs, ServiceObjLocal};
use futures::channel::oneshot;
use futures::prelude::*;
use std::sync::Arc;
use tracing::warn;
use zx::{self as zx, EventPair, HandleBased};

pub struct FidlServer {
    p_external: EventPair,
    // The blocker is shared to support multiple IsCurrentSystemCommitted calls while blocked.
    blocker: future::Shared<oneshot::Receiver<()>>,
}

impl FidlServer {
    pub fn new(p_external: EventPair, blocker: oneshot::Receiver<()>) -> Self {
        Self { p_external, blocker: blocker.shared() }
    }

    pub async fn run(server: Arc<Self>, mut fs: ServiceFs<ServiceObjLocal<'_, IncomingService>>) {
        fs.dir("svc").add_fidl_service(IncomingService::CommitStatusProvider);
        fs.for_each_concurrent(None, |incoming_service| match incoming_service {
            IncomingService::CommitStatusProvider(stream) => {
                Self::handle_commit_status_provider_request_stream(Arc::clone(&server), stream)
                    .unwrap_or_else(|e| {
                        warn!(
                        "error handling fuchsia.update/CommitStatusProvider request stream:  {:#}",
                        anyhow!(e)
                    )
                    })
            }
        })
        .await;
    }

    /// Handle a fuchsia.update/CommitStatusProvider request stream.
    async fn handle_commit_status_provider_request_stream(
        server: Arc<Self>,
        mut stream: CommitStatusProviderRequestStream,
    ) -> Result<(), Error> {
        while let Some(request) =
            stream.try_next().await.context("while receiving CommitStatusProvider request")?
        {
            let () =
                Self::handle_commit_status_provider_request(Arc::clone(&server), request).await?;
        }
        Ok(())
    }

    async fn handle_commit_status_provider_request(
        server: Arc<Self>,
        req: CommitStatusProviderRequest,
    ) -> Result<(), Error> {
        // The server should only unblock when either of these conditions are met:
        // * The system is committed on boot and p_external already has `USER_0` asserted.
        // * The system is pending commit and p_external does not have `USER_0` asserted.
        //
        // This ensures that consumers (e.g. the GC service) will always observe the `USER_0` signal
        // on p_external if the system is committed. Otherwise, there would be an edge case where
        // the system is committed and the consumer received the EventPair, but we haven't yet
        // asserted the signal on the EventPair.
        //
        // If there is an error with `put_metadata_in_happy_state`, the FIDL server will hang here
        // indefinitely. This is acceptable because we'll Soon™ reboot on error.
        let () = server.blocker.clone().await.context("while unblocking fidl server")?;

        let CommitStatusProviderRequest::IsCurrentSystemCommitted { responder } = req;

        responder
            .send(
                server
                    .p_external
                    .duplicate_handle(zx::Rights::BASIC)
                    .context("while duplicating p_external")?,
            )
            .context("while sending IsCurrentSystemCommitted response")
    }
}
pub enum IncomingService {
    CommitStatusProvider(CommitStatusProviderRequestStream),
}