Commit graph

3936 commits

Author SHA1 Message Date
David Anderson
cbd082d46d Disable multilib for CtsInitTestCases.
Bug: 260393485
Test: run cts -m CtsInitTestCases
Change-Id: I92a11b7961411b22525d4434312479878b353b55
2022-12-06 20:13:53 -08:00
Chih-hung Hsieh
48cd41324b Merge "Fix potential memory leaks" 2022-12-07 02:36:27 +00:00
Bart Van Assche
d3484c84bf Merge "init: Enable ANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION" 2022-12-06 23:05:30 +00:00
Bart Van Assche
9dbf8c32e9 Merge "init: Prevent copying of Service objects" 2022-12-06 17:32:23 +00:00
Bart Van Assche
b3f9f6f4c6 Merge "init/Android.bp: Sort cflags alphabetically" 2022-12-06 17:29:41 +00:00
Treehugger Robot
70b22e1c5b Merge "init: Make an error message more informative" 2022-12-06 03:04:16 +00:00
Treehugger Robot
ab3bc215e8 Merge "init: Convert a single-element array into a scalar" 2022-12-06 01:48:58 +00:00
Chih-Hung Hsieh
784e63c9a2 Fix potential memory leaks
Bug: 259995529
Test: make tidy-system_subset
Change-Id: I604a308caf498a854b916dc86a8e274148c21ab0
2022-12-05 23:52:58 +00:00
Bart Van Assche
0bb4757d03 init: Prevent copying of Service objects
Service objects have external state (the child process) and hence must
not be duplicated. Disable the copy constructor and the assignment
operator to prevent that these objects get duplicated accidentally.

Bug: 213617178
Change-Id: Ia5391154b94eca7f12be69eabcdf3f173fc06452
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-12-05 11:41:26 -08:00
Bart Van Assche
42764c4e3e init: Make an error message more informative
Make it easier to diagnose service failures.

Bug: 213617178
Change-Id: I27135cb32b6a98b2fe24ab2324dffbf5b591fdd5
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-12-05 11:41:10 -08:00
Bart Van Assche
c41a4826d1 init: Do not invoke the ServiceList destructor when exiting
From the Google C++ style guide: "Objects with static storage duration
are forbidden unless they are trivially destructible." Hence this CL.

Bug: 213617178
Change-Id: I4c9a51618ee1eb14ed439295a5fc0101b940a63d
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-12-05 10:45:49 -08:00
Bart Van Assche
aee2ec8f1e init: Enable ANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION
From the unique_fd.h header file: "unique_fd's operator int is
dangerous, but we have way too much code that depends on it, so make
this opt-in at first."

From the Google C++ style guide: "Do not define implicit conversions."
See also go/cstyle#Implicit_Conversions.

Hence this CL that disables unique_fd::operator int().

Change-Id: I28d94755d5408f63e5819da8d1cbc285057f867f
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-12-05 09:40:06 -08:00
Bart Van Assche
91ba2197ef init/Android.bp: Sort cflags alphabetically
Sort the compiler flags alphabetically before adding a new macro
definition.

Change-Id: If919333302817406a890c9622396ec96920adf29
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-12-05 09:40:05 -08:00
Bart Van Assche
fdc0f89a4a init: Convert a single-element array into a scalar
Simplify PollIn() by converting a single-element array into a scalar. No
functionality is changed.

Change-Id: I3ef36b9c0daafeed3a92f90c7a7c4fe0654dd586
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-12-05 09:40:05 -08:00
Bart Van Assche
fa4926a9de Merge "Revert "init: Add more diagnostics for signalfd hangs."" 2022-12-01 17:50:27 +00:00
Bart Van Assche
3b21d95a0e init: Fix and re-enable the init#StartConsole test
Skip the test if /dev/console does not exist. Fix the console service
security label. Fix the getsid() test.

