Commit graph

87 commits

Author SHA1 Message Date
Jiyong Park
11d7bc52df init: don't touch mmap_rnd_compat_bits on 64-bit only builds
mmap_rnd_compat_bits is for address space randomization of 32-bit
applications on 64-bit system. Configuring it is not only unnecessary
for 64-bit "only" builds, but also can cause a boot failure if the
kernel is built without CONFIG_COMPAT which is the case for Microdroid.

Use ro.product.abilist32 to determine whether 32-bit applications are
supported and if not, don't configure it, but mmap_rnd_bits.

Bug: 237950549
Test: run Microdroid with the kernel built with aosp/2153639

Change-Id: Ifca6fa02f14ad4c7d8f9b2ab8852494c12945c3a
2022-07-18 14:31:13 +09:00
Eric Biggers
6cb5a36f4c init: fix mkdir to reliably detect top-level /data directories
To determine the default encryption action, the mkdir command checks
whether the given path is a top-level directory of /data.  However, it
assumed a path without any duplicate slashes or trailing slash(es).

While everyone *should* be providing paths without unnecessary slashes,
it is not guaranteed, as paths with unnecessary slashes still work
correctly for all other parts of the mkdir command, including the
SELinux label lookup and the actual directory creation.  In particular,
the /data/fonts directory is being created using 'mkdir /data/fonts/'.

The effect is that the mkdir command thinks that /data/fonts/ is *not* a
top-level directory of /data, so it defaults to no encryption action.
Fortunately, the full command happens to use "encryption=Require", so we
dodged a bullet there, though the warning "Inferred action different
from explicit one" is still triggered.

There are a few approaches we could take here, including even just
fixing the /data/fonts/ command specifically, but I think the best
solution is to have mkdir clean its path at the very beginning.  This
retains the Linux path semantics that people expect, while avoiding
surprises in path processing afterwards.  This CL implements that.

Note, this CL intentionally changes the behavior of, and thus would
break, any existing cases where mkdir is used to create a top-level
/data directory using a path with unnecessary slashes and without using
an explicit encryption action.  There are no known cases where this
already occurs, however.  No cases exist in platform code, and vendor
init scripts shouldn't be creating top-level /data directories anyway.

Test: atest CtsInitTestCases
Test: Booted and verified that a trailing slash is no longer present in
      the log message "Verified that /data/fonts/ has the encryption
      policy ...".  Also verified that the message "Inferred action
      different ..." is no longer present just above it.
Bug: 232554803
Change-Id: Ie55c3ac1a2b1cf50632d54a1e565cb98c17b2a6a
2022-05-13 19:29:00 +00:00
Jiyong Park
3b3d87de79 Don't create vendor_init in microdroid
vendor_init is a subcontext of init which is responsible for handling
the vendor-defined services and vendor-defined actions. This is mainly
to enforce the Treble architecture in which the vendor components are
provided with a less-privileged context than the system components
because they are less-trusted.

However, in case of microdroid, both the system and the vendor
partitions are from the same entity. As VMs don't have direct access to
any of the underlying hardware, the vendor partition is targeting the
virtual platform, not the real hw platform. There really is no need for
the discrimination.

This CL disables the creation of the vendor_init subcontext when init
runs in microdroid.

Bug: 201363575
Test: atest MicrodroidHostTestCases
Change-Id: Ie5e47d84e9e245565239b4f2159e8182b457699d
2021-09-29 21:35:08 +09:00
Kiyoung Kim
0cbee0de2a Check if service is executed before APEX is ready
Any service which is executed when Runtime apex is mounted, but
linkerconfig is not updated can fail to be executed due to missing
information in ld.config.txt. This change updates init to have a status
variable which contains if current mount namespace is default
and APEX is not ready from ld.config.txt, and use bootstrap namespace if
it is not ready.

Bug: 181348374
Test: cuttlefish boot succeeded
Change-Id: Ia574b1fad2110d4e68586680dacbe6137186546e
2021-03-05 16:42:20 +09:00
Devin Moore
a4ef15bebb Support bootconfig in first stage init and fs_mgr
Androidboot parameters are being moved from the kernel commandline to
bootconfig.
fs_mgr looks for these parameters in properties and falls back to
reading directly from /proc/cmdline. So both of these sources are
updated for bootconfig.
The androidboot parameters from /proc/bootconfig
are added as ro.boot properties, and fs_mgr will fall back to searching
/proc/bootconfig if it is too early.

Test: boot cuttlefish with androidboot.fstab_suffix and
androidboot.hardware in bootconfig and not in cmdline.
Test: atest CtsFsMgrTestCases
Bug: 173815685

