From b5d9ff8cdaaa08ae1332d3e8381d1809b0231767 Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Sun, 15 Jan 2017 11:32:52 +0000 Subject: [PATCH] Use docker run to compile C code not docker build C version of #1006 Note that I switched all the C builds to use -O2 and that meant that the compiler found some more warnings so I also fixed these up. The possibly undefined ones were harmless, the aliasing one is now more correct. As these are small programs, the caching from `docker build` makes no real difference, and worst case compile time is much better. Signed-off-by: Justin Cormack --- alpine/base/c-compile/Dockerfile | 15 +++++++ alpine/base/c-compile/Makefile | 29 ++++++++++++ alpine/base/c-compile/compile.sh | 56 ++++++++++++++++++++++++ alpine/packages/9pmount-vsock/Dockerfile | 10 ----- alpine/packages/9pmount-vsock/Makefile | 15 ++++--- alpine/packages/nc-vsock/Dockerfile | 10 ----- alpine/packages/nc-vsock/Makefile | 14 +++--- alpine/packages/nc-vsock/nc-vsock.c | 2 +- alpine/packages/tap-vsockd/Dockerfile | 10 ----- alpine/packages/tap-vsockd/Makefile | 14 +++--- alpine/packages/transfused/Dockerfile | 10 ----- alpine/packages/transfused/Makefile | 14 +++--- alpine/packages/transfused/transfused.c | 17 ++++--- 13 files changed, 143 insertions(+), 73 deletions(-) create mode 100644 alpine/base/c-compile/Dockerfile create mode 100644 alpine/base/c-compile/Makefile create mode 100755 alpine/base/c-compile/compile.sh delete mode 100644 alpine/packages/9pmount-vsock/Dockerfile delete mode 100644 alpine/packages/nc-vsock/Dockerfile delete mode 100644 alpine/packages/tap-vsockd/Dockerfile delete mode 100644 alpine/packages/transfused/Dockerfile diff --git a/alpine/base/c-compile/Dockerfile b/alpine/base/c-compile/Dockerfile new file mode 100644 index 000000000..2b9c4535b --- /dev/null +++ b/alpine/base/c-compile/Dockerfile @@ -0,0 +1,15 @@ +FROM alpine:3.5 +RUN \ + apk update && apk upgrade && \ + apk add \ + curl \ + gcc \ + git \ + libc-dev \ + linux-headers \ + util-linux-dev \ + && true + +COPY compile.sh /usr/bin/ + +ENTRYPOINT ["/usr/bin/compile.sh"] diff --git a/alpine/base/c-compile/Makefile b/alpine/base/c-compile/Makefile new file mode 100644 index 000000000..88f8b0abd --- /dev/null +++ b/alpine/base/c-compile/Makefile @@ -0,0 +1,29 @@ +.PHONY: tag push + +BASE=alpine:3.5 +IMAGE=c-compile + +default: push + +hash: Dockerfile compile.sh + DOCKER_CONTENT_TRUST=1 docker pull $(BASE) + tar cf - $^ | docker build --no-cache -t $(IMAGE):build - + docker run --rm --entrypoint=/bin/sh $(IMAGE):build -c 'cat /lib/apk/db/installed /usr/bin/compile.sh | sha1sum' | sed 's/ .*//' > hash + +push: hash + docker pull mobylinux/$(IMAGE):$(shell cat hash) || \ + (docker tag $(IMAGE):build mobylinux/$(IMAGE):$(shell cat hash) && \ + docker push mobylinux/$(IMAGE):$(shell cat hash)) + docker rmi $(IMAGE):build + rm -f hash + +tag: hash + docker pull mobylinux/$(IMAGE):$(shell cat hash) || \ + docker tag $(IMAGE):build mobylinux/$(IMAGE):$(shell cat hash) + docker rmi $(IMAGE):build + rm -f hash + +clean: + rm -f hash + +.DELETE_ON_ERROR: diff --git a/alpine/base/c-compile/compile.sh b/alpine/base/c-compile/compile.sh new file mode 100755 index 000000000..333cf2449 --- /dev/null +++ b/alpine/base/c-compile/compile.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +# This is designed to compile a single package to a single binary +# so it makes some assumptions about things to simplify config +# to output a single binary (in a tarball) just use -o file +# use --docker to output a tarball for input to docker build - + +set -e + +usage() { + echo "Usage: -o file" + exit 1 +} + +[ $# = 0 ] && usage + +while [ $# -gt 0 ] +do + flag="$1" + case "$flag" in + -o) + [ $# -eq 1 ] && usage + out="$2" + mkdir -p "$(dirname $2)" + shift + ;; + -l*) + LIBS="$LIBS $1" + shift + ;; + *) + echo "Unknown option $1" + exit 1 + esac + shift +done + +[ -z "$out" ] && usage + +package=$(basename "$out") + +dir="/src/$package" + +mkdir -p $dir + +# untar input +tar xf - -C $dir + +( + cd $dir + CFILES=$(find . -name '*.c') + cc -static -O2 -Wall -Werror -o ../../$out $CFILES $LIBS +) + +tar cf - $out +exit 0 diff --git a/alpine/packages/9pmount-vsock/Dockerfile b/alpine/packages/9pmount-vsock/Dockerfile deleted file mode 100644 index 83f9537f3..000000000 --- a/alpine/packages/9pmount-vsock/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2 -FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c - -COPY . /9pmount-vsock - -WORKDIR /9pmount-vsock - -RUN cc -Wall -Werror 9pmount-vsock.c hvsock.c -lpthread -o 9pmount-vsock - -CMD ["tar", "cf", "-", "9pmount-vsock"] diff --git a/alpine/packages/9pmount-vsock/Makefile b/alpine/packages/9pmount-vsock/Makefile index e763666ea..f9da87c8b 100644 --- a/alpine/packages/9pmount-vsock/Makefile +++ b/alpine/packages/9pmount-vsock/Makefile @@ -1,12 +1,13 @@ -DEPS=Dockerfile $(wildcard *.c *.h) +# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3 +C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f -9pmount-vsock: $(DEPS) - mkdir -p sbin - BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \ - [ -n "$$BUILD" ] && \ - echo "Built $$BUILD" && \ - docker run --rm --net=none $$BUILD | tar xf - -C sbin +default: sbin/9pmount-vsock +DEPS=$(wildcard *.c *.h) + +sbin/9pmount-vsock: $(DEPS) + mkdir -p $(dir $@) + tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ -lpthread | tar xf - clean: rm -rf sbin diff --git a/alpine/packages/nc-vsock/Dockerfile b/alpine/packages/nc-vsock/Dockerfile deleted file mode 100644 index d1c498db9..000000000 --- a/alpine/packages/nc-vsock/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2 -FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c - -COPY . /nc-vsock - -WORKDIR /nc-vsock - -RUN cc -Wall -Werror -o nc-vsock nc-vsock.c -luuid - -CMD ["tar", "cf", "-", "nc-vsock"] diff --git a/alpine/packages/nc-vsock/Makefile b/alpine/packages/nc-vsock/Makefile index 97a0071c3..daae6c230 100644 --- a/alpine/packages/nc-vsock/Makefile +++ b/alpine/packages/nc-vsock/Makefile @@ -1,11 +1,13 @@ -DEPS=Dockerfile $(wildcard *.c *.h) +# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3 +C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f + +default: usr/bin/nc-vsock + +DEPS=$(wildcard *.c *.h) usr/bin/nc-vsock: $(DEPS) - mkdir -p usr/bin - BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \ - [ -n "$$BUILD" ] && \ - echo "Built $$BUILD" && \ - docker run --rm --net=none $$BUILD | tar xf - -C usr/bin + mkdir -p $(dir $@) + tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ -luuid | tar xf - clean: rm -rf usr diff --git a/alpine/packages/nc-vsock/nc-vsock.c b/alpine/packages/nc-vsock/nc-vsock.c index ef5d77c43..184e74dce 100644 --- a/alpine/packages/nc-vsock/nc-vsock.c +++ b/alpine/packages/nc-vsock/nc-vsock.c @@ -193,7 +193,7 @@ static int hvsock_listen(const char *port_str) static int tcp_connect(const char *node, const char *service) { - int fd; + int fd = -1; int ret; const struct addrinfo hints = { .ai_family = AF_INET, diff --git a/alpine/packages/tap-vsockd/Dockerfile b/alpine/packages/tap-vsockd/Dockerfile deleted file mode 100644 index a6187dfba..000000000 --- a/alpine/packages/tap-vsockd/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2 -FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c - -COPY . /tap-vsockd - -WORKDIR /tap-vsockd - -RUN cc -Wall -Werror tap-vsockd.c hvsock.c protocol.c -lpthread -o tap-vsockd - -CMD ["tar", "cf", "-", "tap-vsockd"] diff --git a/alpine/packages/tap-vsockd/Makefile b/alpine/packages/tap-vsockd/Makefile index e49cb524f..7f4194dc6 100644 --- a/alpine/packages/tap-vsockd/Makefile +++ b/alpine/packages/tap-vsockd/Makefile @@ -1,11 +1,13 @@ -DEPS=Dockerfile $(wildcard *.c *.h) +# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3 +C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f + +default: sbin/tap-vsockd + +DEPS=$(wildcard *.c *.h) sbin/tap-vsockd: $(DEPS) - mkdir -p sbin - BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \ - [ -n "$$BUILD" ] && \ - echo "Built $$BUILD" && \ - docker run --rm --net=none $$BUILD | tar xf - -C sbin + mkdir -p $(dir $@) + tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ -lpthread | tar xf - clean: rm -rf sbin diff --git a/alpine/packages/transfused/Dockerfile b/alpine/packages/transfused/Dockerfile deleted file mode 100644 index 088944cbf..000000000 --- a/alpine/packages/transfused/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -# Tag: b77cfc4ad0033d4366df830ed697afc7bab458a2 -FROM mobylinux/alpine-build-c@sha256:53739ea6042cb0ac39cf6e262012c1c4224206b2c9b719569fe7efa3a381348c - -COPY . /transfused - -WORKDIR /transfused - -RUN cc -g -static -Wall -Werror -o transfused transfused.c transfused_log.c transfused_vsock.c - -CMD ["tar", "cf", "-", "transfused"] diff --git a/alpine/packages/transfused/Makefile b/alpine/packages/transfused/Makefile index eba8e86f9..5ce360895 100644 --- a/alpine/packages/transfused/Makefile +++ b/alpine/packages/transfused/Makefile @@ -1,11 +1,13 @@ -DEPS=Dockerfile $(wildcard *.c *.h) +# Tag: ac075fed7c87e4af30d8490ae0504166cceb0df3 +C_COMPILE=mobylinux/c-compile@sha256:0e82d441ce112d638f904a08199c76b022c065a2dbf8908bb366755267d4417f + +default: sbin/transfused + +DEPS=$(wildcard *.c *.h) sbin/transfused: $(DEPS) - mkdir -p sbin - BUILD=$$( tar cf - $(DEPS) | docker build -q - ) && \ - [ -n "$$BUILD" ] && \ - echo "Built $$BUILD" && \ - docker run --rm --net=none $$BUILD | tar xf - -C sbin + mkdir -p $(dir $@) + tar cf - $(DEPS) | docker run --rm --net=none --log-driver=none -i $(C_COMPILE) -o $@ | tar xf - clean: rm -rf sbin diff --git a/alpine/packages/transfused/transfused.c b/alpine/packages/transfused/transfused.c index be219e131..7853e441d 100644 --- a/alpine/packages/transfused/transfused.c +++ b/alpine/packages/transfused/transfused.c @@ -97,7 +97,7 @@ void unlock(char *const descr, pthread_mutex_t *mutex) int bind_socket(const char *socket) { - int sock; + int sock = -1; if (socket[0] == 0) die(2, NULL, NULL, "Socket family required"); @@ -118,7 +118,7 @@ int bind_socket(const char *socket) int connect_socket(const char *socket) { - int sock; + int sock = -1; if (socket[0] == 0) die(2, NULL, NULL, "Socket family required"); @@ -549,12 +549,15 @@ void start_writer(connection_t *connection, int fuse) void negotiate_notify_channel(char *mount_point, int notify_sock) { int len = strlen(mount_point); - char hdr[6]; + struct { + uint32_t len; + uint16_t channel; + } __attribute__((packed)) hdr; - *((uint32_t *)hdr) = 6 + len; - *((uint16_t *)(hdr + 4)) = TRANSFUSE_NOTIFY_CHANNEL; + hdr.len = 6 + len; + hdr.channel = TRANSFUSE_NOTIFY_CHANNEL; - write_exactly("negotiate_notify_channel hdr", notify_sock, hdr, 6); + write_exactly("negotiate_notify_channel hdr", notify_sock, &hdr, 6); write_exactly("negotiate_notify_channel mnt", notify_sock, mount_point, len); } @@ -1087,7 +1090,7 @@ void serve(parameters *params) char subproto_selector; pthread_t child; connection_t *conn; - void *(*connection_handler_thread)(void *); + void *(*connection_handler_thread)(void *) = NULL; if (listen(params->data_sock, 16)) die(1, NULL, "listen", "");