Skip to content

Commit

Permalink
Fix build on Android (jart#108)
Browse files Browse the repository at this point in the history
* configure: Check for functions missing on bionic

This includes:
- pthread_setcancelstate
- sockatmark

See termux/proot-distro#281.
  • Loading branch information
trungnt2910 authored Apr 20, 2023
1 parent bf7393b commit 8b17fed
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 8 deletions.
4 changes: 4 additions & 0 deletions blink/demangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ char *Demangle(char *p, const char *symbol, size_t n) {
sigset_t ss, oldss;
sn = strlen(symbol);
if (startswith(symbol, "_Z")) {
#ifdef HAVE_PTHREAD_SETCANCELSTATE
unassert(!pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs));
#endif
unassert(!pthread_once_(&g_cxxfilt.once, InitCxxFilt));
LOCK(&g_cxxfilt.lock);
if (g_cxxfilt.pid != -1) {
Expand All @@ -261,7 +263,9 @@ char *Demangle(char *p, const char *symbol, size_t n) {
r = 0;
}
UNLOCK(&g_cxxfilt.lock);
#ifdef HAVE_PTHREAD_SETCANCELSTATE
unassert(!pthread_setcancelstate(cs, 0));
#endif
} else {
r = 0;
}
Expand Down
2 changes: 2 additions & 0 deletions blink/devfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ struct VfsSystem g_devfs = {.name = "devfs",
.Tcgetsid = NULL,
.Tcgetpgrp = NULL,
.Tcsetpgrp = NULL,
#ifdef HAVE_SOCKATMARK
.Sockatmark = NULL,
#endif
.Fexecve = NULL,
}};

Expand Down
4 changes: 4 additions & 0 deletions blink/errfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ int WriteErrorString(const char *buf) {

int WriteError(int fd, const char *buf, int len) {
int rc, cs;
#ifdef HAVE_PTHREAD_SETCANCELSTATE
unassert(!pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs));
#endif
do rc = write(fd > 0 ? fd : g_errfd, buf, len);
while (rc == -1 && errno == EINTR);
#ifdef HAVE_PTHREAD_SETCANCELSTATE
unassert(!pthread_setcancelstate(cs, 0));
#endif
return rc;
}

Expand Down
4 changes: 4 additions & 0 deletions blink/hostfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ int HostfsTcsetpgrp(struct VfsInfo *info, pid_t pgrp) {
return tcsetpgrp(hostinfo->filefd, pgrp);
}

#ifdef HAVE_SOCKATMARK
int HostfsSockatmark(struct VfsInfo *info) {
struct HostfsInfo *hostinfo;
VFS_LOGF("HostfsSockatmark(%p)", info);
Expand All @@ -1735,6 +1736,7 @@ int HostfsSockatmark(struct VfsInfo *info) {
hostinfo = (struct HostfsInfo *)info->data;
return sockatmark(hostinfo->filefd);
}
#endif

int HostfsPipe(struct VfsInfo *infos[2]) {
int i;
Expand Down Expand Up @@ -2016,7 +2018,9 @@ struct VfsSystem g_hostfs = {.name = "hostfs",
.Tcgetsid = HostfsTcgetsid,
.Tcgetpgrp = HostfsTcgetpgrp,
.Tcsetpgrp = HostfsTcsetpgrp,
#ifdef HAVE_SOCKATMARK
.Sockatmark = HostfsSockatmark,
#endif
.Fexecve = HostfsFexecve,
}};

Expand Down
2 changes: 2 additions & 0 deletions blink/hostfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ int HostfsTcflow(struct VfsInfo *, int);
pid_t HostfsTcgetsid(struct VfsInfo *);
pid_t HostfsTcgetpgrp(struct VfsInfo *);
int HostfsTcsetpgrp(struct VfsInfo *, pid_t);
#ifdef HAVE_SOCKATMARK
int HostfsSockatmark(struct VfsInfo *);
#endif
int HostfsFexecve(struct VfsInfo *, char *const *, char *const *);

int HostfsWrapFd(int fd, bool dodup, struct VfsInfo **output);
Expand Down
4 changes: 4 additions & 0 deletions blink/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ static int IoctlTcflsh(struct Machine *m, int fildes, int queue) {
return VfsTcflush(fildes, queue);
}

#ifdef HAVE_SOCKATMARK
static int IoctlSiocatmark(struct Machine *m, int fildes, i64 addr) {
u8 *p;
int rc;
Expand All @@ -267,6 +268,7 @@ static int IoctlSiocatmark(struct Machine *m, int fildes, i64 addr) {
}
return rc;
}
#endif

static int IoctlGetInt32(struct Machine *m, int fildes, unsigned long cmd,
i64 addr) {
Expand Down Expand Up @@ -350,10 +352,12 @@ int SysIoctl(struct Machine *m, int fildes, u64 request, i64 addr) {
return IoctlTiocgsid(m, fildes, addr);
case TCFLSH_LINUX:
return IoctlTcflsh(m, fildes, addr);
#ifdef HAVE_SOCKATMARK
#ifndef DISABLE_SOCKETS
case SIOCATMARK_LINUX:
return IoctlSiocatmark(m, fildes, addr);
#endif
#endif
#ifdef FIONREAD
case FIONREAD_LINUX:
return IoctlGetInt32(m, fildes, FIONREAD, addr);
Expand Down
16 changes: 9 additions & 7 deletions blink/procfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ struct ProcfsInfo {
char name[PROCFS_NAME_MAX];
struct timespec time;
union {
struct ProcfsOpenFile *openfile;
struct ProcfsOpenDir *opendir;
_Atomic(struct ProcfsOpenFile *)openfile;
_Atomic(struct ProcfsOpenDir *)opendir;
};
union {
int (*readdir)(struct VfsInfo *, struct dirent *);
Expand Down Expand Up @@ -921,7 +921,6 @@ static int ProcfsOpendir(struct VfsInfo *info, struct VfsInfo **output) {
return 0;
}

#ifdef HAVE_SEEKDIR
static void ProcfsSeekdir(struct VfsInfo *info, long offset) {
struct ProcfsInfo *procinfo = (struct ProcfsInfo *)info->data;
if (!S_ISDIR(procinfo->mode) && !procinfo->opendir) {
Expand All @@ -931,6 +930,7 @@ static void ProcfsSeekdir(struct VfsInfo *info, long offset) {
procinfo->opendir->index = offset;
}

#ifdef HAVE_SEEKDIR
static long ProcfsTelldir(struct VfsInfo *info) {
struct ProcfsInfo *procinfo = (struct ProcfsInfo *)info->data;
if (!S_ISDIR(procinfo->mode) && !procinfo->opendir) {
Expand Down Expand Up @@ -958,7 +958,7 @@ static struct dirent *ProcfsReaddir(struct VfsInfo *info) {
}

static void ProcfsRewinddir(struct VfsInfo *info) {
return ProcfsSeekdir(info, 0);
ProcfsSeekdir(info, 0);
}

static int ProcfsClosedir(struct VfsInfo *info) {
Expand All @@ -968,12 +968,12 @@ static int ProcfsClosedir(struct VfsInfo *info) {

////////////////////////////////////////////////////////////////////////////////

static int ProcfsUtime(struct VfsInfo *, const char *, const struct timespec[2],
int) {
static int ProcfsUtime(struct VfsInfo *info, const char *name,
const struct timespec times[2], int flags) {
return einval();
}

static int ProcfsFutime(struct VfsInfo *, const struct timespec[2]) {
static int ProcfsFutime(struct VfsInfo *info, const struct timespec times[2]) {
return einval();
}

Expand Down Expand Up @@ -1488,7 +1488,9 @@ struct VfsSystem g_procfs = {.name = "proc",
.Tcgetsid = NULL,
.Tcgetpgrp = NULL,
.Tcsetpgrp = NULL,
#ifdef HAVE_SOCKATMARK
.Sockatmark = NULL,
#endif
.Fexecve = NULL,
}};

Expand Down
2 changes: 2 additions & 0 deletions blink/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2782,6 +2782,7 @@ int VfsTcsetpgrp(int fd, pid_t pgrp_id) {
return ret;
}

#ifdef HAVE_SOCKATMARK
int VfsSockatmark(int fd) {
struct VfsInfo *info;
int ret;
Expand All @@ -2797,6 +2798,7 @@ int VfsSockatmark(int fd) {
unassert(!VfsFreeInfo(info));
return ret;
}
#endif

int VfsExecve(const char *pathname, char *const *argv, char *const *envp) {
struct VfsInfo *info;
Expand Down
2 changes: 2 additions & 0 deletions blink/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ struct VfsOps {
pid_t (*Tcgetsid)(struct VfsInfo *);
pid_t (*Tcgetpgrp)(struct VfsInfo *);
int (*Tcsetpgrp)(struct VfsInfo *, pid_t);
#ifdef HAVE_SOCKATMARK
int (*Sockatmark)(struct VfsInfo *);
#endif
int (*Fexecve)(struct VfsInfo *, char *const *, char *const *);
};

Expand Down
2 changes: 2 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@
// #define HAVE_SCHED_GETAFFINITY
// #define HAVE_PTHREAD_PROCESS_SHARED
// #define HAVE_SYS_MOUNT_H
// #define HAVE_PTHREAD_SETCANCELSTATE
// #define HAVE_SOCKATMARK

#endif /* BLINK_CONFIG_H_ */
4 changes: 3 additions & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -651,16 +651,18 @@ fi
( config memccpy "checking for memccpy()... " uncomment "#define HAVE_MEMCCPY" ) &
( config seekdir "checking for seekdir()... " uncomment "#define HAVE_SEEKDIR" ) &
( config realpath "checking for realpath()... " uncomment "#define HAVE_REALPATH" ) &
wait
( config setreuid "checking for setreuid()... " uncomment "#define HAVE_SETREUID" ) &
( config sendto_zero "checking for sendto(0.0.0.0)... " uncomment "#define HAVE_SENDTO_ZERO" ) &
wait
( config struct_timezone "checking for struct timezone... " uncomment "#define HAVE_STRUCT_TIMEZONE" ) &
( config sa_len "checking for sockaddr::sa_len... " uncomment "#define HAVE_SA_LEN" ) &
( config map_shared "checking for mmap(MAP_SHARED)... " uncomment "#define HAVE_MAP_SHARED" ) &
( config sched_yield "checking for sched_yield()... " uncomment "#define HAVE_SCHED_YIELD" ) &
( config clock_settime "checking for clock_settime()... " uncomment "#define HAVE_CLOCK_SETTIME" ) &
( config sched_h "checking for sched.h... " uncomment "#define HAVE_SCHED_H" ) &
( config pthread_process_shared "checking for PTHREAD_PROCESS_SHARED... " uncomment "#define HAVE_PTHREAD_PROCESS_SHARED" ) &
( config pthread_setcancelstate "checking for pthread_setcancelstate()... " uncomment "#define HAVE_PTHREAD_SETCANCELSTATE" ) &
( config sockatmark "checking for sockatmark()... " uncomment "#define HAVE_SOCKATMARK" ) &

wait
rm -f "${LOCK}"
Expand Down
14 changes: 14 additions & 0 deletions tool/config/pthread_setcancelstate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// checks if pthread_setcancelstate is available and usable
#include <pthread.h>

void *Worker(void *arg) {
return arg;
}

int main(int argc, char *argv[]) {
if (pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0))
return 1;
if (pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0))
return 1;
return 0;
}
9 changes: 9 additions & 0 deletions tool/config/sockatmark.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// checks if sockatmark is available
#include <sys/socket.h>

int main(int argc, char *argv[]) {
int rc;
socket(AF_INET, SOCK_STREAM, 0);
if (sockatmark(3) == -1) return 1;
return 0;
}

0 comments on commit 8b17fed

Please sign in to comment.