From 742e31022c372915683ae8cc5aa88e6bcc1fb000 Mon Sep 17 00:00:00 2001 From: Oleksii Samorukov Date: Mon, 15 Jul 2019 15:43:48 +0200 Subject: [PATCH 1/4] FreeBSD related changes --- coreutils/mknod.c | 4 ++++ include/platform.h | 2 +- libbb/pw_encrypt.c | 5 ++++- mailutils/mail.c | 15 +++++++++++++- networking/httpd.c | 10 ++++++++++ networking/nslookup.c | 3 +++ networking/ntpd.c | 6 ++++++ networking/ping.c | 44 +++++++++++++++++++++++++++++++++++++++++ networking/traceroute.c | 6 +++++- sysklogd/klogd.c | 2 +- 10 files changed, 92 insertions(+), 5 deletions(-) diff --git a/coreutils/mknod.c b/coreutils/mknod.c index eee0ac71df..8cda2692f2 100644 --- a/coreutils/mknod.c +++ b/coreutils/mknod.c @@ -36,7 +36,11 @@ //usage: "$ mknod /dev/fd0 b 2 0\n" //usage: "$ mknod -m 644 /tmp/pipe p\n" +#ifdef __FreeBSD__ +#include +#else #include // For makedev +#endif #include "libbb.h" #include "libcoreutils/coreutils.h" diff --git a/include/platform.h b/include/platform.h index 085387fd8d..a96de5c9e1 100644 --- a/include/platform.h +++ b/include/platform.h @@ -319,7 +319,7 @@ typedef unsigned smalluint; #endif /* Define bb_setpgrp */ -#if defined(__digital__) && defined(__unix__) +#if (defined(__digital__) && defined(__unix__) || defined(__FreeBSD__)) /* use legacy setpgrp(pid_t, pid_t) for now. move to platform.c */ # define bb_setpgrp() do { pid_t __me = getpid(); setpgrp(__me, __me); } while (0) #else diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c index a60c33c35b..db7d840feb 100644 --- a/libbb/pw_encrypt.c +++ b/libbb/pw_encrypt.c @@ -7,8 +7,11 @@ * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ #if !ENABLE_USE_BB_CRYPT +#ifdef __FreeBSD__ +#include +#else #include -#endif +#endif#endif #include "libbb.h" /* static const uint8_t ascii64[] ALIGN1 = diff --git a/mailutils/mail.c b/mailutils/mail.c index 61e5053bc7..1d01a32eb2 100644 --- a/mailutils/mail.c +++ b/mailutils/mail.c @@ -6,7 +6,15 @@ * * Licensed under GPLv2, see file LICENSE in this source tree. */ + +#ifdef __FreeBSD__ +#include +#define PROCCTL +#else #include +#define PRCTL +#endif + #include "libbb.h" #include "mail.h" @@ -55,7 +63,12 @@ void FAST_FUNC launch_helper(const char **argv) xmove_fd(child_in.rd, STDIN_FILENO); xmove_fd(child_out.wr, STDOUT_FILENO); // if parent dies, get SIGTERM - prctl(PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0); + int signum = SIGTERM; +#ifdef PRCTL + prctl(PR_SET_PDEATHSIG, signum, 0, 0, 0); +#elif defined PROCCTL + procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum); +#endif // try to execute connection helper // NB: SIGCHLD & SIGALRM revert to SIG_DFL on exec BB_EXECVP_or_die((char**)argv); diff --git a/networking/httpd.c b/networking/httpd.c index 4c014bc710..57e0ca4934 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -298,6 +298,16 @@ # include #endif +/* test if it really works!!! */ +#ifdef __FreeBSD__ +#ifndef s6_addr16 +#define s6_addr16 __u6_addr.__u6_addr16 +#endif +#ifndef s6_addr32 +#define s6_addr32 __u6_addr.__u6_addr32 +#endif +#endif + #define DEBUG 0 #define IOBUF_SIZE 8192 diff --git a/networking/nslookup.c b/networking/nslookup.c index c43ad46f34..3019e5ec5e 100644 --- a/networking/nslookup.c +++ b/networking/nslookup.c @@ -34,6 +34,9 @@ //usage: "Name: debian\n" //usage: "Address: 127.0.0.1\n" +#ifdef __FreeBSD__ +#include +#endif #include #include /* for IFNAMSIZ */ //#include diff --git a/networking/ntpd.c b/networking/ntpd.c index 46d8f34956..2e1fbb3798 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -113,6 +113,12 @@ # define IPTOS_DSCP_AF21 0x48 #endif +#ifdef __FreeBSD__ +#define adjtimex ntp_adjtime +#define ADJ_OFFSET MOD_OFFSET +#define ADJ_STATUS MOD_STATUS +#define ADJ_TIMECONST MOD_TIMECONST +#endif /* Verbosity control (max level of -dddd options accepted). * max 6 is very talkative (and bloated). 3 is non-bloated, diff --git a/networking/ping.c b/networking/ping.c index c4a15e06e9..65aa95d2e1 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -122,6 +122,12 @@ //usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n" #include + +#ifdef __FreeBSD__ +#include +#include +#include +#endif #include #include "libbb.h" #include "common_bufsiz.h" @@ -150,6 +156,42 @@ # define SOL_RAW IPPROTO_RAW #endif +#if defined(__FreeBSD__) || defined(__APPLE__) +/** + * On BSD the IPv4 struct is called struct ip and instead of iXX + * the members are called ip_XX. One could change this code to use + * struct ip but that would require to define _BSD_SOURCE and that + * might have other complications. Instead make sure struct iphdr + * is present on FreeBSD. The below is taken from GLIBC. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + */ +struct iphdr + { +#if BYTE_ORDER == LITTLE_ENDIAN + unsigned int ihl:4; + unsigned int version:4; +#elif BYTE_ORDER == BIG_ENDIAN + unsigned int version:4; + unsigned int ihl:4; +#endif + u_int8_t tos; + u_int16_t tot_len; + u_int16_t id; + u_int16_t frag_off; + u_int8_t ttl; + u_int8_t protocol; + u_int16_t check; + u_int32_t saddr; + u_int32_t daddr; + /*The options start here. */ + }; +#endif + + #if ENABLE_PING6 # include /* I see RENUMBERED constants in bits/in.h - !!? @@ -265,8 +307,10 @@ static void ping6(len_and_sockaddr *lsa) pkt->icmp6_type = ICMP6_ECHO_REQUEST; pkt->icmp6_id = G.myid; +#ifdef __linux__ sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); setsockopt_int(pingsock, SOL_RAW, IPV6_CHECKSUM, sockopt); +#endif xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len); diff --git a/networking/traceroute.c b/networking/traceroute.c index bd63e4449d..b9e4aee24d 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -717,7 +717,11 @@ packet4_ok(int read_len, int seq) // but defer it to kernel, we can't set source port, // and thus can't check it here in the reply /* && up->source == ident */ - && up->dest == htons(port + seq) +#ifdef __FreeBSD__ + && up->uh_dport == htons(port + seq) +#else + && up->dest == htons(port + seq) +#endif ) { return (type == ICMP_TIMXCEED ? -1 : code + 1); } diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c index c0ec1c0bfb..82596bc0b9 100644 --- a/sysklogd/klogd.c +++ b/sysklogd/klogd.c @@ -100,7 +100,7 @@ static void klogd_close(void) #else # ifndef _PATH_KLOG -# ifdef __GNU__ +# if defined(__GNU__) || defined (__FreeBSD__) # define _PATH_KLOG "/dev/klog" # else # error "your system's _PATH_KLOG is unknown" From 1cf1f0f7b96c6308031c57badbabe81951c23ba1 Mon Sep 17 00:00:00 2001 From: Alex Samorukov Date: Sun, 3 Jan 2021 08:46:59 +0100 Subject: [PATCH 2/4] Fix line spacing --- libbb/pw_encrypt.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libbb/pw_encrypt.c b/libbb/pw_encrypt.c index db7d840feb..903093846b 100644 --- a/libbb/pw_encrypt.c +++ b/libbb/pw_encrypt.c @@ -11,7 +11,8 @@ #include #else #include -#endif#endif +#endif +#endif #include "libbb.h" /* static const uint8_t ascii64[] ALIGN1 = From 407e548e9a1d243bb675147f7b3da54e0c260f24 Mon Sep 17 00:00:00 2001 From: Alex Samorukov Date: Sun, 3 Jan 2021 08:50:23 +0100 Subject: [PATCH 3/4] Move HAVE_XTABS higher --- include/libbb.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index dad6fc6875..37a2003d98 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -53,6 +53,9 @@ #include #include #include +#ifndef HAVE_XTABS +# define XTABS TAB3 +#endif #include #include #if ENABLE_FEATURE_SHADOWPASSWDS @@ -160,9 +163,6 @@ #ifndef HAVE_FDATASYNC # define fdatasync fsync #endif -#ifndef HAVE_XTABS -# define XTABS TAB3 -#endif /* * Use '%m' to append error string on platforms that support it, * '%s' and strerror() on those that don't. From 02d90f2a5427866badc310ec3fb97a185b866825 Mon Sep 17 00:00:00 2001 From: Alex Samorukov Date: Sun, 3 Jan 2021 09:01:22 +0100 Subject: [PATCH 4/4] Sync patches with FreeBSD port --- networking/httpd.c | 7 +++++++ networking/traceroute.c | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/networking/httpd.c b/networking/httpd.c index 57e0ca4934..ffd5635dc4 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -2225,6 +2225,13 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr) remote_ip = ntohl(fromAddr->u.sin.sin_addr.s_addr); } # if ENABLE_FEATURE_IPV6 +#ifdef __FreeBSD__ +if (fromAddr->u.sa.sa_family == AF_INET6 + && fromAddr->u.sin6.sin6_addr.s6_addr[0] == 0 + && fromAddr->u.sin6.sin6_addr.s6_addr[1] == 0 + && ntohl(fromAddr->u.sin6.sin6_addr.s6_addr[2]) == 0xffff) + remote_ip = ntohl(fromAddr->u.sin6.sin6_addr.s6_addr[3]); +#else if (fromAddr->u.sa.sa_family == AF_INET6 && fromAddr->u.sin6.sin6_addr.s6_addr32[0] == 0 && fromAddr->u.sin6.sin6_addr.s6_addr32[1] == 0 diff --git a/networking/traceroute.c b/networking/traceroute.c index b9e4aee24d..cb88160d29 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -990,7 +990,11 @@ traceroute_init(int op, char **argv) } #if ENABLE_TRACEROUTE6 else { +#ifndef __FreeBSD__ if (setsockopt_int(rcvsock, SOL_RAW, IPV6_CHECKSUM, 2) != 0) +#else + if (setsockopt_int(rcvsock, SOL_IPV6, IPV6_CHECKSUM, 2) != 0) +#endif bb_perror_msg_and_die("setsockopt(%s)", "IPV6_CHECKSUM"); if (op & OPT_USE_ICMP) snd = xsocket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);