Sfoglia il codice sorgente

Removed dependance on lockfile. Huge speed increase.

Tom Flucke 6 anni fa
parent
commit
7912bf8282
4 ha cambiato i file con 75 aggiunte e 15 eliminazioni
  1. 1 0
      .gitignore
  2. 19 0
      src/distributer/Makefile
  3. 23 15
      src/distributer/distribute.sh
  4. 32 0
      src/distributer/fixed-read.c

+ 1 - 0
.gitignore

@@ -13,6 +13,7 @@ data/*/
 src/flow-seperator/flow-seperator
 src/pcap-matcher/pcap-matcher
 src/packet-matcher/packet-matcher
+src/distributer/fixed-read
 src/common/*.a
 data/keylog-matchings.txt
 **/__pycache__/

+ 19 - 0
src/distributer/Makefile

@@ -0,0 +1,19 @@
+TARGET=fixed-read
+CC=gcc
+LIBS=../common
+CCFLAGS=-Wall -O2 -I$(LIBS)
+LDFLAGS=-L$(LIBS)
+
+ODIR=obj
+
+SOURCES = $(wildcard *.c)
+OBJECTS = $(patsubst %.c, $(ODIR)/%.o, $(SOURCES))
+HEADERS = $(wildcard *.h)
+
+default: $(TARGET)
+
+$(TARGET): $(SOURCES)
+	$(CC) $(CCFLAGS) $^ $(LDFLAGS) -o $@ -D"PROG_NAME=\"$@\""
+
+clean:
+	rm -f $(TARGET) $(ODIR)/*.o *~

+ 23 - 15
src/distributer/distribute.sh

@@ -2,10 +2,11 @@
 
 readonly DEFAULT_OUT_FMT="%s.out"
 readonly CMD_FEED="$(mktemp -u /tmp/distributer-XXX.fifo)"
-readonly LOCK="$CMD_FEED.lock"
-readonly LOCK_TIMEOUT="1"
 readonly PROC_BUFFER=10
 readonly MAX_PROCS=$(expr $(ulimit -u) / 3 - $PROC_BUFFER)
+readonly MAX_CMD_SIZE=350
+readonly READER="$(dirname $(realpath $0))/fixed-read"
+readonly TIMEOUT=600 # 10 minutes
 
 readonly CONF_LIST="$1"
 readonly SERVER_LIST="$2"
@@ -40,18 +41,26 @@ clean_server() {
 
 run_server() {
     server="$1"
-    loop="/tmp/$(basename $CMD_FEED .fifo)-$server.fifo"
+    loop="$(mktemp -u /tmp/$(basename $CMD_FEED .fifo)-$server-XXX.fifo)"
+    out_file=$(printf "$OUT_FMT" "$(basename "$loop" .fifo)")
     mkfifo "$loop"
     trap "clean_server $loop" 2 15
-    while lockfile -$LOCK_TIMEOUT "$LOCK"; read cmd; do
-        rm -f "$LOCK"
-        cmd_sanitized="$(echo "$cmd" | sed "$S_SPACE;$R_DASH;$S_QUOTE")"
-        out_file="$(printf "$OUT_FMT" "$cmd_sanitized")"
-        printf "$server: $cmd\n" >&2
-        printf "$cmd > $out_file\necho\n"
-        read line < "$loop" > /dev/null # Block until command completes
-    done | ssh -oBatchMode=yes -oStrictHostKeyChecking=no "$server" "sh" > "$loop"
-    rm -f "$LOCK"
+    {
+        printf "echo > %s\necho\n" "$out_file"
+        # Block until command completes
+        while read -t $TIMEOUT line < "$loop" > /dev/null && \
+            cmd=$("$READER" $MAX_CMD_SIZE)
+        do
+            cmd=$(printf "%s" "$cmd" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
+            #cmd_sanitized="$(echo $cmd | sed "$S_SPACE;$R_DASH;$S_QUOTE")"
+            #out_file="$(printf "$OUT_FMT" $cmd_sanitized)"
+            printf "$server: %s\n" "$cmd" >&2
+            #printf "%s > %s\necho\n" "$cmd" "$out_file"
+            printf "printf \"%s: \" >> %s\n" "$cmd"  "$out_file"
+            printf "%s >> %s\n" "$cmd"  "$out_file"
+            printf "echo\n"
+        done
+    } | ssh -oBatchMode=yes -oStrictHostKeyChecking=no "$server" "sh" > "$loop"
     clean_server "$loop"
     echo "Server '$server' finished!" >&2
 }
@@ -61,15 +70,14 @@ clean_up() {
         pkill -P $pid
     done
     rm "$CMD_FEED"
-    [ -e "$LOCK" ] && rm -f "$LOCK"
     exit 2
 }
 
 main() {
     mkfifo "$CMD_FEED"
     trap clean_up 2 15
-    cat "$CONF_LIST" | sed '/^[[:space:]]*$/d' > "$CMD_FEED" &
-    pids=""
+    cat "$CONF_LIST" | sed '/^[[:space:]]*$/d' | \
+        xargs -d'\n' printf "%-$MAX_CMD_SIZE.${MAX_CMD_SIZE}s" > "$CMD_FEED" &
     for server in $(head -n$MAX_PROCS "$SERVER_LIST"); do
         run_server "$server" < "$CMD_FEED" > /dev/null &
     done

+ 32 - 0
src/distributer/fixed-read.c

@@ -0,0 +1,32 @@
+#include <unistd.h>
+#include <stdio.h>
+
+#ifndef PROG_NAME
+#define PROG_NAME "a.out"
+#endif
+
+void help() {
+  fprintf(stderr, "Usage: %s byte_count\n", PROG_NAME);
+  fprintf(stderr, "    Reads a byte_count bytes and outputs them.\n");
+}
+
+int fixed_read(size_t n) {
+  char buff[n];
+  int err = read(STDIN_FILENO, buff, n);
+  if (-1 == err) {
+    perror(PROG_NAME);
+  }
+  else if (-1 == write(STDOUT_FILENO, buff, err)) {
+    perror(PROG_NAME);
+  }
+  return n != err;
+}
+
+int main(int argn, char** argv) {
+  size_t n;
+  if (1 == argn || 1 != sscanf(argv[1], " %zu", &n)) {
+    help();
+    return 1;
+  }
+  return fixed_read(n);
+}