tMake server mode one-shot - synk - synchronize files between hosts
DIR Log
DIR Files
DIR Refs
DIR README
DIR LICENSE
---
DIR commit 82cf376888b0a53cfc1928a739a951bd0fbf30b1
DIR parent e88965f27c0b666e672353ad1a79558fe0f86f6b
HTML Author: Willy <willyatmailoodotorg>
Date: Fri, 2 Sep 2016 13:52:57 +0200
Make server mode one-shot
This make the server able to handle a single connection only, and thus,
will make it terminate after syncing.
tthis also moves threading out of the equation.
Diffstat:
M config.mk | 2 +-
M synk.c | 35 +++++++++++++-------------------
2 files changed, 15 insertions(+), 22 deletions(-)
---
DIR diff --git a/config.mk b/config.mk
t@@ -9,4 +9,4 @@ MANDIR = ${PREFIX}/man
CPPFLAGS = -DVERSION=\"${VERSION}\"
CFLAGS = ${CPPFLAGS} -Wall -Wextra -pedantic -g
LDFLAGS =
-LIBS = -lpthread
+LIBS =
DIR diff --git a/synk.c b/synk.c
t@@ -1,5 +1,4 @@
#include <limits.h>
-#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
t@@ -60,7 +59,7 @@ enum {
};
void usage(char *name);
-void *sendmetadata(void *arg);
+int sendmetadata(struct client_t *);
int serverloop(in_addr_t, in_port_t);
char *echo(char * []);
t@@ -201,18 +200,17 @@ getmetadata(const char *fn)
* Read a path from a connected client, get the timestamp for this path and
* send it back to the client. Close connection afterward.
*/
-void *
-sendmetadata(void *arg)
+int
+sendmetadata(struct client_t *c)
{
ssize_t len = 0;
struct metadata_t *local, remote;
- struct client_t *c = (struct client_t *)arg;
memset(&remote, 0, sizeof(remote));
if ((len = read(c->fd, &remote, sizeof(remote))) < 0) {
perror(inet_ntoa(c->inet));
- pthread_exit(NULL);
+ return -1;
}
local = getmetadata(remote.path);
t@@ -223,7 +221,7 @@ sendmetadata(void *arg)
free(c);
- pthread_exit(NULL);
+ return -1;
}
/*
t@@ -237,7 +235,6 @@ serverloop(in_addr_t host, in_port_t port)
int sfd;
int cfd;
socklen_t len;
- pthread_t th;
struct sockaddr_in clt;
struct sockaddr_in srv;
struct client_t *c = NULL;
t@@ -263,22 +260,18 @@ serverloop(in_addr_t host, in_port_t port)
}
len = sizeof(clt);
- for (;;) {
- if ((cfd = accept(sfd, (struct sockaddr *)&clt, &len)) < 0) {
- perror("accept");
- return -1;
- }
-
- c = malloc(sizeof(struct client_t));
- c->fd = cfd;
- c->inet = clt.sin_addr;
-
- pthread_create(&th, NULL, sendmetadata, c);
+ if ((cfd = accept(sfd, (struct sockaddr *)&clt, &len)) < 0) {
+ perror("accept");
+ return -1;
}
- close(sfd); /* NOTREACHED */
+ c = malloc(sizeof(struct client_t));
+ c->fd = cfd;
+ c->inet = clt.sin_addr;
- return 0;
+ sendmetadata(c);
+
+ return close(sfd);
}
/*