Skip to content

Commit

Permalink
Add getpwent() family of functions to LibC.
Browse files Browse the repository at this point in the history
Also add a little /etc/passwd database. There's just me in there.
  • Loading branch information
awesomekling committed Oct 31, 2018
1 parent 819ce91 commit 9886b27
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 25 deletions.
1 change: 1 addition & 0 deletions AK/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#define PACKED __attribute__ ((packed))
#define NORETURN __attribute__ ((noreturn))
#undef ALWAYS_INLINE
#define ALWAYS_INLINE __attribute__ ((always_inline))
#define NEVER_INLINE __attribute__ ((noinline))
#define MALLOC_ATTR __attribute__ ((malloc))
Expand Down
15 changes: 15 additions & 0 deletions AK/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,19 @@ ByteBuffer String::toByteBuffer() const
return ByteBuffer::copy(reinterpret_cast<const byte*>(characters()), length());
}

unsigned String::toUInt(bool& ok) const
{
unsigned value = 0;
for (size_t i = 0; i < length(); ++i) {
if (characters()[i] < '0' || characters()[i] > '9') {
ok = false;
return 0;
}
value = value * 10;
value += characters()[i] - '0';
}
ok = true;
return value;
}

}
2 changes: 2 additions & 0 deletions AK/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class String {
{
}

unsigned toUInt(bool& ok) const;

String toLowercase() const
{
if (!m_impl)
Expand Down
2 changes: 2 additions & 0 deletions Base/etc/passwd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
root:x:0:0:root:/:/bin/sh
andreas:x:100:100:Andreas Kling,,,:/users/andreas:/bin/sh
1 change: 1 addition & 0 deletions Kernel/sync-sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cp -p _fs_contents.stock _fs_contents
mkdir mnt
mount -o loop _fs_contents mnt/
cp -R ../Base/* mnt/
cp ../Userland/sh mnt/bin/sh
cp ../Userland/id mnt/bin/id
cp ../Userland/ps mnt/bin/ps
Expand Down
1 change: 1 addition & 0 deletions LibC/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LIBC_OBJS = \
signal.o \
getopt.o \
scanf.o \
pwd.o \
entry.o

OBJS = $(AK_OBJS) $(LIBC_OBJS)
Expand Down
2 changes: 1 addition & 1 deletion LibC/ctype.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ALWAYS_INLINE int isascii(int ch)

ALWAYS_INLINE int isspace(int ch)
{
return ch == ' ' || ch == '\f' || ch == '\n' || ch == '\r' || ch == '\t' == '\v';
return ch == ' ' || ch == '\f' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\v';
}

ALWAYS_INLINE int islower(int c)
Expand Down
117 changes: 117 additions & 0 deletions LibC/pwd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <AK/String.h>

struct passwd_with_strings : public passwd {
char name_buffer[256];
char passwd_buffer[256];
char gecos_buffer[256];
char dir_buffer[256];
char shell_buffer[256];
};

static FILE* __pwdb_stream = nullptr;
static unsigned __pwdb_line_number = 0;
static struct passwd_with_strings* __pwdb_entry = nullptr;

void setpwent()
{
__pwdb_line_number = 0;
if (__pwdb_stream) {
rewind(__pwdb_stream);
} else {
__pwdb_stream = fopen("/etc/passwd", "r");
__pwdb_entry = (struct passwd_with_strings*)mmap(nullptr, getpagesize());
}
}

void endpwent()
{
__pwdb_line_number = 0;
if (__pwdb_stream) {
fclose(__pwdb_stream);
__pwdb_stream = nullptr;
}
if (__pwdb_entry) {
munmap(__pwdb_entry, getpagesize());
__pwdb_entry = nullptr;
}
}

struct passwd* getpwuid(uid_t uid)
{
setpwent();
while (auto* pw = getpwent()) {
if (pw->pw_uid == uid)
return pw;
}
return nullptr;
}

struct passwd* getpwnam(const char* name)
{
setpwent();
while (auto* pw = getpwent()) {
if (!strcmp(pw->pw_name, name))
return pw;
}
return nullptr;
}

struct passwd* getpwent()
{
if (!__pwdb_stream)
setpwent();

if (feof(__pwdb_stream))
return nullptr;

next_entry:
char buffer[1024];
++__pwdb_line_number;
char* s = fgets(buffer, sizeof(buffer), __pwdb_stream);
if (!s)
return nullptr;
if (feof(__pwdb_stream))
return nullptr;
String line(s);
auto parts = line.split(':');
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];
bool ok;
uid_t e_uid = e_uid_string.toUInt(ok);
if (!ok) {
fprintf(stderr, "getpwent(): Malformed UID on line %u\n", __pwdb_line_number);
goto next_entry;
}
gid_t e_gid = e_gid_string.toUInt(ok);
if (!ok) {
fprintf(stderr, "getpwent(): Malformed GID on line %u\n", __pwdb_line_number);
goto next_entry;
}
__pwdb_entry->pw_uid = e_uid;
__pwdb_entry->pw_gid = e_gid;
__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;
strncpy(__pwdb_entry->name_buffer, e_name.characters(), e_name.length());
strncpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), e_passwd.length());
strncpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), e_gecos.length());
strncpy(__pwdb_entry->dir_buffer, e_dir.characters(), e_dir.length());
strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), e_shell.length());
return __pwdb_entry;
}
24 changes: 24 additions & 0 deletions LibC/pwd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <sys/cdefs.h>
#include <sys/types.h>

__BEGIN_DECLS

struct passwd {
char* pw_name;
char* pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
char* pw_gecos;
char* pw_dir;
char* pw_shell;
};

struct passwd* getpwent();
void setpwent();
void endpwent();
struct passwd* getpwnam(const char* name);
struct passwd* getpwuid(uid_t);

__END_DECLS
2 changes: 1 addition & 1 deletion LibC/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ char* fgets(char* buffer, int size, FILE* stream)
int fgetc(FILE* stream)
{
char ch;
read(stream->fd, &ch, 1);
fread(&ch, sizeof(char), 1, stream);
return ch;
}

Expand Down
3 changes: 3 additions & 0 deletions LibC/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ int getc(FILE*);
int getchar();
FILE* fopen(const char* pathname, const char* mode);
int fclose(FILE*);
void rewind(FILE*);
void clearerr(FILE*);
int feof(FILE*);
size_t fread(void* ptr, size_t size, size_t nmemb, FILE*);
size_t fwrite(const void* ptr, size_t size, size_t nmemb, FILE*);
int fprintf(FILE*, const char* fmt, ...);
Expand Down
1 change: 0 additions & 1 deletion LibC/sys/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
#define __BEGIN_DECLS
#define __END_DECLS
#endif

1 change: 1 addition & 0 deletions LibC/sys/mman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <mman.h>
1 change: 1 addition & 0 deletions LibC/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ __BEGIN_DECLS

extern char** environ;

inline int getpagesize() { return 4096; }
uid_t getuid();
gid_t getgid();
pid_t getpid();
Expand Down
5 changes: 0 additions & 5 deletions Userland/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ OBJS = \
sh.o \
ps.o \
ls.o \
pwd.o \
sleep.o \
date.o \
true.o \
Expand All @@ -21,7 +20,6 @@ APPS = \
sh \
ps \
ls \
pwd \
sleep \
date \
true \
Expand Down Expand Up @@ -64,9 +62,6 @@ ps: ps.o
ls: ls.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a

pwd: pwd.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a

sleep: sleep.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a

Expand Down
7 changes: 5 additions & 2 deletions Userland/id.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include <LibC/unistd.h>
#include <LibC/stdio.h>
#include <LibC/pwd.h>

int main(int c, char** v)
{
uid_t uid = getuid();
gid_t gid = getgid();
pid_t pid = getpid();
printf("uid=%u, gid=%u, pid=%u\n", uid, gid, pid);

struct passwd* pw = getpwuid(uid);

printf("uid=%u(%s), gid=%u\n", uid, pw ? pw->pw_name : "n/a", gid);
return 0;
}

15 changes: 0 additions & 15 deletions Userland/pwd.cpp

This file was deleted.

0 comments on commit 9886b27

Please sign in to comment.