make getgr* work

This commit is contained in:
Motiejus Jakštys 2022-07-14 19:14:47 +03:00
parent 8db43a537b
commit c883dcd52c
3 changed files with 23 additions and 22 deletions

View File

@ -295,9 +295,9 @@ pub fn packCGroupNoMembers(group: *const PackedGroup, buf: []u8) error{BufferToo
buf[name_start + name.len] = 0; buf[name_start + name.len] = 0;
return CGroup{ return CGroup{
.gid = group.gid(), .gr_name = buf[name_start .. name_start + name.len :0].ptr,
.name = buf[name_start .. name_start + name.len :0].ptr, .gr_gid = group.gid(),
.members = member_ptrs.ptr, .gr_mem = member_ptrs.ptr,
}; };
} }
@ -364,9 +364,9 @@ pub fn packCGroup(self: *const DB, group: *const PackedGroup, buf: []u8) error{B
buf[buf_offset + name.len] = 0; buf[buf_offset + name.len] = 0;
return CGroup{ return CGroup{
.gid = group.gid(), .gr_name = buf[buf_offset .. buf_offset + name.len :0].ptr,
.name = buf[buf_offset .. buf_offset + name.len :0].ptr, .gr_gid = group.gid(),
.members = member_ptrs.ptr, .gr_mem = member_ptrs.ptr,
}; };
} }
@ -799,9 +799,9 @@ test "DB getgrnam/getgrgid" {
{ {
try testing.expectEqual(try db.getgrnam("doesnotexist", buf, false), null); try testing.expectEqual(try db.getgrnam("doesnotexist", buf, false), null);
const all = (try db.getgrnam("all", buf, false)).?; const all = (try db.getgrnam("all", buf, false)).?;
try testing.expectEqual(all.gid, 9999); try testing.expectEqual(all.gr_gid, 9999);
try testing.expectEqualStrings(all.name[0..4], "all\x00"); try testing.expectEqualStrings(all.gr_name[0..4], "all\x00");
const members = all.members; const members = all.gr_mem;
try testing.expectEqualStrings(mem.sliceTo(members[0].?, 0), "Name" ** 8); try testing.expectEqualStrings(mem.sliceTo(members[0].?, 0), "Name" ** 8);
try testing.expectEqualStrings(mem.sliceTo(members[1].?, 0), "root"); try testing.expectEqualStrings(mem.sliceTo(members[1].?, 0), "root");
try testing.expectEqualStrings(mem.sliceTo(members[2].?, 0), "svc-bar"); try testing.expectEqualStrings(mem.sliceTo(members[2].?, 0), "svc-bar");
@ -811,16 +811,16 @@ test "DB getgrnam/getgrgid" {
{ {
const all = (try db.getgrnam("all", buf, true)).?; const all = (try db.getgrnam("all", buf, true)).?;
try testing.expectEqual(all.gid, 9999); try testing.expectEqual(all.gr_gid, 9999);
try testing.expectEqualStrings(all.name[0..4], "all\x00"); try testing.expectEqualStrings(all.gr_name[0..4], "all\x00");
try testing.expectEqual(all.members[0], null); try testing.expectEqual(all.gr_mem[0], null);
} }
{ {
try testing.expectEqual(try db.getgrgid(42, buf, false), null); try testing.expectEqual(try db.getgrgid(42, buf, false), null);
const all = (try db.getgrgid(9999, buf, false)).?; const all = (try db.getgrgid(9999, buf, false)).?;
try testing.expectEqual(all.gid, 9999); try testing.expectEqual(all.gr_gid, 9999);
try testing.expectEqualStrings(all.name[0..3], "all"); try testing.expectEqualStrings(all.gr_name[0..3], "all");
} }
try testing.expectEqualStrings("", errc.unwrap().constSlice()); try testing.expectEqualStrings("", errc.unwrap().constSlice());

View File

@ -126,15 +126,16 @@ pub fn strlenZ(self: *const Group) usize {
} }
pub const CGroup = extern struct { pub const CGroup = extern struct {
gid: u32, gr_name: [*:0]const u8,
name: [*:0]const u8, gr_passwd: [*:0]const u8 = "x",
gr_gid: u32,
// Should be [*:null]align(1) const ?[*:0]const u8 // Should be [*:null]align(1) const ?[*:0]const u8
// https://github.com/ziglang/zig/issues/7517 // https://github.com/ziglang/zig/issues/7517
members: [*]align(1) const ?[*:0]const u8, gr_mem: [*]align(1) const ?[*:0]const u8,
}; };
// size of the pointer to a single member. // size of the pointer to a single member.
pub const ptr_size = @sizeOf(meta.Child(meta.fieldInfo(CGroup, .members).field_type)); pub const ptr_size = @sizeOf(meta.Child(meta.fieldInfo(CGroup, .gr_mem).field_type));
const testing = std.testing; const testing = std.testing;

View File

@ -94,7 +94,7 @@ fn init() void {
//if (verbose) //if (verbose)
// std.debug.print("opening '{s}'\n", .{fname}); // std.debug.print("opening '{s}'\n", .{fname});
const fname = turbonss_db_path; const fname = turbonss_db_path;
const verbose = true; const verbose = false;
const omit_members = true; const omit_members = true;
const file = File.open(fname) catch |err| { const file = File.open(fname) catch |err| {
@ -617,9 +617,9 @@ test "libnss getgrgid_r and getgrnam_r" {
} }
fn testSvcGroup(g: CGroup) !void { fn testSvcGroup(g: CGroup) !void {
try testing.expectEqual(@as(u32, 100000), g.gid); try testing.expectEqual(@as(u32, 100000), g.gr_gid);
try testing.expectEqualStrings("service-group", mem.sliceTo(g.name, 0)); try testing.expectEqualStrings("service-group", mem.sliceTo(g.gr_name, 0));
const members = g.members; const members = g.gr_mem;
try testing.expect(members[0] != null); try testing.expect(members[0] != null);
try testing.expect(members[1] != null); try testing.expect(members[1] != null);
try testing.expectEqualStrings("root", mem.sliceTo(members[0].?, 0)); try testing.expectEqualStrings("root", mem.sliceTo(members[0].?, 0));