Get us closer to building busybox

This change undefines __linux__ and adds APIs like clock_settime(). The
gosh darned getopt_long() API has been reintroduced, thanks to OpenBSD.
This commit is contained in:
Justine Tunney 2023-06-18 04:02:01 -07:00
parent 5ac522f0de
commit 0409096658
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
52 changed files with 914 additions and 37 deletions

View file

@ -91,3 +91,17 @@ int cfsetispeed(struct termios *t, uint32_t speed) {
return 0;
}
}
/**
* Sets input and output baud rate.
*
* @param speed can be `B0`, `B50`, `B38400`, `B4000000`, etc.
* @return 0 on success, or -1 w/ errno
* @raise EINVAL if `speed` isn't valid
* @asyncsignalsafe
*/
int cfsetspeed(struct termios *t, uint32_t speed) {
if (cfsetispeed(t, speed) == -1) return -1;
if (cfsetospeed(t, speed) == -1) return -1;
return 0;
}

View file

@ -0,0 +1,53 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or 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.
*/
#include "libc/calls/asan.internal.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timespec.internal.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h"
#include "libc/dce.h"
#include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/errfuns.h"
/**
* Changes time.
*/
int clock_settime(int clockid, const struct timespec *ts) {
int rc;
struct timeval tv;
if (clockid == 127) {
rc = einval(); // 127 is used by consts.sh to mean unsupported
} else if (!ts || (IsAsan() && !__asan_is_valid_timespec(ts))) {
rc = efault();
} else if (IsXnu()) {
if (clockid == CLOCK_REALTIME) {
tv = timespec_totimeval(*ts);
rc = sys_settimeofday(&tv, 0);
} else {
rc = einval();
}
} else {
rc = sys_clock_settime(clockid, ts);
}
STRACE("clock_settime(%s, %s) → %d% m", DescribeClockName(clockid),
DescribeTimespec(0, ts), rc);
return rc;
}

39
libc/calls/settimeofday.c Normal file
View file

@ -0,0 +1,39 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or 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.
*/
#include "libc/calls/asan.internal.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/timeval.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/struct/timezone.h"
/**
* Changes time.
*/
int settimeofday(const struct timeval *tv, const struct timezone *tz) {
int rc;
if (!tv || (IsAsan() && !__asan_is_valid_timeval(tv))) {
rc = efault();
} else {
rc = sys_settimeofday(tv, 0);
}
STRACE("settimeofday(%s, %p) → %d% m", DescribeTimeval(0, tv), tz, rc);
return rc;
}

View file

