user: implement mlibc as the libc, finally.
It's finally done.. Signed-off-by: kaguya <vpshinomiya@protonmail.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#include <errno.h>
|
||||
#include <arpa/nameser.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <mlibc/debug.hpp>
|
||||
|
||||
// The ns_get* and ns_put* functions are taken from musl.
|
||||
unsigned ns_get16(const unsigned char *cp) {
|
||||
return cp[0] << 8 | cp[1];
|
||||
}
|
||||
|
||||
unsigned long ns_get32(const unsigned char *cp) {
|
||||
return (unsigned)cp[0] << 24 | cp[1] << 16 | cp[2] << 8 | cp[3];
|
||||
}
|
||||
|
||||
void ns_put16(unsigned s, unsigned char *cp) {
|
||||
*cp++ = s >> 8;
|
||||
*cp++ = s;
|
||||
}
|
||||
|
||||
void ns_put32(unsigned long l, unsigned char *cp) {
|
||||
*cp++ = l >> 24;
|
||||
*cp++ = l >> 16;
|
||||
*cp++ = l >> 8;
|
||||
*cp++ = l;
|
||||
}
|
||||
|
||||
int ns_initparse(const unsigned char *, int, ns_msg *) {
|
||||
__ensure(!"Not implemented");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int ns_parserr(ns_msg *, ns_sect, int, ns_rr *) {
|
||||
__ensure(!"Not implemented");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
int ns_name_uncompress(const unsigned char *, const unsigned char *,
|
||||
const unsigned char *, char *, size_t) {
|
||||
__ensure(!"Not implemented");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <bits/ensure.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <mlibc/bsd-sysdeps.hpp>
|
||||
|
||||
int getloadavg(double *samples, int nsample) {
|
||||
auto sysdep = MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getloadavg, -1);
|
||||
if (nsample < 0) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (nsample > 3) {
|
||||
nsample = 3;
|
||||
}
|
||||
double s[3];
|
||||
if (int e = sysdep(s); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < nsample; i++) {
|
||||
samples[i] = s[i];
|
||||
}
|
||||
return nsample;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <bits/ensure.h>
|
||||
#include <netinet/ether.h>
|
||||
|
||||
char *ether_ntoa(const struct ether_addr *addr) {
|
||||
static char x[18];
|
||||
return ether_ntoa_r (addr, x);
|
||||
}
|
||||
|
||||
char *ether_ntoa_r(const struct ether_addr *addr, char *buf) {
|
||||
char *orig_ptr = buf;
|
||||
|
||||
for(int i = 0; i < ETH_ALEN; i++) {
|
||||
buf += sprintf(buf, i == 0 ? "%.2X" : ":%.2X", addr->ether_addr_octet[i]);
|
||||
}
|
||||
|
||||
return orig_ptr;
|
||||
}
|
||||
|
||||
struct ether_addr *ether_aton(const char *) {
|
||||
__ensure(!"Not implemented");
|
||||
__builtin_unreachable();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <getopt.h>
|
||||
|
||||
#if __MLIBC_GLIBC_OPTION
|
||||
|
||||
int __optreset = 0;
|
||||
extern int optreset __attribute__((__weak__, __alias__("__optreset")));
|
||||
|
||||
#endif //__MLIBC_GLIBC_OPTION
|
||||
@@ -0,0 +1,110 @@
|
||||
|
||||
#include <bits/ensure.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <pty.h>
|
||||
#include <utmp.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <mlibc/debug.hpp>
|
||||
#include <mlibc/posix-sysdeps.hpp>
|
||||
#include <mlibc/bsd-sysdeps.hpp>
|
||||
|
||||
int openpty(int *mfd, int *sfd, char *name, const struct termios *ios, const struct winsize *win) {
|
||||
if(mlibc::sys_openpty) {
|
||||
if(int e = mlibc::sys_openpty(mfd, sfd, name, ios, win); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ptmx_fd;
|
||||
if(int e = mlibc::sys_open("/dev/ptmx", O_RDWR | O_NOCTTY, 0, &ptmx_fd); e) {
|
||||
errno = e;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
char spath[32];
|
||||
if(!name)
|
||||
name = spath;
|
||||
if(ptsname_r(ptmx_fd, name, 32))
|
||||
goto fail;
|
||||
|
||||
int pts_fd;
|
||||
unlockpt(ptmx_fd);
|
||||
if(int e = mlibc::sys_open(name, O_RDWR | O_NOCTTY, 0, &pts_fd); e) {
|
||||
errno = e;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if(ios)
|
||||
tcsetattr(ptmx_fd, TCSAFLUSH, ios);
|
||||
|
||||
if(win)
|
||||
ioctl(ptmx_fd, TIOCSWINSZ, (void*)win);
|
||||
|
||||
*mfd = ptmx_fd;
|
||||
*sfd = pts_fd;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
mlibc::sys_close(ptmx_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int login_tty(int fd) {
|
||||
if(setsid() == -1)
|
||||
return -1;
|
||||
if(ioctl(fd, TIOCSCTTY, 0))
|
||||
return -1;
|
||||
|
||||
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_dup2, -1);
|
||||
if(int e = mlibc::sys_dup2(fd, 0, STDIN_FILENO); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
if(int e = mlibc::sys_dup2(fd, 0, STDOUT_FILENO); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
if(int e = mlibc::sys_dup2(fd, 0, STDERR_FILENO); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(int e = mlibc::sys_close(fd); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int forkpty(int *mfd, char *name, const struct termios *ios, const struct winsize *win) {
|
||||
int sfd;
|
||||
if(openpty(mfd, &sfd, name, ios, win))
|
||||
return -1;
|
||||
|
||||
pid_t child;
|
||||
MLIBC_CHECK_OR_ENOSYS(mlibc::sys_fork, -1);
|
||||
if(int e = mlibc::sys_fork(&child); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(!child) {
|
||||
if(login_tty(sfd))
|
||||
mlibc::panicLogger() << "mlibc: TTY login fail in forkpty() child" << frg::endlog;
|
||||
}else{
|
||||
if(int e = mlibc::sys_close(sfd); e) {
|
||||
errno = e;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user