projects
/
reposerve
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
server responds to flist
[reposerve]
/
server
/
src
/
main.c
diff --git
a/server/src/main.c
b/server/src/main.c
index
ab5c055
..
c1e19d9
100644
(file)
--- a/
server/src/main.c
+++ b/
server/src/main.c
@@
-4,20
+4,25
@@
#include <errno.h>
#include <limits.h>
#include <time.h>
#include <errno.h>
#include <limits.h>
#include <time.h>
+#include <signal.h>
#include <unistd.h>
#include <unistd.h>
+#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
+#include <arpa/inet.h>
#include "repo.h"
#include "client.h"
static const char *guess_repo_name(const char *path);
#include "repo.h"
#include "client.h"
static const char *guess_repo_name(const char *path);
+static void sighandler(int s);
static int parse_args(int argc, char **argv);
static const char *repo_name;
static int lis = -1;
static int port = 64357;
static int parse_args(int argc, char **argv);
static const char *repo_name;
static int lis = -1;
static int port = 64357;
+static int quit;
static struct client *clients;
static struct client *clients;
@@
-32,6
+37,12
@@
int main(int argc, char **argv)
return 1;
}
return 1;
}
+ signal(SIGINT, sighandler);
+ signal(SIGQUIT, sighandler);
+ signal(SIGILL, sighandler);
+ signal(SIGSEGV, sighandler);
+ signal(SIGTERM, sighandler);
+
if(repo_init(repo_path) == -1) {
return 1;
}
if(repo_init(repo_path) == -1) {
return 1;
}
@@
-49,6
+60,7
@@
int main(int argc, char **argv)
fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
return 1;
}
fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
return 1;
}
+ fcntl(lis, F_SETFL, fcntl(lis, F_GETFL) | O_NONBLOCK);
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
@@
-80,7
+92,7
@@
int main(int argc, char **argv)
perror("select failed");
break;
}
perror("select failed");
break;
}
-
+ if(quit) break;
if(res <= 0) continue;
dummy.next = clients;
if(res <= 0) continue;
dummy.next = clients;
@@
-90,7
+102,10
@@
int main(int argc, char **argv)
if(handle_client(c->next) == -1) {
struct client *tmp = c->next;
c->next = tmp->next;
if(handle_client(c->next) == -1) {
struct client *tmp = c->next;
c->next = tmp->next;
+
+ printf("Client %s disconnected\n", tmp->addr ? tmp->addr : "unknown");
close(tmp->s);
close(tmp->s);
+ free(tmp->addr);
free(tmp);
continue;
}
free(tmp);
continue;
}
@@
-100,10
+115,15
@@
int main(int argc, char **argv)
clients = dummy.next;
if(FD_ISSET(lis, &rdset)) {
clients = dummy.next;
if(FD_ISSET(lis, &rdset)) {
- if((s = accept(lis, 0, 0)) == -1) {
+ struct sockaddr_in addr;
+ unsigned int addrsz = sizeof addr;
+
+ if((s = accept(lis, (struct sockaddr*)&addr, &addrsz)) == -1) {
perror("failed to accept connection");
continue;
}
perror("failed to accept connection");
continue;
}
+ fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
+
if(!(c = calloc(1, sizeof *c))) {
perror("failed to allocate memory for client structure");
close(s);
if(!(c = calloc(1, sizeof *c))) {
perror("failed to allocate memory for client structure");
close(s);
@@
-111,12
+131,16
@@
int main(int argc, char **argv)
}
c->s = s;
c->next = clients;
}
c->s = s;
c->next = clients;
+ c->addr = strdup(inet_ntoa(addr.sin_addr));
clients = c;
send_string(c, "reposerve-0.1 protocol:0\n");
clients = c;
send_string(c, "reposerve-0.1 protocol:0\n");
+
+ printf("Connection from: %s\n", c->addr ? c->addr : "unknown");
}
}
}
}
+ printf("Shutting down\n");
return 0;
}
return 0;
}
@@
-146,6
+170,11
@@
fail:
return 0;
}
return 0;
}
+static void sighandler(int s)
+{
+ quit = 1;
+}
+
static void print_usage(const char *argv0)
{
printf("Usage: %s [options] [repo path]\n", argv0);
static void print_usage(const char *argv0)
{
printf("Usage: %s [options] [repo path]\n", argv0);