proj: Add server.c in perf
This commit is contained in:
6
proj/perf/.gitignore
vendored
Normal file
6
proj/perf/.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
main
|
||||
server
|
||||
*.csv
|
||||
*.txt
|
||||
*.log
|
||||
*.zip
|
||||
@@ -4,13 +4,16 @@ CFLAGS=-std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -
|
||||
|
||||
.PHONY: all clean
|
||||
all: default
|
||||
default: main
|
||||
default: main server
|
||||
|
||||
main.o: main.c
|
||||
%.o: %.c
|
||||
$(CC) -c -o $@ $^ $(CFLAGS)
|
||||
|
||||
main: main.o
|
||||
$(CC) -o $@ $^ $(CFLAGS) -lc
|
||||
|
||||
server: server.o
|
||||
$(CC) -o $@ $^ $(CFLAGS) -lc
|
||||
|
||||
clean:
|
||||
rm -rf main *.o
|
||||
|
||||
66
proj/perf/server.c
Normal file
66
proj/perf/server.c
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(const int argc, const char *argv[]) {
|
||||
if (argc != 3) {
|
||||
fprintf(stderr, "usage: server <path> <mode>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *path = argv[1];
|
||||
const char *mode = argv[2];
|
||||
|
||||
int fd;
|
||||
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
||||
fprintf(stderr, "unable to create socket: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct sockaddr_un addr = {.sun_family = AF_UNIX};
|
||||
strcpy(addr.sun_path, path);
|
||||
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
|
||||
fprintf(stderr, "unable to bind socket: %s\n", strerror(errno));
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listen(fd, 4) != 0) {
|
||||
fprintf(stderr, "unable to listen on socket: %s\n", strerror(errno));
|
||||
close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t len = 4096;
|
||||
char *line = malloc(len);
|
||||
while (1) {
|
||||
int client_fd;
|
||||
if ((client_fd = accept(fd, NULL, NULL)) == -1) {
|
||||
fprintf(stderr, "unable to accept connection: %s\n", strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
FILE *client;
|
||||
if ((client = fdopen(client_fd, "w+")) == NULL) {
|
||||
fprintf(stderr, "unable open stream: %s\n", strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
int n = 0;
|
||||
while (getline(&line, &len, client) > 0) {
|
||||
if (n % 2 == 1) {
|
||||
fprintf(client, "%s\n", mode);
|
||||
}
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
free(line);
|
||||
close(fd);
|
||||
unlink(path);
|
||||
}
|
||||
@@ -14,9 +14,22 @@ function test() {
|
||||
done
|
||||
}
|
||||
|
||||
rm /tmp/test.ok.sock /tmp/test.return.sock /tmp/intercept.performance.ok.sock /tmp/intercept.performance.return.sock
|
||||
./server /tmp/test.ok.sock 'ok' &
|
||||
./server /tmp/test.return.sock 'return 0' &
|
||||
|
||||
test ./main
|
||||
cd ../intercept
|
||||
test ./intercept -o -i - -- ../perf/main
|
||||
test ./intercept -o -i stderr -- ../perf/main
|
||||
test ./intercept -o -i file:out.log -- ../perf/main
|
||||
#test ./intercept -o -i unix:/ -- ../perf/main
|
||||
|
||||
cd ../server/src
|
||||
./performance -m ok &
|
||||
./performance -m return &
|
||||
cd ../../intercept
|
||||
sleep 1
|
||||
test ./intercept -o -i unix:/tmp/test.ok.sock -- ../perf/main
|
||||
test ./intercept -o -i unix:/tmp/test.return.sock -- ../perf/main
|
||||
test ./intercept -o -i unix:/tmp/intercept.performance.ok.sock -- ../perf/main
|
||||
test ./intercept -o -i unix:/tmp/intercept.performance.return.sock -- ../perf/main
|
||||
|
||||
Reference in New Issue
Block a user