From 6030b585aaa31b1b3ef2527b830223d48da9c99c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 9 Oct 2007 01:36:18 +0000 Subject: [PATCH] * Implemented gethostname() and sethostname(). * uname() now calls gethostname() instead of using a hard-coded value. * This fixes bug #1250. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22492 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/sys/uname.c | 12 +-- src/system/libroot/posix/unistd/Jamfile | 2 +- src/system/libroot/posix/unistd/hostname.c | 26 ------ src/system/libroot/posix/unistd/hostname.cpp | 89 ++++++++++++++++++++ 4 files changed, 97 insertions(+), 32 deletions(-) delete mode 100644 src/system/libroot/posix/unistd/hostname.c create mode 100644 src/system/libroot/posix/unistd/hostname.cpp diff --git a/src/system/libroot/posix/sys/uname.c b/src/system/libroot/posix/sys/uname.c index 8c41d7f99a..4f09407b58 100644 --- a/src/system/libroot/posix/sys/uname.c +++ b/src/system/libroot/posix/sys/uname.c @@ -1,15 +1,17 @@ /* - * Copyright 2004, Jérôme Duval jerome.duval@free.fr. All rights reserved. + * Copyright 2004-2007, Jérôme Duval jerome.duval@free.fr. All rights reserved. * Distributed under the terms of the MIT License. */ -#include - #include + #include #include #include +#include + +#include // Haiku SVN revision. Will be set when copying libroot.so to the image. @@ -61,8 +63,8 @@ uname(struct utsname *info) } strlcpy(info->machine, platform, sizeof(info->machine)); - // TODO fill nodename field when we have hostname info - strlcpy(info->nodename, "unknown", sizeof(info->nodename)); + if (gethostname(info->nodename, sizeof(info->nodename)) != 0) + strlcpy(info->nodename, "unknown", sizeof(info->nodename)); return 0; } diff --git a/src/system/libroot/posix/unistd/Jamfile b/src/system/libroot/posix/unistd/Jamfile index 40c4bbd2f8..9a9799a3a0 100644 --- a/src/system/libroot/posix/unistd/Jamfile +++ b/src/system/libroot/posix/unistd/Jamfile @@ -14,7 +14,7 @@ MergeObject posix_unistd.o : _exit.c fcntl.c fork.c - hostname.c + hostname.cpp ioctl.c link.c lseek.c diff --git a/src/system/libroot/posix/unistd/hostname.c b/src/system/libroot/posix/unistd/hostname.c deleted file mode 100644 index 150d163ea3..0000000000 --- a/src/system/libroot/posix/unistd/hostname.c +++ /dev/null @@ -1,26 +0,0 @@ -/* -** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the OpenBeOS License. -*/ - - -#include -#include -#include -#include - - -int -sethostname(const char *hostName, size_t nameSize) -{ - return EPERM; -} - - -int -gethostname(char *hostName, size_t nameSize) -{ - strlcpy(hostName, "beast", nameSize); - return 0; -} - diff --git a/src/system/libroot/posix/unistd/hostname.cpp b/src/system/libroot/posix/unistd/hostname.cpp new file mode 100644 index 0000000000..35fe1080a5 --- /dev/null +++ b/src/system/libroot/posix/unistd/hostname.cpp @@ -0,0 +1,89 @@ +/* + * 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 + + +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) { + 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) { + 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; +} +