Add getdnsconfig(2) and setdnsconfig(2).

This commit is contained in:
Jonas 'Sortie' Termansen 2016-07-26 02:33:40 +02:00
parent 23ddc536bc
commit 3b036b6c5d
9 changed files with 204 additions and 1 deletions

View File

@ -91,6 +91,7 @@ disk/ata/ata.o \
disk/ata/hba.o \
disk/ata/port.o \
disk/node.o \
dnsconfig.o \
dtable.o \
elf.o \
fcache.o \

77
kernel/dnsconfig.cpp Normal file
View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2016 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* dnsconfig.cpp
* System calls for managing the dns configuration of the current system.
*/
#include <sys/dnsconfig.h>
#include <errno.h>
#include <string.h>
#include <sortix/kernel/copy.h>
#include <sortix/kernel/kernel.h>
#include <sortix/kernel/kthread.h>
#include <sortix/kernel/syscall.h>
namespace Sortix {
static kthread_mutex_t dnsconfig_lock = KTHREAD_MUTEX_INITIALIZER;
static struct dnsconfig dnsconfig;
int sys_getdnsconfig(struct dnsconfig* user_cfg)
{
ScopedLock lock(&dnsconfig_lock);
if ( !CopyToUser(user_cfg, &dnsconfig, sizeof(dnsconfig)) )
return -1;
return 0;
}
int sys_setdnsconfig(const struct dnsconfig* user_cfg)
{
ScopedLock lock(&dnsconfig_lock);
struct dnsconfig newcfg;
if ( !CopyFromUser(&newcfg, user_cfg, sizeof(dnsconfig)) )
return -1;
if ( DNSCONFIG_MAX_SERVERS < newcfg.servers_count )
return errno = EINVAL, -1;
for ( size_t i = 0; i < newcfg.servers_count; i++ )
{
struct dnsconfig_server* server = &newcfg.servers[i];
if ( server->family == AF_INET )
{
if ( server->addrsize != sizeof(struct in_addr) )
return errno = EINVAL, -1;
memset(&server->addr.in + 1, 0,
sizeof(server->addr) - sizeof(server->addr.in));
}
else if ( server->family == AF_INET6 )
{
if ( server->addrsize != sizeof(struct in6_addr) )
return errno = EINVAL, -1;
memset(&server->addr.in6 + 1, 0,
sizeof(server->addr) - sizeof(server->addr.in6));
}
else
return errno = EAFNOSUPPORT, -1;
}
for ( size_t i = newcfg.servers_count; i < DNSCONFIG_MAX_SERVERS; i++ )
memset(&newcfg.servers[i], 0, sizeof(newcfg.servers[i]));
memcpy(&dnsconfig, &newcfg, sizeof(dnsconfig));
return 0;
}
} // namespace Sortix

View File

@ -20,6 +20,7 @@
#ifndef INCLUDE_SORTIX_KERNEL_SYSCALL_H
#define INCLUDE_SORTIX_KERNEL_SYSCALL_H
#include <sys/dnsconfig.h>
#include <sys/socket.h>
#include <sys/types.h>
@ -89,6 +90,7 @@ int sys_fstatvfsat(int, const char*, struct statvfs*, int);
int sys_fsync(int);
int sys_ftruncate(int, off_t);
int sys_futimens(int, const struct timespec*);
int sys_getdnsconfig(struct dnsconfig*);
gid_t sys_getegid(void);
int sys_getentropy(void*, size_t);
uid_t sys_geteuid(void);
@ -142,6 +144,7 @@ void sys_scram(int, const void*);
int sys_sched_yield(void);
ssize_t sys_send(int, const void*, size_t, int);
ssize_t sys_sendmsg(int, const struct msghdr*, int);
int sys_setdnsconfig(const struct dnsconfig*);
int sys_setegid(gid_t);
int sys_seteuid(uid_t);
int sys_setgid(gid_t);

View File

@ -186,6 +186,8 @@
#define SYSCALL_GETSID 163
#define SYSCALL_SETSID 164
#define SYSCALL_SOCKET 165
#define SYSCALL_MAX_NUM 166 /* index of highest constant + 1 */
#define SYSCALL_GETDNSCONFIG 166
#define SYSCALL_SETDNSCONFIG 167
#define SYSCALL_MAX_NUM 168 /* index of highest constant + 1 */
#endif

View File

@ -200,6 +200,8 @@ void* syscall_list[SYSCALL_MAX_NUM + 1] =
[SYSCALL_GETSID] = (void*) sys_getsid,
[SYSCALL_SETSID] = (void*) sys_setsid,
[SYSCALL_SOCKET] = (void*) sys_socket,
[SYSCALL_GETDNSCONFIG] = (void*) sys_getdnsconfig,
[SYSCALL_SETDNSCONFIG] = (void*) sys_setdnsconfig,
[SYSCALL_MAX_NUM] = (void*) sys_bad_syscall,
};
} /* extern "C" */

View File

@ -555,6 +555,8 @@ stdlib/system.o \
stdlib/unlockpt.o \
stdlib/unsetenv.o \
sys/display/dispmsg_issue.o \
sys/dnsconfig/getdnsconfig.o \
sys/dnsconfig/setdnsconfig.o \
sys/ioctl/ioctl.o \
sys/kernelinfo/kernelinfo.o \
syslog/closelog.o \

View File

@ -0,0 +1,60 @@
/*
* Copyright (c) 2016 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* sys/dnsconfig.h
* Domain name system configuration.
*/
#ifndef INCLUDE_SYS_DNSCONFIG_H
#define INCLUDE_SYS_DNSCONFIG_H
#include <sys/cdefs.h>
#include <netinet/in.h>
#include <stddef.h>
union dnsconfig_server_union
{
struct in_addr in;
struct in6_addr in6;
};
struct dnsconfig_server
{
sa_family_t family;
size_t addrsize;
union dnsconfig_server_union addr;
};
#define DNSCONFIG_MAX_SERVERS 3
struct dnsconfig
{
size_t servers_count;
struct dnsconfig_server servers[DNSCONFIG_MAX_SERVERS];
};
#ifdef __cplusplus
extern "C" {
#endif
int getdnsconfig(struct dnsconfig*);
int setdnsconfig(const struct dnsconfig*);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2016 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* sys/dnsconfig/getdnsconfig.c
* Get the domain name system configuration.
*/
#include <sys/dnsconfig.h>
#include <sys/syscall.h>
DEFN_SYSCALL1(int, sys_getdnsconfig, SYSCALL_GETDNSCONFIG, struct dnsconfig*);
int getdnsconfig(struct dnsconfig* cfg)
{
return sys_getdnsconfig(cfg);
}

View File

@ -0,0 +1,28 @@
/*
* Copyright (c) 2016 Jonas 'Sortie' Termansen.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* sys/dnsconfig/setdnsconfig.c
* Set the domain name system configuration.
*/
#include <sys/dnsconfig.h>
#include <sys/syscall.h>
DEFN_SYSCALL1(int, sys_setdnsconfig, SYSCALL_SETDNSCONFIG, const struct dnsconfig*);
int setdnsconfig(const struct dnsconfig* cfg)
{
return sys_setdnsconfig(cfg);
}