1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:37:43 +00:00

LibC: Use AK::String-backed buffers instead of static buffers

Also, refactor the hell out of pwd.cpp & grp.cpp
This commit is contained in:
Sergey Bugaev 2020-08-25 17:21:45 +03:00 committed by Andreas Kling
parent 5fd88e51c5
commit 17109a3a31
4 changed files with 192 additions and 189 deletions

View file

@ -26,54 +26,51 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <errno_numbers.h>
#include <grp.h> #include <grp.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
extern "C" { extern "C" {
#define GRDB_STR_MAX_LEN 256 static FILE* s_stream = nullptr;
static unsigned s_line_number = 0;
static struct group s_group;
struct group_with_strings : public group { static String s_name;
char name_buffer[GRDB_STR_MAX_LEN]; static String s_passwd;
char passwd_buffer[GRDB_STR_MAX_LEN]; static Vector<String> s_members;
char* members[32]; static Vector<const char*> s_members_ptrs;
char members_buffer[32][32];
};
static FILE* __grdb_stream = nullptr;
static unsigned __grdb_line_number = 0;
static struct group_with_strings* __grdb_entry = nullptr;
void setgrent() void setgrent()
{ {
__grdb_line_number = 0; s_line_number = 0;
if (__grdb_stream) { if (s_stream) {
rewind(__grdb_stream); rewind(s_stream);
} else { } else {
__grdb_stream = fopen("/etc/group", "r"); s_stream = fopen("/etc/group", "r");
if (!__grdb_stream) { if (!s_stream) {
perror("open /etc/group"); perror("open /etc/group");
} }
assert(__grdb_stream);
__grdb_entry = (struct group_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setgrent");
} }
} }
void endgrent() void endgrent()
{ {
__grdb_line_number = 0; s_line_number = 0;
if (__grdb_stream) { if (s_stream) {
fclose(__grdb_stream); fclose(s_stream);
__grdb_stream = nullptr; s_stream = nullptr;
}
if (__grdb_entry) {
munmap(__grdb_entry, getpagesize());
__grdb_entry = nullptr;
} }
memset(&s_group, 0, sizeof(s_group));
s_name = {};
s_passwd = {};
s_members = {};
s_members_ptrs = {};
} }
struct group* getgrgid(gid_t gid) struct group* getgrgid(gid_t gid)
@ -96,52 +93,69 @@ struct group* getgrnam(const char* name)
return nullptr; return nullptr;
} }
static bool parse_grpdb_entry(const String& line)
{
auto parts = line.split_view(':', true);
if (parts.size() != 4) {
fprintf(stderr, "getgrent(): Malformed entry on line %u: '%s' has %zu parts\n", s_line_number, line.characters(), parts.size());
return false;
}
s_name = parts[0];
s_passwd = parts[1];
auto& gid_string = parts[2];
String members_string = parts[3];
auto gid = gid_string.to_uint();
if (!gid.has_value()) {
fprintf(stderr, "getgrent(): Malformed GID on line %u\n", s_line_number);
return false;
}
s_members = members_string.split(',');
s_members_ptrs.clear_with_capacity();
s_members_ptrs.ensure_capacity(s_members.size() + 1);
for (auto& member : s_members) {
s_members_ptrs.append(member.characters());
}
s_members_ptrs.append(nullptr);
s_group.gr_gid = gid.value();
s_group.gr_name = const_cast<char*>(s_name.characters());
s_group.gr_passwd = const_cast<char*>(s_passwd.characters());
s_group.gr_mem = const_cast<char**>(s_members_ptrs.data());
return true;
}
struct group* getgrent() struct group* getgrent()
{ {
if (!__grdb_stream) if (!s_stream)
setgrent(); setgrent();
assert(__grdb_stream); while (true) {
if (feof(__grdb_stream)) if (!s_stream || feof(s_stream))
return nullptr; return nullptr;
next_entry: if (ferror(s_stream)) {
char buffer[1024]; fprintf(stderr, "getgrent(): Read error: %s\n", strerror(ferror(s_stream)));
++__grdb_line_number; return nullptr;
char* s = fgets(buffer, sizeof(buffer), __grdb_stream); }
if (!s)
return nullptr; char buffer[1024];
assert(__grdb_stream); ++s_line_number;
if (feof(__grdb_stream)) char* s = fgets(buffer, sizeof(buffer), s_stream);
return nullptr;
String line(s, Chomp); // Silently tolerate an empty line at the end.
auto parts = line.split(':', true); if ((!s || !s[0]) && feof(s_stream))
if (parts.size() != 4) { return nullptr;
fprintf(stderr, "getgrent(): Malformed entry on line %u: '%s' has %zu parts\n", __grdb_line_number, line.characters(), parts.size());
goto next_entry; String line(s, Chomp);
if (parse_grpdb_entry(line))
return &s_group;
// Otherwise, proceed to the next line.
} }
auto& e_name = parts[0];
auto& e_passwd = parts[1];
auto& e_gid_string = parts[2];
auto& e_members_string = parts[3];
auto e_gid = e_gid_string.to_uint();
if (!e_gid.has_value()) {
fprintf(stderr, "getgrent(): Malformed GID on line %u\n", __grdb_line_number);
goto next_entry;
}
auto members = e_members_string.split(',');
__grdb_entry->gr_gid = e_gid.value();
__grdb_entry->gr_name = __grdb_entry->name_buffer;
__grdb_entry->gr_passwd = __grdb_entry->passwd_buffer;
for (size_t i = 0; i < members.size(); ++i) {
__grdb_entry->members[i] = __grdb_entry->members_buffer[i];
strlcpy(__grdb_entry->members_buffer[i], members[i].characters(), sizeof(__grdb_entry->members_buffer[i]));
}
__grdb_entry->members[members.size()] = nullptr;
__grdb_entry->gr_mem = __grdb_entry->members;
strlcpy(__grdb_entry->name_buffer, e_name.characters(), GRDB_STR_MAX_LEN);
strlcpy(__grdb_entry->passwd_buffer, e_passwd.characters(), GRDB_STR_MAX_LEN);
return __grdb_entry;
} }
int initgroups(const char* user, gid_t extra_gid) int initgroups(const char* user, gid_t extra_gid)

View file

@ -41,12 +41,10 @@ extern "C" {
int h_errno; int h_errno;
static hostent __gethostbyname_buffer; static hostent __gethostbyname_buffer;
static char __gethostbyname_name_buffer[512];
static in_addr_t __gethostbyname_address; static in_addr_t __gethostbyname_address;
static in_addr_t* __gethostbyname_address_list_buffer[2]; static in_addr_t* __gethostbyname_address_list_buffer[2];
static hostent __gethostbyaddr_buffer; static hostent __gethostbyaddr_buffer;
static char __gethostbyaddr_name_buffer[512];
static in_addr_t* __gethostbyaddr_address_list_buffer[2]; static in_addr_t* __gethostbyaddr_address_list_buffer[2];
//Get service entry buffers and file information for the getservent() family of functions //Get service entry buffers and file information for the getservent() family of functions
@ -55,8 +53,8 @@ static const char* services_path = "/etc/services";
static bool fill_getserv_buffers(char* line, ssize_t read); static bool fill_getserv_buffers(char* line, ssize_t read);
static servent __getserv_buffer; static servent __getserv_buffer;
static char __getserv_name_buffer[512]; static String __getserv_name_buffer;
static char __getserv_protocol_buffer[10]; static String __getserv_protocol_buffer;
static int __getserv_port_buffer; static int __getserv_port_buffer;
static Vector<ByteBuffer> __getserv_alias_list_buffer; static Vector<ByteBuffer> __getserv_alias_list_buffer;
static Vector<char*> __getserv_alias_list; static Vector<char*> __getserv_alias_list;
@ -69,7 +67,7 @@ static const char* protocols_path = "/etc/protocols";
static bool fill_getproto_buffers(char* line, ssize_t read); static bool fill_getproto_buffers(char* line, ssize_t read);
static protoent __getproto_buffer; static protoent __getproto_buffer;
static char __getproto_name_buffer[512]; static String __getproto_name_buffer;
static Vector<ByteBuffer> __getproto_alias_list_buffer; static Vector<ByteBuffer> __getproto_alias_list_buffer;
static Vector<char*> __getproto_alias_list; static Vector<char*> __getproto_alias_list;
static int __getproto_protocol_buffer; static int __getproto_protocol_buffer;
@ -98,12 +96,13 @@ static int connect_to_lookup_server()
hostent* gethostbyname(const char* name) hostent* gethostbyname(const char* name)
{ {
static String s_name_buffer;
auto ipv4_address = IPv4Address::from_string(name); auto ipv4_address = IPv4Address::from_string(name);
if (ipv4_address.has_value()) { if (ipv4_address.has_value()) {
auto ip4_string = ipv4_address.value().to_string(); s_name_buffer = ipv4_address.value().to_string();
ASSERT(ip4_string.length() < sizeof(__gethostbyname_name_buffer)); __gethostbyname_buffer.h_name = const_cast<char*>(s_name_buffer.characters());
strlcpy(__gethostbyname_name_buffer, ip4_string.characters(), sizeof(__gethostbyname_name_buffer));
__gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
__gethostbyname_buffer.h_aliases = nullptr; __gethostbyname_buffer.h_aliases = nullptr;
__gethostbyname_buffer.h_addrtype = AF_INET; __gethostbyname_buffer.h_addrtype = AF_INET;
new (&__gethostbyname_address) IPv4Address(ipv4_address.value()); new (&__gethostbyname_address) IPv4Address(ipv4_address.value());
@ -152,9 +151,8 @@ hostent* gethostbyname(const char* name)
if (rc <= 0) if (rc <= 0)
return nullptr; return nullptr;
strlcpy(__gethostbyname_name_buffer, name, sizeof(__gethostbyaddr_name_buffer)); s_name_buffer = name;
__gethostbyname_buffer.h_name = const_cast<char*>(s_name_buffer.characters());
__gethostbyname_buffer.h_name = __gethostbyname_name_buffer;
__gethostbyname_buffer.h_aliases = nullptr; __gethostbyname_buffer.h_aliases = nullptr;
__gethostbyname_buffer.h_addrtype = AF_INET; __gethostbyname_buffer.h_addrtype = AF_INET;
__gethostbyname_address_list_buffer[0] = &__gethostbyname_address; __gethostbyname_address_list_buffer[0] = &__gethostbyname_address;
@ -167,6 +165,8 @@ hostent* gethostbyname(const char* name)
hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type) hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
{ {
static String s_name_buffer;
if (type != AF_INET) { if (type != AF_INET) {
errno = EAFNOSUPPORT; errno = EAFNOSUPPORT;
return nullptr; return nullptr;
@ -214,11 +214,8 @@ hostent* gethostbyaddr(const void* addr, socklen_t addr_size, int type)
if (responses.is_empty()) if (responses.is_empty())
return nullptr; return nullptr;
auto& response = responses[0]; s_name_buffer = responses[0];
__gethostbyaddr_buffer.h_name = const_cast<char*>(s_name_buffer.characters());
strlcpy(__gethostbyaddr_name_buffer, response.characters(), sizeof(__gethostbyaddr_name_buffer));
__gethostbyaddr_buffer.h_name = __gethostbyaddr_name_buffer;
__gethostbyaddr_buffer.h_aliases = nullptr; __gethostbyaddr_buffer.h_aliases = nullptr;
__gethostbyaddr_buffer.h_addrtype = AF_INET; __gethostbyaddr_buffer.h_addrtype = AF_INET;
// FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host? // FIXME: Should we populate the hostent's address list here with a sockaddr_in for the provided host?
@ -275,9 +272,9 @@ struct servent* getservent()
if (!fill_getserv_buffers(line, read)) if (!fill_getserv_buffers(line, read))
return nullptr; return nullptr;
__getserv_buffer.s_name = __getserv_name_buffer; __getserv_buffer.s_name = const_cast<char*>(__getserv_name_buffer.characters());
__getserv_buffer.s_port = __getserv_port_buffer; __getserv_buffer.s_port = __getserv_port_buffer;
__getserv_buffer.s_proto = __getserv_protocol_buffer; __getserv_buffer.s_proto = const_cast<char*>(__getserv_protocol_buffer.characters());
__getserv_alias_list.clear(); __getserv_alias_list.clear();
for (auto& alias : __getserv_alias_list_buffer) { for (auto& alias : __getserv_alias_list_buffer) {
@ -373,12 +370,7 @@ static bool fill_getserv_buffers(char* line, ssize_t read)
perror("malformed services file: entry"); perror("malformed services file: entry");
return false; return false;
} }
if (sizeof(__getserv_name_buffer) >= split_line[0].length() + 1) { __getserv_name_buffer = split_line[0];
strlcpy(__getserv_name_buffer, split_line[0].characters(), sizeof(__getserv_name_buffer));
} else {
perror("invalid buffer length: service name");
return false;
}
auto port_protocol_split = String(split_line[1]).split('/'); auto port_protocol_split = String(split_line[1]).split('/');
if (port_protocol_split.size() < 2) { if (port_protocol_split.size() < 2) {
@ -396,13 +388,7 @@ static bool fill_getserv_buffers(char* line, ssize_t read)
port_protocol_split[1].replace("\t", "", true); port_protocol_split[1].replace("\t", "", true);
port_protocol_split[1].replace("\n", "", true); port_protocol_split[1].replace("\n", "", true);
if (sizeof(__getserv_protocol_buffer) >= port_protocol_split[1].length()) { __getserv_protocol_buffer = port_protocol_split[1];
strlcpy(__getserv_protocol_buffer, port_protocol_split[1].characters(), sizeof(__getserv_protocol_buffer));
} else {
perror("malformed services file: protocol");
return false;
}
__getserv_alias_list_buffer.clear(); __getserv_alias_list_buffer.clear();
//If there are aliases for the service, we will fill the alias list buffer. //If there are aliases for the service, we will fill the alias list buffer.
@ -468,7 +454,7 @@ struct protoent* getprotoent()
if (!fill_getproto_buffers(line, read)) if (!fill_getproto_buffers(line, read))
return nullptr; return nullptr;
__getproto_buffer.p_name = __getproto_name_buffer; __getproto_buffer.p_name = const_cast<char*>(__getproto_name_buffer.characters());
__getproto_buffer.p_proto = __getproto_protocol_buffer; __getproto_buffer.p_proto = __getproto_protocol_buffer;
__getproto_alias_list.clear(); __getproto_alias_list.clear();
@ -565,12 +551,7 @@ static bool fill_getproto_buffers(char* line, ssize_t read)
perror("malformed protocols file: entry"); perror("malformed protocols file: entry");
return false; return false;
} }
if (sizeof(__getproto_name_buffer) >= split_line[0].length() + 1) { __getproto_name_buffer = split_line[0];
strlcpy(__getproto_name_buffer, split_line[0].characters(), sizeof(__getproto_name_buffer));
} else {
perror("invalid buffer length: protocol name");
return false;
}
auto number = split_line[1].to_int(); auto number = split_line[1].to_int();
if (!number.has_value()) if (!number.has_value())

View file

@ -26,55 +26,53 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <errno_numbers.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
extern "C" { extern "C" {
#define PWDB_STR_MAX_LEN 256 static FILE* s_stream = nullptr;
static unsigned s_line_number = 0;
static struct passwd s_passwd_entry;
struct passwd_with_strings : public passwd { static String s_name;
char name_buffer[PWDB_STR_MAX_LEN]; static String s_passwd;
char passwd_buffer[PWDB_STR_MAX_LEN]; static String s_gecos;
char gecos_buffer[PWDB_STR_MAX_LEN]; static String s_dir;
char dir_buffer[PWDB_STR_MAX_LEN]; static String s_shell;
char shell_buffer[PWDB_STR_MAX_LEN];
};
static FILE* __pwdb_stream = nullptr;
static unsigned __pwdb_line_number = 0;
static struct passwd_with_strings* __pwdb_entry = nullptr;
void setpwent() void setpwent()
{ {
__pwdb_line_number = 0; s_line_number = 0;
if (__pwdb_stream) { if (s_stream) {
rewind(__pwdb_stream); rewind(s_stream);
} else { } else {
__pwdb_stream = fopen("/etc/passwd", "r"); s_stream = fopen("/etc/passwd", "r");
if (!__pwdb_stream) { if (!s_stream) {
perror("open /etc/passwd"); perror("open /etc/passwd");
} }
assert(__pwdb_stream);
__pwdb_entry = (struct passwd_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setpwent");
} }
} }
void endpwent() void endpwent()
{ {
__pwdb_line_number = 0; s_line_number = 0;
if (__pwdb_stream) { if (s_stream) {
fclose(__pwdb_stream); fclose(s_stream);
__pwdb_stream = nullptr; s_stream = nullptr;
}
if (__pwdb_entry) {
munmap(__pwdb_entry, getpagesize());
__pwdb_entry = nullptr;
} }
memset(&s_passwd_entry, 0, sizeof(s_passwd_entry));
s_name = {};
s_passwd = {};
s_gecos = {};
s_dir = {};
s_shell = {};
} }
struct passwd* getpwuid(uid_t uid) struct passwd* getpwuid(uid_t uid)
@ -97,61 +95,71 @@ struct passwd* getpwnam(const char* name)
return nullptr; return nullptr;
} }
static bool parse_pwddb_entry(const String& line)
{
auto parts = line.split_view(':', true);
if (parts.size() != 7) {
fprintf(stderr, "getpwent(): Malformed entry on line %u\n", s_line_number);
return false;
}
s_name = parts[0];
s_passwd = parts[1];
auto& uid_string = parts[2];
auto& gid_string = parts[3];
s_gecos = parts[4];
s_dir = parts[5];
s_shell = parts[6];
auto uid = uid_string.to_uint();
if (!uid.has_value()) {
fprintf(stderr, "getpwent(): Malformed UID on line %u\n", s_line_number);
return false;
}
auto gid = gid_string.to_uint();
if (!gid.has_value()) {
fprintf(stderr, "getpwent(): Malformed GID on line %u\n", s_line_number);
return false;
}
s_passwd_entry.pw_name = const_cast<char*>(s_name.characters());
s_passwd_entry.pw_passwd = const_cast<char*>(s_passwd.characters());
s_passwd_entry.pw_uid = uid.value();
s_passwd_entry.pw_gid = gid.value();
s_passwd_entry.pw_gecos = const_cast<char*>(s_gecos.characters());
s_passwd_entry.pw_dir = const_cast<char*>(s_dir.characters());
s_passwd_entry.pw_shell = const_cast<char*>(s_shell.characters());
return true;
}
struct passwd* getpwent() struct passwd* getpwent()
{ {
if (!__pwdb_stream) if (!s_stream)
setpwent(); setpwent();
assert(__pwdb_stream); while (true) {
if (feof(__pwdb_stream)) if (!s_stream || feof(s_stream))
return nullptr; return nullptr;
next_entry: if (ferror(s_stream)) {
char buffer[1024]; fprintf(stderr, "getpwent(): Read error: %s\n", strerror(ferror(s_stream)));
++__pwdb_line_number; return nullptr;
char* s = fgets(buffer, sizeof(buffer), __pwdb_stream); }
if (!s)
return nullptr;
assert(__pwdb_stream);
if (feof(__pwdb_stream))
return nullptr;
String line(s, Chomp);
auto parts = line.split(':', true);
if (parts.size() != 7) {
fprintf(stderr, "getpwent(): Malformed entry on line %u\n", __pwdb_line_number);
goto next_entry;
}
auto& e_name = parts[0];
auto& e_passwd = parts[1];
auto& e_uid_string = parts[2];
auto& e_gid_string = parts[3];
auto& e_gecos = parts[4];
auto& e_dir = parts[5];
auto& e_shell = parts[6];
auto e_uid = e_uid_string.to_uint();
if (!e_uid.has_value()) {
fprintf(stderr, "getpwent(): Malformed UID on line %u\n", __pwdb_line_number);
goto next_entry;
}
auto e_gid = e_gid_string.to_uint();
if (!e_gid.has_value()) {
fprintf(stderr, "getpwent(): Malformed GID on line %u\n", __pwdb_line_number);
goto next_entry;
}
__pwdb_entry->pw_uid = e_uid.value();
__pwdb_entry->pw_gid = e_gid.value();
__pwdb_entry->pw_name = __pwdb_entry->name_buffer;
__pwdb_entry->pw_passwd = __pwdb_entry->passwd_buffer;
__pwdb_entry->pw_gecos = __pwdb_entry->gecos_buffer;
__pwdb_entry->pw_dir = __pwdb_entry->dir_buffer;
__pwdb_entry->pw_shell = __pwdb_entry->shell_buffer;
strlcpy(__pwdb_entry->name_buffer, e_name.characters(), PWDB_STR_MAX_LEN); char buffer[1024];
strlcpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), PWDB_STR_MAX_LEN); ++s_line_number;
strlcpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), PWDB_STR_MAX_LEN); char* s = fgets(buffer, sizeof(buffer), s_stream);
strlcpy(__pwdb_entry->dir_buffer, e_dir.characters(), PWDB_STR_MAX_LEN);
strlcpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN); // Silently tolerate an empty line at the end.
return __pwdb_entry; if ((!s || !s[0]) && feof(s_stream))
return nullptr;
String line(s, Chomp);
if (parse_pwddb_entry(line))
return &s_passwd_entry;
// Otherwise, proceed to the next line.
}
} }
int putpwent(const struct passwd* p, FILE* stream) int putpwent(const struct passwd* p, FILE* stream)

View file

@ -557,14 +557,14 @@ int set_process_icon(int icon_id)
char* getlogin() char* getlogin()
{ {
static char __getlogin_buffer[256]; static String buffer;
if (auto* passwd = getpwuid(getuid())) { if (buffer.is_null()) {
strlcpy(__getlogin_buffer, passwd->pw_name, sizeof(__getlogin_buffer)); if (auto* passwd = getpwuid(getuid())) {
buffer = String(passwd->pw_name);
}
endpwent(); endpwent();
return __getlogin_buffer;
} }
endpwent(); return const_cast<char*>(buffer.characters());
return nullptr;
} }
int ftruncate(int fd, off_t length) int ftruncate(int fd, off_t length)