diff --git a/.github/workflows/bins-extra-development.yaml b/.github/workflows/bins-extra-development.yaml index c9734de45..c8b3d330b 100644 --- a/.github/workflows/bins-extra-development.yaml +++ b/.github/workflows/bins-extra-development.yaml @@ -207,3 +207,41 @@ jobs: user: tf-zos-bins.dev name: logtrunc.flist target: tf-autobuilder/${{ steps.package.outputs.name }}.flist + + shim-logs: + name: 'Package: shim-logs' + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Setup basesystem + run: | + cd bins + sudo ./bins-extra.sh --package basesystem + + - name: Build package + id: package + run: | + cd bins + sudo ./bins-extra.sh --package shimlogs + + - name: Publish flist (tf-autobuilder, ${{ steps.package.outputs.name }}) + if: success() + uses: threefoldtech/publish-flist@master + with: + token: ${{ secrets.HUB_JWT }} + action: publish + user: tf-autobuilder + root: bins/releases/shimlogs + name: ${{ steps.package.outputs.name }}.flist + + - name: Crosslink flist (tf-zos-bins.dev) + if: success() && github.ref == 'refs/heads/master' + uses: threefoldtech/publish-flist@master + with: + token: ${{ secrets.HUB_JWT }} + action: crosslink + user: tf-zos-bins.dev + name: shim-logs.flist + target: tf-autobuilder/${{ steps.package.outputs.name }}.flist diff --git a/.github/workflows/bins-extra-pre-release.yaml b/.github/workflows/bins-extra-pre-release.yaml index d579aaf67..5d230056a 100644 --- a/.github/workflows/bins-extra-pre-release.yaml +++ b/.github/workflows/bins-extra-pre-release.yaml @@ -212,3 +212,41 @@ jobs: user: tf-zos-bins.test name: logtrunc.flist target: tf-autobuilder/${{ steps.package.outputs.name }}.flist + + shim-logs: + name: 'Package: shim-logs' + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Setup basesystem + run: | + cd bins + sudo ./bins-extra.sh --package basesystem + + - name: Build package + id: package + run: | + cd bins + sudo ./bins-extra.sh --package shimlogs + + - name: Publish flist (tf-autobuilder, ${{ steps.package.outputs.name }}) + if: success() + uses: threefoldtech/publish-flist@master + with: + token: ${{ secrets.HUB_JWT }} + action: publish + user: tf-autobuilder + root: bins/releases/shimlogs + name: ${{ steps.package.outputs.name }}.flist + + - name: crosslink flist (tf-zos-bins.test) + if: success() + uses: threefoldtech/publish-flist@master + with: + token: ${{ secrets.hub_jwt }} + action: crosslink + user: tf-zos-bins.test + name: shim-logs.flist + target: tf-autobuilder/${{ steps.package.outputs.name }}.flist diff --git a/.github/workflows/bins-extra-release.yaml b/.github/workflows/bins-extra-release.yaml index 0cced08a9..58c43cd0f 100644 --- a/.github/workflows/bins-extra-release.yaml +++ b/.github/workflows/bins-extra-release.yaml @@ -215,3 +215,41 @@ jobs: name: logtrunc.flist target: tf-autobuilder/${{ steps.package.outputs.name }}.flist + shim-logs: + name: 'Package: shim-logs' + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v1 + + - name: Setup basesystem + run: | + cd bins + sudo ./bins-extra.sh --package basesystem + + - name: Build package + id: package + run: | + cd bins + sudo ./bins-extra.sh --package shimlogs + + - name: Publish flist (tf-autobuilder, ${{ steps.package.outputs.name }}) + if: success() + uses: threefoldtech/publish-flist@master + with: + token: ${{ secrets.HUB_JWT }} + action: publish + user: tf-autobuilder + root: bins/releases/shimlogs + name: ${{ steps.package.outputs.name }}.flist + + - name: crosslink flist (tf-zos-bins) + if: success() + uses: threefoldtech/publish-flist@master + with: + token: ${{ secrets.hub_jwt }} + action: crosslink + user: tf-zos-bins + name: shim-logs.flist + target: tf-autobuilder/${{ steps.package.outputs.name }}.flist + diff --git a/.github/workflows/publish-development.yaml b/.github/workflows/publish-development.yaml index a6d4f08c9..d57153fb5 100644 --- a/.github/workflows/publish-development.yaml +++ b/.github/workflows/publish-development.yaml @@ -18,11 +18,6 @@ jobs: go-version: 1.14 id: go - - name: Prepare dependencies - run: | - sudo apt-get update - sudo apt-get install -y libjansson-dev libhiredis-dev - - name: Checkout code into the Go module directory uses: actions/checkout@v1 diff --git a/.github/workflows/publish-pre-release.yaml b/.github/workflows/publish-pre-release.yaml index d7b106cf7..aa69fb0f5 100644 --- a/.github/workflows/publish-pre-release.yaml +++ b/.github/workflows/publish-pre-release.yaml @@ -25,11 +25,6 @@ jobs: go-version: 1.14 id: go - - name: Prepare dependencies - run: | - sudo apt-get update - sudo apt-get install -y libjansson-dev libhiredis-dev - - name: Checkout code into the Go module directory uses: actions/checkout@v1 diff --git a/.github/workflows/publish-release.yaml b/.github/workflows/publish-release.yaml index 97143d635..d5b5695ee 100644 --- a/.github/workflows/publish-release.yaml +++ b/.github/workflows/publish-release.yaml @@ -26,11 +26,6 @@ jobs: go-version: 1.14 id: go - - name: Prepare dependencies - run: | - sudo apt-get update - sudo apt-get install -y libjansson-dev libhiredis-dev - - name: Checkout code into the Go module directory uses: actions/checkout@v1 diff --git a/bins/packages/shimlogs/shimlogs.sh b/bins/packages/shimlogs/shimlogs.sh new file mode 100644 index 000000000..c31fb1e31 --- /dev/null +++ b/bins/packages/shimlogs/shimlogs.sh @@ -0,0 +1,52 @@ +SHIMLOGS_VERSION="0.1" +SHIMLOGS_CHECKSUM="f97c7067064d0cd1643c8141d982c293" +SHIMLOGS_LINK="https://github.com/threefoldtech/shim-logs/archive/v${SHIMLOGS_VERSION}.tar.gz" + +dependencies_shimlogs() { + apt-get install -y libjansson-dev libhiredis-dev build-essential +} + +download_shimlogs() { + download_file ${SHIMLOGS_LINK} ${SHIMLOGS_CHECKSUM} shim-logs-${SHIMLOGS_VERSION}.tar.gz +} + +extract_shimlogs() { + tar -xf ${DISTDIR}/shim-logs-${SHIMLOGS_VERSION}.tar.gz -C ${WORKDIR} +} + +prepare_shimlogs() { + echo "[+] prepare shim-logs" + github_name "shim-logs-${SHIMLOGS_VERSION}" +} + +compile_shimlogs() { + echo "[+] compile shim-logs" + make +} + +install_shimlogs() { + echo "[+] install shim-logs" + + mkdir -p "${ROOTDIR}/bin" + + cp shim-logs ${ROOTDIR}/bin/shim-logs + chmod +x ${ROOTDIR}/bin/shim-logs +} + +build_shimlogs() { + pushd "${DISTDIR}" + + dependencies_shimlogs + download_shimlogs + extract_shimlogs + + popd + pushd ${WORKDIR}/shim-logs-${SHIMLOGS_VERSION} + + prepare_shimlogs + compile_shimlogs + install_shimlogs + + popd +} + diff --git a/cmds/Makefile b/cmds/Makefile index 1cb8a387f..0b305d34e 100644 --- a/cmds/Makefile +++ b/cmds/Makefile @@ -5,13 +5,9 @@ dirty = $(shell test -n "`git diff --shortstat 2> /dev/null | tail -n1`" && echo version = github.com/threefoldtech/zos/pkg/version ldflags = '-w -s -X $(version).Branch=$(branch) -X $(version).Revision=$(revision) -X $(version).Dirty=$(dirty)' -all: $(shell ls -d */) logs +all: $(shell ls -d */) strip $(OUT)/* -logs: - $(MAKE) -C shim-logs - cp shim-logs/shim-logs $(OUT)/ - .PHONY: output clean output: diff --git a/cmds/shim-logs/Makefile b/cmds/shim-logs/Makefile deleted file mode 100644 index 582c52089..000000000 --- a/cmds/shim-logs/Makefile +++ /dev/null @@ -1,29 +0,0 @@ -EXEC = shim-logs - -CFLAGS = -g -W -Wall -O2 -LDFLAGS = -Wl,-Bstatic -lhiredis -ljansson -Wl,-Bdynamic -CC = cc - -SRC=$(wildcard *.c) -OBJ=$(SRC:.c=.o) - -all: $(EXEC) - -musl: CFLAGS = -g -W -Wall -O2 -march=x86-64 -musl: LDFLAGS = -static -lhiredis -ljansson -musl: CC = x86_64-pc-linux-musl-gcc -musl: clean $(EXEC) - -$(EXEC): $(OBJ) - $(CC) -o $@ $^ $(LDFLAGS) - -%.o: %.c - $(CC) -o $@ -c $< $(CFLAGS) - -clean: - rm -fv *.o - -mrproper: clean - rm -fv $(EXEC) - - diff --git a/cmds/shim-logs/README.md b/cmds/shim-logs/README.md deleted file mode 100644 index 7b0423f39..000000000 --- a/cmds/shim-logs/README.md +++ /dev/null @@ -1,27 +0,0 @@ -# Zero-OS custom shim-logs - -In order to provide enduser a way to get their container logs, we expose -to the user some tools to send logs to a remote location. - -In a first time, this was implemented in `contd` but if `contd` were restarted for -update or so, logs were not attached anymore. Logs handling was managed by `containerd-shim` which -is made to handle containers and let the daemon restart without loosing connection to container. - -Since containerd support [external binary to handle logs](https://gitlab.dev.cncf.ci/containerd/containerd/commit/e6ae9cc64f61fc5f65bdb5a8efeeca23ac1d28ea) -we use this method to fetch logs next-to the container and sending them to user-defined endpoint. - -# Implementation - -First implementation was made in Go like the example `containerd` provided, but since this -process will be running for each containers which needs logs handling, this can become a lot. - -Resource wise, this implementation uses ~160K of memory for each process, which is a lower footprint -than others implementation we tried. - -# How it works - -It follow the workflow that `containerd` provides: -- Log binary is started with 3 more file descriptors (3, 4 and 5), which are -respectively stdout, stderr and ready notifier. -- As soon as logs are ready, 5 is closed -- 3 and 4 are read async and forwarded to specified endpoint diff --git a/cmds/shim-logs/container.c b/cmds/shim-logs/container.c deleted file mode 100644 index f3b303f3a..000000000 --- a/cmds/shim-logs/container.c +++ /dev/null @@ -1,130 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "shim-logs.h" - -container_t *container_init() { - container_t *container; - - if(!(container = calloc(sizeof(container_t), 1))) - return NULL; - - container->id = getenv("CONTAINER_ID"); - container->namespace = getenv("CONTAINER_NAMESPACE"); - container->lockfd = 5; // lock wait fd - container->outfd = 3; // stdout fd - container->errfd = 4; // stderr fd - - if(container->id == NULL) { - fprintf(stderr, "[-] could not find container id\n"); - fprintf(stderr, "[-] ensure your environment is well set\n"); - exit(EXIT_FAILURE); - } - - if(!(container->logout = log_new(container->outfd))) - exit(EXIT_FAILURE); - - if(!(container->logerr = log_new(container->errfd))) - exit(EXIT_FAILURE); - - return container; -} - -void container_ready(container_t *container) { - printf("[+] sending ready signal\n"); - - if(write(container->lockfd, "X", 1) != 1) - perror("write"); - - close(container->lockfd); -} - -char *container_load_config(char *path) { - int fd; - char *buffer; - - if((fd = open(path, O_RDONLY)) < 0) - return NULL; - - // grabbing length - off_t end = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); - - if(!(buffer = malloc(end))) - return NULL; - - if(read(fd, buffer, end) != end) - perror("read"); - - close(fd); - - return buffer; -} - -static void *json_error(json_t *root, char *message) { - fprintf(stderr, "[-] json: %s\n", message); - json_decref(root); - return NULL; -} - -container_t *container_load_parse(container_t *c, json_t *root) { - if(!json_is_array(root)) - return json_error(root, "expected root array"); - - for(size_t i = 0; i < json_array_size(root); i++) { - json_t *data, *type; - - data = json_array_get(root, i); - if(!json_is_object(data)) - return json_error(root, "array item not an object"); - - type = json_object_get(data, "type"); - if(!json_is_string(type)) - return json_error(root, "type is not a string"); - - const char *stype = json_string_value(type); - - if(strcmp(stype, "redis") == 0) { - json_t *config = json_object_get(data, "data"); - redis_extract(c, config); - - } else { - // only supporting redis for now - fprintf(stderr, "[-] config: unsupported <%s> target\n", stype); - } - } - - return c; -} - -container_t *container_load(container_t *c) { - char path[512]; - char *buffer; - json_t *root; - json_error_t error; - - // setting up config path - sprintf(path, "%s/%s/%s-logs.json", CONFDIR, c->namespace, c->id); - - printf("[+] loading configuration: %s\n", path); - if(!(buffer = container_load_config(path))) - return NULL; - - // parsing json config - // printf(">> %s\n", buffer); - - if(!(root = json_loads(buffer, 0, &error))) { - fprintf(stderr, "json error: %d: %s\n", error.line, error.text); - return NULL; - } - - free(buffer); - - // fetching data from json and populate config - return container_load_parse(c, root); -} - - diff --git a/cmds/shim-logs/log_file.c b/cmds/shim-logs/log_file.c deleted file mode 100644 index a42a2dad2..000000000 --- a/cmds/shim-logs/log_file.c +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include "shim-logs.h" - -int file_write(void *_self, char *line, int len) { - file_t *self = (file_t *) _self; - - if(fwrite(line, len, 1, self->fp) != (size_t) len) - return 1; - - fflush(self->fp); - - return 0; -} - -file_t *file_new(char *path) { - file_t *backend; - - if(!(backend = calloc(sizeof(file_t), 1))) - diep("calloc"); - - if(!(backend->fp = fopen(path, "w"))) - diep("fopen"); - - backend->write = file_write; - - return backend; -} - diff --git a/cmds/shim-logs/log_redis.c b/cmds/shim-logs/log_redis.c deleted file mode 100644 index 00bc01388..000000000 --- a/cmds/shim-logs/log_redis.c +++ /dev/null @@ -1,79 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "shim-logs.h" -#include "url_parser.h" - -int redis_write(void *_self, char *line, int len) { - (void) len; - redis_t *self = (redis_t *) _self; - redisReply *reply; - - if(!(reply = redisCommand(self->conn, "PUBLISH %s %s", self->channel, line))) - diep("redis"); - - freeReplyObject(reply); - - return 0; -} - -redis_t *redis_new(char *host, int port, char *channel) { - redis_t *backend; - struct timeval timeout = { 2, 0 }; - - printf("[+] redis backend: [%s:%d / %s]\n", host, port, channel); - - if(!(backend = calloc(sizeof(redis_t), 1))) - diep("calloc"); - - if(!(backend->conn = redisConnectWithTimeout(host, port, timeout))) - diep("redis"); - - if(backend->conn->err) { - printf("redis: %s\n", backend->conn->errstr); - return NULL; - } - - backend->channel = strdup(channel); - backend->write = redis_write; - - return backend; -} - -static int redis_attach(const char *url, log_t *target) { - int port = 6379; - struct parsed_url *purl; - redis_t *redis; - - if(!(purl = parse_url(url))) - return 1; - - if(purl->port) - port = atoi(purl->port); - - if(!(redis = redis_new(purl->host, port, purl->path))) - return 1; - - parsed_url_free(purl); - log_attach(target, redis, redis->write); - - return 0; -} - -int redis_extract(container_t *c, json_t *root) { - json_t *sout = json_object_get(root, "stdout"); - json_t *serr = json_object_get(root, "stderr"); - - if(!json_is_string(sout) || !json_is_string(serr)) - return 1; - - redis_attach(json_string_value(sout), c->logout); - redis_attach(json_string_value(serr), c->logerr); - - return 0; -} diff --git a/cmds/shim-logs/logs.c b/cmds/shim-logs/logs.c deleted file mode 100644 index 6afdb2ae0..000000000 --- a/cmds/shim-logs/logs.c +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include -#include -#include "shim-logs.h" - -log_t *log_new(int fd) { - log_t *log; - - if(!(log = malloc(sizeof(log_t)))) - return NULL; - - log->backlen = 0; - log->backends = malloc(log->backlen * sizeof(void **)); - log->writers = malloc(log->backlen * sizeof(void **)); - log->fd = fd; - - if(!(log->stream = stream_new(4096))) - return NULL; - - return log; -} - -log_t *log_attach(log_t *log, void *backend, int (*writer)(void *, char *, int)) { - log->backlen += 1; - - if(!(log->backends = realloc(log->backends, log->backlen * sizeof(void **)))) - return NULL; - - if(!(log->writers = realloc(log->writers, log->backlen * sizeof(void **)))) - return NULL; - - log->backends[log->backlen - 1] = backend; - log->writers[log->backlen - 1] = writer; - - return log; -} - -log_t *log_dispatch(log_t *log, char *line) { - size_t length = strlen(line); - // printf("[+] dispatcher: %s", line); - - // calling each writers - for(size_t i = 0; i < log->backlen; i++) - log->writers[i](log->backends[i], line, length); - - return log; -} - - diff --git a/cmds/shim-logs/shim-logs.c b/cmds/shim-logs/shim-logs.c deleted file mode 100644 index 5ff591b99..000000000 --- a/cmds/shim-logs/shim-logs.c +++ /dev/null @@ -1,127 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "shim-logs.h" - -void diep(char *str) { - perror(str); - exit(EXIT_FAILURE); -} - -void attach_localfile(container_t *container) { - char path[512]; - - // we don't check if it works, let assume - // contd is running - mkdir(LOGSDIR, 0775); - - sprintf(path, "%s/%s", LOGSDIR, container->namespace); - mkdir(path, 0775); - - sprintf(path, "%s/%s/%s.log", LOGSDIR, container->namespace, container->id); - - file_t *local; - if(!(local = file_new(path))) - return; - - // attaching stdout and stderr to the same file - log_attach(container->logout, local, local->write); - log_attach(container->logerr, local, local->write); -} - -int main() { - printf("[+] initializing shim-logs\n"); - - // - // container object - // - container_t *container; - - if(!(container = container_init())) - diep("container"); - - if(!(container_load(container))) { - fprintf(stderr, "[-] could not load configuration\n"); - exit(EXIT_FAILURE); - } - - // - // debug file backend - // - attach_localfile(container); - - // - // initialize async - // - struct epoll_event event; - struct epoll_event *events = NULL; - int evfd; - - memset(&event, 0, sizeof(struct epoll_event)); - - if((evfd = epoll_create1(0)) < 0) - diep("epoll_create1"); - - event.data.fd = 3; - event.events = EPOLLIN; - - if(epoll_ctl(evfd, EPOLL_CTL_ADD, 3, &event) < 0) - diep("epoll_ctl"); - - event.data.fd = 4; - event.events = EPOLLIN; - - if(epoll_ctl(evfd, EPOLL_CTL_ADD, 4, &event) < 0) - diep("epoll_ctl"); - - if(!(events = calloc(MAXEVENTS, sizeof(event)))) - diep("calloc"); - - // - // notify caller we are ready - // - container_ready(container); - - // - // async fetching logs - // - while(1) { - int n = epoll_wait(evfd, events, MAXEVENTS, -1); - - if(n < 0) - diep("epoll_wait"); - - for(int i = 0; i < n; i++) { - struct epoll_event *ev = events + i; - - if(ev->events & EPOLLIN) { - log_t *target = NULL; - - if(ev->data.fd == container->logout->fd) - target = container->logout; - - if(ev->data.fd == container->logerr->fd) - target = container->logerr; - - // printf("[+] reading fd: %d\n", target->fd); - stream_read(target->fd, target->stream); - - char *line; - while((line = stream_line(target->stream))) - log_dispatch(target, line); - - if(stream_remain(target->stream) == 0) { - // printf("[+] recall stream buffer\n"); - stream_recall(target->stream); - } - } - } - } - - return 0; -} diff --git a/cmds/shim-logs/shim-logs.h b/cmds/shim-logs/shim-logs.h deleted file mode 100644 index 10a503689..000000000 --- a/cmds/shim-logs/shim-logs.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef SHIM_LOGS_H - #define SHIM_LOGS_H - - #include - #include - - #define MAXEVENTS 64 - #define CONFDIR "/var/cache/modules/contd/config" - #define LOGSDIR "/var/cache/modules/contd/logs" - // #define CONFDIR "/tmp/logconf" - // #define LOGSDIR "/tmp/logconf" - - // - // stream - // contains tools to read and bufferize - // from low level file descriptor - // - typedef struct stream_t { - char *buffer; - int length; - char *reader; - char *writer; - char *line; - - } stream_t; - - stream_t *stream_new(int size); - int stream_remain(stream_t *s); - int stream_read(int fd, stream_t *s); - char *stream_line(stream_t *s); - void stream_recall(stream_t *s); - - // - // logs - // contains list of backends for a specific stream - // eg: stdout stream with 3 backends (2 redis and 1 file) - // - typedef struct log_t { - void **backends; - int (**writers)(void *, char *, int); - size_t backlen; - stream_t *stream; - int fd; - - } log_t; - - log_t *log_new(int fd); - log_t *log_attach(log_t *log, void *backend, int (*writer)(void *, char *, int)); - log_t *log_dispatch(log_t *log, char *line); - - // - // container - // make the link between containerd process and - // our logger - // - typedef struct container_t { - char *id; - char *namespace; - int lockfd; - int outfd; - int errfd; - log_t *logout; - log_t *logerr; - - } container_t; - - container_t *container_init(); - void container_ready(container_t *container); - char *container_load_config(char *path); - container_t *container_load_parse(container_t *c, json_t *root); - container_t *container_load(container_t *c); - - // - // redis - // specific backend to publish some line into redis - // - typedef struct redis_t { - redisContext *conn; - char *channel; - int (*write)(void *self, char *line, int len); - - } redis_t; - - int redis_write(void *_self, char *line, int len); - redis_t *redis_new(char *host, int port, char *channel); - int redis_extract(container_t *c, json_t *root); - - // - // file - // specific backend to write log line into a file - // - typedef struct file_t { - FILE *fp; - char *path; - int (*write)(void *self, char *line, int len); - - } file_t; - - int file_write(void *_self, char *line, int len); - file_t *file_new(char *path); - - // - // utilities - // - void diep(char *s); - -#endif diff --git a/cmds/shim-logs/stream.c b/cmds/shim-logs/stream.c deleted file mode 100644 index f1a1ac5aa..000000000 --- a/cmds/shim-logs/stream.c +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include -#include -#include -#include "shim-logs.h" - -stream_t *stream_new(int size) { - stream_t *s; - - if(!(s = malloc(sizeof(stream_t)))) - return NULL; - - s->length = size; - s->buffer = malloc(s->length); - s->reader = s->buffer; - s->writer = s->buffer; - s->line = NULL; - - return s; -} - -int stream_remain(stream_t *s) { - return s->length - (s->writer - s->buffer); -} - -int stream_read(int fd, stream_t *s) { - int len; - - if((len = read(fd, s->writer, stream_remain(s))) < 0) - diep("read"); - - s->writer += len; - *s->writer = '\0'; - - return len; -} - -char *stream_line(stream_t *s) { - if(s->line == NULL) - s->line = malloc(s->length); - - char *found = strchr(s->reader, '\n'); - if(!found) - return NULL; - - size_t length = found - s->reader + 1; - - strncpy(s->line, s->reader, length); - s->line[length] = '\0'; - s->reader += length; - - return s->line; -} - -void stream_recall(stream_t *s) { - size_t length = s->length - (s->reader - s->buffer); - - memmove(s->buffer, s->reader, length); - - s->writer = s->buffer + length; - s->reader = s->buffer; - *s->writer = '\0'; -} - diff --git a/cmds/shim-logs/tools/Makefile b/cmds/shim-logs/tools/Makefile deleted file mode 100644 index 8da668230..000000000 --- a/cmds/shim-logs/tools/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -SPAWN = shim-spawn -DEBUG = shim-debug - -CFLAGS = -g -W -Wall -O2 -LDFLAGS = -CC = cc - -all: $(SPAWN) $(DEBUG) - -$(SPAWN): spawn.o - $(CC) -o $@ $^ $(LDFLAGS) - -$(DEBUG): debug.o - $(CC) -o $@ $^ $(LDFLAGS) - -%.o: %.c - $(CC) -o $@ -c $< $(CFLAGS) - -clean: - rm -fv *.o - -mrproper: clean - rm -fv $(SPAWN) $(DEBUG) - - diff --git a/cmds/shim-logs/tools/debug.c b/cmds/shim-logs/tools/debug.c deleted file mode 100644 index 71c081bab..000000000 --- a/cmds/shim-logs/tools/debug.c +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -#include -#include - -char *availables = "ABCDEFGHIKLMOPQRSTUVWXUZabcdefghijklmnopqrstuvwxyz0123456789$#"; - -char *rn(char *buffer, size_t len) { - size_t max = strlen(availables); - - for(size_t i = 0; i < len - 1; i++) - buffer[i] = availables[rand() % max]; - - return buffer; -} - -int main(void) { - char buffer[512], rnds[65]; - int written = 0, errors = 0; - - memset(rnds, 0x00, sizeof(rnds)); - srand(time(NULL)); - - for(int i = 0; i < 64; i++) { - written += sprintf(buffer, "[+ %6d] %s", written, rn(rnds, sizeof(rnds))); - puts(buffer); - usleep(60000); - - if(written % 72 == 0) { - errors += sprintf(buffer, "[- %6d] %s", errors, rn(rnds, sizeof(rnds))); - fprintf(stderr, "%s\n", buffer); - } - } - - return 0; -} diff --git a/cmds/shim-logs/tools/spawn.c b/cmds/shim-logs/tools/spawn.c deleted file mode 100644 index a2a026673..000000000 --- a/cmds/shim-logs/tools/spawn.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -void diep(char *str) { - perror(str); - exit(EXIT_FAILURE); -} - -int main() { - int stdo[2], stde[2], lock[2]; - int skip[2]; - - printf("[+] spawn: garbage pipes\n"); - if(pipe(skip)) - diep("skip"); - - if(pipe(skip)) - diep("skip"); - - printf("[+] spawn: opening real pipes\n"); - if(pipe(stdo)) - diep("stdout"); - - if(pipe(stde)) - diep("stderr"); - - if(pipe(lock)) - diep("lock"); - - printf("[+] spawn: forking\n"); - pid_t p = fork(); - if(p == 0) { - printf("[+] spawn: fork: changing fd\n"); - dup2(stdo[0], 3); - dup2(stde[0], 4); - dup2(lock[1], 5); - - printf("[+] spawn: fork: closing pipes reader\n"); - close(stdo[1]); - close(stde[1]); - close(lock[0]); - - printf("[+] spawn: fork: setting environment variables\n"); - setenv("CONTAINER_ID", "debug", 0); - setenv("CONTAINER_NAMESPACE", "maxux", 0); - - printf("[+] spawn: fork: executing shim-logs\n"); - if(execlp("../shim-logs", "../shim-logs", (char *) NULL) < 0) - diep("execlp"); - - } else { - pid_t px = fork(); - - if(px == 0) { - printf("[+] spawn: waiting for lock\n"); - - char buff[32]; - if(read(lock[0], buff, sizeof(buff)) < 0) - perror("read"); - - printf("[+] spawn: starting real process\n"); - dup2(stdo[1], 1); - dup2(stde[1], 2); - - execlp("./shim-debug", "./shim-debug", (char *) NULL); - } - - printf("[+] spawn (new): waiting for shim-debug [%d] to finish\n", px); - int status; - waitpid(px, &status, 0); - - printf("[+] spawn (new): shim-debug done, status: %d\n", WEXITSTATUS(status)); - } - - printf("[+] spawn: waiting for shim-logs [%d] to finish\n", p); - int status; - waitpid(p, &status, 0); - - printf("[+] spawn: shim-logs done, status: %d\n", WEXITSTATUS(status)); - - - return 0; -} diff --git a/cmds/shim-logs/url_parser.c b/cmds/shim-logs/url_parser.c deleted file mode 100644 index 1062bdf86..000000000 --- a/cmds/shim-logs/url_parser.c +++ /dev/null @@ -1,322 +0,0 @@ -/*_ - * Copyright 2010-2011 Scyphus Solutions Co. Ltd. All rights reserved. - * - * Authors: - * Hirochika Asai - */ - -#include "url_parser.h" - -#include -#include -#include -#include - -/* - * Prototype declarations - */ -static __inline__ int _is_scheme_char(int); - -/* - * Check whether the character is permitted in scheme string - */ -static __inline__ int -_is_scheme_char(int c) -{ - return (!isalpha(c) && '+' != c && '-' != c && '.' != c) ? 0 : 1; -} - -/* - * See RFC 1738, 3986 - */ -struct parsed_url * -parse_url(const char *url) -{ - struct parsed_url *purl; - const char *tmpstr; - const char *curstr; - int len; - int i; - int userpass_flag; - int bracket_flag; - - /* Allocate the parsed url storage */ - purl = malloc(sizeof(struct parsed_url)); - if ( NULL == purl ) { - return NULL; - } - purl->scheme = NULL; - purl->host = NULL; - purl->port = NULL; - purl->path = NULL; - purl->query = NULL; - purl->fragment = NULL; - purl->username = NULL; - purl->password = NULL; - - curstr = url; - - /* - * : - * := [a-z\+\-\.]+ - * upper case = lower case for resiliency - */ - /* Read scheme */ - tmpstr = strchr(curstr, ':'); - if ( NULL == tmpstr ) { - /* Not found the character */ - parsed_url_free(purl); - return NULL; - } - /* Get the scheme length */ - len = tmpstr - curstr; - /* Check restrictions */ - for ( i = 0; i < len; i++ ) { - if ( !_is_scheme_char(curstr[i]) ) { - /* Invalid format */ - parsed_url_free(purl); - return NULL; - } - } - - /* Copy the scheme to the storage */ - purl->scheme = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->scheme ) { - parsed_url_free(purl); - return NULL; - } - - strncpy(purl->scheme, curstr, (unsigned) len); - purl->scheme[len] = '\0'; - /* Make the character to lower if it is upper case. */ - for ( i = 0; i < len; i++ ) { - purl->scheme[i] = tolower(purl->scheme[i]); - } - /* Skip ':' */ - tmpstr++; - curstr = tmpstr; - - /* - * //:@:/ - * Any ":", "@" and "/" must be encoded. - */ - /* Eat "//" */ - for ( i = 0; i < 2; i++ ) { - if ( '/' != *curstr ) { - parsed_url_free(purl); - return NULL; - } - curstr++; - } - - /* Check if the user (and password) are specified. */ - userpass_flag = 0; - tmpstr = curstr; - while ( '\0' != *tmpstr ) { - if ( '@' == *tmpstr ) { - /* Username and password are specified */ - userpass_flag = 1; - break; - } else if ( '/' == *tmpstr ) { - /* End of : specification */ - userpass_flag = 0; - break; - } - tmpstr++; - } - - /* User and password specification */ - tmpstr = curstr; - if ( userpass_flag ) { - /* Read username */ - while ( '\0' != *tmpstr && ':' != *tmpstr && '@' != *tmpstr ) { - tmpstr++; - } - len = tmpstr - curstr; - purl->username = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->username ) { - parsed_url_free(purl); - return NULL; - } - - memcpy(purl->username, curstr, len); - purl->username[len] = '\0'; - /* Proceed current pointer */ - curstr = tmpstr; - if ( ':' == *curstr ) { - /* Skip ':' */ - curstr++; - /* Read password */ - tmpstr = curstr; - while ( '\0' != *tmpstr && '@' != *tmpstr ) { - tmpstr++; - } - len = tmpstr - curstr; - purl->password = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->password ) { - parsed_url_free(purl); - return NULL; - } - (void)strncpy(purl->password, curstr, len); - purl->password[len] = '\0'; - curstr = tmpstr; - } - /* Skip '@' */ - if ( '@' != *curstr ) { - parsed_url_free(purl); - return NULL; - } - curstr++; - } - - if ( '[' == *curstr ) { - bracket_flag = 1; - } else { - bracket_flag = 0; - } - /* Proceed on by delimiters with reading host */ - tmpstr = curstr; - while ( '\0' != *tmpstr ) { - if ( bracket_flag && ']' == *tmpstr ) { - /* End of IPv6 address. */ - tmpstr++; - break; - } else if ( !bracket_flag && (':' == *tmpstr || '/' == *tmpstr) ) { - /* Port number is specified. */ - break; - } - tmpstr++; - } - len = tmpstr - curstr; - purl->host = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->host || len <= 0 ) { - parsed_url_free(purl); - return NULL; - } - (void)strncpy(purl->host, curstr, len); - purl->host[len] = '\0'; - curstr = tmpstr; - - /* Is port number specified? */ - if ( ':' == *curstr ) { - curstr++; - /* Read port number */ - tmpstr = curstr; - while ( '\0' != *tmpstr && '/' != *tmpstr ) { - tmpstr++; - } - len = tmpstr - curstr; - purl->port = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->port ) { - parsed_url_free(purl); - return NULL; - } - (void)strncpy(purl->port, curstr, len); - purl->port[len] = '\0'; - curstr = tmpstr; - } - - /* End of the string */ - if ( '\0' == *curstr ) { - return purl; - } - - /* Skip '/' */ - if ( '/' != *curstr ) { - parsed_url_free(purl); - return NULL; - } - curstr++; - - /* Parse path */ - tmpstr = curstr; - while ( '\0' != *tmpstr && '#' != *tmpstr && '?' != *tmpstr ) { - tmpstr++; - } - len = tmpstr - curstr; - purl->path = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->path ) { - parsed_url_free(purl); - return NULL; - } - (void)strncpy(purl->path, curstr, len); - purl->path[len] = '\0'; - curstr = tmpstr; - - /* Is query specified? */ - if ( '?' == *curstr ) { - /* Skip '?' */ - curstr++; - /* Read query */ - tmpstr = curstr; - while ( '\0' != *tmpstr && '#' != *tmpstr ) { - tmpstr++; - } - len = tmpstr - curstr; - purl->query = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->query ) { - parsed_url_free(purl); - return NULL; - } - (void)strncpy(purl->query, curstr, len); - purl->query[len] = '\0'; - curstr = tmpstr; - } - - /* Is fragment specified? */ - if ( '#' == *curstr ) { - /* Skip '#' */ - curstr++; - /* Read fragment */ - tmpstr = curstr; - while ( '\0' != *tmpstr ) { - tmpstr++; - } - len = tmpstr - curstr; - purl->fragment = malloc(sizeof(char) * (len + 1)); - if ( NULL == purl->fragment ) { - parsed_url_free(purl); - return NULL; - } - (void)strncpy(purl->fragment, curstr, len); - purl->fragment[len] = '\0'; - curstr = tmpstr; - } - - return purl; -} - -/* - * Free memory of parsed url - */ -void -parsed_url_free(struct parsed_url *purl) -{ - if ( NULL != purl ) { - if ( NULL != purl->scheme ) { - free(purl->scheme); - } - if ( NULL != purl->host ) { - free(purl->host); - } - if ( NULL != purl->port ) { - free(purl->port); - } - if ( NULL != purl->path ) { - free(purl->path); - } - if ( NULL != purl->query ) { - free(purl->query); - } - if ( NULL != purl->fragment ) { - free(purl->fragment); - } - if ( NULL != purl->username ) { - free(purl->username); - } - if ( NULL != purl->password ) { - free(purl->password); - } - free(purl); - } -} diff --git a/cmds/shim-logs/url_parser.h b/cmds/shim-logs/url_parser.h deleted file mode 100644 index e5b2590c8..000000000 --- a/cmds/shim-logs/url_parser.h +++ /dev/null @@ -1,39 +0,0 @@ -/*_ - * Copyright 2010 Scyphus Solutions Co. Ltd. All rights reserved. - * - * Authors: - * Hirochika Asai - */ - -#ifndef _URL_PARSER_H -#define _URL_PARSER_H - -/* - * URL storage - */ -struct parsed_url { - char *scheme; /* mandatory */ - char *host; /* mandatory */ - char *port; /* optional */ - char *path; /* optional */ - char *query; /* optional */ - char *fragment; /* optional */ - char *username; /* optional */ - char *password; /* optional */ -}; - -#ifdef __cplusplus -extern "C" { -#endif - - /* - * Declaration of function prototypes - */ - struct parsed_url * parse_url(const char *); - void parsed_url_free(struct parsed_url *); - -#ifdef __cplusplus -} -#endif - -#endif /* _URL_PARSER_H */