Bug: 260104465
Test: atest 'CtsInitTestCases:init#StartConsole' on a P2023 development board
Change-Id: If2533a3f205f922a9b04a748cb558a09c5925986
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-30 13:25:47 -08:00
Bart Van Assche
29d8a42d14 Revert "init: Add more diagnostics for signalfd hangs."
Revert commit 14f9c15e05 ("init: Add more diagnostics for signalfd
hangs") because:
* That commit was intented to help with root-causing b/223076262.
* The root cause of b/223076262 has been fixed (not blocking SIGCHLD
  in all threads in the init process).

Test: Treehugger
Change-Id: I586663ec0588e74a9d58512f7f31155398cf4f52
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-30 09:17:16 -08:00
Bart Van Assche
027b275b57 init: Disable test init#StartConsole
Bug: 260104465
Change-Id: I43a19b2451bad955d101d9ebdc3c98419d83597d
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-22 16:38:43 -08:00
Bart Van Assche
01e6669c66 init: Fix a race condition in KillProcessGroup()
Multiple tests in CtsInitTestCases, e.g. RebootTest#StopServicesSIGKILL,
can trigger the following race condition:
* A service is started. This involves calling fork() and also to call
  RunService() in the child process. RunService() calls setpgid().
* Service::Stop() is called and calls KillProcessGroup().
  KillProcessGroup() calls kill(-pgid, SIGKILL) before the child process
  has called setpgid(). pgid is the process ID of the child process. The
  kill() call fails because setpgid() has not yet been called.

Fix this race condition by adding a setpgid() call in the parent process
and by waiting from the parent until the child has called setsid() if a
console is attached.

Bug: 213617178
Change-Id: Ieb9e6908df725447e3695ed66bb8bd30e4e38aa9
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-21 11:42:44 -08:00
Bart Van Assche
987391656f init: Introduce RequiresConsole()
Prepare for adding more code in the parent process that depends on
whether or not a console is required.

Bug: 213617178
Change-Id: I066ede32fcd4ce09d06be23158f3c1970064a697
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-18 09:49:43 -08:00
Bart Van Assche
c8f34254b8 init: Introduce symbolic names for certain constants
Make the code easier to read by introducing symbolic names for the
constants used by Service::Start() for communication between the parent
and child processes.

Bug: 213617178
Change-Id: I3e735e149682fa9df2ed57f75eb5a67d7c68bd92
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-18 09:49:01 -08:00
Bart Van Assche
d394f74365 init: Reduce the number of system calls
Change setpgid(0, getpid()) into setpgid(0, 0). This patch removes one
system call but does not change the behavior of the code. From the
setpgid() man page: "If pgid is zero, then the PGID of the process
specified by pid is made the same as its process ID."

Bug: 213617178
Change-Id: I7031d9eb3711f526751da495c07a8927f9386d97
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-18 09:45:33 -08:00
Bart Van Assche
41787239ec Revert "init: Rename 'cgroups_activated' into 'fifo'"
Revert commit 9c61dad67e in preparation of
introducing a second interprocess communication channel.

Bug: 213617178
Change-Id: I2959a3902a1b994cca2ac99855be1fc60d63bcbb
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-18 09:42:14 -08:00
Bart Van Assche
bfcf4374ed init: Document which class Service members are not modified
It is nontrivial to derive from the implementation of class Service
which members are not modified. Hence this CL that documents this by
declaring these members 'const'.

Change-Id: I27b907a1c7044376d5c5393a29050c66cbdab7bf
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-18 09:42:14 -08:00
Bart Van Assche
5d18891e60 init: Add a unit test for the "start console" action
Treehugger starts Cuttlefish with the console disabled. Add a test that
enables the console. The purpose of this test is to trigger the code paths
in Service::Start() that are unique to processes associated with a
console.

Bug: 213617178
Change-Id: I834632ce6ec5c237c9c2c3f5b1aa7bc98c3ef260
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-18 09:42:05 -08:00
Bart Van Assche
f26e59ebba Revert "init: Fix a race condition in KillProcessGroup()"
This reverts commit 15e5ecdcd7.

Reason for revert: breaks console support.
Bug: 213617178
Bug: 258754901
Change-Id: Iffe213e2cd295461a427621f2b84933f1bebd39f
2022-11-15 00:55:45 +00:00
Bart Van Assche
15e5ecdcd7 init: Fix a race condition in KillProcessGroup()
Multiple tests in CtsInitTestCases, e.g. RebootTest#StopServicesSIGKILL,
can trigger the following race condition:
* A service is started. This involves calling fork() and also to call
  RunService() in the child process. RunService() calls setpgid().
* Service::Stop() is called and calls KillProcessGroup().
  KillProcessGroup() calls kill(-pgid, SIGKILL) before the child process
  has called setpgid(). pgid is the process ID of the child process. The
  kill() call fails because setpgid() has not yet been called.

Fix this race condition by adding a setpgid() call in the parent process
and by waiting from the parent until the child has called setsid() if a
console is attached.

Bug: 213617178
Test: Cuttlefish + atest 'CtsInitTestCases'
Change-Id: I6931cd579e607c247b4f79a5b375455ca3d52e29
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-10 09:08:21 -08:00
Inseob Kim
0fbc1d7e31 Don't unmount APEXes in microdroid
In microdroid, APEXes are passed as block devices from host, and then
mounted directly, without using /data. Unmounting APEXes on reboot is
for clean unmount of /data, so it's not needed in microdroid.

This skips unmounting in microdroid to remove unnecessary SELinux
denials.

Bug: 256793087
Test: run microdroid and reboot, see no denials happen
Change-Id: Iaf737876f5ab5de5e8f76ed8bb0963cc25d73a80
2022-11-09 12:50:14 +09:00
Bart Van Assche
9c61dad67e init: Rename 'cgroups_activated' into 'fifo'
Prepare for using the interprocess communication channel in two
directions.

Bug: 213617178
Change-Id: Ic78a3d8a2ec1f808fa5b4c4b198051655ee1b0ec
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-11-04 14:56:43 -07:00
Bart Van Assche
8462ceef62 Merge "Revert "init: Fix a race condition in KillProcessGroup()"" 2022-11-03 18:21:59 +00:00
Bart Van Assche
dcc378e53c Revert "init: Fix a race condition in KillProcessGroup()"
This reverts commit d8ef6f84d4.

Reason for revert: b/256874349

Change-Id: I86a1e03a0d2979db1c54abd3e78c32591fda98a1
2022-11-03 15:15:25 +00:00
Shen Lin
63ae3fdd9d init: Amend code format for DoLoadApex
Test: m init
Change-Id: I9d873cdf067ea586f75e62ff8a39cb753151f9ec
2022-11-02 11:01:04 +08:00
Treehugger Robot
c8c24a7255 Merge changes I4c55790c,I14baaa7a
* changes:
  init: Fix a race condition in KillProcessGroup()
  init: Document that ReapOneProcess() does not modify 'pid'
2022-10-27 23:17:03 +00:00
Treehugger Robot
0eaca46f48 Merge "Add mmap_rnd_bits routine for riscv64" 2022-10-27 22:28:21 +00:00
Bart Van Assche
d8ef6f84d4 init: Fix a race condition in KillProcessGroup()
Multiple tests in CtsInitTestCases, e.g. RebootTest#StopServicesSIGKILL,
can trigger the following race condition:
* A service is started. This involves calling fork() and also to call
  RunService() in the child process. RunService() calls setpgid().
* Service::Stop() is called and calls KillProcessGroup().
  KillProcessGroup() calls kill(-pgid, SIGKILL) before the child process
  has called setpgid(). pgid is the process ID of the child process. The
  kill() call fails because setpgid() has not yet been called.

Fix this race condition by adding a setpgid() call in the parent process
and by waiting from the parent until the child has called setsid() if a
console is attached.

Bug: 213617178
Test: Cuttlefish + atest 'CtsInitTestCases'
Change-Id: I4c55790c2dcde8716b860aecd57708d51a081086
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-27 14:29:35 -07:00
Bart Van Assche
c7d7ed0eae init: Document that ReapOneProcess() does not modify 'pid'
It is essential for correct operation of ReapAnyOutstandingChildren()
that ReapOneProcess() does not modify 'pid'. Make it easier to verify
for humans that ReapOneProcess() does not modify the 'pid' local
variable. Document the value of siginfo.si_signo with DCHECK_EQ()
statements.

Bug: 213617178
Change-Id: I14baaa7adbe5416b7f777f32fb76896f2ae27b4e
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-27 14:29:35 -07:00
Treehugger Robot
1b2d443626 Merge "init: Log more information if stopping times out" 2022-10-27 21:12:19 +00:00
Bart Van Assche
ea595ba2a0 init: Log more information if stopping times out
The newly logged information includes the pending signals bitmask (SigPnd).
That information allows to determine whether kill() did not deliver a
signal or whether SIGCHLD has not been processed in time. This patch is
expected to help with root-causing b/254878413.

Bug: 254878413
Change-Id: Id33d1fb724163f8e5d4b3edfeb6c4be5321c0d95
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-27 12:17:34 -07:00
Mao Han
c09eee098b Add mmap_rnd_bits routine for riscv64
Signed-off-by: Mao Han <han_mao@linux.alibaba.com>
Change-Id: Ide793f4365fee67f30e35302365c4a7a5ba1f021
2022-10-27 17:59:25 +00:00
Bart Van Assche
1a5b59386d init: Fix HandleSignalFd()
There are two bugs in HandleSignalFd():
* If the one_off argument is true and if no data is read from the
  signalfd, 'siginfo' is left uninitialized and used in the switch
  statement in HandleSignalFd().
* The PLOG() statement in the switch statement should be a LOG()
  statement since it does not report a failed system call.

This CL has been tested by changing kDiagnosticTimeout locally from 10s
into 100ms.

Change-Id: I0e488dd95bc13e1befaef770c0748d1d47f6e431
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-27 08:07:26 -07:00
Bart Van Assche
874448f4aa Merge "subcontext: Change a std::string argument into std::string_view" 2022-10-27 14:34:57 +00:00
Nathan Huckleberry
f2d93cebfd Merge changes I8174adf8,I9d252b0b
* changes:
  Expose system property for dm-verity check_at_most_once
  Fix flaky AVB test from late verity_update_state
2022-10-27 00:45:01 +00:00
Bart Van Assche
462ea55095 subcontext: Change a std::string argument into std::string_view
This change prevents that a later CL will trigger the following warning:

parameter 'test_context' is passed by value and only copied once; consider
moving it to avoid unnecessary copies

Change-Id: If5837ee6438efdf194781de041779c1089897789
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-26 09:31:59 -07:00
Nathan Huckleberry
997d738dda Expose system property for dm-verity check_at_most_once
Allow us to check if check_at_most_once is set for any partitions.

This property should be false for any device with a reasonable amount of
RAM and a modern CPU. Enabling check_at_most_once violates AVB best
practices, it should only be allowed on performance limited devices.

Bug: 253033920
Test: Ensure that avbHashtreeNotUsingSha1 CTS test still passes
    and that partition.system.verified.check_at_most_once is set.
Change-Id: I8174adf81111cc0df547ea01f81b0dfaca32631f
Signed-off-by: Nathan Huckleberry <nhuck@google.com>
2022-10-25 21:12:52 +00:00
Bart Van Assche
1693f420d4 init: Introduce class InterprocessFifo
Prepare for introducing a second interprocess communication channel by
introducing the class InterprocessFifo. Stop using std::unique_ptr<> for
holding the pipe file descriptors. Handle EOF consistently.

Bug: 213617178
Change-Id: Ic0cf18d3d8ea61b8ee17e64de8a9df2736e26728
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-25 06:56:50 -07:00
Nikita Ioffe
537ab23872 Merge "init: skip cgroup/task_profiles configuration if cgroups are disabled" 2022-10-24 07:14:12 +00:00
Bart Van Assche
77f3fe5e68 init: Fix the implementation of the task_profiles keyword
The documentation added by commit c9c0bbac53 ("init: Add task_profiles
init command") mentions that the task_profiles keyword sets process
attributes. Make the implementation of that keyword match the
documentation.

Change-Id: Ia080132f16bfc2488f8c25176d6aed37a2c42780
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-21 15:34:19 -07:00
Bart Van Assche
4029b05b04 Add __ANDROID_API_[STU]__ definitions
Bring the host_init_stubs.h header file in sync with the
<android/api-level.h> header file.

Change-Id: I0d344b5f89f909e5315a0075773efd0b725a72a4
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-21 15:33:09 -07:00
Nikita Ioffe
c2b1654c11 init: skip cgroup/task_profiles configuration if cgroups are disabled
We are planning to remove cgroups from the Micrdroid kernel, since the
entire VM belongs exclusively to a single owner, and is in the control
of the cgroups on the host side.

This patch expoxes CgroupAvailable API from libprocessgroup, and changes
init to query the CgroupAvailable API before doing any
cgroups/task_profiles related work.

Bug: 239367015
Test: run MicrodroidDemoApp
Test: atest --test-mapping packages/modules/Virtualization:avf-presubmit
Change-Id: I82787141cd2a7f9309a4e9b24acbd92ca21c145b
2022-10-21 13:14:23 +01:00
Bart Van Assche
dcd23dfc58 init/epoll: Stop using smart pointers
Change the type of 'handler' from std::shared_ptr<Handler> into Handler.
This change is safe since a previous CL moved the handler calls from the
Epoll::Wait() caller into Epoll::Wait() itself.

Bug: 213617178
Change-Id: Ife79e6863536b96ee4bb3cd778f6b0b164a95fed
Signed-off-by: Bart Van Assche <bvanassche@google.com>
2022-10-20 09:32:51 -07:00