added sethostname syscall with proper error checks

Change-Id: I4b4e0a7c4035e19d5843b86ee1f714096adcecd2
This commit is contained in:
Morgawr
2014-04-24 02:12:48 +02:00
committed by Lionel Sambuc
parent b6bd719869
commit 91c835edc2
9 changed files with 375 additions and 6 deletions

View File

@@ -10,7 +10,7 @@
.if defined(__MINIX)
# Unsupported by Minix.
# closefrom.c confstr.c extattr.c getdevmajor.c \
# pthread_atfork.c setdomainname.c sethostname.c setproctitle.c \
# pthread_atfork.c setdomainname.c setproctitle.c \
# sysctl.c sysctlbyname.c sysctlgetmibinfo.c sysctlnametomib.c \
# wait3.c
@@ -39,7 +39,7 @@ SRCS+= _errno.c alarm.c alphasort.c arc4random.c assert.c basename.c clock.c \
psignal.c \
ptree.c pwcache.c pw_scan.c raise.c randomid.c rb.c readdir.c \
rewinddir.c scandir.c seekdir.c \
setjmperr.c setmode.c setprogname.c \
sethostname.c setjmperr.c setmode.c setprogname.c \
shquote.c shquotev.c sighold.c sigignore.c siginterrupt.c \
sysctl.c sysctlbyname.c sysctlgetmibinfo.c sysctlnametomib.c \
siglist.c signal.c signame.c sigrelse.c \

View File

@@ -7,20 +7,19 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <minix/paths.h>
#ifdef __weak_alias
__weak_alias(gethostname, _gethostname)
#endif
#define HOSTNAME_FILE "/etc/hostname.file"
int gethostname(char *buf, size_t len)
{
int fd;
int r;
char *nl;
if ((fd= open(HOSTNAME_FILE, O_RDONLY)) < 0) return -1;
if ((fd= open(_PATH_HOSTNAME_FILE, O_RDONLY)) < 0) return -1;
r= read(fd, buf, len);
close(fd);

View File

@@ -0,0 +1,64 @@
/* gethostname(2) system call emulation */
#include <sys/cdefs.h>
#include "namespace.h"
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <minix/paths.h>
#ifdef __weak_alias
__weak_alias(sethostname, _sethostname)
#endif
int sethostname(const char *buf, size_t len)
{
int fd;
int r;
int tmperr;
char name[20];
strlcpy(name, "/tmp/hostname.XXXXX",sizeof(name));
fd = mkstemp(name);
if (fd == -1)
return -1;
r = fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (r == -1) {
tmperr = errno;
close(fd);
unlink(name);
errno = tmperr;
return -1;
}
r = write(fd, buf, len);
tmperr = errno;
close(fd);
if (r == -1) {
unlink(name);
errno = tmperr;
return -1;
}
if (r < len) {
unlink(name);
errno = ENOSPC;
return -1;
}
r = rename(name, _PATH_HOSTNAME_FILE);
if (r == -1) {
tmperr = errno;
unlink(name);
errno = tmperr;
}
return 0;
}