Change-Id: Iea36a0da94c26e1aa37d97c576725e0ad77cd3ad
2021-02-23 07:42:06 -08:00
Tom Cherry
959ea63b30 init: remove DumpShutdownDebugInformation()
This hasn't helped investigating the issue, and the issue itself isn't
a problem anymore, so we remove these logs.

Bug: 155203339
Test: reboot
Change-Id: I20e51d8fcad5572906a8d556bec8a8dee4522834
2020-12-10 07:34:29 -08:00
Alistair Delva
de28a8651f Respect ro.boot.fstab_suffix in swapon_all
While mount_all and umount_all were updated to use ro.boot.fstab_suffix,
I neglected to update swapon_all. Trivially copied from umount_all.

Bug: 142424832
Change-Id: Icd706fe7a1fe16c687cd2811b0a3158d7d2e224e
Merged-In: Icd706fe7a1fe16c687cd2811b0a3158d7d2e224e
2020-06-08 23:41:40 +00:00
Alistair Delva
a2cc1ebb72 Add ro.boot.fstab_suffix and modify mount_all to use it
Currently the ReadDefaultFstab function, which calls GetFstabPath,
makes some assumptions about what the fstab will be called and where
it is located. This is being used by vold to set up userdata encryption
and for gsid, and is even used in the default boot control HAL, so it
has become quite baked.

The original way for a board to specify things to mount was to use the
"mount_all /path/to/fstab" command in init.rc. However, due to the
above functionality, the path after mount_all is no longer very useful,
as it cannot differ from the inferred path, or userdata encryption and
other features will be broken.

On Cuttlefish, we have an interest in being able to test alternative
userdata configurations (ext4 vs f2fs, encryption on/off, etc.) and
currently the only way to achieve this is to either a) modify the
ro.hardware or ro.hardware.platform properties, which breaks a bunch
of things like default HAL filenames, or regenerate our odm.img or
vendor.img filesystems. We can't simply install another fstab and
point to it with "mount_all".

This change allows the fstab path to be omitted from "mount_all", and
adds another property which overrides the existing checks for
fstab.${ro.hardware} and fstab.${ro.hardware.platform}. Specifying
${ro.boot.fstab_suffix} will cause fstab.${ro.boot.fstab_suffix}
to be checked first.

Bug: 142424832
Test: booted cuttlefish with 'mount_all ${ro.hardware} --late'
Test: booted cuttlefish with 'mount_all --late'
Test: booted cuttlefish with 'mount_all --late' and fstab_suffix=f2fs
Test: partially booted cuttlefish with 'mount_all ${ro.hardware}'
Test: partially booted cuttlefish with 'mount_all'
Change-Id: I3e10f66aecfcd48bdb9ebf1d304b7aae745cbd3c
2020-05-21 16:38:40 -07:00
Tom Cherry
1c84135144 init: add even more debugging information for reboot
There are devices stuck waiting for vendor_init to finish a command,
without giving much more information.  Instead of setting aside the
last run command, it's more valuable to store and dump the last 30
logs seen.

Bug: 155203339
Test: these logs appear during hung reboots
Test: normal reboots have no difference.
Change-Id: I99cae248eb81eaf34ef66b910fa653a9fa135f68
2020-04-29 09:01:53 -07:00
Tom Cherry
c88d8f93cf init: Replace property_set() with android::base::SetProperty()
Init is no longer a special case and talks to property service just
like every other client, therefore move it away from property_set()
and to android::base::SetProperty().

In doing so, this change moves the initial property set up from the
kernel command line and property files directly into PropertyInit().
This makes the responsibilities between init and property services
more clear.

Test: boot, unit test cases
Change-Id: I36b8c83e845d887f1b203355c2391ec123c3d05f
2019-12-04 15:43:21 -08:00
Tom Cherry
18278d2e9c init: make triggering shutdown from vendor_init better
Previously, we assumed that TriggerShutdown() should never be called
from vendor_init and used property service as a back up in case it
ever did.  We have since then found out that vendor_init may indeed
call TriggerShutdown() and we want to make it just as strict as it is
in init, wherein it will immediately start the shutdown sequence
without executing any further commands.

Test: init unit tests, trigger shuttdown from init and vendor_init
Change-Id: I1f44dae801a28269eb8127879a8b7d6adff6f353
2019-11-13 12:41:34 -08:00
Paul Crowley
68258e8444 Make encryption action an argument to mkdir
FscryptSetDirectoryPolicy no longer tries to infer the action from the
filename. Well mostly; it still assumes top-level directories in /data
should be encrypted unless the mkdir arguments say otherwise, but
it warns.

