foo
[reposerve] / server / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <time.h>
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/select.h>
11 #include <netinet/in.h>
12 #include "repo.h"
13 #include "client.h"
14
15 static const char *guess_repo_name(const char *path);
16 static int parse_args(int argc, char **argv);
17
18 static const char *repo_path, *repo_name;
19 static int lis = -1;
20 static int port = 64357;
21
22 static struct client *clients;
23
24 int main(int argc, char **argv)
25 {
26         struct sockaddr_in addr;
27
28         if(parse_args(argc, argv) == -1) {
29                 return 1;
30         }
31
32         if(repo_init(repo_path) == -1) {
33                 return 1;
34         }
35         /* repo_cleanup(); */
36
37         if(!repo_name) {
38                 if(!(repo_name = guess_repo_name(repo_path))) {
39                         return 1;
40                 }
41         }
42
43         printf("Serving %s from %s\n", repo_name, repo_path);
44
45         if(!(lis = socket(PF_INET, SOCK_STREAM, 0))) {
46                 fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
47                 return 1;
48         }
49
50         memset(&addr, 0, sizeof addr);
51         addr.sin_family = AF_INET;
52         addr.sin_port = htons(port);
53         addr.sin_addr.s_addr = INADDR_ANY;
54
55         if(bind(lis, (struct sockaddr*)&addr, sizeof addr) == -1) {
56                 fprintf(stderr, "failed to bind listening socket to port %d\n", port);
57                 return 1;
58         }
59         listen(lis, 8);
60
61         for(;;) {
62                 int res, max_fd, s;
63                 fd_set rdset;
64                 struct client *c, dummy;
65
66                 FD_SET(lis, &rdset);
67                 max_fd = lis;
68
69                 c = clients;
70                 while(c) {
71                         FD_SET(c->s, &rdset);
72                         if(c->s > max_fd) max_fd = c->s;
73                         c = c->next;
74                 }
75
76                 if((res = select(max_fd + 1, &rdset, 0, 0, 0)) == -1 && errno != EINTR) {
77                         perror("select failed");
78                         break;
79                 }
80
81                 if(res <= 0) continue;
82
83                 dummy.next = clients;
84                 c = &dummy;
85                 while(c->next) {
86                         if(FD_ISSET(c->next->s, &rdset)) {
87                                 if(handle_client(c->next) == -1) {
88                                         struct client *tmp = c->next;
89                                         c->next = tmp->next;
90                                         close(tmp->s);
91                                         free(tmp);
92                                         continue;
93                                 }
94                         }
95                         c = c->next;
96                 }
97                 clients = dummy.next;
98
99                 if(FD_ISSET(lis, &rdset)) {
100                         if((s = accept(lis, 0, 0)) == -1) {
101                                 perror("failed to accept connection");
102                                 continue;
103                         }
104                         if(!(c = malloc(sizeof *c))) {
105                                 perror("failed to allocate memory for client structure");
106                                 close(s);
107                                 continue;
108                         }
109                         c->s = s;
110                         c->next = clients;
111                         clients = c;
112                 }
113         }
114
115         return 0;
116 }
117
118 static const char *guess_repo_name(const char *path)
119 {
120         static char pathbuf[PATH_MAX];
121         char *last_slash;
122         int len;
123
124         if(!realpath(path, pathbuf)) {
125                 goto fail;
126         }
127         len = strlen(pathbuf);
128
129         if(len > 0 && pathbuf[len - 1] == '/') {
130                 pathbuf[--len] = 0;
131         }
132         if(len <= 0) goto fail;
133
134         if((last_slash = strrchr(pathbuf, '/'))) {
135                 return last_slash + 1;
136         }
137         return pathbuf;
138
139 fail:
140         fprintf(stderr, "failed to resolve path %s while determining the repo name. Please specify an explicit name with -id\n", repo_path);
141         return 0;
142 }
143
144 static void print_usage(const char *argv0)
145 {
146         printf("Usage: %s [options] [repo path]\n", argv0);
147         printf("options:\n");
148         printf("  -id <name>: repo identifier (defaults to directory name)\n");
149         printf("  -h,-help: print help and exit\n");
150 }
151
152 static int parse_args(int argc, char **argv)
153 {
154         int i;
155
156         for(i=1; i<argc; i++) {
157                 if(argv[i][0] == '-') {
158                         if(strcmp(argv[i], "-id") == 0) {
159                                 repo_name = argv[++i];
160                         } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
161                                 print_usage(argv[0]);
162                                 exit(0);
163                         } else {
164                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
165                                 return -1;
166                         }
167                 } else {
168                         if(repo_path) {
169                                 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
170                                 return -1;
171                         }
172                         repo_path = argv[i];
173                 }
174         }
175
176         if(!repo_path) repo_path = ".";
177         return 0;
178 }