Skip to content

Commit

Permalink
Build For FreeBSD and Fix Client Connect on Success Problem
Browse files Browse the repository at this point in the history
In client.cc, the code was failing to call registerFdOneShot after
::connect returned success. Apparently in all the other *nix we've
tried, ::connect returns -1 with errno EINPROGRESS, whereas in FreeBSD
::connect simply returns 0 (success); tweaked the code so
registerFdOneShot is called in the "::connect returns 0" case. This
makes cookie_test_3 pass, it fails on FreeBSD otherwise.

emosandlibevdefs.h: Add comment on why FreeBSD uses libevent and not
the FreebSD Linuxulator.

.clang-format-ignore: Ignore the formatting in emosandlibevdefs.h
(emosandlibevdefs.h uses the same formatting as eventmeth.h - see
previous commit for why it was pulled out of eventmeth.h).

meson.build: Include execinfo library explicitly for FreeBSD (it is a
separate library in FreeBSD, but not in other *nix seemingly).

eventmeth.cc: Fixed an indentation issue (no change in functionality).
  • Loading branch information
dgreatwood committed Jul 20, 2024
1 parent 83a8425 commit 8e816a6
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
1 change: 1 addition & 0 deletions .clang-format-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
subprojects/**
src/common/eventmeth.cc
include/pistache/eventmeth.h
include/pistache/emosandlibevdefs.h
src/common/pist_timelog.cc
include/pistache/pist_timelog.h
src/common/pist_check.cc
Expand Down
9 changes: 9 additions & 0 deletions include/pistache/emosandlibevdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
#include <sys/param.h>
#if defined(BSD)
// FreeBSD, NetBSD, OpenBSD, DragonFly BSD
//
// Note - FreeBSD may support epoll via FreeBSD's Linux emulation layer
// (see https://wiki.freebsd.org/Linuxulator). We can check for FreeBSD
// using "ifdef __FreeBSD__" and also check __FreeBSD_version (see
// https://docs.freebsd.org/en/books/porters-handbook/versions/).
// However, since FreeBSD's Linuxulator supports Linux binaries, a user
// might perhaps just as well run the Linux version of Pistache if
// using the Linuxulator. So we use libevent (which will likely use
// kqueue on FreeBSD) even in the FreeBSD case.
#ifndef _USE_LIBEVENT
#define _USE_LIBEVENT 1
#endif
Expand Down
8 changes: 7 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,17 @@ if get_option('PISTACHE_USE_SSL')
deps_libpistache += dependency('openssl')
endif

if host_machine.system() == 'darwin' or host_machine.system() == 'openbsd' or host_machine.system() == 'netbsd' or get_option('PISTACHE_FORCE_LIBEVENT')
if host_machine.system() == 'darwin' or host_machine.system() == 'freebsd' or host_machine.system() == 'openbsd' or host_machine.system() == 'netbsd' or get_option('PISTACHE_FORCE_LIBEVENT')
deps_libpistache += dependency('libevent')
deps_libpistache += dependency('libevent_pthreads')
endif

if host_machine.system() == 'freebsd'
# Does not appear to be necessary for OpenBSD
libexecinfo_dep = compiler.find_library('execinfo')
deps_libpistache += libexecinfo_dep
endif

version_array = []
if meson.version().version_compare('>=0.57.0')
version_array = import('fs').read('version.txt').strip().split('.')
Expand Down
23 changes: 12 additions & 11 deletions src/client/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -491,18 +491,19 @@ namespace Pistache::Http::Experimental
PS_LOG_DEBUG_ARGS("Calling ::connect fs %d", GET_ACTUAL_FD(fd));

int res = ::connect(GET_ACTUAL_FD(fd), data->getAddr(), data->addr_len);
if (res == -1)
PS_LOG_DEBUG_ARGS("::connect res %d, errno on fail %d (%s)",
res, (res < 0) ? errno : 0,
(res < 0) ? strerror(errno) : "success");

if ((res == 0) || ((res == -1) && (errno == EINPROGRESS)))
{
if (errno == EINPROGRESS)
{
reactor()->registerFdOneShot(key(), fd,
NotifyOn::Write | NotifyOn::Hangup | NotifyOn::Shutdown);
}
else
{
data->reject(Error::system("Failed to connect"));
continue;
}
reactor()->registerFdOneShot(key(), fd,
NotifyOn::Write | NotifyOn::Hangup | NotifyOn::Shutdown);
}
else
{
data->reject(Error::system("Failed to connect"));
continue;
}
connections.insert(std::make_pair(fd, std::move(*data)));
}
Expand Down
10 changes: 5 additions & 5 deletions src/common/eventmeth.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1464,12 +1464,12 @@ namespace Pistache
"EmEventCtr %p also being activated for write",
this);

flags |= EV_WRITE;
}

event_active(ev_, flags, 0 /* obsolete parm*/);
}
flags |= EV_WRITE;
}

event_active(ev_, flags, 0 /* obsolete parm*/);
}
}


std::shared_ptr<std::condition_variable> tmp_cv_sptr(
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.1.20240712
0.4.1.20240719

0 comments on commit 8e816a6

Please sign in to comment.