Bug: 26641735
Test: boot, check log messages
Change-Id: Id6d2cea7fb856f17323897d85cf6190c981b443c
2019-11-05 16:26:43 -08:00
Tom Cherry
4772f1da47 init: check the arguments of builtins during the build
Host init verifier already checks that the names and number of
arguments for builtins are correct, but it can check more.  This
change ensures that property expansions are well formed, and that
arguments that can be parsed on the host are correct.  For example it
checks that UIDs and GIDs exist, that numerical values can be parsed,
and that rlimit strings are correct.

Test: build

Change-Id: Ied8882498a88a9f8324db6b8d1020aeeccc8177b
2019-08-01 10:54:47 -07:00
Tom Cherry
c5cf85db23 init: don't log in expand_props directly
It's better to pass the error message to the caller to determine how
best to print the error.

Test: build
Change-Id: Id8857c459df2f26c031650166609608d20e4d051
2019-08-01 10:34:58 -07:00
Tom Cherry
2e4c85f157 init: clean up file / socket descriptor creation
clang-tidy hinted that some of this code wasn't right.  Looking
deeper, there is really not much related to file and socket
descriptors, except that they're published in similar ways to the
environment.  All of the abstraction into a 'Descriptor' class takes
us further away from specifying what we really mean.

This removes that abstraction, adds stricter checks and better errors
for parsing init scripts, reports sockets and files that are unable to
be acquired before exec, and updates the README.md for the passcred
option.

Test: build, logd (uses files and sockets) works
Change-Id: I59e611e95c85bdbefa779ef69b32b9dd4ee203e2
2019-07-15 12:17:30 -07:00
Tom Cherry
39fafedc5a init: use a property instead of file to communicate cold boot done
Ueventd can't set properties currently, but this is an artificial
limitation, since ueventd communicates to init that it has finished
cold boot via a file, and init polls this file instead of returning to
the epoll loop, where properties are handled.

This change replaces that file with a property and thus frees ueventd
to be able to set properties.

Bug: 62301678
Test: boot, check that properties are set
Change-Id: I985688e9299456efcb2dfeef9b92668991aa9c05
2019-06-10 18:08:01 -07:00
Tom Cherry
bbcbc2ffb3 init: replace Result<Success> with Result<void>
Now that Result<T> is actually expected<T, ...>, and the expected
proposal states expected<void, ...> as the way to indicate an expected
object that returns either successfully with no object or an error,
let's move init's Result<Success> to the preferred Result<void>.

Bug: 132145659
Test: boot, init unit tests
Change-Id: Ib2f98396d8e6e274f95a496fcdfd8341f77585ee
2019-06-10 12:39:18 -07:00
Tom Cherry
59656fb377 init: dump stack when aborting
Dump init stacks when aborting either due to LOG(FATAL) or in
userdebug/eng builds due to signals, including signals from
sanitizers.

Doesn't work for static first stage init yet, b/133450393 tracks
that.

Also, ensure that LOG(FATAL) in child processes calls abort() in all
stages of init, not just 2nd stage init.

Bug: 131747478
Test: abort init in various ways and see stacks
Test: hang or crash in backtrace handler and see child reboot
Change-Id: Ib53b5d3e7e814244203f875de016ada9900dfce8
2019-05-29 08:58:29 -07:00
Jiyong Park
6866041ff0 Proper mount namespace configuration for bionic
This CL fixes the design problem of the previous mechanism for providing
the bootstrap bionic and the runtime bionic to the same path.

Previously, bootstrap bionic was self-bind-mounted; i.e.
/system/bin/libc.so is bind-mounted to itself. And the runtime bionic
was bind-mounted on top of the bootstrap bionic. This has not only caused
problems like `adb sync` not working(b/122737045), but also is quite
difficult to understand due to the double-and-self mounting.

This is the new design:

Most importantly, these four are all distinct:
1) bootstrap bionic (/system/lib/bootstrap/libc.so)
2) runtime bionic (/apex/com.android.runtime/lib/bionic/libc.so)
3) mount point for 1) and 2) (/bionic/lib/libc.so)
4) symlink for 3) (/system/lib/libc.so -> /bionic/lib/libc.so)

Inside the mount namespace of the pre-apexd processes, 1) is
bind-mounted to 3). Likewise, inside the mount namespace of the
post-apexd processes, 2) is bind-mounted to 3). In other words, there is
no self-mount, and no double-mount.

Another change is that mount points are under /bionic and the legacy
paths become symlinks to the mount points. This is to make sure that
there is no bind mounts under /system, which is breaking some apps.

Finally, code for creating mount namespaces, mounting bionic, etc are
refactored to mount_namespace.cpp

