Turns out that although there's no <endian.h> or <sys/endian.h>, there
are <machine/endian.h> and <sys/_endian.h>, and they're included by
other system headers such as <dirent.h>.
Reuse the contents of <sys/_endian.h> here for better interop.
Bug: http://b/139203733
Test: treehugger
Change-Id: Ic0e9bfa1a5b56d05e9e542839d237b6ceae4aa8c
It helps to fix a compilation issue with host libc:
core/base/errors_unix.cpp:25:10: error: use of undeclared identifier 'strerror'
return strerror(error_code);
^
Change-Id: I71d4221cc1bfa6f0cc764b40b6b28159994b8e04
While I'm here, I'll fix how unique_fd disallows copy and assignment
constructors (detele instead of marking them private).
Bug: 135918744
Test: WiP change in master
Change-Id: Idefcc685943326c511f59d18790c1c4fa2e04989
These move and assignment operations are conditionally noexcept, so
add that to their definitions. This is a performance issue as well as
correctness, since std containers will copy instead of move during
resize if these operations are not noexcept.
Test: build
Change-Id: I148f7eb3489e7f1dd68cc0fb0e555b56470e42da
clang-tidy flagged the anonymous namespace in the header as a warning,
but in any case, it'll be cleaner to implement this in terms of the
existing type traits.
Test: build
Change-Id: I189986d2a855c028e28dd9d62ab9da012feddc9b
android-base:
* Add NOLINT for expanding namespace std for std::string* ostream
overload
libdm:
* Fix missing parentesis around macro parameters
init:
* Fix missing CLOEXEC usage and add NOLINT for the intended
usages.
* Fix missing parentesis around macro parameters
* Fix erase() / remove_if() idiom
* Correctly specific unsigned char when intended
* 'namespace flags' should be signed, since 'flags' it signed for
clone()
* Add clear to property restore vector<string> to empty after move
* Explicit comparison against 0 for strcmp
Test: build
Change-Id: I8c31dafda2c43ebc5aa50124cbbd6e23ed2c4101
We loop over /proc to iterate through pids in a lot of code, so let's
consolidate this into a single function in libbase.
Test: new unit test
Change-Id: I908fab90b603546d0e3e8b8acdc8dadfc3552d62
Bug: 135683564
Test: Included expected.h from two different files. Builds succesfully
with the fix. Does not build without the fix.
Change-Id: If1962a3b70f7580fa652a043aaf29bf592d1926c
We're already including fmtlib statically in libbase, so let's also
expose its functionality directly.
Also create a small benchmark of the most common operations for
format() and StringPrintf(): int, float, and string arguments. Note
that fmt::format is faster than StringPrintf() in all of these cases.
Test: build, run benchmark
Change-Id: I1e18f13673dd89ea936ea5c51418fad723495b08
String formatting for error message can be done by using Errorf and
ErrnoErrorf. e.g. `return Errorf("{} failures", num_failure);`
The string format follows that of fmtlib, not printf.
Bug: 132145659
Test: libbase_test
Change-Id: I65f45d5f12ed23e2c14d6eb442278f467cc42e83
It is typical to pass error to callers like following;
if (!result) {
return Error() << result.error();
}
To transfer errno(or ResultError#code()), Error defines a specialization
operator<<(const ResultError&).
This change fixes so that ResultError is properly handled
Bug: 132145659
Test: atest libbase_test
Change-Id: Ib35457da2d4b923d8e652c54ac510a75546cf918
I was looking for 'MmapFile' earlier and couldn't find it. After
being pointed to it, we thought that adding this synonym would help
others trying to find this file.
Test: n/a
Change-Id: I7fbfd8e090f21183a5de3603a76f884562b99c4a
The Result, Error, ErrnoError are quite generic. Moving them from init
to libbase so that they can be used from other places.
Bug: 132145659
Test: libbase_test
Change-Id: Id774a587f74380fadd7a0fc88c0aa892c3d9a489
As of Android-P, __system_property_set() is synchronous and only will
return '0' if the property was successfully set.
Test: n/a
Change-Id: Icbc0a6d8be1ae683565f378c534ea8e5c5f25357
This is a follow-up change of 7d89fb164b.
- Some of the missing conditions for SFNAIE were added
- Fixed indentation
- The assignment operator became "= default"
Bug: 132145659
Test: libbase_test
Change-Id: Ib5232a6e5e1d3df67e185d6e8c03374105c1ce94
Memory mapping the central directory of specific APKs caused memory
mappings to build up over time because they were never unmapped
correctly. This is because MappedFile is not calling munmap with the
size of the data after aligning the mmap offset to a page boundary.
Bug: 133463863
Test: install APKs and verify that the entire mapped CD is unmaped
Test: ran aapt2 as daemon and confirmed that mapped CD is unmapped
Change-Id: I26bda27c83615d1a166d92e43a2327909c453b89
android::base::expected is an Android implementation of the
std::expected proposal.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0323r7.html
For usage, refer to the expected.h header file and
expected_test.cpp
Bug: 132145659
Test: libbase_test
Change-Id: I65d3a1ecf8654d9858989755dfd0065c81f7b209
The original commit broke aosp-master-with-phones, because of
vendor libraries the depended on the int versions of libbase functions.
This patch reverts the revert, and also adds ABI-compatibility shims for
the replaced functions.
This reverts commit 2c58e1924a.
Bug: http://b/131312539
Test: treehugger
Test: forrest run of aosp-master-with-phones
Change-Id: I75cc84ec8d963e20862f7662e8e2f409471f41cc
Enable native bridge support for libbase, liblog,
libziparchive and libpropertyinfoparser.
This makes it possible to use them in binaries for translated
architectures.
Bug: http://b/77159578
Test: make
Change-Id: If67ce92288b17a052ea1e79a268e284f7d941439
adb was already using ConsumePrefix, and now we have another would-be
user in cutils. (There appears to be one place in adb that should use
ConsumeSuffix, so I'm assuming we'll want that sooner or later.)
I've kept these inline because adb and google3's versions both were, and
I'm easily led.
Test: treehugger
Change-Id: I29d99032f6f6ccbfaefece59725db8afb02a4c87
This reverts commit 93d63c010a.
Reason for revert: b/131312539 - All aosp-master-with-phones phones failing boot / health check.
Change-Id: Id4239f93484b7d6b8d9bcd355a59cbd40d9766e2
unique_fd's implicit conversion to int has led to tons of problems (see
all of the overloads for close, fdopen, fdopendir, etc.). Add a switch
that can turn it off, and reduce the ridiculous amount of work to fix up
callers by introducing a borrowed_fd type that can be constructed from
either int or unique_fd.
Test: treehugger
Change-Id: If77cf5cbcaddacdaec5919a15b3520fb68f51a62
This catches a common mistake where client code checks for errors using
the common idiom that works for std::iostream and other file-like
classes:
unique_fd fd = open(...);
if (!fd) {
}
Test: atest libbase_test
Test: m droid
Change-Id: I9629a7795537ecb3b57be9c741c06f80967e4cc2
* changes:
libbase: realpath is wrapped with TEMP_FAILURE_RETRY
liblp: Replace open with GetControlFileOrOpen
init: expand prop in 'file'
libcutils: android_get_control_file uses realpath.
Although man page for realpath doesn't say so, the bionic
implementation of realpath may exit with error code
EINTR. In such cases, retry.
Test: boots (sanity)
Change-Id: Ic5feb7152374a81bd2f475ad74c4bc8c3afb3a20
Bug: http://b/119818070 "app crashes when reading asset of zero length"
Add support for zero-length mappings for the Windows code path as well.
Test: ran libbase_test on Windows under wine.
Change-Id: Iccb65fa800c636444100c9369f41e36d24a53a99
Silence narrowing warnings by manually casting to a smaller type,
convert static_asserts to CHECKs, and switch from using MSG_CMSG_CLOEXEC
to manually setting FD_CLOEXEC.
Test: mma
Change-Id: I8aa02804179d754de860279fe34773800b937ab6
Almost all of the uses of cmsg(3) in our source tree are wrong in at
least one of the following ways:
- not aligning the cmsg buffer
- leaking fds if more fds are received than expected
- blindly dereferencing CMSG_DATA without checking the header
- using CMSG_SPACE instead of CMSG_LEN for .cmsg_len
- using CMSG_LEN instead of CMSG_SPACE for .msg_controllen
- using a length specified in number of fds instead of bytes
Implement wrapper functions that implement this dumpster fire of an
API correctly.
Bug: http://b/122047630
Test: libbase_test32
Change-Id: I6ac34d67bbbf1bfa9727ab598248fc178ea19df9
This is a useful helper for anyone that's using thread safety
annotations alongside std::condition_variable, extract it from adb and
move it to libbase.
Test: mma
Change-Id: Ic51e2f2a0e6a16628034b29d8ff32bf2155afccd
unique_fd's destructor potentially mangling errno makes it difficult to
use correctly in code that sets errno (or, in reality, it makes it so
that errno values get randomly stomped upon if close actually sets
errno, because no one accounts for this case).
Preserve errno ourselves to avoid this.
Test: treehugger
Change-Id: Ib06e6f65866d86fff4032b2311021eaf9226a1af
sehr points out that we already have two bugs caused by not doing this:
file.cpp has a call to pread with an off64_t that gets truncated, and
mapped_file.cpp does similar with mmap.
Bug: N/A
Test: ran tests
Change-Id: Ia098a836457e5adf109541235ad71c0ad7219da8
Currently bionic only stores the first invocation of
android_set_abort_message, libbase splits the logging invocations
into discrete lines, and liblog automatically calls bionic when
fatal severity is used. This leads to only the first line of
LOG(FATAL) being stored for tombstoned.
Eagerly set the abort message directly before logging when the
severity is FATAL. This ensures the complete message will be
available.
Bug: 120506942
Test: m
Test: manual
Change-Id: I104d6960a2b1f66f21f5ada383fb4ab0f35e96a9