getgrnam, getgrgid, getpwnam, getpwuid for host

The functions now read /etc/group and /etc/passwd on host machines.
Android-specific IDs are not recognized in the host.

getpwent and getgrent are still not working though. They require a
bigger refactoring to sequentially advance in the database files.

Bug: 171718702
Test: run assemble_cvd in aosp_cf_arm64_phone
Change-Id: Ie8da382a467bbd0bffac7b4b8592cd871db80181
This commit is contained in:
Jiyong Park 2020-10-27 03:23:02 +09:00
parent c3ecfb623f
commit bf383283d9
4 changed files with 78 additions and 12 deletions

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2008 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(__ANDROID__)
#error "This file is for host only"
#endif
struct android_id_info {
const char name[17];
unsigned aid;
};
static const struct android_id_info android_ids[] = {};
#define android_id_count 0

View File

@ -46,24 +46,30 @@
#include "private/android_filesystem_config.h"
#include "platform/bionic/macros.h"
#if defined(__ANDROID__)
// Generated android_ids array
#include "generated_android_ids.h"
#else
// Empty array for host; everything is from the database files
#include "empty_android_ids.h"
#endif
#include "grp_pwd_file.h"
static PasswdFile passwd_files[] = {
{ "/system/etc/passwd", "system_" },
{ "/vendor/etc/passwd", "vendor_" },
{ "/odm/etc/passwd", "odm_" },
{ "/product/etc/passwd", "product_" },
{ "/system_ext/etc/passwd", "system_ext_" },
{"/etc/passwd", "system_"}, // symlinks to /system/etc/passwd in Android
{"/vendor/etc/passwd", "vendor_"},
{"/odm/etc/passwd", "odm_"},
{"/product/etc/passwd", "product_"},
{"/system_ext/etc/passwd", "system_ext_"},
};
static GroupFile group_files[] = {
{ "/system/etc/group", "system_" },
{ "/vendor/etc/group", "vendor_" },
{ "/odm/etc/group", "odm_" },
{ "/product/etc/group", "product_" },
{ "/system_ext/etc/group", "system_ext_" },
{"/etc/group", "system_"}, // symlinks to /system/etc/group in Android
{"/vendor/etc/group", "vendor_"},
{"/odm/etc/group", "odm_"},
{"/product/etc/group", "product_"},
{"/system_ext/etc/group", "system_ext_"},
};
// POSIX seems to envisage an implementation where the <pwd.h> functions are
@ -194,6 +200,7 @@ static bool platform_id_secondary_user_allowed(id_t id) {
return false;
}
#if defined(__ANDROID__)
static bool is_valid_app_id(id_t id, bool is_group) {
id_t appid = id % AID_USER_OFFSET;
@ -226,6 +233,12 @@ static bool is_valid_app_id(id_t id, bool is_group) {
return false;
}
#else
static bool is_valid_app_id(id_t, bool) {
// Host doesn't have the concept of app_id
return false;
}
#endif // if defined(__ANDROID__)
// This provides an iterater for app_ids within the first user's app id's.
static id_t get_next_app_id(id_t current_id, bool is_group) {
@ -386,6 +399,7 @@ static void print_app_name_from_gid(const gid_t gid, char* buffer, const int buf
}
}
#if defined(__ANDROID__)
static bool device_launched_before_api_29() {
// Check if ro.product.first_api_level is set to a value > 0 and < 29, if so, this device was
// launched before API 29 (Q). Any other value is considered to be either in development or
@ -420,6 +434,12 @@ static bool is_oem_id(id_t id) {
return (id >= AID_OEM_RESERVED_START && id <= AID_OEM_RESERVED_END) ||
(id >= AID_OEM_RESERVED_2_START && id <= AID_OEM_RESERVED_2_END);
}
#else
static bool is_oem_id(id_t) {
// no OEM ids in host
return false;
}
#endif // if defined(__ANDROID__)
// Translate an OEM name to the corresponding user/group id.
static id_t oem_id_from_name(const char* name) {
@ -522,7 +542,7 @@ passwd* getpwuid_internal(uid_t uid, passwd_state_t* state) {
return android_iinfo_to_passwd(state, android_id_info);
}
// Handle OEM range.
// Find an entry from the database file
passwd* pw = oem_id_to_passwd(uid, state);
if (pw != nullptr) {
return pw;
@ -540,6 +560,7 @@ passwd* getpwnam_internal(const char* login, passwd_state_t* state) {
return android_iinfo_to_passwd(state, android_id_info);
}
// Find an entry from the database file
for (auto& passwd_file : passwd_files) {
if (passwd_file.FindByName(login, state)) {
return &state->passwd_;
@ -681,7 +702,7 @@ static group* getgrgid_internal(gid_t gid, group_state_t* state) {
return android_iinfo_to_group(state, android_id_info);
}
// Handle OEM range.
// Find an entry from the database file
group* grp = oem_id_to_group(gid, state);
if (grp != nullptr) {
return grp;
@ -699,6 +720,7 @@ static group* getgrnam_internal(const char* name, group_state_t* state) {
return android_iinfo_to_group(state, android_id_info);
}
// Find an entry from the database file
for (auto& group_file : group_files) {
if (group_file.FindByName(name, state)) {
return &state->group_;

View File

@ -268,6 +268,7 @@ bool MmapFile::Find(Line* line, Predicate predicate) {
while (line_beginning < end) {
line_beginning = ParseLine(line_beginning, end, line->fields, line->kNumFields);
#if defined(__ANDROID__)
// To comply with Treble, users/groups from each partition need to be prefixed with
// the partition name.
if (required_prefix_ != nullptr) {
@ -280,6 +281,7 @@ bool MmapFile::Find(Line* line, Predicate predicate) {
continue;
}
}
#endif
if (predicate(line)) return true;
}

View File

@ -65,7 +65,10 @@ class MmapFile {
const char* filename_ = nullptr;
const char* start_ = nullptr;
const char* end_ = nullptr;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-private-field"
const char* required_prefix_;
#pragma clang diagnostic pop
};
class PasswdFile {