Merge pull request #3298 from ziglang/gethostname

gethostname
This commit is contained in:
Andrew Kelley 2019-09-22 18:19:37 -04:00 committed by GitHub
commit 989cd4233e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 61 additions and 0 deletions

View File

@ -107,6 +107,7 @@ pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void,
pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
pub extern "c" fn gethostname(name: [*]u8, len: usize) c_int;
pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;

View File

@ -2688,6 +2688,38 @@ pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
}
}
pub const GetHostNameError = error{
PermissionDenied,
Unexpected,
};
pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
if (builtin.link_libc) {
switch (errno(system.gethostname(name_buffer, name_buffer.len))) {
0 => return mem.toSlice(u8, name_buffer),
EFAULT => unreachable,
ENAMETOOLONG => unreachable, // HOST_NAME_MAX prevents this
EPERM => return error.PermissionDenied,
else => |err| return unexpectedErrno(err),
}
}
if (linux.is_the_target) {
var uts: utsname = undefined;
switch (errno(system.uname(&uts))) {
0 => {
const hostname = mem.toSlice(u8, &uts.nodename);
mem.copy(u8, name_buffer, hostname);
return name_buffer[0..hostname.len];
},
EFAULT => unreachable,
EPERM => return error.PermissionDenied,
else => |err| return unexpectedErrno(err),
}
}
@compileError("TODO implement gethostname for this OS");
}
test "" {
_ = @import("os/darwin.zig");
_ = @import("os/freebsd.zig");

View File

@ -1175,3 +1175,4 @@ pub fn S_ISSOCK(m: u32) bool {
pub fn S_IWHT(m: u32) bool {
return m & S_IFMT == S_IFWHT;
}
pub const HOST_NAME_MAX = 72;

View File

@ -935,3 +935,5 @@ pub fn S_ISSOCK(m: u32) bool {
pub fn S_IWHT(m: u32) bool {
return m & S_IFMT == S_IFWHT;
}
pub const HOST_NAME_MAX = 255;

View File

@ -1175,3 +1175,13 @@ pub const IORING_REGISTER_FILES = 2;
pub const IORING_UNREGISTER_FILES = 3;
pub const IORING_REGISTER_EVENTFD = 4;
pub const IORING_UNREGISTER_EVENTFD = 5;
pub const utsname = extern struct {
sysname: [65]u8,
nodename: [65]u8,
release: [65]u8,
version: [65]u8,
machine: [65]u8,
domainname: [65]u8,
};
pub const HOST_NAME_MAX = 64;

View File

@ -819,3 +819,5 @@ pub fn S_ISSOCK(m: u32) bool {
pub fn S_IWHT(m: u32) bool {
return m & S_IFMT == S_IFWHT;
}
pub const HOST_NAME_MAX = 255;

View File

@ -965,6 +965,10 @@ pub fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) usize {
return syscall2(SYS_sigaltstack, @ptrToInt(ss), @ptrToInt(old_ss));
}
pub fn uname(uts: *utsname) usize {
return syscall1(SYS_uname, @ptrToInt(uts));
}
// XXX: This should be weak
extern const __ehdr_start: elf.Ehdr = undefined;

View File

@ -210,3 +210,12 @@ test "dl_iterate_phdr" {
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
expect(counter != 0);
}
test "gethostname" {
if (os.windows.is_the_target)
return error.SkipZigTest;
var buf: [os.HOST_NAME_MAX]u8 = undefined;
const hostname = try os.gethostname(&buf);
expect(hostname.len != 0);
}