Bug: 120266448
Bug: 123275379
Test: m, device boots, adb sync/push/pull works,
especially with following paths:
/bionic/lib64/libc.so
/bionic/bin/linker64
/system/lib64/bootstrap/libc.so
/system/bin/bootstrap/linker64
Change-Id: Icdfbdcc1efca540ac854d4df79e07ee61fca559f
2019-01-30 19:18:22 +09:00
Tom Cherry
d6c2feb9fa init: delete code that sets ro.recovery_id
It looks like this code is dead currently.  From history, this was
meant to be used as a way to check that the recovery image is what was
expected during runtime, but that effort never completed, and we have
full verification of the recovery image when booting into recovery
anyway.

The code is functionally dead as is too, since /recovery doesn't
actually exist in any fstab, since recovery is either mounted as a
ramdisk during recovery or not mounted during normal boot.

Test: boot
Change-Id: I48cd324ef0d5a163db2df2648f6042174b83f10e
2019-01-29 21:17:53 +00:00
Tom Cherry
48e83e629f init: move InitKernelLogging() to first stage init
The kernel opens /dev/console and uses that fd for stdin/stdout/stderr
if there is a serial console enabled and no initramfs, otherwise it
does not provide any fds for stdin/stdout/stderr.  InitKernelLogging()
is used to close these existing fds if they exist and replace them
with /dev/null.

Currently, InitKernelLogging() is only called in second stage init,
which means that processes exec'ed from first stage init will inherit
the kernel provided fds if any are provided.

In the case that they are provided, the exec of second stage init
causes an SELinux denial as it does not have access to /dev/console.
In the case that they are not provided, exec of any further process is
potentially dangerous as the first fd's opened by that process will
take the stdin/stdout/stderr fileno's, which can cause issues if
printf(), etc is then used by that process.

Lastly, simply moving InitKernelLogging() to first stage init is not
enough, since first stage init still runs in kernel context and future
child processes will not have permissions to access kernel context
resources.  Therefore, it must be done for a second time in second
stage init.

Bug: 117281017
Test: no audits when booting marlin.
Change-Id: If27edab5c32b27765e24c32fbed506ef625889de
2018-10-05 10:36:38 -07:00
Tom Cherry
de6bd50d42 init: add host side parser for init
Create a host side parser for init such that init rc files can be
verified for syntax correctness before being used on the device.

Bug: 36970783
Test: run the parser on init files on host

Change-Id: I7e8772e278ebaff727057308596ebacf28b6fdda
2018-02-28 10:45:45 -08:00
Tom Cherry
d8db7ab80d init: replace panic() with LOG(FATAL)
Test: boot bullhead
Test: Introduce LOG(FATAL) at various points of init and ensure that
      it reboots to the bootloader successfully
Test: Introduce LOG(FATAL) during DoReboot() and ensure that it reboots
      instead of recursing infinitely
Test: Ensure that fatal signals reboot to bootloader

Change-Id: I409005b6fab379df2d635e3e33d2df48a1a97df3
2017-08-17 18:16:51 -07:00
Tom Cherry
11a3aeeae3 init: introduce Result<T> for return values and error handling
init tries to propagate error information up to build context before
logging errors.  This is a good thing, however too often init has the
overly verbose paradigm for error handling, below:

bool CalculateResult(const T& input, U* output, std::string* err)

bool CalculateAndUseResult(const T& input, std::string* err) {
  U output;
  std::string calculate_result_err;
  if (!CalculateResult(input, &output, &calculate_result_err)) {
    *err = "CalculateResult " + input + " failed: " +
      calculate_result_err;
      return false;
  }
  UseResult(output);
  return true;
}

Even more common are functions that return only true/false but also
require passing a std::string* err in order to see the error message.

This change introduces a Result<T> that is use to either hold a
successful return value of type T or to hold an error message as a
std::string.  If the functional only returns success or a failure with
an error message, Result<Success> may be used.  The classes Error and
ErrnoError are used to indicate a failed Result<T>.

A successful Result<T> is constructed implicitly from any type that
can be implicitly converted to T or from the constructor arguments for
T.  This allows you to return a type T directly from a function that
returns Result<T>.

Error and ErrnoError are used to construct a Result<T> has
failed. Each of these classes take an ostream as an input and are
implicitly cast to a Result<T> containing that failure.  ErrnoError()
additionally appends ": " + strerror(errno) to the end of  the failure
string to aid in interacting with C APIs.

The end result is that the above code snippet is turned into the much
clearer example below:

Result<U> CalculateResult(const T& input);

Result<Success> CalculateAndUseResult(const T& input) {
  auto output = CalculateResult(input);
  if (!output) {
    return Error() << "CalculateResult " << input << " failed: "
                   << output.error();
  }
  UseResult(*output);
  return Success();
}

This change also makes this conversion for some of the util.cpp
functions that used the old paradigm.

