1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:07:45 +00:00

LibCore: Fix building LibCore on FreeBSD

This commit is contained in:
Gunnar Beutner 2021-06-07 10:12:58 +02:00 committed by Andreas Kling
parent a8ccc9580d
commit 764a41e284
2 changed files with 19 additions and 16 deletions

View file

@ -15,10 +15,8 @@
#include <errno.h> #include <errno.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
# include <shadow.h> # include <shadow.h>
#else
# include <LibC/shadow.h>
#endif #endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -62,7 +60,7 @@ Result<Account, String> Account::from_passwd(const passwd& pwd, const spwd& spwd
{ {
Account account(pwd, spwd, get_extra_gids(pwd)); Account account(pwd, spwd, get_extra_gids(pwd));
endpwent(); endpwent();
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
endspent(); endspent();
#endif #endif
return account; return account;
@ -81,7 +79,7 @@ Result<Account, String> Account::from_name(const char* username)
spwd spwd_dummy = {}; spwd spwd_dummy = {};
spwd_dummy.sp_namp = const_cast<char*>(username); spwd_dummy.sp_namp = const_cast<char*>(username);
spwd_dummy.sp_pwdp = const_cast<char*>(""); spwd_dummy.sp_pwdp = const_cast<char*>("");
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
auto* spwd = getspnam(username); auto* spwd = getspnam(username);
if (!spwd) if (!spwd)
spwd = &spwd_dummy; spwd = &spwd_dummy;
@ -104,7 +102,7 @@ Result<Account, String> Account::from_uid(uid_t uid)
spwd spwd_dummy = {}; spwd spwd_dummy = {};
spwd_dummy.sp_namp = pwd->pw_name; spwd_dummy.sp_namp = pwd->pw_name;
spwd_dummy.sp_pwdp = const_cast<char*>(""); spwd_dummy.sp_pwdp = const_cast<char*>("");
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
auto* spwd = getspnam(pwd->pw_name); auto* spwd = getspnam(pwd->pw_name);
if (!spwd) if (!spwd)
spwd = &spwd_dummy; spwd = &spwd_dummy;
@ -211,7 +209,7 @@ String Account::generate_passwd_file() const
return builder.to_string(); return builder.to_string();
} }
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
String Account::generate_shadow_file() const String Account::generate_shadow_file() const
{ {
StringBuilder builder; StringBuilder builder;
@ -259,13 +257,13 @@ bool Account::sync()
{ {
auto new_passwd_file_content = generate_passwd_file(); auto new_passwd_file_content = generate_passwd_file();
VERIFY(!new_passwd_file_content.is_null()); VERIFY(!new_passwd_file_content.is_null());
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
auto new_shadow_file_content = generate_shadow_file(); auto new_shadow_file_content = generate_shadow_file();
VERIFY(!new_shadow_file_content.is_null()); VERIFY(!new_shadow_file_content.is_null());
#endif #endif
char new_passwd_name[] = "/etc/passwd.XXXXXX"; char new_passwd_name[] = "/etc/passwd.XXXXXX";
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
char new_shadow_name[] = "/etc/shadow.XXXXXX"; char new_shadow_name[] = "/etc/shadow.XXXXXX";
#endif #endif
@ -276,7 +274,7 @@ bool Account::sync()
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); }; ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
auto new_shadow_fd = mkstemp(new_shadow_name); auto new_shadow_fd = mkstemp(new_shadow_name);
if (new_shadow_fd < 0) { if (new_shadow_fd < 0) {
perror("mkstemp"); perror("mkstemp");
@ -297,7 +295,7 @@ bool Account::sync()
} }
VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length()); VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length()); nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length());
if (nwritten < 0) { if (nwritten < 0) {
perror("write"); perror("write");
@ -312,7 +310,7 @@ bool Account::sync()
return false; return false;
} }
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
if (rename(new_shadow_name, "/etc/shadow") < 0) { if (rename(new_shadow_name, "/etc/shadow") < 0) {
perror("Failed to install new /etc/shadow"); perror("Failed to install new /etc/shadow");
return false; return false;

View file

@ -11,15 +11,20 @@
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <pwd.h> #include <pwd.h>
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
# include <shadow.h> # include <shadow.h>
#else
# include <LibC/shadow.h>
#endif #endif
#include <sys/types.h> #include <sys/types.h>
namespace Core { namespace Core {
#ifdef AK_OS_BSD_GENERIC
struct spwd {
char* sp_namp;
char* sp_pwdp;
};
#endif
class Account { class Account {
public: public:
static Result<Account, String> from_name(const char* username); static Result<Account, String> from_name(const char* username);
@ -56,7 +61,7 @@ private:
Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids); Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids);
String generate_passwd_file() const; String generate_passwd_file() const;
#ifndef AK_OS_MACOS #ifndef AK_OS_BSD_GENERIC
String generate_shadow_file() const; String generate_shadow_file() const;
#endif #endif