diff --git a/apps/glogin-provider.c b/apps/glogin-provider.c index 0e6cbcb9..c55acfa3 100644 --- a/apps/glogin-provider.c +++ b/apps/glogin-provider.c @@ -14,7 +14,6 @@ #include #include #include -#include #include #include @@ -188,7 +187,7 @@ void draw_login_container(gfx_context_t * ctx, struct login_container * lc) { static void get_updated_hostname_with_time_info(char hostname[]) { // get hostname char _hostname[256]; - syscall_gethostname(_hostname); + gethostname(_hostname, 255); // get current time struct tm * timeinfo; diff --git a/apps/hostname.c b/apps/hostname.c index d5c95c0a..4fb36f70 100644 --- a/apps/hostname.c +++ b/apps/hostname.c @@ -1,30 +1,28 @@ -/* This file is part of ToaruOS and is released under the terms +/* vim: ts=4 sw=4 noexpandtab + * This file is part of ToaruOS and is released under the terms * of the NCSA / University of Illinois License - see LICENSE.md * Copyright (C) 2013-2018 K. Lange - */ -/* vim: tabstop=4 shiftwidth=4 noexpandtab * * hostname * * Prints or sets the system hostname. */ #include -#include - -#define ROOT_UID 0 +#include +#include int main(int argc, char * argv[]) { if ((argc > 1 && argv[1][0] == '-') || (argc < 2)) { - char tmp[256]; - syscall_gethostname(tmp); + char tmp[256] = {0}; + gethostname(tmp, 255); printf("%s\n", tmp); return 0; } else { - if (syscall_getuid() != ROOT_UID) { + if (getuid() != 0) { fprintf(stderr,"Must be root to set hostname.\n"); return 1; } else { - syscall_sethostname(argv[1]); + sethostname(argv[1], strlen(argv[1])); FILE * file = fopen("/etc/hostname", "w"); if (!file) { return 1; diff --git a/apps/init.c b/apps/init.c index 1c09f8b0..593127b0 100644 --- a/apps/init.c +++ b/apps/init.c @@ -3,8 +3,9 @@ #include #include #include +#include -void set_console() { +void set_console(void) { int _stdin = syscall_open("/dev/null", 0, 0); int _stdout = syscall_open("/dev/ttyS0", 1, 0); int _stderr = syscall_open("/dev/ttyS0", 1, 0); @@ -18,6 +19,22 @@ void set_console() { (void)_stdin; } +void set_hostname(void) { + FILE * f = fopen("/etc/hostname", "r"); + + if (!f) { + /* set fallback hostname */ + sethostname("base", 4); + } else { + char tmp[128]; + fgets(tmp, 128, f); + char * nl = strchr(tmp, '\n'); + if (nl) *nl = '\0'; + sethostname(tmp, strlen(tmp)); + } + +} + int start_options(char * args[]) { int pid = syscall_fork(); if (!pid) { @@ -41,8 +58,7 @@ int start_options(char * args[]) { int main(int argc, char * argv[]) { set_console(); - - syscall_sethostname("base"); + set_hostname(); if (argc > 1) { if (!strcmp(argv[1], "--vga")) { diff --git a/apps/login.c b/apps/login.c index 328932e2..ec9e02f1 100644 --- a/apps/login.c +++ b/apps/login.c @@ -11,7 +11,6 @@ #include #include -#include #include #include #include @@ -58,7 +57,7 @@ int main(int argc, char ** argv) { /* TODO: gethostname() */ char _hostname[256]; - syscall_gethostname(_hostname); + gethostname(_hostname, 255); fprintf(stdout, "%s login: ", _hostname); fflush(stdout); diff --git a/base/etc/hostname b/base/etc/hostname new file mode 100644 index 00000000..f1060b43 --- /dev/null +++ b/base/etc/hostname @@ -0,0 +1 @@ +livecd diff --git a/base/usr/include/unistd.h b/base/usr/include/unistd.h index f45d1997..c842a5f7 100644 --- a/base/usr/include/unistd.h +++ b/base/usr/include/unistd.h @@ -75,3 +75,6 @@ extern int chown(const char * pathname, uid_t owner, gid_t group); #define SEEK_SET 0 #define SEEK_CUR 1 #define SEEK_END 2 + +extern int gethostname(char * name, size_t len); +extern int sethostname(const char * name, size_t len); diff --git a/libc/main.c b/libc/main.c index 7554da5c..31e11172 100644 --- a/libc/main.c +++ b/libc/main.c @@ -21,8 +21,6 @@ DEFN_SYSCALL1(kernel_string_XXX, 25, char *); DEFN_SYSCALL0(reboot, 26); DEFN_SYSCALL3(readdir, 27, int, int, void *); DEFN_SYSCALL3(clone, 30, uintptr_t, uintptr_t, void *); -DEFN_SYSCALL1(sethostname, 31, char *); -DEFN_SYSCALL1(gethostname, 32, char *); DEFN_SYSCALL0(mousedevice, 33); DEFN_SYSCALL2(mkdir, 34, char *, unsigned int); DEFN_SYSCALL2(shm_obtain, 35, char *, size_t *); diff --git a/libc/unistd/hostname.c b/libc/unistd/hostname.c new file mode 100644 index 00000000..92774b8e --- /dev/null +++ b/libc/unistd/hostname.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +DEFN_SYSCALL1(sethostname, SYS_SETHOSTNAME, char *); +DEFN_SYSCALL1(gethostname, SYS_GETHOSTNAME, char *); + +int gethostname(char * name, size_t len) { + (void)len; /* TODO */ + __sets_errno(syscall_gethostname(name)); +} + +int sethostname(const char * name, size_t len) { + (void)len; /* TODO */ + __sets_errno(syscall_sethostname((char*)name)); +}