Test: boot bullhead, init unit tests
Merged-In: I1e7d3a8820a79362245041251057fbeed2f7979b
Change-Id: I1e7d3a8820a79362245041251057fbeed2f7979b
2017-08-14 14:07:30 -07:00
Tom Cherry
0c8d6d2730 init: split security functions out of init.cpp
This change splits out the selinux initialization and supporting
functionality into selinux.cpp and splits the security related
initialization of the rng, etc to security.cpp.  It also provides
additional documentation for SEPolicy loading as this has been
requested by some teams.

It additionally cleans up sehandle and sehandle_prop.  The former is
static within selinux.cpp and new wrapper functions are created around
selabel_lookup*() to better serve the users.  The latter is moved to
property_service.cpp as it is isolated to that file for its usage.

Test: boot bullhead
Merged-In: Idc95d493cebc681fbe686b5160502f36af149f60
Change-Id: Idc95d493cebc681fbe686b5160502f36af149f60
2017-08-14 09:40:01 -07:00
Yu Ning
c01022a62e Allow the use of a custom Android DT directory
On platforms that use ACPI instead of Device Tree (DT), such as
Ranchu x86/x86_64, /proc/device-tree/firmware/android/ does not
exist. As a result, Android O is unable to mount /system, etc.
at the first stage of init:

 init: First stage mount skipped (missing/incompatible fstab in
 device tree)

Those platforms may create another directory that mimics the layout
of the standard DT directory in procfs, and store early mount
configuration there. E.g., Ranchu x86/x86_64 creates one in sysfs
using information encoded in the ACPI tables:

 https://android-review.googlesource.com/442472
 https://android-review.googlesource.com/443432
 https://android-review.googlesource.com/442393
 https://android-review.googlesource.com/442395

Therefore, instead of hardcoding the Android DT path, load it from
the kernel command line using a new Android-specific property key
("androidboot.android_dt_dir"). If no such property exists, fall
back to the standard procfs path (so no change is needed for DT-
aware platforms).

Note that init/ and fs_mgr/ each have their own copy of the Android
DT path, because they do not share any global state. A future CL
should remove the duplication by refactoring.

With this CL as well as the above ones, the said warning is gone,
but early mount fails. That is a separate bug, though, and will be
addressed by another CL.

Test: Boot patched sdk_phone_x86-userdebug system image with patched
      Goldfish 3.18 x86 kernel in patched Android Emulator, verify
      the "init: First stage mount skipped" warning no longer shows
      in dmesg.

Change-Id: Ib6df577319503ec1ca778de2b5458cc72ce07415
Signed-off-by: Yu Ning <yu.ning@intel.com>
2017-07-28 11:10:48 +08:00
Tom Cherry
ede0d53850 Move Timer from init to libbase
Test: boot bullhead
Test: new libbase unit tests

Change-Id: Ic398a1daa1fe92c10ea7bc1e6ac3f781cee9a5b5
2017-07-10 09:28:24 -07:00
Tom Cherry
81f5d3ebef init: create android::init:: namespace
With some small fixups along the way

Test: Boot bullhead
Test: init unit tests
Change-Id: I7beaa473cfa9397f845f810557d1631b4a462d6a
2017-06-23 13:21:20 -07:00
Tom Cherry
482f36cf74 init: remove restorecon() from util.cpp
restorecon() has become nothing more than a small wrapper around
selinux_android_restore().  This itself isn't super problematic, but
it is an obstacle for compiling util.cpp on the host as that function
is not available on the host.

Bug: 36970783
Test: Boot bullhead
Merged-In: I7e209ece6898f9a0d5eb9e5d5d8155c2f1ba9faf
Change-Id: I7e209ece6898f9a0d5eb9e5d5d8155c2f1ba9faf
2017-05-09 02:25:32 +00:00
Mark Salyzyn
b066fccc5c init: add "+passcred" for socket to set SO_PASSCRED
In the init scripts for socket, the type can have a suffix of
"+passcred" to request that the socket be bound to report SO_PASSCRED
credentials as part of socket transactions.

Test: gTest logd-unit-tests --gtest_filter=logd.statistics right after boot
      (fails without logd.rc change)
Bug: 37985222
Change-Id: Ie5b50e99fb92fa9bec9a32463a0e6df26a968bfd
2017-05-08 14:04:13 -07:00
Tom Cherry
2cbbe9f7a3 init: do not log directly from read_file() and write_file()
Their callers may be able to add more context, so use an error string
to record the error.

Bug: 38038887
Test: boot bullhead
Test: Init unit tests
Change-Id: I46690d1c66e00a4b15cadc6fd0d6b50e990388c3
2017-05-05 14:37:12 -07:00
Tom Cherry
517e1f17cf init: Check DecodeUid() result and use error string
Check the result of DecodeUid() and return failure when uids/gids are
unable to be decoded.

Also, use an error string instead of logging directly such that more
context can be added when decoding fails.

