Browse Source

get/sethostname

K. Lange 4 years ago
parent
commit
bd3f1b0334
8 changed files with 50 additions and 19 deletions
  1. 1 2
      apps/glogin-provider.c
  2. 8 10
      apps/hostname.c
  3. 19 3
      apps/init.c
  4. 1 2
      apps/login.c
  5. 1 0
      base/etc/hostname
  6. 3 0
      base/usr/include/unistd.h
  7. 0 2
      libc/main.c
  8. 17 0
      libc/unistd/hostname.c

+ 1 - 2
apps/glogin-provider.c

@@ -14,7 +14,6 @@
 #include <math.h>
 #include <string.h>
 #include <time.h>
-#include <syscall.h>
 
 #include <sys/utsname.h>
 #include <sys/wait.h>
@@ -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;

+ 8 - 10
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 <stdio.h>
-#include <syscall.h>
-
-#define ROOT_UID 0
+#include <unistd.h>
+#include <string.h>
 
 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;

+ 19 - 3
apps/init.c

@@ -3,8 +3,9 @@
 #include <errno.h>
 #include <wait.h>
 #include <string.h>
+#include <unistd.h>
 
-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")) {

+ 1 - 2
apps/login.c

@@ -11,7 +11,6 @@
 
 #include <stdio.h>
 #include <stdint.h>
-#include <syscall.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -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);

+ 1 - 0
base/etc/hostname

@@ -0,0 +1 @@
+livecd

+ 3 - 0
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);

+ 0 - 2
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 *);

+ 17 - 0
libc/unistd/hostname.c

@@ -0,0 +1,17 @@
+#include <unistd.h>
+#include <syscall.h>
+#include <syscall_nums.h>
+#include <errno.h>
+
+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));
+}