@ -10,6 +10,7 @@ struct timespec {
int clock_getres(int, struct timespec *);
int clock_gettime(int, struct timespec *);
int clock_settime(int, const struct timespec *);
int clock_nanosleep(int, int, const struct timespec *, struct timespec *);
int futimens(int, const struct timespec[2]);
int nanosleep(const struct timespec *, struct timespec *);

View file

@ -10,6 +10,7 @@ int __sys_clock_nanosleep(int, int, const struct timespec *, struct timespec *)
int __sys_utimensat(int, const char *, const struct timespec[2], int) _Hide;
int __utimens(int, const char *, const struct timespec[2], int) _Hide;
int sys_clock_getres(int, struct timespec *) _Hide;
int sys_clock_settime(int, const struct timespec *);
int sys_clock_gettime(int, struct timespec *) _Hide;
int sys_clock_gettime_nt(int, struct timespec *) _Hide;
int sys_clock_gettime_m1(int, struct timespec *) _Hide;

View file

@ -13,6 +13,7 @@ struct timeval {
int futimes(int, const struct timeval[2]);
int futimesat(int, const char *, const struct timeval[2]);
int gettimeofday(struct timeval *, struct timezone *);
int settimeofday(const struct timeval *, const struct timezone *);
int lutimes(const char *, const struct timeval[2]);
int utimes(const char *, const struct timeval[2]);

View file

@ -7,6 +7,7 @@
COSMOPOLITAN_C_START_
axdx_t sys_gettimeofday(struct timeval *, struct timezone *, void *) _Hide;
int sys_settimeofday(const struct timeval *, const struct timezone *) _Hide;
int sys_futimes(int, const struct timeval *) _Hide;
int sys_lutimes(const char *, const struct timeval *) _Hide;
int sys_utimes(const char *, const struct timeval *) _Hide;

View file

@ -29,8 +29,9 @@ int tcflow(int, int);
int tcflush(int, int);
int tcsendbreak(int, int);
void cfmakeraw(struct termios *);
int cfsetospeed(struct termios *, unsigned);
int cfsetispeed(struct termios *, unsigned);
int cfsetspeed(struct termios *, uint32_t);
int cfsetospeed(struct termios *, uint32_t);
int cfsetispeed(struct termios *, uint32_t);
uint32_t cfgetospeed(const struct termios *);
uint32_t cfgetispeed(const struct termios *);
int tcsetwinsize(int, const struct winsize *);

View file

@ -86,9 +86,10 @@ libcesque nosideeffect;
cosmopolitan § conversion » manipulation
*/
#ifdef COSMO
char *dirname(char *);
char *basename(char *);
#ifdef COSMO
char *stripext(char *);
char *stripexts(char *);
#endif

View file

@ -62,6 +62,14 @@
#undef linux
#endif
#ifdef __linux
#undef __linux
#endif
#ifdef __linux__
#undef __linux__
#endif
#ifndef __BIGGEST_ALIGNMENT__
#define __BIGGEST_ALIGNMENT__ 16
#endif

View file

@ -1,4 +1,5 @@
#ifndef _GETOPT_H
#define _GETOPT_H
#include "third_party/getopt/getopt.h"
#include "third_party/getopt/long.h"
#endif /* _GETOPT_H */

View file

@ -1,4 +1,5 @@
#ifndef _LIBGEN_H
#define _LIBGEN_H
#include "libc/fmt/conv.h"
#endif /* _LIBGEN_H */

View file

@ -0,0 +1,5 @@
#ifndef _NET_ETHERNET_H
#define _NET_ETHERNET_H
#include "libc/sock/struct/ether_header.h"
#include "libc/sysv/consts/ethernet.h"
#endif /* _NET_ETHERNET_H */

View file

@ -0,0 +1,6 @@
#ifndef _NET_IF_ARP_H
#define _NET_IF_ARP_H
#include "libc/sock/struct/arphdr.h"
#include "libc/sock/struct/arpreq.h"
#include "libc/sysv/consts/arp.h"
#endif /* _NET_IF_ARP_H */

View file

@ -1,5 +1,22 @@
#ifndef _NETDB_H
#define _NETDB_H
#include "libc/calls/weirdtypes.h"
#include "libc/dns/dns.h"
#include "libc/dns/ent.h"
#include "libc/sock/sock.h"
#include "libc/sock/struct/in6_pktinfo.h"
#include "libc/sock/struct/in_pktinfo.h"
#include "libc/sock/struct/ip_mreq.h"
#include "libc/sock/struct/sockaddr.h"
#include "libc/sock/struct/sockaddr6.h"
#include "libc/sysv/consts/in.h"
#include "libc/sysv/consts/inaddr.h"
#include "libc/sysv/consts/inet6.h"
#include "libc/sysv/consts/ip.h"
#include "libc/sysv/consts/ipport.h"
#include "libc/sysv/consts/ipproto.h"
#include "libc/sysv/consts/ipv6.h"
#include "libc/sysv/consts/mcast.h"
#include "libc/sysv/consts/pf.h"
#include "libc/sysv/consts/sock.h"
#endif /* _NETDB_H */

View file

@ -2,6 +2,8 @@
#define COSMOPOLITAN_LIBC_ISYSTEM_NETINET_IN_H_
#include "libc/calls/weirdtypes.h"
#include "libc/sock/sock.h"
#include "libc/sock/struct/in6_pktinfo.h"
#include "libc/sock/struct/in_pktinfo.h"
#include "libc/sock/struct/ip_mreq.h"
#include "libc/sock/struct/sockaddr.h"
#include "libc/sock/struct/sockaddr6.h"

View file

@ -5,6 +5,7 @@
#include "libc/fmt/conv.h"
#include "libc/limits.h"
#include "libc/mem/alg.h"
#include "libc/mem/alloca.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/dprintf.h"

View file

@ -5,6 +5,7 @@
#include "libc/calls/struct/winsize.h"
#include "libc/sysv/consts/fd.h"
#include "libc/sysv/consts/fio.h"
#include "libc/sysv/consts/io.h"
#include "libc/sysv/consts/modem.h"
#include "libc/sysv/consts/pty.h"
#include "libc/sysv/consts/sio.h"

24
libc/mem/mallopt.c Normal file
View file

@ -0,0 +1,24 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2023 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or 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.
*/
#include "libc/mem/mem.h"
#include "third_party/dlmalloc/dlmalloc.h"
int mallopt(int param_number, int value) {
return dlmallopt(param_number, value);
}

View file

@ -28,6 +28,7 @@ void *aligned_alloc(size_t, size_t) attributeallocalign((1))
int posix_memalign(void **, size_t, size_t);
bool __grow(void *, size_t *, size_t, size_t) paramsnonnull((1, 2)) libcesque;
int mallopt(int, int);
int malloc_trim(size_t);
size_t bulk_free(void **, size_t);
size_t malloc_usable_size(void *);

16
libc/sock/struct/arphdr.h Normal file
View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_ARPHDR_H_
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_ARPHDR_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct arphdr {
uint16_t ar_hrd;
uint16_t ar_pro;
uint8_t ar_hln;
uint8_t ar_pln;
uint16_t ar_op;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_ARPHDR_H_ */

17
libc/sock/struct/arpreq.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_ARPREQ_H_
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_ARPREQ_H_
#include "libc/sock/struct/sockaddr.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct arpreq {
struct sockaddr arp_pa;
struct sockaddr arp_ha;
int arp_flags;
struct sockaddr arp_netmask;
char arp_dev[16];
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_ARPREQ_H_ */

View file

@ -0,0 +1,21 @@
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_ETHER_HEADER_H_
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_ETHER_HEADER_H_
#define ETH_ALEN 6
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct ether_addr {
uint8_t ether_addr_octet[ETH_ALEN];
};
struct ether_header {
uint8_t ether_dhost[ETH_ALEN];
uint8_t ether_shost[ETH_ALEN];
uint16_t ether_type;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_ETHER_HEADER_H_ */

View file

@ -0,0 +1,14 @@
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_IN6_PKTINFO_H_
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_IN6_PKTINFO_H_
#include "libc/sock/struct/sockaddr6.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct in6_pktinfo {
struct in6_addr ipi6_addr;
unsigned ipi6_ifindex;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_IN6_PKTINFO_H_ */

View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_SOCK_STRUCT_IN_PKTINFO_H_
#define COSMOPOLITAN_LIBC_SOCK_STRUCT_IN_PKTINFO_H_
#include "libc/sock/struct/sockaddr.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct in_pktinfo {
int ipi_ifindex;
struct in_addr ipi_spec_dst;
struct in_addr ipi_addr;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_SOCK_STRUCT_IN_PKTINFO_H_ */

View file

@ -33,7 +33,7 @@
#endif
#if defined(__cplusplus) && __cplusplus >= 201103L
#include <type_traits>
#include "third_party/libcxx/type_traits"
#define __ckd_is_unsigned(res) std::is_unsigned<decltype(*(res))>::value
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define __ckd_is_unsigned(res) \

View file

@ -1,2 +1,2 @@
#include "libc/sysv/macros.internal.h"
.scall sys_clock_settime,0x1ac0580e9ffff0e3,112,4095,globl
.scall sys_clock_settime,0x1ac0580e9ffff0e3,112,4095,globl,hidden

6
libc/sysv/consts/arp.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_ARP_H_
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_ARP_H_
#define ARPHRD_ETHER 1
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_ARP_H_ */

View file

@ -0,0 +1,32 @@
#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_ETHERNET_H_
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_ETHERNET_H_
#define ETHERTYPE_PUP 0x0200
#define ETHERTYPE_SPRITE 0x0500
#define ETHERTYPE_IP 0x0800
#define ETHERTYPE_ARP 0x0806
#define ETHERTYPE_REVARP 0x8035
#define ETHERTYPE_AT 0x809B
#define ETHERTYPE_AARP 0x80F3
#define ETHERTYPE_VLAN 0x8100
#define ETHERTYPE_IPX 0x8137
#define ETHERTYPE_IPV6 0x86dd
#define ETHERTYPE_LOOPBACK 0x9000
#define ETHER_ADDR_LEN ETH_ALEN
#define ETHER_TYPE_LEN 2
#define ETHER_CRC_LEN 4
#define ETHER_HDR_LEN ETH_HLEN
#define ETHER_MIN_LEN (ETH_ZLEN + ETHER_CRC_LEN)
#define ETHER_MAX_LEN (ETH_FRAME_LEN + ETHER_CRC_LEN)
#define ETHER_IS_VALID_LEN(foo) \
((foo) >= ETHER_MIN_LEN && (foo) <= ETHER_MAX_LEN)
#define ETHERTYPE_TRAIL 0x1000
#define ETHERTYPE_NTRAILER 16
#define ETHERMTU ETH_DATA_LEN
#define ETHERMIN (ETHER_MIN_LEN - ETHER_HDR_LEN - ETHER_CRC_LEN)
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_ETHERNET_H_ */

18
libc/sysv/consts/io.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_IO_H_
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_IO_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define _IOC(a, b, c, d) (((a) << 30) | ((b) << 8) | (c) | ((d) << 16))
#define _IOC_NONE 0U
#define _IOC_WRITE 1U
#define _IOC_READ 2U
#define _IO(a, b) _IOC(_IOC_NONE, (a), (b), 0)
#define _IOW(a, b, c) _IOC(_IOC_WRITE, (a), (b), sizeof(c))
#define _IOR(a, b, c) _IOC(_IOC_READ, (a), (b), sizeof(c))
#define _IOWR(a, b, c) _IOC(_IOC_READ | _IOC_WRITE, (a), (b), sizeof(c))
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_IO_H_ */

View file

@ -250,7 +250,7 @@ scall sys_ktimer_delete 0xffffff0ecfffffff 0xfff globl # no wrapper
scall sys_ktimer_getoverrun 0xffffff0effffffff 0xfff globl # no wrapper
scall sys_ktimer_gettime 0xffffff0eefffffff 0xfff globl # no wrapper
scall sys_ktimer_settime 0xffffff0edfffffff 0xfff globl # no wrapper
scall sys_clock_settime 0x1ac0580e9ffff0e3 0x070 globl # no wrapper
scall sys_clock_settime 0x1ac0580e9ffff0e3 0x070 globl hidden # no wrapper
scall sys_clock_gettime 0x1ab0570e8ffff0e4 0x071 globl hidden # Linux 2.6+ (c. 2003); XNU uses magic address
scall sys_clock_getres 0x1ad0590eaffff0e5 0x072 globl hidden
scall sys_mbind 0xfffffffffffff0ed 0x0eb globl # no wrapper; numa numa yeah