starnix_kernel_runner/
container.rs

1// Copyright 2022 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::{
6    Features, MountAction, expose_root, parse_features, parse_numbered_handles,
7    run_container_features, serve_component_runner, serve_container_controller,
8    serve_graphical_presenter, serve_lutex_controller,
9};
10use anyhow::{Context, Error, anyhow, bail};
11use bootreason::get_android_bootreason;
12use bstr::{BString, ByteSlice};
13use devicetree::parser::parse_devicetree;
14use devicetree::types::Devicetree;
15use fidl::endpoints::{ControlHandle, RequestStream, ServerEnd};
16use fidl_fuchsia_component_runner::{TaskProviderRequest, TaskProviderRequestStream};
17use fidl_fuchsia_feedback::CrashReporterMarker;
18use fidl_fuchsia_time_external::AdjustMarker;
19use fuchsia_async::DurationExt;
20use fuchsia_component::client::{connect_to_protocol, connect_to_protocol_sync};
21use fuchsia_component::server::ServiceFs;
22use futures::channel::oneshot;
23use futures::{FutureExt, StreamExt, TryStreamExt};
24use serde::Deserialize;
25use starnix_container_structured_config::Config as ContainerStructuredConfig;
26use starnix_core::device::remote_block_device::remote_block_device_init;
27use starnix_core::execution::{
28    create_init_process, create_system_task, execute_task_with_prerun_result,
29};
30use starnix_core::fs::fuchsia::create_remotefs_filesystem;
31use starnix_core::fs::tmpfs::TmpFs;
32use starnix_core::security;
33use starnix_core::task::container_namespace::ContainerNamespace;
34use starnix_core::task::{
35    CurrentTask, ExitStatus, Kernel, RoleOverrides, SchedulerManager, parse_cmdline,
36};
37use starnix_core::vfs::{FileSystemOptions, FsContext, LookupContext, Namespace, WhatToMount};
38use starnix_logging::{
39    CATEGORY_STARNIX, NAME_CREATE_CONTAINER, log_debug, log_error, log_info, log_warn,
40    trace_duration,
41};
42use starnix_modules::{init_common_devices, register_common_file_systems};
43use starnix_modules_layeredfs::LayeredFs;
44use starnix_modules_magma::get_magma_params;
45use starnix_modules_overlayfs::OverlayStack;
46use starnix_modules_rtc::rtc_device_init;
47use starnix_sync::{Locked, Unlocked};
48use starnix_task_command::TaskCommand;
49use starnix_uapi::errors::{ENOENT, SourceContext};
50use starnix_uapi::open_flags::OpenFlags;
51use starnix_uapi::resource_limits::Resource;
52use starnix_uapi::{errno, tid_t};
53use std::collections::BTreeMap;
54use std::ffi::CString;
55use std::ops::DerefMut;
56use std::sync::Arc;
57use zx::Task as _;
58use {
59    fidl_fuchsia_boot as fboot, fidl_fuchsia_component as fcomponent,
60    fidl_fuchsia_component_runner as frunner, fidl_fuchsia_element as felement,
61    fidl_fuchsia_io as fio, fidl_fuchsia_mem as fmem,
62    fidl_fuchsia_memory_attribution as fattribution, fidl_fuchsia_starnix_binder as fbinder,
63    fidl_fuchsia_starnix_container as fstarcontainer, fuchsia_async as fasync,
64    fuchsia_inspect as inspect, fuchsia_runtime as fruntime, fuchsia_zbi as zbi,
65};
66
67use std::sync::Weak;
68
69use crate::serve_memory_attribution_provider_container;
70use attribution_server::{AttributionServer, AttributionServerHandle};
71use fidl::HandleBased;
72
73/// Manages the memory attribution protocol for a Starnix container.
74struct ContainerMemoryAttributionManager {
75    /// Holds state for the hanging-get attribution protocol.
76    memory_attribution_server: AttributionServerHandle,
77}
78
79impl ContainerMemoryAttributionManager {
80    /// Creates a new [ContainerMemoryAttributionManager] from a Starnix kernel and the moniker
81    /// token of the container component.
82    pub fn new(kernel: Weak<Kernel>, component_instance: zx::Event) -> Self {
83        let memory_attribution_server = AttributionServer::new(Box::new(move || {
84            let kernel_ref = match kernel.upgrade() {
85                None => return vec![],
86                Some(k) => k,
87            };
88            attribution_info_for_kernel(kernel_ref.as_ref(), &component_instance)
89        }));
90
91        ContainerMemoryAttributionManager { memory_attribution_server }
92    }
93
94    /// Creates a new observer for the attribution information from this container.
95    pub fn new_observer(
96        &self,
97        control_handle: fattribution::ProviderControlHandle,
98    ) -> attribution_server::Observer {
99        self.memory_attribution_server.new_observer(control_handle)
100    }
101}
102
103/// Generates the attribution information for the Starnix kernel ELF component. The attribution
104/// information for the container is handled by the container component, not the kernel
105/// component itself, even if both are hosted within the same kernel process.
106fn attribution_info_for_kernel(
107    kernel: &Kernel,
108    component_instance: &zx::Event,
109) -> Vec<fattribution::AttributionUpdate> {
110    // Start the server to handle the memory attribution requests for the container, and provide
111    // a handle to get detailed attribution. We start a new task as each incoming connection is
112    // independent.
113    let (client_end, server_end) =
114        fidl::endpoints::create_request_stream::<fattribution::ProviderMarker>();
115    fuchsia_async::Task::spawn(serve_memory_attribution_provider_container(server_end, kernel))
116        .detach();
117
118    let starnix_kernel_id = Some(1);
119    let starnix_kernel_principal = fattribution::NewPrincipal {
120        identifier: starnix_kernel_id,
121        description: Some(fattribution::Description::Part("starnix_kernel".to_string())),
122        principal_type: Some(fattribution::PrincipalType::Part),
123        // This part is created for accounting. It holds the resource used for starnix
124        // kernel operation. It neither has sub-principals, nor publishes attribution,
125        // hence it does not need to be tied to a provider server end.
126        detailed_attribution: None,
127        ..Default::default()
128    };
129
130    let starnix_kernel_attribution = fattribution::UpdatedPrincipal {
131        identifier: starnix_kernel_id, // Recipient.
132        resources: Some(fattribution::Resources::Data(fattribution::Data {
133            resources: vec![fattribution::Resource::ProcessMapped(fattribution::ProcessMapped {
134                process: fuchsia_runtime::process_self().koid().unwrap().raw_koid(),
135                base: 0, // Attribute all the range.
136                len: u64::max_value(),
137                hint_skip_handle_table: false,
138            })],
139        })),
140        ..Default::default()
141    };
142
143    let container_id = Some(2);
144    let new_principal = fattribution::NewPrincipal {
145        identifier: container_id,
146        description: Some(fattribution::Description::Component(
147            component_instance.duplicate_handle(zx::Rights::SAME_RIGHTS).unwrap(),
148        )),
149        principal_type: Some(fattribution::PrincipalType::Runnable),
150        detailed_attribution: Some(client_end),
151        ..Default::default()
152    };
153    let attribution = fattribution::UpdatedPrincipal {
154        identifier: container_id,
155        resources: Some(fattribution::Resources::Data(fattribution::Data {
156            resources: vec![fattribution::Resource::KernelObject(
157                fuchsia_runtime::job_default().koid().unwrap().raw_koid(),
158            )],
159        })),
160        ..Default::default()
161    };
162
163    vec![
164        fattribution::AttributionUpdate::Add(new_principal),
165        fattribution::AttributionUpdate::Add(starnix_kernel_principal),
166        fattribution::AttributionUpdate::Update(attribution),
167        fattribution::AttributionUpdate::Update(starnix_kernel_attribution),
168    ]
169}
170
171#[derive(Debug)]
172pub struct ContainerStartInfo {
173    /// Configuration specified by the component's `program` block.
174    pub program: ContainerProgram,
175
176    pub config: ContainerStructuredConfig,
177
178    /// The outgoing directory of the container, used to serve protocols on behalf of the container.
179    /// For example, the starnix_kernel serves a component runner in the containers' outgoing
180    /// directory.
181    outgoing_dir: Option<zx::Channel>,
182
183    /// Mapping of top-level namespace entries to an associated channel.
184    /// For example, "/svc" to the respective channel.
185    pub container_namespace: ContainerNamespace,
186
187    /// The runtime directory of the container, used to provide CF introspection.
188    runtime_dir: Option<ServerEnd<fio::DirectoryMarker>>,
189
190    /// An eventpair that debuggers can use to defer the launch of the container.
191    break_on_start: Option<zx::EventPair>,
192
193    /// Component moniker token for the container component. This token is used in various protocols
194    /// to uniquely identify a component.
195    component_instance: Option<zx::Event>,
196}
197
198const MISSING_CONFIG_VMO_CONTEXT: &str = concat!(
199    "Retrieving container config VMO. ",
200    "If this fails, make sure your container CML includes ",
201    "//src/starnix/containers/container.shard.cml.",
202);
203
204impl ContainerStartInfo {
205    fn new(mut start_info: frunner::ComponentStartInfo) -> Result<Self, Error> {
206        let program = start_info.program.as_ref().context("retrieving program block")?;
207        let program: ContainerProgram =
208            runner::serde::deserialize_program(&program).context("parsing program block")?;
209
210        let encoded_config =
211            start_info.encoded_config.as_ref().context(MISSING_CONFIG_VMO_CONTEXT)?;
212        let config = match encoded_config {
213            fmem::Data::Bytes(b) => ContainerStructuredConfig::from_bytes(b),
214            fmem::Data::Buffer(b) => ContainerStructuredConfig::from_vmo(&b.vmo),
215            other => anyhow::bail!("unknown Data variant {other:?}"),
216        }
217        .context("parsing container structured config")?;
218
219        let ns = start_info.ns.take().context("retrieving container namespace")?;
220        let container_namespace = ContainerNamespace::from(ns);
221
222        let outgoing_dir = start_info.outgoing_dir.take().map(|dir| dir.into_channel());
223        let component_instance = start_info.component_instance;
224
225        Ok(Self {
226            program,
227            config,
228            outgoing_dir,
229            container_namespace,
230            component_instance,
231            break_on_start: start_info.break_on_start,
232            runtime_dir: start_info.runtime_dir,
233        })
234    }
235}
236
237#[derive(Debug, Default, Deserialize)]
238#[serde(deny_unknown_fields)]
239pub struct ContainerProgram {
240    /// The name of this container.
241    name: String,
242
243    /// The command line for the initial process for this container.
244    init: Vec<String>,
245
246    /// The command line for the kernel.
247    #[serde(default)]
248    kernel_cmdline: String,
249
250    /// The specifications for the file system mounts for this container.
251    #[serde(default)]
252    mounts: Vec<String>,
253
254    /// The features enabled for this container.
255    #[serde(default)]
256    pub features: Vec<String>,
257
258    /// The resource limits to apply to this container.
259    #[serde(default)]
260    rlimits: Vec<String>,
261
262    /// The path that the container will wait until exists before considering itself to have started.
263    #[serde(default)]
264    startup_file_path: String,
265
266    /// The default seclabel that is applied to components that are instantiated in this container.
267    ///
268    /// Components can override this by setting the `seclabel` field in their program block.
269    #[serde(default)]
270    pub default_seclabel: Option<String>,
271
272    /// The default uid that is applied to components that are instantiated in this container.
273    ///
274    /// Components can override this by setting the `uid` field in their program block.
275    #[serde(default = "default_uid")]
276    pub default_uid: runner::serde::StoreAsString<u32>,
277
278    /// The default mount options to use when mounting directories from a component's namespace.
279    ///
280    /// Each string is expected to follow the format: "<namespace_path>:<mount_options>".
281    pub default_ns_mount_options: Option<Vec<String>>,
282
283    /// Specifies role names to use for "realtime" tasks based on their process & thread names.
284    ///
285    /// Zircon's scheduler doesn't support configuring tasks to always preempt non-"realtime"
286    /// tasks without specifying a constant bandwidth profile. These profiles specify the period and
287    /// expected runtime of a "realtime" task, bounding the amount of work it is allowed to perform
288    /// at an elevated "realtime" priority.
289    ///
290    /// Because constant bandwidth profiles require workload-specific tuning, we can't uniformly
291    /// apply a single profile for all "realtime" tasks. Instead, this container configuration
292    /// allows us to specify different constant bandwidth profiles for different workloads.
293    #[serde(default)]
294    task_role_overrides: Vec<TaskSchedulerMapping>,
295}
296
297/// Specifies a role override for a class of tasks whose process and thread names match provided
298/// patterns.
299#[derive(Default, Deserialize)]
300struct TaskSchedulerMapping {
301    /// The role name to use for tasks matching the provided patterns.
302    role: String,
303    /// A regular expression that will be matched against the process' command.
304    process: String,
305    /// A regular expression that will be matched against the thread's command.
306    thread: String,
307}
308
309impl std::fmt::Debug for TaskSchedulerMapping {
310    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
311        write!(f, "process `{}` thread `{}` role `{}`", self.process, self.thread, self.role)
312    }
313}
314
315fn default_uid() -> runner::serde::StoreAsString<u32> {
316    runner::serde::StoreAsString(42)
317}
318
319// Creates a CString from a String. Calling this with an invalid CString will panic.
320fn to_cstr(str: &str) -> CString {
321    CString::new(str.to_string()).unwrap()
322}
323
324#[must_use = "The container must run serve on this config"]
325pub struct ContainerServiceConfig {
326    start_info: ContainerStartInfo,
327    request_stream: frunner::ComponentControllerRequestStream,
328    receiver: oneshot::Receiver<Result<ExitStatus, Error>>,
329}
330
331pub struct Container {
332    /// The `Kernel` object that is associated with the container.
333    pub kernel: Arc<Kernel>,
334
335    memory_attribution_manager: ContainerMemoryAttributionManager,
336
337    /// Inspect node holding information about the state of the container.
338    _node: inspect::Node,
339
340    /// Until negative trait bound are implemented, using `*mut u8` to prevent transferring
341    /// Container across threads.
342    _thread_bound: std::marker::PhantomData<*mut u8>,
343}
344
345impl Container {
346    pub fn system_task(&self) -> &CurrentTask {
347        self.kernel.kthreads.system_task()
348    }
349
350    async fn serve_outgoing_directory(
351        &self,
352        outgoing_dir: Option<zx::Channel>,
353    ) -> Result<(), Error> {
354        if let Some(outgoing_dir) = outgoing_dir {
355            // Add `ComponentRunner` to the exposed services of the container, and then serve the
356            // outgoing directory.
357            let mut fs = ServiceFs::new_local();
358            fs.dir("svc")
359                .add_fidl_service(ExposedServices::ComponentRunner)
360                .add_fidl_service(ExposedServices::ContainerController)
361                .add_fidl_service(ExposedServices::GraphicalPresenter)
362                .add_fidl_service(ExposedServices::LutexController);
363
364            // Expose the root of the container's filesystem.
365            let (fs_root, fs_root_server_end) = fidl::endpoints::create_proxy();
366            fs.add_remote("fs_root", fs_root);
367            expose_root(
368                self.kernel.kthreads.unlocked_for_async().deref_mut(),
369                self.system_task(),
370                fs_root_server_end,
371            )?;
372
373            fs.serve_connection(outgoing_dir.into()).map_err(|_| errno!(EINVAL))?;
374
375            fs.for_each_concurrent(None, |request_stream| async {
376                match request_stream {
377                    ExposedServices::ComponentRunner(request_stream) => {
378                        match serve_component_runner(request_stream, self.system_task()).await {
379                            Ok(_) => {}
380                            Err(e) => {
381                                log_error!("Error serving component runner: {:?}", e);
382                            }
383                        }
384                    }
385                    ExposedServices::ContainerController(request_stream) => {
386                        serve_container_controller(request_stream, self.system_task())
387                            .await
388                            .expect("failed to start container.")
389                    }
390                    ExposedServices::GraphicalPresenter(request_stream) => {
391                        serve_graphical_presenter(request_stream, &self.kernel)
392                            .await
393                            .expect("failed to start GraphicalPresenter.")
394                    }
395                    ExposedServices::LutexController(request_stream) => {
396                        serve_lutex_controller(request_stream, self.system_task())
397                            .await
398                            .expect("failed to start LutexController.")
399                    }
400                }
401            })
402            .await
403        }
404        Ok(())
405    }
406
407    pub async fn serve(&self, service_config: ContainerServiceConfig) -> Result<(), Error> {
408        let (r, _) = futures::join!(
409            self.serve_outgoing_directory(service_config.start_info.outgoing_dir),
410            server_component_controller(
411                self.kernel.clone(),
412                service_config.request_stream,
413                service_config.receiver
414            )
415        );
416        r
417    }
418
419    pub fn new_memory_attribution_observer(
420        &self,
421        control_handle: fattribution::ProviderControlHandle,
422    ) -> attribution_server::Observer {
423        self.memory_attribution_manager.new_observer(control_handle)
424    }
425}
426
427/// The services that are exposed in the container component's outgoing directory.
428enum ExposedServices {
429    ComponentRunner(frunner::ComponentRunnerRequestStream),
430    ContainerController(fstarcontainer::ControllerRequestStream),
431    GraphicalPresenter(felement::GraphicalPresenterRequestStream),
432    LutexController(fbinder::LutexControllerRequestStream),
433}
434
435type TaskResult = Result<ExitStatus, Error>;
436
437async fn server_component_controller(
438    kernel: Arc<Kernel>,
439    request_stream: frunner::ComponentControllerRequestStream,
440    task_complete: oneshot::Receiver<TaskResult>,
441) {
442    *kernel.container_control_handle.lock() = Some(request_stream.control_handle());
443
444    enum Event<T, U> {
445        Controller(T),
446        Completion(U),
447    }
448
449    let mut stream = futures::stream::select(
450        request_stream.map(Event::Controller),
451        task_complete.into_stream().map(Event::Completion),
452    );
453
454    while let Some(event) = stream.next().await {
455        match event {
456            Event::Controller(Ok(frunner::ComponentControllerRequest::Stop { .. })) => {
457                log_info!("Stopping the container.");
458            }
459            Event::Controller(Ok(frunner::ComponentControllerRequest::Kill { control_handle })) => {
460                log_info!("Killing the container's job.");
461                control_handle.shutdown_with_epitaph(zx::Status::from_raw(
462                    fcomponent::Error::InstanceDied.into_primitive() as i32,
463                ));
464                fruntime::job_default().kill().expect("Failed to kill job");
465            }
466            Event::Controller(Ok(frunner::ComponentControllerRequest::_UnknownMethod {
467                ordinal,
468                method_type,
469                ..
470            })) => {
471                log_error!(ordinal, method_type:?; "Unknown component controller request received.");
472            }
473            Event::Controller(Err(e)) => {
474                log_warn!(e:?; "Container component controller channel encountered an error.");
475            }
476            Event::Completion(result) => {
477                log_info!(result:?; "init process exited.");
478            }
479        }
480
481        // We treat any event in the stream as an invitation to shut down.
482        if !kernel.is_shutting_down() {
483            kernel.shut_down();
484        }
485    }
486
487    log_debug!("done listening for container-terminating events");
488
489    // In case the stream ended without an event, shut down the kernel here.
490    if !kernel.is_shutting_down() {
491        kernel.shut_down();
492    }
493}
494
495pub async fn create_component_from_stream(
496    mut request_stream: frunner::ComponentRunnerRequestStream,
497    kernel_extra_features: Vec<String>,
498) -> Result<(Container, ContainerServiceConfig), Error> {
499    if let Some(event) = request_stream.try_next().await? {
500        match event {
501            frunner::ComponentRunnerRequest::Start { start_info, controller, .. } => {
502                let request_stream = controller.into_stream();
503                let mut start_info = ContainerStartInfo::new(start_info)?;
504                let (sender, receiver) = oneshot::channel::<TaskResult>();
505                let container = create_container(&mut start_info, &kernel_extra_features, sender)
506                    .await
507                    .with_source_context(|| {
508                        format!("creating container \"{}\"", start_info.program.name)
509                    })?;
510                let service_config =
511                    ContainerServiceConfig { start_info, request_stream, receiver };
512                return Ok((container, service_config));
513            }
514            frunner::ComponentRunnerRequest::_UnknownMethod { ordinal, .. } => {
515                log_warn!("Unknown ComponentRunner request: {ordinal}");
516            }
517        }
518    }
519    bail!("did not receive Start request");
520}
521
522async fn get_bootargs(device_tree: &Devicetree) -> Result<String, Error> {
523    device_tree
524        .root_node
525        .find("chosen")
526        .and_then(|n| {
527            n.get_property("bootargs").map(|p| {
528                let end =
529                    if p.value.last() == Some(&0) { p.value.len() - 1 } else { p.value.len() };
530                match std::str::from_utf8(&p.value[..end]) {
531                    Ok(s) => Ok(s.to_owned()),
532                    Err(e) => {
533                        log_warn!("Bootargs are not valid UTF-8: {e}");
534                        Err(anyhow!("Bootargs are not valid UTF-8"))
535                    }
536                }
537            })
538        })
539        .context("Couldn't find bootargs")?
540}
541
542async fn get_bootitems() -> Result<std::vec::Vec<u8>, Error> {
543    let items =
544        connect_to_protocol::<fboot::ItemsMarker>().context("Failed to connect to boot items")?;
545
546    let items_response = items
547        .get2(zbi::ZbiType::DeviceTree.into_raw(), None)
548        .await
549        .context("FIDL: Failed to get devicetree item")?
550        .map_err(|e| anyhow!("Failed to get devicetree item {:?}", e))?;
551
552    let Some(item) = items_response.last() else {
553        return Err(anyhow!("Failed to get items"));
554    };
555
556    let devicetree_vmo = &item.payload;
557    let bytes = devicetree_vmo
558        .read_to_vec(0, item.length as u64)
559        .context("Failed to read devicetree vmo")?;
560
561    Ok(bytes)
562}
563
564async fn create_container(
565    start_info: &mut ContainerStartInfo,
566    kernel_extra_features: &[String],
567    task_complete: oneshot::Sender<TaskResult>,
568) -> Result<Container, Error> {
569    trace_duration!(CATEGORY_STARNIX, NAME_CREATE_CONTAINER);
570    const DEFAULT_INIT: &str = "/container/init";
571
572    let pkg_channel = start_info.container_namespace.get_namespace_channel("/pkg").unwrap();
573    let pkg_dir_proxy = fio::DirectorySynchronousProxy::new(pkg_channel);
574
575    let device_tree: Option<Devicetree> = match get_bootitems().await {
576        Ok(items) => match parse_devicetree(&items) {
577            Ok(device_tree) => Some(device_tree),
578            Err(e) => {
579                log_warn!("Failed to parse devicetree: {e:?}");
580                None
581            }
582        },
583        Err(e) => {
584            log_warn!("Failed to get boot items for devicetree: {e:?}");
585            None
586        }
587    };
588    let mut features = parse_features(&start_info, kernel_extra_features)?;
589
590    log_debug!("Creating container with {:#?}", features);
591    let mut kernel_cmdline = BString::from(start_info.program.kernel_cmdline.as_bytes());
592    if features.android_serialno {
593        if let Some(device_tree) = &device_tree {
594            match get_bootargs(device_tree).await {
595                Ok(args) => {
596                    for item in parse_cmdline(&args) {
597                        if item.starts_with("androidboot.force_normal_boot") {
598                            // TODO(https://fxbug.dev/424152964): Support force_normal_boot.
599                            continue;
600                        }
601                        if item.starts_with("androidboot.bootreason") && features.android_bootreason
602                        {
603                            // androidboot.rebootreason is sourced from the Fuchsia reboot reason.
604                            continue;
605                        }
606                        kernel_cmdline.extend(b" ");
607                        kernel_cmdline.extend(item.bytes());
608                    }
609                }
610                Err(err) => log_warn!("could not get bootargs: {err:?}"),
611            }
612        } else {
613            log_warn!("No devicetree available to get bootargs for android.serialno");
614        }
615    }
616    if features.android_bootreason {
617        kernel_cmdline.extend(b" androidboot.bootreason=");
618
619        let tmp_channel = start_info.container_namespace.get_namespace_channel("/tmp_lifecycle");
620        let tmp_proxy = match tmp_channel {
621            Ok(channel) => {
622                Some(fio::DirectoryProxy::new(fidl::AsyncChannel::from_channel(channel)))
623            }
624            _ => None,
625        };
626
627        match get_android_bootreason(tmp_proxy).await {
628            Ok(reason) => {
629                kernel_cmdline.extend(reason.bytes());
630            }
631            Err(err) => {
632                log_warn!("could not get android bootreason: {err:?}. falling back to 'unknown'");
633                kernel_cmdline.extend(b"unknown");
634            }
635        }
636    }
637    if let Some(supported_vendors) = &features.magma_supported_vendors {
638        kernel_cmdline.extend(b" ");
639        let params = get_magma_params(supported_vendors);
640        kernel_cmdline.extend(&*params);
641    }
642
643    // Check whether we actually have access to a role manager by trying to set our own
644    // thread's role.
645    let mut task_mappings = RoleOverrides::new();
646    for m in &start_info.program.task_role_overrides {
647        task_mappings.add(m.process.clone(), m.thread.clone(), m.role.clone());
648    }
649    let task_mappings = task_mappings.build().context("adding custom task role")?;
650    let scheduler_manager = SchedulerManager::new(task_mappings);
651
652    let crash_reporter = connect_to_protocol::<CrashReporterMarker>().unwrap();
653
654    let node = inspect::component::inspector().root().create_child("container");
655    let kernel_node = node.create_child("kernel");
656    kernel_node.record_int("created_at", zx::MonotonicInstant::get().into_nanos());
657    features.record_inspect(&kernel_node);
658
659    let security_state = security::kernel_init_security(
660        features.selinux.enabled,
661        features.selinux.options.clone(),
662        features.selinux.exceptions.clone(),
663        &kernel_node,
664    );
665
666    // `config.enable_utc_time_adjustment` is set through config capability
667    // `fuchsia.time.config.WritableUTCTime`.
668    let time_adjustment_proxy = if features.enable_utc_time_adjustment {
669        connect_to_protocol_sync::<AdjustMarker>()
670            .map_err(|e| log_error!("could not connect to fuchsia.time.external/Adjust: {:?}", e))
671            .ok()
672    } else {
673        // See the comment above. UTC adjustment is a per-product setting.
674        log_info!("UTC adjustment is forbidden.");
675        None
676    };
677
678    log_info!("final kernel cmdline: {kernel_cmdline:?}");
679    kernel_node.record_string("cmdline", kernel_cmdline.to_str_lossy());
680
681    let kernel = Kernel::new(
682        kernel_cmdline,
683        features.kernel.clone(),
684        std::mem::take(&mut features.system_limits),
685        start_info.container_namespace.try_clone()?,
686        scheduler_manager,
687        Some(crash_reporter),
688        kernel_node,
689        security_state,
690        time_adjustment_proxy,
691        device_tree,
692    )
693    .with_source_context(|| format!("creating Kernel: {}", &start_info.program.name))?;
694    let fs_context = create_fs_context(
695        kernel.kthreads.unlocked_for_async().deref_mut(),
696        &kernel,
697        &features,
698        start_info,
699        &pkg_dir_proxy,
700    )
701    .source_context("creating FsContext")?;
702    let init_pid = kernel.pids.write().allocate_pid();
703    // Lots of software assumes that the pid for the init process is 1.
704    debug_assert_eq!(init_pid, 1);
705
706    let system_task = create_system_task(
707        kernel.kthreads.unlocked_for_async().deref_mut(),
708        &kernel,
709        Arc::clone(&fs_context),
710    )
711    .source_context("create system task")?;
712    // The system task gives pid 2. This value is less critical than giving
713    // pid 1 to init, but this value matches what is supposed to happen.
714    debug_assert_eq!(system_task.tid, 2);
715
716    kernel.kthreads.init(system_task).source_context("initializing kthreads")?;
717    let system_task = kernel.kthreads.system_task();
718
719    kernel.syslog.init(&system_task).source_context("initializing syslog")?;
720
721    kernel.hrtimer_manager.init(system_task).source_context("initializing HrTimer manager")?;
722
723    log_info!("Initializing suspend resume manager.");
724    if let Err(e) = kernel.suspend_resume_manager.init(&system_task) {
725        log_warn!("Suspend/Resume manager initialization failed: ({e:?})");
726    }
727
728    // Real Time clock is present in all configuration.
729    log_info!("Initializing RTC device.");
730    rtc_device_init(kernel.kthreads.unlocked_for_async().deref_mut(), &system_task)
731        .context("in starnix_kernel_runner, while initializing RTC")?;
732
733    // Register common devices and add them in sysfs and devtmpfs.
734    log_info!("Registering devices and filesystems.");
735    init_common_devices(kernel.kthreads.unlocked_for_async().deref_mut(), &kernel)?;
736    register_common_file_systems(kernel.kthreads.unlocked_for_async().deref_mut(), &kernel);
737
738    log_info!("Mounting filesystems.");
739    mount_filesystems(
740        kernel.kthreads.unlocked_for_async().deref_mut(),
741        &system_task,
742        start_info,
743        &pkg_dir_proxy,
744    )
745    .source_context("mounting filesystems")?;
746
747    // Run all common features that were specified in the .cml.
748    {
749        log_info!("Running container features.");
750        run_container_features(
751            kernel.kthreads.unlocked_for_async().deref_mut(),
752            &system_task,
753            &features,
754        )?;
755    }
756
757    log_info!("Initializing remote block devices.");
758    init_remote_block_devices(kernel.kthreads.unlocked_for_async().deref_mut(), &system_task)
759        .source_context("initalizing remote block devices")?;
760
761    // If there is an init binary path, run it, optionally waiting for the
762    // startup_file_path to be created. The task struct is still used
763    // to initialize the system up until this point, regardless of whether
764    // or not there is an actual init to be run.
765    let argv = if start_info.program.init.is_empty() {
766        vec![DEFAULT_INIT.to_string()]
767    } else {
768        start_info.program.init.clone()
769    }
770    .iter()
771    .map(|s| to_cstr(s))
772    .collect::<Vec<_>>();
773
774    log_info!("Opening start_info file.");
775    let executable = system_task
776        .open_file(
777            kernel.kthreads.unlocked_for_async().deref_mut(),
778            argv[0].as_bytes().into(),
779            OpenFlags::RDONLY,
780        )
781        .with_source_context(|| format!("opening init: {:?}", &argv[0]))?;
782
783    let initial_name = if start_info.program.init.is_empty() {
784        TaskCommand::default()
785    } else {
786        TaskCommand::new(start_info.program.init[0].as_bytes())
787    };
788
789    let rlimits = parse_rlimits(&start_info.program.rlimits)?;
790
791    // Serve the runtime directory.
792    log_info!("Starting runtime directory.");
793    if let Some(runtime_dir) = start_info.runtime_dir.take() {
794        kernel.kthreads.spawn_future(
795            move || async move { serve_runtime_dir(runtime_dir).await },
796            "serve_runtime_dir",
797        );
798    }
799
800    // At this point the runtime environment has been prepared but nothing is actually running yet.
801    // Pause here if a debugger needs time to attach to the job.
802    if let Some(break_on_start) = start_info.break_on_start.take() {
803        log_info!("Waiting for signal from debugger before spawning init process...");
804        if let Err(e) =
805            fuchsia_async::OnSignals::new(break_on_start, zx::Signals::EVENTPAIR_PEER_CLOSED).await
806        {
807            log_warn!(e:%; "Received break_on_start eventpair but couldn't wait for PEER_CLOSED.");
808        }
809    }
810
811    log_info!("Creating init process.");
812    let init_task = create_init_process(
813        kernel.kthreads.unlocked_for_async().deref_mut(),
814        &kernel,
815        init_pid,
816        initial_name,
817        Arc::clone(&fs_context),
818        &rlimits,
819    )
820    .with_source_context(|| format!("creating init task: {:?}", &start_info.program.init))?;
821
822    execute_task_with_prerun_result(
823        kernel.kthreads.unlocked_for_async().deref_mut(),
824        init_task,
825        move |locked, init_task| {
826            parse_numbered_handles(locked, init_task, None, &init_task.files).expect("");
827            init_task.exec(locked, executable, argv[0].clone(), argv.clone(), vec![])
828        },
829        move |result| {
830            log_info!("Finished running init process: {:?}", result);
831            let _ = task_complete.send(result);
832        },
833        None,
834    )?;
835
836    if !start_info.program.startup_file_path.is_empty() {
837        wait_for_init_file(&start_info.program.startup_file_path, &system_task, init_pid).await?;
838    };
839
840    let memory_attribution_manager = ContainerMemoryAttributionManager::new(
841        Arc::downgrade(&kernel),
842        start_info.component_instance.take().ok_or_else(|| Error::msg("No component instance"))?,
843    );
844
845    Ok(Container {
846        kernel,
847        memory_attribution_manager,
848        _node: node,
849        _thread_bound: Default::default(),
850    })
851}
852
853fn create_fs_context(
854    locked: &mut Locked<Unlocked>,
855    kernel: &Kernel,
856    features: &Features,
857    start_info: &ContainerStartInfo,
858    pkg_dir_proxy: &fio::DirectorySynchronousProxy,
859) -> Result<Arc<FsContext>, Error> {
860    // The mounts are applied in the order listed. Mounting will fail if the designated mount
861    // point doesn't exist in a previous mount. The root must be first so other mounts can be
862    // applied on top of it.
863    let mut mounts_iter =
864        start_info.program.mounts.iter().chain(start_info.config.additional_mounts.iter());
865    let mut root = MountAction::new_for_root(
866        locked,
867        kernel,
868        pkg_dir_proxy,
869        mounts_iter.next().ok_or_else(|| anyhow!("Mounts list is empty"))?,
870    )?;
871    if root.path != "/" {
872        anyhow::bail!("First mount in mounts list is not the root");
873    }
874
875    // Create a layered fs to handle /container and /container/component
876    let mut mappings = vec![];
877    if features.container {
878        // /container/component will be a tmpfs where component using the starnix kernel will have their
879        // package mounted.
880        let component_tmpfs_options = FileSystemOptions {
881            params: kernel
882                .features
883                .ns_mount_options("#component_tmpfs")
884                .context("#component_tmpfs options")?,
885            ..Default::default()
886        };
887        let component_tmpfs = TmpFs::new_fs_with_options(locked, kernel, component_tmpfs_options)?;
888
889        // /container will mount the container pkg
890        let container_remotefs_options = FileSystemOptions {
891            source: "data".into(),
892            params: kernel.features.ns_mount_options("#container").context("#container options")?,
893            ..Default::default()
894        };
895        let container_remotefs = create_remotefs_filesystem(
896            locked,
897            kernel,
898            pkg_dir_proxy,
899            container_remotefs_options,
900            fio::PERM_READABLE | fio::PERM_EXECUTABLE,
901        )?;
902
903        let container_fs = LayeredFs::new_fs(
904            locked,
905            kernel,
906            container_remotefs,
907            BTreeMap::from([("component".into(), component_tmpfs)]),
908        );
909        mappings.push(("container".into(), container_fs));
910    }
911    if features.custom_artifacts {
912        let mount_options = FileSystemOptions {
913            params: kernel
914                .features
915                .ns_mount_options("#custom_artifacts")
916                .context("#custom_artifacts options")?,
917            ..Default::default()
918        };
919        mappings.push((
920            "custom_artifacts".into(),
921            TmpFs::new_fs_with_options(locked, kernel, mount_options)?,
922        ));
923    }
924    if features.test_data {
925        let mount_options = FileSystemOptions {
926            params: kernel.features.ns_mount_options("#test_data").context("#test_data options")?,
927            ..Default::default()
928        };
929        mappings
930            .push(("test_data".into(), TmpFs::new_fs_with_options(locked, kernel, mount_options)?));
931    }
932
933    if !mappings.is_empty() {
934        // If this container has enabled any features that mount directories that might not exist
935        // in the root file system, we add a LayeredFs to hold these mappings.
936        root.fs = LayeredFs::new_fs(locked, kernel, root.fs, mappings.into_iter().collect());
937    }
938    if features.rootfs_rw {
939        root.fs = OverlayStack::wrap_fs_in_writable_layer(locked, kernel, root.fs)?;
940    }
941    Ok(FsContext::new(Namespace::new_with_flags(root.fs, root.flags)))
942}
943
944fn parse_rlimits(rlimits: &[String]) -> Result<Vec<(Resource, u64)>, Error> {
945    let mut res = Vec::new();
946
947    for rlimit in rlimits {
948        let (key, value) =
949            rlimit.split_once('=').ok_or_else(|| anyhow!("Invalid rlimit: {rlimit}"))?;
950        let value = value.parse::<u64>()?;
951        let kv = match key {
952            "RLIMIT_NOFILE" => (Resource::NOFILE, value),
953            "RLIMIT_RTPRIO" => (Resource::RTPRIO, value),
954            _ => bail!("Unknown rlimit: {key}"),
955        };
956        res.push(kv);
957    }
958
959    Ok(res)
960}
961
962fn mount_filesystems(
963    locked: &mut Locked<Unlocked>,
964    system_task: &CurrentTask,
965    start_info: &ContainerStartInfo,
966    pkg_dir_proxy: &fio::DirectorySynchronousProxy,
967) -> Result<(), Error> {
968    // Skip the first mount, that was used to create the root filesystem.
969    let mut mounts_iter =
970        start_info.program.mounts.iter().chain(start_info.config.additional_mounts.iter());
971    let _ = mounts_iter.next();
972    for mount_spec in mounts_iter {
973        let action = MountAction::from_spec(locked, system_task, pkg_dir_proxy, mount_spec)
974            .with_source_context(|| format!("creating filesystem from spec: {}", &mount_spec))?;
975        let mount_point = system_task
976            .lookup_path_from_root(locked, action.path.as_ref())
977            .with_source_context(|| format!("lookup path from root: {}", action.path))?;
978        mount_point.mount(WhatToMount::Fs(action.fs), action.flags)?;
979    }
980    Ok(())
981}
982
983fn init_remote_block_devices(
984    locked: &mut Locked<Unlocked>,
985    system_task: &CurrentTask,
986) -> Result<(), Error> {
987    remote_block_device_init(locked, system_task);
988    let entries = match std::fs::read_dir("/block") {
989        Ok(entries) => entries,
990        Err(e) => {
991            log_warn!("Failed to read block directory: {}", e);
992            return Ok(());
993        }
994    };
995    for entry in entries {
996        let entry = entry?;
997        let path_buf = entry.path();
998        let path = path_buf.to_str().ok_or_else(|| anyhow!("Invalid block device path"))?;
999        let (client_end, server_end) = fidl::endpoints::create_endpoints();
1000        match fdio::service_connect(
1001            &format!("{}/fuchsia.storage.block.Block", path),
1002            server_end.into(),
1003        ) {
1004            Ok(()) => (),
1005            Err(e) => {
1006                log_warn!("Failed to connect to block device at {}: {}", path, e);
1007                continue;
1008            }
1009        }
1010        system_task.kernel().remote_block_device_registry.create_remote_block_device(
1011            locked,
1012            system_task,
1013            entry.file_name().to_str().unwrap(),
1014            client_end,
1015        )?;
1016    }
1017    Ok(())
1018}
1019
1020async fn wait_for_init_file(
1021    startup_file_path: &str,
1022    current_task: &CurrentTask,
1023    init_tid: tid_t,
1024) -> Result<(), Error> {
1025    // TODO(https://fxbug.dev/42178400): Use inotify machinery to wait for the file.
1026    loop {
1027        fasync::Timer::new(fasync::MonotonicDuration::from_millis(100).after_now()).await;
1028
1029        let creds = security::creds_start_internal_operation(current_task);
1030        if let Some(result) = current_task.override_creds(creds, || {
1031            let root = current_task.fs().root();
1032            let mut context = LookupContext::default();
1033
1034            match current_task.lookup_path(
1035                current_task.kernel().kthreads.unlocked_for_async().deref_mut(),
1036                &mut context,
1037                root,
1038                startup_file_path.into(),
1039            ) {
1040                Ok(_) => return Some(Ok(())),
1041                Err(error) if error == ENOENT => {}
1042                Err(error) => return Some(Err(anyhow::Error::from(error))),
1043            };
1044
1045            if current_task.get_task(init_tid).upgrade().is_none() {
1046                return Some(Err(anyhow!(
1047                    "Init task terminated before startup_file_path was ready"
1048                )));
1049            }
1050
1051            None
1052        }) {
1053            return result;
1054        }
1055    }
1056}
1057
1058async fn serve_runtime_dir(runtime_dir: ServerEnd<fio::DirectoryMarker>) {
1059    let mut fs = fuchsia_component::server::ServiceFs::new();
1060    match create_job_id_vmo() {
1061        Ok(vmo) => {
1062            fs.dir("elf").add_vmo_file_at("job_id", vmo);
1063        }
1064        Err(e) => log_error!(e:%; "failed to create vmo with job id for debuggers"),
1065    }
1066    match fs.serve_connection(runtime_dir) {
1067        Ok(_) => {
1068            fs.add_fidl_service(|job_requests: TaskProviderRequestStream| {
1069                fuchsia_async::Task::local(async move {
1070                    if let Err(e) = serve_task_provider(job_requests).await {
1071                        log_warn!(e:?; "Error serving TaskProvider");
1072                    }
1073                })
1074                .detach();
1075            });
1076            fs.collect::<()>().await;
1077        }
1078        Err(e) => log_error!("Couldn't serve runtime directory: {e:?}"),
1079    }
1080}
1081
1082fn create_job_id_vmo() -> Result<zx::Vmo, Error> {
1083    let job_id = fuchsia_runtime::job_default().koid().context("reading own job koid")?;
1084    let job_id_str = job_id.raw_koid().to_string();
1085    let job_id_vmo = zx::Vmo::create(job_id_str.len() as u64).context("creating job id vmo")?;
1086    job_id_vmo.write(job_id_str.as_bytes(), 0).context("write job id to vmo")?;
1087    Ok(job_id_vmo)
1088}
1089
1090async fn serve_task_provider(mut job_requests: TaskProviderRequestStream) -> Result<(), Error> {
1091    while let Some(request) = job_requests.next().await {
1092        match request.context("getting next TaskProvider request")? {
1093            TaskProviderRequest::GetJob { responder } => {
1094                responder
1095                    .send(
1096                        fuchsia_runtime::job_default()
1097                            .duplicate_handle(zx::Rights::SAME_RIGHTS)
1098                            .map_err(|s| s.into_raw()),
1099                    )
1100                    .context("sending job for runtime dir")?;
1101            }
1102            unknown => bail!("Unknown TaskProvider method {unknown:?}"),
1103        }
1104    }
1105    Ok(())
1106}
1107
1108#[cfg(test)]
1109mod test {
1110    use super::wait_for_init_file;
1111    use fuchsia_async as fasync;
1112    use futures::{SinkExt, StreamExt};
1113    #[allow(deprecated, reason = "pre-existing usage")]
1114    use starnix_core::testing::create_kernel_task_and_unlocked;
1115    use starnix_core::vfs::FdNumber;
1116    use starnix_uapi::CLONE_FS;
1117    use starnix_uapi::file_mode::{AccessCheck, FileMode};
1118    use starnix_uapi::open_flags::OpenFlags;
1119    use starnix_uapi::signals::SIGCHLD;
1120    use starnix_uapi::vfs::ResolveFlags;
1121
1122    #[fuchsia::test]
1123    async fn test_init_file_already_exists() {
1124        #[allow(deprecated, reason = "pre-existing usage")]
1125        let (_kernel, current_task, locked) = create_kernel_task_and_unlocked();
1126        let (mut sender, mut receiver) = futures::channel::mpsc::unbounded();
1127
1128        let path = "/path";
1129        current_task
1130            .open_file_at(
1131                locked,
1132                FdNumber::AT_FDCWD,
1133                path.into(),
1134                OpenFlags::CREAT,
1135                FileMode::default(),
1136                ResolveFlags::empty(),
1137                AccessCheck::default(),
1138            )
1139            .expect("Failed to create file");
1140
1141        fasync::Task::local(async move {
1142            wait_for_init_file(path, &current_task, current_task.get_tid())
1143                .await
1144                .expect("failed to wait for file");
1145            sender.send(()).await.expect("failed to send message");
1146        })
1147        .detach();
1148
1149        // Wait for the file creation to have been detected.
1150        assert!(receiver.next().await.is_some());
1151    }
1152
1153    #[fuchsia::test]
1154    async fn test_init_file_wait_required() {
1155        #[allow(deprecated, reason = "pre-existing usage")]
1156        let (_kernel, current_task, locked) = create_kernel_task_and_unlocked();
1157        let (mut sender, mut receiver) = futures::channel::mpsc::unbounded();
1158
1159        let init_task = current_task.clone_task_for_test(locked, CLONE_FS as u64, Some(SIGCHLD));
1160        let path = "/path";
1161
1162        let test_init_tid = current_task.get_tid();
1163        fasync::Task::local(async move {
1164            sender.send(()).await.expect("failed to send message");
1165            wait_for_init_file(path, &init_task, test_init_tid)
1166                .await
1167                .expect("failed to wait for file");
1168            sender.send(()).await.expect("failed to send message");
1169        })
1170        .detach();
1171
1172        // Wait for message that file check has started.
1173        assert!(receiver.next().await.is_some());
1174
1175        // Create the file that is being waited on.
1176        current_task
1177            .open_file_at(
1178                locked,
1179                FdNumber::AT_FDCWD,
1180                path.into(),
1181                OpenFlags::CREAT,
1182                FileMode::default(),
1183                ResolveFlags::empty(),
1184                AccessCheck::default(),
1185            )
1186            .expect("Failed to create file");
1187
1188        // Wait for the file creation to be detected.
1189        assert!(receiver.next().await.is_some());
1190    }
1191
1192    #[fuchsia::test]
1193    async fn test_init_exits_before_file_exists() {
1194        #[allow(deprecated, reason = "pre-existing usage")]
1195        let (_kernel, current_task, locked) = create_kernel_task_and_unlocked();
1196        let (mut sender, mut receiver) = futures::channel::mpsc::unbounded();
1197
1198        let init_task = current_task.clone_task_for_test(locked, CLONE_FS as u64, Some(SIGCHLD));
1199        const STARTUP_FILE_PATH: &str = "/path";
1200
1201        let test_init_tid = init_task.get_tid();
1202        fasync::Task::local(async move {
1203            sender.send(()).await.expect("failed to send message");
1204            wait_for_init_file(STARTUP_FILE_PATH, &current_task, test_init_tid)
1205                .await
1206                .expect_err("Did not detect init exit");
1207            sender.send(()).await.expect("failed to send message");
1208        })
1209        .detach();
1210
1211        // Wait for message that file check has started.
1212        assert!(receiver.next().await.is_some());
1213
1214        // Drop the `init_task`.
1215        std::mem::drop(init_task);
1216
1217        // Wait for the init failure to be detected.
1218        assert!(receiver.next().await.is_some());
1219    }
1220}