Bug: 38038887
Test: Boot bullhead
Test: Init unit tests
Change-Id: I84c11aa5a8041bf5d2f754ee9af748344b789b37
2017-05-05 14:37:01 -07:00
Tom Cherry
e7656b7200 ueventd: do not reference init's sehandle
Init exposes a global 'sehandle' that ueventd references as part of
devices.cpp and util.cpp.  This is particularly dangerous in
device_init() in which both uevent and init write to this global.

This change creates a separate local copy for devices.cpp and puts
restrictions on where init.h can be included to make sure the global
used by init is not reference by non-init code.  Future changes to
init should remove this global.

Test: Boot bullhead

Change-Id: Ifefa9e1932e9d647d06cca2618f5c8e5a7a85460
2017-05-01 17:22:49 -07:00
Bowgo Tsai
d262017fef init: moving early mount logic into init_first_stage.cpp
Also renames "early mount" to "first stage mount" to prevent confusion
with "mount_all --early", which is run in the init second stage.

Also creates a base class: FirstStageMount and two derived classes:
FirstStageMountVBootV1 and FirstStageMountVBootV2 to replace/refactor
existing functions:

   - early_mount() -> DoFirstStageMount() and FirstStageMount::DoFirstStageMount()

   - vboot_1_0_early_partitions -> FirstStageMountVBootV1::GetRequiredDevices()
   - vboot_2_0_early_partitions -> FirstStageMountVBootV2::GetRequiredDevices()

   - vboot_1_0_mount_partitions ->
       FirstStageMount::MountPartitions() and
       FirstStageMountVBootV1::SetUpDmVerity()

   - vboot_2_0_mount_partitions ->
       FirstStageMount::MountPartitions() and
       FirstStageMountVBootV2::SetUpDmVerity()

Bug: 37413399
Test: first stage mount /vendor with vboot 2.0 (avb) on bullhead
Test: first stage mount /system with without verity on bullhead
Test: first stage mount /vendor with with vboot 1.0 on sailfish
Change-Id: I6584bdf7d832c9fbc8740f97c9b8b94e68a90783
2017-04-24 23:10:10 +08:00
Tom Cherry
060b74baad ueventd: convert mkdir_recursive() to std::string
Bug: 36250207

Test: Boot bullhead
Test: Boot sailfish, observe no boot time regression
Test: init unit tests

Change-Id: I5a2ac369d846e044230b709fd07eb21ad12d47bb
2017-04-12 16:36:44 -07:00
Tom Cherry
c44f6a4073 ueventd: Write tests for the get_*_symlinks() functions
Bug: 33785894
Bug: 36250207
Test: Boot bullhead + new unit tests
Change-Id: Ia0f290542eb1cffce5ae876dfedb453dde960253
2017-04-05 18:21:39 -07:00
Tom Cherry
53089aa25c init: Use std::string for write_file()
The content parameter of write_file() previously took a char* that was
then converted to a std::string in WriteStringToFd().  One unfortunate
effect of this, is that it is impossible to write data that contains
'\0' within it, as the new string will only contain characters up
until the '\0'.

This changes write_file() to take an std::string, such that
std::string::size() is used to determine the length of the string,
allowing it to contain null characters.

Also change the path parameter of read_file() and write_file() for
consistency.

Lastly, add a test for handling strings with '\0' in them.

Bug: 36726045
Test: Boot bullhead, run unit tests
Change-Id: Idad60e4228ee2de741ab3ab6a4917065b5e63cd8
2017-04-03 16:41:22 -07:00
James Hawkins
e78ea77f69 bootstat: Refactor init/utils/boot_clock into base/chrono_utils.
Use this for bootstat and init. This replaces the custom uptime parser in
bootstat.

This is a reland of aosp/338325 with a stubbed implementation for Darwin.

This change also has clang_format fixes (automatic).

Bug: 34352037
Test: chrono_utils_test
Change-Id: I72a62a3ca1ccfc0a4ccc6294ff1776c263144686
2017-03-30 14:24:12 -07:00
Keun-young Park
8d01f63f50 remove emergency shutdown and improve init's reboot logic
- Emergency shutdown just marks the fs as clean while leaving fs
  in the middle of any state. Do not use it anymore.

- Changed android_reboot to set sys.powerctl property so that
  all shutdown can be done by init.

- Normal reboot sequence changed to
    1. Terminate processes (give time to clean up). And wait for
      completion based on ro.build.shutdown_timeout.
        Default value (when not set) is changed to 3 secs. If it is 0, do not
        terminate processes.
    2. Kill all remaining services except critical services for shutdown.
    3. Shutdown vold using "vdc volume shutdown"
    4. umount all emulated partitions. If it fails, just detach.
       Wait in step 5 can handle it.
    5. Try umounting R/W block devices for up to max timeout.
      If it fails, try DETACH.
      If umount fails to complete before reboot, it can be detected when
      system reboots.
    6. Reboot

