get/sethostname

This commit is contained in:
K. Lange 2018-08-02 03:40:43 +09:00
parent 2078efccd7
commit bd3f1b0334
8 changed files with 50 additions and 19 deletions

View File

@ -14,7 +14,6 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <syscall.h>
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/wait.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[]) { static void get_updated_hostname_with_time_info(char hostname[]) {
// get hostname // get hostname
char _hostname[256]; char _hostname[256];
syscall_gethostname(_hostname); gethostname(_hostname, 255);
// get current time // get current time
struct tm * timeinfo; struct tm * timeinfo;

View File

@ -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 * of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2013-2018 K. Lange * Copyright (C) 2013-2018 K. Lange
*/
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* *
* hostname * hostname
* *
* Prints or sets the system hostname. * Prints or sets the system hostname.
*/ */
#include <stdio.h> #include <stdio.h>
#include <syscall.h> #include <unistd.h>
#include <string.h>
#define ROOT_UID 0
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
if ((argc > 1 && argv[1][0] == '-') || (argc < 2)) { if ((argc > 1 && argv[1][0] == '-') || (argc < 2)) {
char tmp[256]; char tmp[256] = {0};
syscall_gethostname(tmp); gethostname(tmp, 255);
printf("%s\n", tmp); printf("%s\n", tmp);
return 0; return 0;
} else { } else {
if (syscall_getuid() != ROOT_UID) { if (getuid() != 0) {
fprintf(stderr,"Must be root to set hostname.\n"); fprintf(stderr,"Must be root to set hostname.\n");
return 1; return 1;
} else { } else {
syscall_sethostname(argv[1]); sethostname(argv[1], strlen(argv[1]));
FILE * file = fopen("/etc/hostname", "w"); FILE * file = fopen("/etc/hostname", "w");
if (!file) { if (!file) {
return 1; return 1;

View File

@ -3,8 +3,9 @@
#include <errno.h> #include <errno.h>
#include <wait.h> #include <wait.h>
#include <string.h> #include <string.h>
#include <unistd.h>
void set_console() { void set_console(void) {
int _stdin = syscall_open("/dev/null", 0, 0); int _stdin = syscall_open("/dev/null", 0, 0);
int _stdout = syscall_open("/dev/ttyS0", 1, 0); int _stdout = syscall_open("/dev/ttyS0", 1, 0);
int _stderr = syscall_open("/dev/ttyS0", 1, 0); int _stderr = syscall_open("/dev/ttyS0", 1, 0);
@ -18,6 +19,22 @@ void set_console() {
(void)_stdin; (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 start_options(char * args[]) {
int pid = syscall_fork(); int pid = syscall_fork();
if (!pid) { if (!pid) {
@ -41,8 +58,7 @@ int start_options(char * args[]) {
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
set_console(); set_console();
set_hostname();
syscall_sethostname("base");
if (argc > 1) { if (argc > 1) {
if (!strcmp(argv[1], "--vga")) { if (!strcmp(argv[1], "--vga")) {

View File

@ -11,7 +11,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <syscall.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -58,7 +57,7 @@ int main(int argc, char ** argv) {
/* TODO: gethostname() */ /* TODO: gethostname() */
char _hostname[256]; char _hostname[256];
syscall_gethostname(_hostname); gethostname(_hostname, 255);
fprintf(stdout, "%s login: ", _hostname); fprintf(stdout, "%s login: ", _hostname);
fflush(stdout); fflush(stdout);

1
base/etc/hostname Normal file
View File

@ -0,0 +1 @@
livecd

View File

@ -75,3 +75,6 @@ extern int chown(const char * pathname, uid_t owner, gid_t group);
#define SEEK_SET 0 #define SEEK_SET 0
#define SEEK_CUR 1 #define SEEK_CUR 1
#define SEEK_END 2 #define SEEK_END 2
extern int gethostname(char * name, size_t len);
extern int sethostname(const char * name, size_t len);

View File

@ -21,8 +21,6 @@ DEFN_SYSCALL1(kernel_string_XXX, 25, char *);
DEFN_SYSCALL0(reboot, 26); DEFN_SYSCALL0(reboot, 26);
DEFN_SYSCALL3(readdir, 27, int, int, void *); DEFN_SYSCALL3(readdir, 27, int, int, void *);
DEFN_SYSCALL3(clone, 30, uintptr_t, uintptr_t, 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_SYSCALL0(mousedevice, 33);
DEFN_SYSCALL2(mkdir, 34, char *, unsigned int); DEFN_SYSCALL2(mkdir, 34, char *, unsigned int);
DEFN_SYSCALL2(shm_obtain, 35, char *, size_t *); DEFN_SYSCALL2(shm_obtain, 35, char *, size_t *);

17
libc/unistd/hostname.c Normal file
View File

@ -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));
}