diff --git a/src/add-ons/kernel/file_systems/nfs4/RPCAuth.cpp b/src/add-ons/kernel/file_systems/nfs4/RPCAuth.cpp index 6c6f1ba5d7..e9ce2aa365 100644 --- a/src/add-ons/kernel/file_systems/nfs4/RPCAuth.cpp +++ b/src/add-ons/kernel/file_systems/nfs4/RPCAuth.cpp @@ -54,7 +54,7 @@ Auth::CreateSys() xdr.AddUInt(time(NULL)); char hostname[255]; - //if (gethostname(hostname, 255) < 0) + if (gethostname(hostname, 255) < 0) strcpy(hostname, "unknown"); xdr.AddString(hostname, 255); diff --git a/src/system/kernel/util/Jamfile b/src/system/kernel/util/Jamfile index 59ff30c7c6..23577a6981 100644 --- a/src/system/kernel/util/Jamfile +++ b/src/system/kernel/util/Jamfile @@ -4,6 +4,7 @@ UsePrivateHeaders [ FDirName kernel util ] ; KernelMergeObject kernel_util.o : AVLTreeBase.cpp + hostname.cpp kernel_cpp.cpp KernelReferenceable.cpp khash.cpp diff --git a/src/system/kernel/util/hostname.cpp b/src/system/kernel/util/hostname.cpp new file mode 100644 index 0000000000..676f16990e --- /dev/null +++ b/src/system/kernel/util/hostname.cpp @@ -0,0 +1,91 @@ +/* + * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ + + +#include +#include +#include +#include + +#include +#include + +#include + + +static status_t +get_path(char *path, bool create) +{ + status_t status = find_directory(B_COMMON_SETTINGS_DIRECTORY, -1, create, + path, B_PATH_NAME_LENGTH); + if (status != B_OK) + return status; + + strlcat(path, "/network", B_PATH_NAME_LENGTH); + if (create) + mkdir(path, 0755); + strlcat(path, "/hostname", B_PATH_NAME_LENGTH); + return B_OK; +} + + +extern "C" int +sethostname(const char *hostName, size_t nameSize) +{ + char path[B_PATH_NAME_LENGTH]; + if (get_path(path, false) != B_OK) { + __set_errno(B_ERROR); + return -1; + } + + int file = open(path, O_WRONLY | O_CREAT, 0644); + if (file < 0) + return -1; + + nameSize = min_c(nameSize, MAXHOSTNAMELEN); + + if (write(file, hostName, nameSize) != (ssize_t)nameSize + || write(file, "\n", 1) != 1) { + close(file); + return -1; + } + + close(file); + return 0; +} + + +extern "C" int +gethostname(char *hostName, size_t nameSize) +{ + // look up hostname from network settings hostname file + + char path[B_PATH_NAME_LENGTH]; + if (get_path(path, false) != B_OK) { + __set_errno(B_ERROR); + return -1; + } + + int file = open(path, O_RDONLY); + if (file < 0) + return -1; + + nameSize = min_c(nameSize, MAXHOSTNAMELEN); + + int length = read(file, hostName, nameSize - 1); + close(file); + + if (length < 0) + return -1; + + hostName[length] = '\0'; + + char *end = strpbrk(hostName, "\r\n\t"); + if (end != NULL) + end[0] = '\0'; + + return 0; +} +