- Log shutdown time and umount stat to log so that it can be collected after reboot

- To umount emulated partitions, all pending writes inside kernel should
  be completed.
- To umount /data partition, all emulated partitions on top of /data should
  be umounted and all pending writes should be completed.
- umount retry will only wait up to timeout. If there are too many pending
  writes, reboot will discard them and e2fsck after reboot will fix any file system
  issues.

bug: 36004738
bug: 32246772

Test: many reboots combining reboot from UI and adb reboot. Check last_kmsg and
      fs_stat after reboot.

Change-Id: I6e74d6c68a21e76e08cc0438573d1586fd9aaee2
2017-03-22 11:23:31 -07:00
James Hawkins
c8ac067773 Revert "bootstat: Refactor init/utils/boot_clock into base/chrono_utils."
This reverts commit 7c92e48450.

Mac sdk still broken (despite testing locally).

Change-Id: I7d9206e15997cd0efe081bd3fa17d53d2b20ec32
2017-02-14 19:20:20 +00:00
James Hawkins
7c92e48450 bootstat: Refactor init/utils/boot_clock into base/chrono_utils.
Use this for bootstat and init. This replaces the custom uptime parser in
bootstat.

This is a reland of aosp/332854 with a fix for Darwin.

Bug: 34352037
Test: chrono_utils_test
Change-Id: Ib2567d8df0e460ab59753ac1c053dd7f9f1008a7
2017-02-13 15:47:21 -08:00
James Hawkins
0e3167e203 Revert "bootstat: Remove custom uptime parser in favor of elapsedRealtime."
This reverts commit 26f40c04c3.

This change broke the Darwin SDK target.

Test: none
Change-Id: Ia54fe2c31da8d8fa2825e023b035fb8321dcd457
2017-02-08 14:16:51 -08:00
James Hawkins
26f40c04c3 bootstat: Remove custom uptime parser in favor of elapsedRealtime.
Refactored init/utils/boot_clock into base/chrono_utils.

Bug: 34352037
Test: none
Change-Id: Ied0c00867336b85922369d7ff37520e3d28fc61e
2017-02-07 15:43:32 -08:00
James Hawkins
27c052263c boottime/init: Report ro.boottime.init* properties in milliseconds.
* Nanosecond precision ended up being harder to grok.
* This change modifies the Timer class to have duration_ms instead of
duration_ns.

Bug: 34466121
Test: adb logcat | grep bootstat
Change-Id: Ibd1c27dc3cb29d838a956e342281b2fb98d752a6
2017-01-27 08:26:14 -08:00
Jorge Lucangeli Obes
77f0e9fda8 init: Make 'write_file' return bool to match 'read_file'.
The mismatch of return values makes reasoning about the correctness of
CLs like https://android-review.googlesource.com/317923 quite hard.

Bug: 33941660
Test: Init builds, HiKey boots.
Change-Id: Ia4b8a9af420682997b154a594892740181980921
2016-12-28 14:16:54 -05:00
Mark Salyzyn
978fd0ea25 init: service file command only opens existing files
Mixing open or create, along with attribute(MAC) and permissions(DAC)
is a security and confusion issue.

Fix an issue where fcntl F_SETFD was called to clear O_NONBLOCK, when
it should have been F_SETFL.  Did not present a problem because the
current user of this feature does writes and control messages only.

Test: gTest logd-unit-tests and check dmesg for logd content.
Bug: 32450474
Bug: 33242020
Change-Id: I23cb9a9be5ddb7e8e9c58c79838bc07536e766e6
2016-12-05 11:26:39 -08:00
Elliott Hughes
331cf2fb7c Replace the "coldboot" timeout with a property.
Also rename init's existing boot-time related properties so they're
all "ro.*" properties.

