From 0dbfbe63958890e3ffc814c49fe8033892095b11 Mon Sep 17 00:00:00 2001 From: apio Date: Thu, 18 May 2023 21:47:46 +0200 Subject: [PATCH] libc+apps: Avoid calling endpwent() and endgrent() after every call to get{pw,gr}{nam,uid,gid} --- apps/chown.cpp | 4 ++++ apps/sh.cpp | 1 + apps/su.cpp | 2 ++ libc/src/grp.cpp | 16 ++++------------ libc/src/pwd.cpp | 16 ++++------------ 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/apps/chown.cpp b/apps/chown.cpp index b856c0dc..c1f258a5 100644 --- a/apps/chown.cpp +++ b/apps/chown.cpp @@ -40,6 +40,8 @@ Result luna_main(int argc, char** argv) } gid = grp->gr_gid; + + endgrent(); } } } @@ -73,6 +75,8 @@ Result luna_main(int argc, char** argv) gid = pw->pw_gid; } + + endpwent(); } } diff --git a/apps/sh.cpp b/apps/sh.cpp index 2791465f..51ff4854 100644 --- a/apps/sh.cpp +++ b/apps/sh.cpp @@ -74,6 +74,7 @@ Result luna_main(int argc, char** argv) struct passwd* pw = getpwuid(getuid()); if (pw) { username = pw->pw_name; } else { username = getenv("USER"); } + endpwent(); } while (1) diff --git a/apps/su.cpp b/apps/su.cpp index 0696311b..2109a663 100644 --- a/apps/su.cpp +++ b/apps/su.cpp @@ -78,6 +78,8 @@ Result luna_main(int argc, char** argv) return 1; } + endpwent(); + if ((prompt_password || getuid() != geteuid()) && *entry->pw_passwd) { char* pass = getpass(); diff --git a/libc/src/grp.cpp b/libc/src/grp.cpp index 37efaa3e..d266d435 100644 --- a/libc/src/grp.cpp +++ b/libc/src/grp.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -20,6 +21,7 @@ extern "C" { f = fopen("/etc/group", "r"); if (!f) return nullptr; + fcntl(fileno(f), F_SETFD, FD_CLOEXEC); } while (true) @@ -82,14 +84,9 @@ extern "C" while ((entry = getgrent())) { - if (!strcmp(entry->gr_name, name)) - { - endgrent(); - return entry; - } + if (!strcmp(entry->gr_name, name)) { return entry; } } - endgrent(); return entry; } @@ -101,14 +98,9 @@ extern "C" while ((entry = getgrent())) { - if (entry->gr_gid == gid) - { - endgrent(); - return entry; - } + if (entry->gr_gid == gid) { return entry; } } - endgrent(); return entry; } diff --git a/libc/src/pwd.cpp b/libc/src/pwd.cpp index 80a0bae7..4c61861f 100644 --- a/libc/src/pwd.cpp +++ b/libc/src/pwd.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -45,6 +46,7 @@ extern "C" { f = fopen("/etc/passwd", "r"); if (!f) return nullptr; + fcntl(fileno(f), F_SETFD, FD_CLOEXEC); } while (true) @@ -93,14 +95,9 @@ extern "C" while ((entry = getpwent())) { - if (!strcmp(entry->pw_name, name)) - { - endpwent(); - return entry; - } + if (!strcmp(entry->pw_name, name)) { return entry; } } - endpwent(); return entry; } @@ -112,14 +109,9 @@ extern "C" while ((entry = getpwent())) { - if (entry->pw_uid == uid) - { - endpwent(); - return entry; - } + if (entry->pw_uid == uid) { return entry; } } - endpwent(); return entry; }