diff options
author | The Android Open Source Project <initial-contribution@android.com> | 2009-02-10 15:43:56 -0800 |
---|---|---|
committer | The Android Open Source Project <initial-contribution@android.com> | 2009-02-10 15:43:56 -0800 |
commit | 9f65adf2ba3bb15feb8b7a7b3eef788df3fd270e (patch) | |
tree | c06064fc9022ef63a40f83a91292103784f49780 /libc/bionic/stubs.c | |
parent | d37527501c85edcb3a6a7c8a0b6297d52d434897 (diff) | |
download | bionic-9f65adf2ba3bb15feb8b7a7b3eef788df3fd270e.zip bionic-9f65adf2ba3bb15feb8b7a7b3eef788df3fd270e.tar.gz bionic-9f65adf2ba3bb15feb8b7a7b3eef788df3fd270e.tar.bz2 |
auto import from //branches/cupcake/...@130745
Diffstat (limited to 'libc/bionic/stubs.c')
-rw-r--r-- | libc/bionic/stubs.c | 172 |
1 files changed, 120 insertions, 52 deletions
diff --git a/libc/bionic/stubs.c b/libc/bionic/stubs.c index 1f76bba..365f21a 100644 --- a/libc/bionic/stubs.c +++ b/libc/bionic/stubs.c @@ -35,6 +35,7 @@ #include <pthread.h> #include <stdlib.h> #include <errno.h> +#include <ctype.h> /** Thread-specific state for the stubs functions **/ @@ -95,8 +96,9 @@ __stubs_state(void) return s; } -static struct passwd *android_iinfo_to_passwd( - struct passwd *pw, struct android_id_info *iinfo) +static struct passwd* +android_iinfo_to_passwd( struct passwd *pw, + struct android_id_info *iinfo ) { pw->pw_name = (char*)iinfo->name; pw->pw_uid = iinfo->aid; @@ -106,8 +108,9 @@ static struct passwd *android_iinfo_to_passwd( return pw; } -static struct group *android_iinfo_to_group( - struct group *gr, struct android_id_info *iinfo) +static struct group* +android_iinfo_to_group( struct group *gr, + struct android_id_info *iinfo ) { gr->gr_name = (char*) iinfo->name; gr->gr_gid = iinfo->aid; @@ -116,8 +119,8 @@ static struct group *android_iinfo_to_group( return gr; } -static struct passwd *android_id_to_passwd( - struct passwd *pw, unsigned id) +static struct passwd * +android_id_to_passwd( struct passwd *pw, unsigned id) { struct android_id_info *iinfo = android_ids; unsigned n; @@ -126,11 +129,11 @@ static struct passwd *android_id_to_passwd( return android_iinfo_to_passwd(pw, iinfo + n); } } - return 0; + return NULL; } -static struct passwd *android_name_to_passwd( - struct passwd *pw, const char *name) +static struct passwd* +android_name_to_passwd(struct passwd *pw, const char *name) { struct android_id_info *iinfo = android_ids; unsigned n; @@ -139,11 +142,11 @@ static struct passwd *android_name_to_passwd( return android_iinfo_to_passwd(pw, iinfo + n); } } - return 0; + return NULL; } -static struct group *android_id_to_group( - struct group *gr, unsigned id) +static struct group* +android_id_to_group( struct group *gr, unsigned id ) { struct android_id_info *iinfo = android_ids; unsigned n; @@ -152,11 +155,11 @@ static struct group *android_id_to_group( return android_iinfo_to_group(gr, iinfo + n); } } - return 0; + return NULL; } -static struct group *android_name_to_group( - struct group *gr, const char *name) +static struct group* +android_name_to_group( struct group *gr, const char *name ) { struct android_id_info *iinfo = android_ids; unsigned n; @@ -165,21 +168,47 @@ static struct group *android_name_to_group( return android_iinfo_to_group(gr, iinfo + n); } } - return 0; + return NULL; } -struct passwd* getpwuid(uid_t uid) +/* translate a user/group name like app_1234 into the + * corresponding user/group id (AID_APP + 1234) + * returns 0 and sets errno to ENOENT in case of error + */ +static unsigned +app_id_from_name( const char* name ) { - stubs_state_t* state = __stubs_state(); - struct passwd* pw; + unsigned long id; + char* end; - if (state == NULL) - return NULL; + if (memcmp(name, "app_", 4) != 0 || !isdigit(name[4])) + goto FAIL; - pw = &state->passwd; + id = strtoul(name+4, &end, 10); + if (id == 0 || *end != '\0') + goto FAIL; - if ( android_id_to_passwd(pw, uid) != NULL ) - return pw; + id += AID_APP; + + /* check for overflow and that the value can be + * stored in our 32-bit uid_t/gid_t */ + if (id < AID_APP || (unsigned)id != id) + goto FAIL; + + return (unsigned)id; + +FAIL: + errno = ENOENT; + return 0; +} + +/* translate a uid into the corresponding app_<uid> + * passwd structure (sets errno to ENOENT on failure) + */ +static struct passwd* +app_id_to_passwd(uid_t uid, stubs_state_t* state) +{ + struct passwd* pw = &state->passwd; if (uid < AID_APP) { errno = ENOENT; @@ -187,7 +216,7 @@ struct passwd* getpwuid(uid_t uid) } snprintf( state->app_name_buffer, sizeof state->app_name_buffer, - "app_%d", uid - AID_APP ); + "app_%u", uid - AID_APP ); pw->pw_name = state->app_name_buffer; pw->pw_dir = "/data"; @@ -198,18 +227,66 @@ struct passwd* getpwuid(uid_t uid) return pw; } -struct passwd* getpwnam(const char *login) +/* translate a gid into the corresponding app_<gid> + * group structure (sets errno to ENOENT on failure) + */ +static struct group* +app_id_to_group(gid_t gid, stubs_state_t* state) +{ + struct group* gr = &state->group; + + if (gid < AID_APP) { + errno = ENOENT; + return NULL; + } + + snprintf(state->group_name_buffer, sizeof state->group_name_buffer, + "app_%u", gid - AID_APP); + + gr->gr_name = state->group_name_buffer; + gr->gr_gid = gid; + gr->gr_mem[0] = gr->gr_name; + gr->gr_mem[1] = NULL; + + return gr; +} + + +struct passwd* +getpwuid(uid_t uid) +{ + stubs_state_t* state = __stubs_state(); + struct passwd* pw; + + if (state == NULL) + return NULL; + + pw = &state->passwd; + + if ( android_id_to_passwd(pw, uid) != NULL ) + return pw; + + return app_id_to_passwd(uid, state); +} + +struct passwd* +getpwnam(const char *login) { stubs_state_t* state = __stubs_state(); if (state == NULL) return NULL; - return android_name_to_passwd(&state->passwd, login); + if (android_name_to_passwd(&state->passwd, login) != NULL) + return &state->passwd; + + return app_id_to_passwd( app_id_from_name(login), state ); } -int getgrouplist (const char *user, gid_t group, - gid_t *groups, int *ngroups) { +int +getgrouplist (const char *user, gid_t group, + gid_t *groups, int *ngroups) +{ if (*ngroups < 1) { *ngroups = 1; return -1; @@ -218,18 +295,20 @@ int getgrouplist (const char *user, gid_t group, return (*ngroups = 1); } -char* getlogin(void) +char* +getlogin(void) { struct passwd *pw = getpwuid(getuid()); if(pw) { return pw->pw_name; } else { - return 0; + return NULL; } } -struct group* getgrgid(gid_t gid) +struct group* +getgrgid(gid_t gid) { stubs_state_t* state = __stubs_state(); struct group* gr; @@ -241,34 +320,25 @@ struct group* getgrgid(gid_t gid) if (gr != NULL) return gr; - if (gid < AID_APP) { - errno = ENOENT; - return NULL; - } - - snprintf(state->group_name_buffer, sizeof state->group_name_buffer, - "app_%d", gid - AID_APP); - - gr = &state->group; - - gr->gr_name = state->group_name_buffer; - gr->gr_gid = gid; - gr->gr_mem[0] = gr->gr_name; - gr->gr_mem[1] = NULL; - - return gr; + return app_id_to_group(gid, state); } -struct group* getgrnam(const char *name) +struct group* +getgrnam(const char *name) { stubs_state_t* state = __stubs_state(); + unsigned id; if (state == NULL) return NULL; - return android_name_to_group(&state->group, name); + if (android_name_to_group(&state->group, name) != 0) + return &state->group; + + return app_id_to_group( app_id_from_name(name), state ); } + struct netent* getnetbyname(const char *name) { fprintf(stderr, "FIX ME! implement getgrnam() %s:%d\n", __FILE__, __LINE__); @@ -308,5 +378,3 @@ struct protoent *getprotobynumber(int proto) fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__); return NULL; } - - |