Example result:

  # Three properties showing when init started...
  [ro.boottime.init]: [5294587604]
  # ...how long it waited for ueventd...
  [ro.boottime.init.cold_boot_wait]: [646956470]
  # ...and how long SELinux initialization took...
  [ro.boottime.init.selinux]: [45742921]

  # Plus one property for each service, showing when it first started.
  [ro.boottime.InputEventFind]: [10278767840]
  [ro.boottime.adbd]: [8359267180]
  [ro.boottime.atfwd]: [10338554773]
  [ro.boottime.audioserver]: [10298157478]
  [ro.boottime.bootanim]: [9323670089]
  [ro.boottime.cameraserver]: [10299402321]
  [ro.boottime.cnd]: [10335931856]
  [ro.boottime.debuggerd]: [7001352774]
  [ro.boottime.debuggerd64]: [7002261785]
  [ro.boottime.drm]: [10301082113]
  [ro.boottime.fingerprintd]: [10331443314]
  [ro.boottime.flash-nanohub-fw]: [6995265534]
  [ro.boottime.gatekeeperd]: [10340355242]
  [ro.boottime.healthd]: [7856893380]
  [ro.boottime.hwservicemanager]: [7856051088]
  [ro.boottime.imscmservice]: [10290530758]
  [ro.boottime.imsdatadaemon]: [10358136702]
  [ro.boottime.imsqmidaemon]: [10289084872]
  [ro.boottime.installd]: [10303296020]
  [ro.boottime.irsc_util]: [10279807632]
  [ro.boottime.keystore]: [10305034093]
  [ro.boottime.lmkd]: [7863506714]
  [ro.boottime.loc_launcher]: [10324525241]
  [ro.boottime.logd]: [6526221633]
  [ro.boottime.logd-reinit]: [7850662702]
  [ro.boottime.mcfg-sh]: [10337268315]
  [ro.boottime.media]: [10312152687]
  [ro.boottime.mediacodec]: [10306852530]
  [ro.boottime.mediadrm]: [10308707999]
  [ro.boottime.mediaextractor]: [10310681177]
  [ro.boottime.msm_irqbalance]: [7862451974]
  [ro.boottime.netd]: [10313523104]
  [ro.boottime.netmgrd]: [10285009351]
  [ro.boottime.oem_qmi_server]: [10293329092]
  [ro.boottime.per_mgr]: [7857915776]
  [ro.boottime.per_proxy]: [8335121605]
  [ro.boottime.perfd]: [10283443101]
  [ro.boottime.qcamerasvr]: [10329644772]
  [ro.boottime.qmuxd]: [10282346643]
  [ro.boottime.qseecomd]: [6855708593]
  [ro.boottime.qti]: [10286196851]
  [ro.boottime.ril-daemon]: [10314933677]
  [ro.boottime.rmt_storage]: [7859105047]
  [ro.boottime.servicemanager]: [7864555881]
  [ro.boottime.ss_ramdump]: [8337634938]
  [ro.boottime.ssr_setup]: [8336268324]
  [ro.boottime.surfaceflinger]: [7866921402]
  [ro.boottime.thermal-engine]: [10281249924]
  [ro.boottime.time_daemon]: [10322006542]
  [ro.boottime.ueventd]: [5618663938]
  [ro.boottime.vold]: [7003493920]
  [ro.boottime.wificond]: [10316641073]
  [ro.boottime.wpa_supplicant]: [18959816881]
  [ro.boottime.zygote]: [10295295029]
  [ro.boottime.zygote_secondary]: [10296637269]

Bug: http://b/31800756
Test: boots
Change-Id: I094cce0c1bab9406d950ca94212689dc2e15dba5
2016-12-03 10:54:26 -08:00
Paul Lawrence
a8d8434c42 Add flags to restorecon_recursive to traverse filesystems
Use to solve the problem of tracefs conditionally being mounted
under debugfs and needing restorecon'd without boot performance
penalty.

Also move skip-ce to a flag for consistency.

Test: Check that trace_mount has correct attributes after boot
Bug: 32849675
Change-Id: Ib6731f502b6afc393ea5ada96fa95b339f14da49
2016-11-16 22:27:45 +00:00
Jeff Sharkey
d1d3bdd16f Only restorecon CE storage after unlocked.
On FBE devices, the filenames inside credential-encrypted directories
are mangled until the key is installed.  This means the initial
restorecon at boot needs to skip these directories until the keys
are installed.

This CL changes the implementation of the "restorecon_recursive"
built-in command to use the new SKIPCE flag to avoid labeling files
in CE directories.  vold will request a restorecon when the keys
are actually installed.

(cherrypicked from commit 1635afe83d)

Bug: 30126557
Test: Cherry-picked from master
Change-Id: I320584574a4d712c493b5bbd8a79b56c0c04aa58
2016-11-16 21:19:17 +00:00
Elliott Hughes
9605a945f7 init start time tracking.
With this change, init sets a property "init.start" to show the
CLOCK_BOOTTIME time at which init itself started, and for each service
an "init.svc.<name>.start" property to show the CLOCK_BOOTTIME time at
which that service was most recently started.

These times can be used by tools like bootstat to track boot time.

As part of this change, move init over to std::chrono. Also, rather than
make the command-line argument handling more complex, I've switched to
using an environment variable for communication between first- and
second-stage init, and added another environment variable to pass the
start time of the first stage through to the second stage.

Bug: http://b/32780225
Test: manual
Change-Id: Ia65a623e1866ea688b9a5433d6507926ce301dfe
2016-11-12 11:17:40 -08:00