Kernel: Added the ability to set the hostname via new syscall

Userland/hostname: Now takes parameter to set the hostname
LibC/unistd: Added sethostname function
This commit is contained in:
Luke Payne 2020-04-26 15:59:47 +10:00 committed by Andreas Kling
parent 03f68a51af
commit f191b84b50
Notes: sideshowbarker 2024-07-19 07:17:18 +09:00
7 changed files with 40 additions and 9 deletions

View File

@ -712,6 +712,18 @@ int Process::sys$gethostname(char* buffer, ssize_t size)
return 0;
}
int Process::sys$sethostname(const char* hostname, ssize_t length)
{
REQUIRE_PROMISE(stdio);
if (length < 0)
return -EINVAL;
LOCKER(*s_hostname_lock, Lock::Mode::Shared);
if (length > 64)
return -ENAMETOOLONG;
*s_hostname = validate_and_copy_string_from_user(hostname, length);
return 0;
}
pid_t Process::sys$fork(RegisterState& regs)
{
REQUIRE_PROMISE(proc);

View File

@ -223,6 +223,7 @@ public:
int sys$clock_settime(clockid_t, timespec*);
int sys$clock_nanosleep(const Syscall::SC_clock_nanosleep_params*);
int sys$gethostname(char*, ssize_t);
int sys$sethostname(const char*, ssize_t);
int sys$uname(utsname*);
int sys$readlink(const Syscall::SC_readlink_params*);
int sys$ttyname_r(int fd, char*, ssize_t);

View File

@ -63,6 +63,7 @@ namespace Kernel {
__ENUMERATE_SYSCALL(getcwd) \
__ENUMERATE_SYSCALL(gettimeofday) \
__ENUMERATE_SYSCALL(gethostname) \
__ENUMERATE_SYSCALL(sethostname) \
__ENUMERATE_SYSCALL(chdir) \
__ENUMERATE_SYSCALL(uname) \
__ENUMERATE_SYSCALL(set_mmap_name) \

View File

@ -352,6 +352,12 @@ int gethostname(char* buffer, size_t size)
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int sethostname(const char* hostname, ssize_t size)
{
int rc = syscall(SC_sethostname, hostname, size);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
ssize_t readlink(const char* path, char* buffer, size_t size)
{
Syscall::SC_readlink_params params { { path, strlen(path) }, { buffer, size } };

View File

@ -104,6 +104,7 @@ int stat(const char* path, struct stat* statbuf);
int sleep(unsigned seconds);
int usleep(useconds_t);
int gethostname(char*, size_t);
int sethostname(const char*, ssize_t);
ssize_t readlink(const char* path, char* buffer, size_t);
char* ttyname(int fd);
int ttyname_r(int fd, char* buffer, size_t);

View File

@ -36,7 +36,7 @@ struct GlobalState {
String username;
String home;
char ttyname[32];
char hostname[32];
char hostname[64];
uid_t uid;
struct termios termios;
struct termios default_termios;

View File

@ -36,14 +36,24 @@ int main(int argc, char** argv)
return 1;
}
(void)argc;
(void)argv;
char buffer[HOST_NAME_MAX];
int rc = gethostname(buffer, sizeof(buffer));
if (rc < 0) {
printf("gethostname() error: %s\n", strerror(errno));
return 1;
if (argc == 1) {
char buffer[HOST_NAME_MAX];
int rc = gethostname(buffer, sizeof(buffer));
if (rc < 0) {
printf("gethostname() error: %s\n", strerror(errno));
return 1;
}
printf("%s\n", buffer);
}
printf("%s\n", buffer);
else if (argc == 2) {
if (strlen(argv[1]) >= HOST_NAME_MAX) {
printf("hostname must be less than %i characters\n", HOST_NAME_MAX);
return 1;
}
else {
sethostname(argv[1], strlen(argv[1]));
}
}
return 0;
}