libpwdgrp: fixes suggested by Tito, comment tweaks

function                                             old     new   delta
bb_internal_getpwent_r                               100     121     +21
parse_common                                         202     203      +1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2015-01-05 15:09:04 +01:00
parent 4bf88d9094
commit c5d4a04e45

View File

@ -9,13 +9,13 @@
* Rewrite of some parts. Main differences are: * Rewrite of some parts. Main differences are:
* *
* 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically * 1) the buffer for getpwuid, getgrgid, getpwnam, getgrnam is dynamically
* allocated and reused by later calls. * allocated.
* If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program * If ENABLE_FEATURE_CLEAN_UP is set the buffers are freed at program
* exit using the atexit function to make valgrind happy. * exit using the atexit function to make valgrind happy.
* 2) the passwd/group files: * 2) the passwd/group files:
* a) must contain the expected number of fields (as per count of field * a) must contain the expected number of fields (as per count of field
* delimeters ":") or we will complain with a error message. * delimeters ":") or we will complain with a error message.
* b) leading or trailing whitespace in fields is stripped. * b) leading and trailing whitespace in fields is stripped.
* c) some fields are not allowed to be empty (e.g. username, uid/gid, * c) some fields are not allowed to be empty (e.g. username, uid/gid,
* homedir, shell) and in this case NULL is returned and errno is * homedir, shell) and in this case NULL is returned and errno is
* set to EINVAL. This behaviour could be easily changed by * set to EINVAL. This behaviour could be easily changed by
@ -23,7 +23,7 @@
* makes a field mandatory). * makes a field mandatory).
* d) the string representing uid/gid must be convertible by strtoXX * d) the string representing uid/gid must be convertible by strtoXX
* functions, or errno is set to EINVAL. * functions, or errno is set to EINVAL.
* e) leading or trailing whitespace in group member names are stripped. * e) leading and trailing whitespace in group member names is stripped.
* 3) the internal function for getgrouplist uses dynamically allocated buffer. * 3) the internal function for getgrouplist uses dynamically allocated buffer.
* 4) at the moment only the functions really used by busybox code are * 4) at the moment only the functions really used by busybox code are
* implemented, if you need a particular missing function it should be * implemented, if you need a particular missing function it should be
@ -34,15 +34,15 @@
struct const_passdb { struct const_passdb {
const char *filename; const char *filename;
const char def[7 + 2*ENABLE_USE_BB_SHADOW]; char def[7 + 2*ENABLE_USE_BB_SHADOW];
const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW]; uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
uint8_t numfields; uint8_t numfields;
uint8_t size_of; uint8_t size_of;
}; };
struct passdb { struct passdb {
const char *filename; const char *filename;
const char def[7 + 2*ENABLE_USE_BB_SHADOW]; char def[7 + 2*ENABLE_USE_BB_SHADOW];
const uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW]; uint8_t off[7 + 2*ENABLE_USE_BB_SHADOW];
uint8_t numfields; uint8_t numfields;
uint8_t size_of; uint8_t size_of;
FILE *fp; FILE *fp;
@ -105,7 +105,7 @@ static const struct const_passdb const_sp_db = {
/* We avoid having big global data. */ /* We avoid having big global data. */
struct statics { struct statics {
/* It's ok to use same buffer (db[0].malloced) for getpwuid and getpwnam. /* We use same buffer (db[0].malloced) for getpwuid and getpwnam.
* Manpage says: * Manpage says:
* "The return value may point to a static area, and may be overwritten * "The return value may point to a static area, and may be overwritten
* by subsequent calls to getpwent(), getpwnam(), or getpwuid()." * by subsequent calls to getpwent(), getpwnam(), or getpwuid()."
@ -203,7 +203,7 @@ static char *parse_common(FILE *fp, struct passdb *db,
goto free_and_next; goto free_and_next;
} }
if (!key) { if (field_pos == -1) {
/* no key specified: sequential read, return a record */ /* no key specified: sequential read, return a record */
break; break;
} }
@ -292,7 +292,6 @@ static void *convert_to_struct(struct passdb *db,
( ((intptr_t)S.tokenize_end + sizeof(members[0])) ( ((intptr_t)S.tokenize_end + sizeof(members[0]))
& -(intptr_t)sizeof(members[0]) & -(intptr_t)sizeof(members[0])
); );
((struct group *)result)->gr_mem = members; ((struct group *)result)->gr_mem = members;
while (--i >= 0) { while (--i >= 0) {
if (buffer[0]) { if (buffer[0]) {
@ -391,7 +390,9 @@ static int FAST_FUNC getXXent_r(uintptr_t db_idx, char *buffer, size_t buflen,
close_on_exec_on(fileno(db->fp)); close_on_exec_on(fileno(db->fp));
} }
buf = parse_common(db->fp, db, /*no search key:*/ NULL, 0); buf = parse_common(db->fp, db, /*no search key:*/ NULL, -1);
if (!buf && !errno)
errno = ENOENT;
return massage_data_for_r_func(db, buffer, buflen, result, buf); return massage_data_for_r_func(db, buffer, buflen, result, buf);
} }
@ -493,7 +494,7 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
if (fp) { if (fp) {
struct passdb *db = &get_S()->db[1]; struct passdb *db = &get_S()->db[1];
char *buf; char *buf;
while ((buf = parse_common(fp, db, NULL, 0)) != NULL) { while ((buf = parse_common(fp, db, NULL, -1)) != NULL) {
char **m; char **m;
struct group group; struct group group;
if (!convert_to_struct(db, buf, &group)) if (!convert_to_struct(db, buf, &group))
@ -505,7 +506,7 @@ static gid_t* FAST_FUNC getgrouplist_internal(int *ngroups_ptr,
continue; continue;
group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups); group_list = xrealloc_vector(group_list, /*8=2^3:*/ 3, ngroups);
group_list[ngroups++] = group.gr_gid; group_list[ngroups++] = group.gr_gid;
break; goto next;
} }
next: next:
free(buf); free(buf);