Skip to main content

starnix_core/task/
limits.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::mm::{DumpPolicy, PAGE_SIZE};
6use crossbeam::atomic::AtomicCell;
7use starnix_sync::{IcmpPingGidsLock, LockDepMutex};
8use std::ops::Range;
9use std::sync::atomic::{AtomicI32, AtomicUsize};
10
11/// Corresponds to files in /proc/sys/fs/inotify/, but cannot be negative.
12#[derive(Debug)]
13pub struct InotifyLimits {
14    // This value is used when creating an inotify instance.
15    // Updating this value does not affect already-created inotify instances.
16    pub max_queued_events: AtomicI32,
17
18    // TODO(b/297439734): Make this a real user limit on inotify instances.
19    pub max_user_instances: AtomicI32,
20
21    // TODO(b/297439734): Make this a real user limit on inotify watches.
22    pub max_user_watches: AtomicI32,
23}
24
25#[derive(Default, Debug)]
26pub struct SocketLimits {
27    /// The maximum backlog size for a socket.
28    pub max_connections: AtomicI32,
29
30    // Range of GIDs that can create ICMP Ping sockets.
31    pub icmp_ping_gids: LockDepMutex<Range<u32>, IcmpPingGidsLock>,
32}
33
34#[derive(Debug)]
35pub struct SystemLimits {
36    /// Limits applied to inotify objects.
37    pub inotify: InotifyLimits,
38
39    /// Limits applied to socket objects.
40    pub socket: SocketLimits,
41
42    /// The maximum size of pipes in the system.
43    pub pipe_max_size: AtomicUsize,
44
45    /// Whether IoUring is disabled.
46    ///
47    ///  0 -> io_uring is enabled (default)
48    ///  1 -> io_uring is enabled for processes in the io_uring_group
49    ///  2 -> io_uring is disabled
50    ///
51    /// See https://docs.kernel.org/admin-guide/sysctl/kernel.html#io-uring-disabled
52    pub io_uring_disabled: AtomicI32,
53
54    /// If io_uring_disabled is 1, then io_uring is enabled only for processes with CAP_SYS_ADMIN
55    /// or that are members of this group.
56    ///
57    /// See https://docs.kernel.org/admin-guide/sysctl/kernel.html#io-uring-group
58    pub io_uring_group: AtomicI32,
59
60    /// The default "dumpable" policy assigned to processes when their effective or filesystem
61    /// user/group IDs change, or when executing a set-user-ID, set-group-ID, or capability-endowed
62    /// binary. Exposed via `/proc/sys/fs/suid_dumpable`.
63    pub suid_dumpable: AtomicCell<DumpPolicy>,
64}
65
66impl Default for SystemLimits {
67    fn default() -> SystemLimits {
68        SystemLimits {
69            inotify: InotifyLimits {
70                max_queued_events: AtomicI32::new(16384),
71                max_user_instances: AtomicI32::new(128),
72                max_user_watches: AtomicI32::new(1048576),
73            },
74            socket: SocketLimits {
75                max_connections: AtomicI32::new(4096),
76                icmp_ping_gids: LockDepMutex::new(1..1),
77            },
78            pipe_max_size: AtomicUsize::new((*PAGE_SIZE * 256) as usize),
79            io_uring_disabled: AtomicI32::new(0),
80            io_uring_group: AtomicI32::new(-1),
81            suid_dumpable: AtomicCell::new(DumpPolicy::Disable),
82        }
83    }
84}