init, get/sethostname

This commit is contained in:
Kevin Lange 2012-01-30 12:10:53 -06:00
parent 84bbcf5f95
commit 238278c3fc
5 changed files with 93 additions and 10 deletions

View File

@ -136,21 +136,16 @@ int main(struct multiboot *mboot, uint32_t mboot_mag, uintptr_t esp) {
parse_args(cmdline); parse_args(cmdline);
} }
/* /* Prepare to run /bin/init */
* XXX: Execute /bin/init
*/
char * argv[] = { char * argv[] = {
"terminal", "/bin/init",
#if 1
"-f",
#endif
NULL NULL
}; };
int argc = 0; int argc = 0;
while (argv[argc]) { while (argv[argc]) {
argc++; argc++;
} }
system("/bin/terminal", argc, argv); /* Run init */ system(argv[0], argc, argv); /* Run init */
return 0; return 0;
} }

View File

@ -340,6 +340,28 @@ static char * getcwd(char * buf, size_t size) {
return buf; return buf;
} }
static char hostname[256];
static size_t hostname_len = 0;
static int sethostname(char * new_hostname) {
if (current_process->user == USER_ROOT_UID) {
size_t len = strlen(new_hostname) + 1;
if (len > 256) {
return 1;
}
hostname_len = len;
memcpy(hostname, new_hostname, hostname_len);
return 0;
} else {
return 1;
}
}
static int gethostname(char * buffer) {
memcpy(buffer, hostname, hostname_len);
return hostname_len;
}
/* /*
* System Call Internals * System Call Internals
*/ */
@ -377,6 +399,8 @@ static uintptr_t syscalls[] = {
(uintptr_t)&chdir, /* 28 */ (uintptr_t)&chdir, /* 28 */
(uintptr_t)&getcwd, (uintptr_t)&getcwd,
(uintptr_t)&clone, (uintptr_t)&clone,
(uintptr_t)&sethostname,
(uintptr_t)&gethostname, /* 32 */
0 0
}; };
uint32_t num_syscalls; uint32_t num_syscalls;

View File

@ -18,6 +18,7 @@ DEFN_SYSCALL2(getcwd, 29, char *, size_t);
DEFN_SYSCALL1(chdir, 28, char *); DEFN_SYSCALL1(chdir, 28, char *);
DEFN_SYSCALL0(getuid, 23); DEFN_SYSCALL0(getuid, 23);
DEFN_SYSCALL0(gethostname, 32);
#define LINE_LEN 4096 #define LINE_LEN 4096
@ -28,6 +29,7 @@ struct timeval {
}; };
char username[1024]; char username[1024];
char _hostname[256];
void getusername() { void getusername() {
FILE * passwd = fopen("/etc/passwd", "r"); FILE * passwd = fopen("/etc/passwd", "r");
@ -54,6 +56,12 @@ void getusername() {
fclose(passwd); fclose(passwd);
} }
void gethostname() {
char buffer[256];
size_t len = syscall_gethostname(buffer);
memcpy(_hostname, buffer, len);
}
void draw_prompt(int ret) { void draw_prompt(int ret) {
struct tm * timeinfo; struct tm * timeinfo;
struct timeval now; struct timeval now;
@ -64,7 +72,7 @@ void draw_prompt(int ret) {
char time_buffer[80]; char time_buffer[80];
strftime(time_buffer, 80, "%H:%M:%S", timeinfo); strftime(time_buffer, 80, "%H:%M:%S", timeinfo);
printf("\033[1m[\033[1;33m%s \033[1;32m%s \033[1;31m%s \033[1;34m%s\033[0m ", printf("\033[1m[\033[1;33m%s \033[1;32m%s \033[1;31m%s \033[1;34m%s\033[0m ",
username, "esh", date_buffer, time_buffer); username, _hostname, date_buffer, time_buffer);
if (ret != 0) { if (ret != 0) {
printf("\033[1;31m%d ", ret); printf("\033[1;31m%d ", ret);
} }
@ -114,6 +122,7 @@ int main(int argc, char ** argv) {
int last_ret = 0; int last_ret = 0;
getusername(); getusername();
gethostname();
FILE * motd = fopen("/etc/motd", "r"); FILE * motd = fopen("/etc/motd", "r");
if (motd) { if (motd) {

53
userspace/init.c Normal file
View File

@ -0,0 +1,53 @@
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* Initial Startup
*/
#include <stdio.h>
#include <syscall.h>
DEFN_SYSCALL1(sethostname, 31, char *);
#define DEFAULT_HOSTNAME "toaru-test"
#define FORK_FOR_TERMINAL 0
#define FREETYPE 1
/* Set the hostname to whatever is in /etc/hostname */
void set_hostname() {
FILE * _host_file = fopen("/etc/hostname", "r");
if (!_host_file) {
/* No /etc/hostname, use the default */
syscall_sethostname(DEFAULT_HOSTNAME);
} else {
char buf[256];
fgets(buf, 255, _host_file);
syscall_sethostname(buf);
fclose(_host_file);
}
}
void start_terminal() {
#if FORK_FOR_TERMINAL
int pid = fork();
if (!pid) {
#endif
char * tokens[] = {
"/bin/terminal",
#if FREETYPE
"-f",
#endif
NULL
};
int i = execve(tokens[0], tokens, NULL);
exit(0);
#if FORK_FOR_TEMRINAL
}
#endif
}
void main(int argc, char * argv[]) {
/* Hostname */
set_hostname();
/* Terminal */
start_terminal();
}

View File

@ -17,6 +17,7 @@
DEFN_SYSCALL1(wait, 17, unsigned int); DEFN_SYSCALL1(wait, 17, unsigned int);
DEFN_SYSCALL1(setuid, 24, unsigned int); DEFN_SYSCALL1(setuid, 24, unsigned int);
DEFN_SYSCALL1(kernel_string_XXX, 25, char *); DEFN_SYSCALL1(kernel_string_XXX, 25, char *);
DEFN_SYSCALL0(gethostname, 32);
int readline(char * buf, size_t size, uint8_t display) { int readline(char * buf, size_t size, uint8_t display) {
size_t collected = 0; size_t collected = 0;
@ -104,7 +105,8 @@ int main(int argc, char ** argv) {
char * password = malloc(sizeof(char) * 1024); char * password = malloc(sizeof(char) * 1024);
/* TODO: gethostname() */ /* TODO: gethostname() */
char * _hostname = "test"; char _hostname[256];
syscall_gethostname(_hostname);
fprintf(stdout, "%s login: ", _hostname); fprintf(stdout, "%s login: ", _hostname);
fflush(stdout); fflush(stdout);