Skip to content

Commit 314c2ef

Browse files
committed
utils: refactor set_home_env
Use the return value from fgetpwent_r as error value. Reference: https://man7.org/linux/man-pages/man3/fgetpwent_r.3.html#RETURN_VALUE Remove ENOTSUP check as it is not needed since containers@6ed7bab Closes: containers#2010 Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
1 parent 94fda0b commit 314c2ef

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/libcrun/container.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ setup_environment (runtime_spec_schema_config_schema *def, uid_t container_uid,
11861186
if (getenv ("HOME") == NULL)
11871187
{
11881188
ret = set_home_env (container_uid);
1189-
if (UNLIKELY (ret < 0 && errno != ENOTSUP))
1189+
if (UNLIKELY (ret < 0))
11901190
{
11911191
setenv ("HOME", "/", 1);
11921192
libcrun_warning ("cannot detect HOME environment variable, setting default");
@@ -3689,7 +3689,7 @@ exec_process_entrypoint (libcrun_context_t *context,
36893689
if (getenv ("HOME") == NULL)
36903690
{
36913691
ret = set_home_env (container_uid);
3692-
if (UNLIKELY (ret < 0 && errno != ENOTSUP))
3692+
if (UNLIKELY (ret < 0))
36933693
{
36943694
setenv ("HOME", "/", 1);
36953695
libcrun_warning ("cannot detect HOME environment variable, setting default");

src/libcrun/utils.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,6 @@ set_home_env (uid_t id)
14471447
cleanup_free char *buf = NULL;
14481448
long buf_size;
14491449
cleanup_file FILE *stream = NULL;
1450-
int ret = -1;
14511450

14521451
buf_size = sysconf (_SC_GETPW_R_SIZE_MAX);
14531452
if (buf_size < 0)
@@ -1457,17 +1456,21 @@ set_home_env (uid_t id)
14571456

14581457
stream = fopen ("/etc/passwd", "re");
14591458
if (stream == NULL)
1460-
goto error;
1459+
return -1;
14611460

14621461
for (;;)
14631462
{
14641463
struct passwd *ret_pw = NULL;
14651464

1466-
ret = fgetpwent_r (stream, &pwd, buf, buf_size, &ret_pw);
1465+
int ret = fgetpwent_r (stream, &pwd, buf, buf_size, &ret_pw);
14671466
if (UNLIKELY (ret != 0))
14681467
{
1469-
if (errno != ERANGE)
1470-
goto error;
1468+
if (ret != ERANGE)
1469+
{
1470+
/* Let callers handle the error if the user was not found. */
1471+
errno = ret;
1472+
return -1;
1473+
}
14711474

14721475
buf_size *= 2;
14731476
buf = xrealloc (buf, buf_size);
@@ -1480,10 +1483,6 @@ set_home_env (uid_t id)
14801483
return 0;
14811484
}
14821485
}
1483-
1484-
error:
1485-
/* Let callers handle the error if the user was not found. */
1486-
return ret ? -errno : 0;
14871486
}
14881487

14891488
/*if subuid or subgid exist, take the first range for the user */

0 commit comments

Comments
 (0)