From ee603c47ca2c40770dd5cd9a6899265a24f4af38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 20 Apr 2022 07:19:34 +0300 Subject: [PATCH] wip so --- lib/File.zig | 35 +++++++++++++++++++++++ lib/State.zig | 15 ---------- lib/libnss.zig | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/test_all.zig | 2 +- 4 files changed, 110 insertions(+), 16 deletions(-) create mode 100644 lib/File.zig delete mode 100644 lib/State.zig create mode 100644 lib/libnss.zig diff --git a/lib/File.zig b/lib/File.zig new file mode 100644 index 0000000..35d2765 --- /dev/null +++ b/lib/File.zig @@ -0,0 +1,35 @@ +const std = @import("std"); +const os = std.os; + +const DB = @import("DB.zig"); +const InvalidHeader = @import("header.zig").Invalid; + +const File = @This(); + +db: DB, +ptr: []align(4096) const u8, + +pub const Error = os.OpenError || os.FStatError || os.MMapError || InvalidHeader; +pub fn open(fname: []const u8) Error!File { + const fd = try os.open(fname, os.O.RDONLY, 0); + var fd_open = true; + errdefer { + if (fd_open) os.close(fd); + } + + const st = try os.fstat(fd); + const size = @intCast(u64, st.size); + const ptr = try os.mmap(null, size, os.PROT.READ, os.MAP.SHARED, fd, 0); + errdefer os.munmap(ptr); + + os.close(fd); + fd_open = false; + + const db = try DB.fromBytes(ptr); + return File{ .db = db, .ptr = ptr }; +} + +pub fn close(self: *File) void { + os.munmap(self.ptr); + self.* = undefined; +} diff --git a/lib/State.zig b/lib/State.zig deleted file mode 100644 index 465f1a1..0000000 --- a/lib/State.zig +++ /dev/null @@ -1,15 +0,0 @@ -const std = @import("std"); -const os = std.os; - -const DB = @import("DB.zig"); -const PackedUser = @import("PackedUser.zig"); - -// State is a type of the global variable holding the process state: -// the DB handle and all the iterators. -const State = struct { - DB: *const DB, - getpwent_iterator: *PackedUser.Iterator, -}; - -// state is initialized on library startup. -var state: State = undefined; diff --git a/lib/libnss.zig b/lib/libnss.zig new file mode 100644 index 0000000..53d25b9 --- /dev/null +++ b/lib/libnss.zig @@ -0,0 +1,74 @@ +const std = @import("std"); +const os = std.os; +const fmt = std.fmt; +const mem = std.mem; + +const DB = @import("DB.zig"); +const File = @import("File.zig"); +const CGroup = @import("Group.zig").CGroup; +const PackedGroup = @import("PackedGroup.zig"); +const CUser = @import("User.zig").CUser; +const PackedUser = @import("PackedUser.zig"); + +const c = @cImport({ + @cInclude("stdlib.h"); + @cInclude("nss.h"); +}); + +export const turbonss_default_path: [:0]const u8 = "/etc/turbonss/db.turbo"; + +// State is a type of the global variable holding the process state: +// the DB handle and all the iterators. +const State = struct { + file: ?File, + getpwent_iterator: ?PackedUser.Iterator, + getgrent_iterator: ?PackedGroup.Iterator, + omit_members: bool, + err_msg: [1024]u8, +}; + +// state is initialized on library startup. +var state: State = undefined; + +// constructor +export fn _turbo_init() void { + const fname = os.getenvZ("TURBONSS_DB") orelse turbonss_default_path[0..]; + state.file = File.open(fname) catch |err| { + _ = fmt.bufPrint(&state.err_msg, "open {s}: {s}", .{ fname, @errorName(err) }) catch return; + return; + }; + + const omit_members_env = os.getenvZ("TURBONSS_OMIT_MEMBERS") orelse "auto"; + state.omit_members = shouldOmitMembers(omit_members_env, os.argv); + + return; +} + +fn shouldOmitMembers(env: []const u8, argv: [][*:0]u8) bool { + if (mem.eql(u8, env, "1")) return true; + if (mem.eql(u8, env, "0")) return false; + if (argv.len == 0) return false; + + return mem.eql(u8, mem.sliceTo(argv[0], 0), "id"); +} + +// destructor +export fn _turbo_fini() void { + if (state.file) |*fooo| + fooo.close(); +} + +export fn _nss_turbo_getpwuid_r( + uid: c_uint, + res: *CUser, + buf: [*]u8, + len: usize, + errnop: *c_int, +) c.enum_nss_status { + _ = uid; + _ = res; + _ = buf; + _ = len; + _ = errnop; + return 0; +} diff --git a/lib/test_all.zig b/lib/test_all.zig index 990a59f..b3ba8e0 100644 --- a/lib/test_all.zig +++ b/lib/test_all.zig @@ -6,11 +6,11 @@ test "turbonss test suite" { _ = @import("DB.zig"); _ = @import("Group.zig"); _ = @import("header.zig"); + _ = @import("libnss.zig"); _ = @import("PackedGroup.zig"); _ = @import("PackedUser.zig"); _ = @import("padding.zig"); _ = @import("shell.zig"); - _ = @import("State.zig"); _ = @import("User.zig"); _ = @import("validate.zig"); }