Skip to content

feat: use unix domain socket to interact with runner #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 57 additions & 29 deletions baihook/patch-libs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#include <cerrno>

#include <ctype.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

static const char *input_sock = "/tmp/bai-user-input.sock";
static const char *input_host = "127.0.0.1";
static const int input_port = 65000;


OVERRIDE_LIBC_SYMBOL(long, sysconf, int flag)
switch (flag) {
case _SC_NPROCESSORS_ONLN:
Expand All @@ -38,30 +39,67 @@ OVERRIDE_LIBC_SYMBOL(long, sysconf, int flag)
return orig_sysconf(flag);
}


extern "C"
int scanf(const char *format, ...)
int connect_input(int *fd)
{
va_list args;
char buffer[1024];
struct sockaddr_in addr;
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_un un_addr;
int sockfd;

sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
return -errno;
}

fflush(stdout);
memset(&un_addr, 0, sizeof(un_addr));
un_addr.sun_family = AF_UNIX;
strcpy(un_addr.sun_path, input_sock);

if (connect(sockfd, (struct sockaddr *) &un_addr, sizeof(un_addr)) == -1) {
if (errno == ENOENT) {
goto inet;
}
perror("connect");
return -errno;
}
*fd = sockfd;
return 0;

inet:
struct sockaddr_in in_addr;

sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
return -errno;
}

memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(input_host);
addr.sin_port = htons(input_port);
memset(&in_addr, 0, sizeof(in_addr));
in_addr.sin_family = AF_INET;
in_addr.sin_addr.s_addr = inet_addr(input_host);
in_addr.sin_port = htons(input_port);

if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
if (connect(sockfd, (struct sockaddr *) &in_addr, sizeof(in_addr)) == -1) {
perror("connect");
return -errno;
}
*fd = sockfd;
return 0;
}


extern "C"
int scanf(const char *format, ...)
{
va_list args;
char buffer[1024];
int sockfd, err;

err = connect_input(&sockfd);
if (err < 0) {
return err;
}

fflush(stdout);

int recvsz = read(sockfd, buffer, 1023);
close(sockfd);
Expand All @@ -78,25 +116,15 @@ extern "C"
int vscanf(const char *format, va_list args)
{
char buffer[1024];
struct sockaddr_in addr;
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket");
return -errno;
int sockfd, err;

err = connect_input(&sockfd);
if (err < 0) {
return err;
}

fflush(stdout);

memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(input_host);
addr.sin_port = htons(input_port);

if (connect(sockfd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
perror("connect");
return -errno;
}

int recvsz = read(sockfd, buffer, 1023);
close(sockfd);
buffer[recvsz] = '\0';
Expand Down
81 changes: 52 additions & 29 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,51 @@ usage() {
echo ""
echo "Usage: $0 [OPTIONS] DISTRO"
echo ""
echo "DISTRO is either ubuntu or alpine."
echo "DISTRO can be one of: centos ubuntu alpine."
echo "You can also pass `all` as DISTRO to build Backend.AI Hook for all supported distros."
echo ""
echo "OPTIONS"
echo " -h, --help Show this help message and exit."
echo " --clean Run 'make clean' instead of 'make'."
echo " --force-cmake Force to re-run cmake to refresh build scripts."
}

build() {
distro="$1"
ver="$2"
# match the container's user/group with this script
user="$(id -u):$(id -g)"
# to prevent "fatal: unable to look up current user in the passwd file: no such user" error from git
git_fix="-e GIT_COMMITTER_NAME=devops -e [email protected]"

docker build -t lablup/hook-dev:${distro} -f Dockerfile.${distro} .
docker_run="docker run --rm -it ${git_fix} -v "$(pwd):/root" -u ${user} -w=/root lablup/hook-dev:${distro} /bin/sh -c"

if [ "$FORCE_CMAKE" -eq 1 -o ! -f "Makefile" ]; then
echo ">> Running CMake to (re-)generate build scripts..."
$docker_run 'cmake CMakeLists.txt'
fi
if [ "$CLEAN" -eq 1 ]; then
echo ">> Cleaning up..."
$docker_run 'make clean'
else
echo ">> Building for ${distro} ${ver} ${arch} ..."
$docker_run 'make'
cp "baihook/libbaihook.so" "libbaihook.${distro}${ver}.${arch}.so"
cp "test/test-hook" "test-hook.${distro}${ver}.${arch}.bin"
cp "test/test-hooked" "test-hooked.${distro}${ver}.${arch}.bin"
fi
}

build_all() {
FORCE_CMAKE=1
build "ubuntu" "16.04"
build "ubuntu" "18.04"
build "ubuntu" "20.04"
build "centos" "7.6"
build "alpine" "3.8"
}

while [ $# -gt 0 ]; do
case $1 in
-h | --help) usage; exit 1 ;;
Expand All @@ -29,36 +66,22 @@ while [ $# -gt 0 ]; do
shift
done

distro="$1"
arg="$1"
arch="$(uname -m)"
case $distro in
ubuntu) distro_ver="${distro}20.04" ;;
centos) distro_ver="${distro}7.6" ;;
alpine) distro_ver="${distro}3.8" ;;
if [ "${arch}" = "arm64" ]; then
arch="aarch64"
fi

case $arg in
ubuntu) build "${arg}" "20.04" ;;
ubuntu16.04) build "${arg}" "16.04" ;;
ubuntu18.04) build "${arg}" "18.04" ;;
ubuntu20.04) build "${arg}" "20.04" ;;
centos) build "${arg}" "7.6" ;;
alpine) build "${arg}" "3.8" ;;
all) build_all ;;
*)
echo "Unknown distro value: ${distro}"
echo "Unknown distro value: ${arg}"
exit 1
esac

# match the container's user/group with this script
user="$(id -u):$(id -g)"
# to prevent "fatal: unable to look up current user in the passwd file: no such user" error from git
git_fix="-e GIT_COMMITTER_NAME=devops -e [email protected]"

docker build -t lablup/hook-dev:${distro} -f Dockerfile.${distro} .
docker_run="docker run --rm -it ${git_fix} -v "$(pwd):/root" -u ${user} -w=/root lablup/hook-dev:${distro} /bin/sh -c"

if [ "$FORCE_CMAKE" -eq 1 -o ! -f "Makefile" ]; then
echo ">> Running CMake to (re-)generate build scripts..."
$docker_run 'cmake CMakeLists.txt'
fi
if [ "$CLEAN" -eq 1 ]; then
echo ">> Cleaning up..."
$docker_run 'make clean'
else
echo ">> Building for ${distro} ${arch} ..."
$docker_run 'make'
cp "baihook/libbaihook.so" "libbaihook.${distro_ver}.${arch}.so"
cp "test/test-hook" "test-hook.${distro_ver}.${arch}.bin"
cp "test/test-hooked" "test-hooked.${distro_ver}.${arch}.bin"
fi