flist request
[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 <signal.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/select.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include "repo.h"
16 #include "client.h"
17
18 static const char *guess_repo_name(const char *path);
19 static void sighandler(int s);
20 static int parse_args(int argc, char **argv);
21
22 static const char *repo_name;
23 static int lis = -1;
24 static int port = 64357;
25 static int quit;
26
27 static struct client *clients;
28
29 const char *repo_path;
30
31
32 int main(int argc, char **argv)
33 {
34         struct sockaddr_in addr;
35
36         if(parse_args(argc, argv) == -1) {
37                 return 1;
38         }
39
40         signal(SIGINT, sighandler);
41         signal(SIGQUIT, sighandler);
42         signal(SIGILL, sighandler);
43         signal(SIGSEGV, sighandler);
44         signal(SIGTERM, sighandler);
45         signal(SIGPIPE, SIG_IGN);
46
47         if(repo_init(repo_path) == -1) {
48                 return 1;
49         }
50         /* repo_cleanup(); */
51
52         if(!repo_name) {
53                 if(!(repo_name = guess_repo_name(repo_path))) {
54                         return 1;
55                 }
56         }
57
58         printf("Serving %s from %s\n", repo_name, repo_path);
59
60         if(!(lis = socket(PF_INET, SOCK_STREAM, 0))) {
61                 fprintf(stderr, "failed to create socket: %s\n", strerror(errno));
62                 return 1;
63         }
64         fcntl(lis, F_SETFL, fcntl(lis, F_GETFL) | O_NONBLOCK);
65
66         memset(&addr, 0, sizeof addr);
67         addr.sin_family = AF_INET;
68         addr.sin_port = htons(port);
69         addr.sin_addr.s_addr = INADDR_ANY;
70
71         if(bind(lis, (struct sockaddr*)&addr, sizeof addr) == -1) {
72                 fprintf(stderr, "failed to bind listening socket to port %d\n", port);
73                 return 1;
74         }
75         listen(lis, 8);
76
77         for(;;) {
78                 int res, max_fd, s;
79                 fd_set rdset;
80                 struct client *c, dummy;
81
82                 FD_SET(lis, &rdset);
83                 max_fd = lis;
84
85                 c = clients;
86                 while(c) {
87                         FD_SET(c->s, &rdset);
88                         if(c->s > max_fd) max_fd = c->s;
89                         c = c->next;
90                 }
91
92                 if((res = select(max_fd + 1, &rdset, 0, 0, 0)) == -1 && errno != EINTR) {
93                         perror("select failed");
94                         break;
95                 }
96                 if(quit) break;
97                 if(res <= 0) continue;
98
99                 dummy.next = clients;
100                 c = &dummy;
101                 while(c->next) {
102                         if(FD_ISSET(c->next->s, &rdset)) {
103                                 if(handle_client(c->next) == -1) {
104                                         struct client *tmp = c->next;
105                                         c->next = tmp->next;
106
107                                         printf("Client %s disconnected\n", tmp->addr ? tmp->addr : "unknown");
108                                         close(tmp->s);
109                                         free(tmp->addr);
110                                         free(tmp);
111                                         continue;
112                                 }
113                         }
114                         c = c->next;
115                 }
116                 clients = dummy.next;
117
118                 if(FD_ISSET(lis, &rdset)) {
119                         struct sockaddr_in addr;
120                         unsigned int addrsz = sizeof addr;
121
122                         if((s = accept(lis, (struct sockaddr*)&addr, &addrsz)) == -1) {
123                                 perror("failed to accept connection");
124                                 continue;
125                         }
126                         fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
127
128                         if(!(c = calloc(1, sizeof *c))) {
129                                 perror("failed to allocate memory for client structure");
130                                 close(s);
131                                 continue;
132                         }
133                         c->s = s;
134                         c->next = clients;
135                         c->addr = strdup(inet_ntoa(addr.sin_addr));
136                         clients = c;
137
138                         send_string(c, "reposerve-0.1 protocol:0\n");
139
140                         printf("Connection from: %s\n", c->addr ? c->addr : "unknown");
141                 }
142         }
143
144         printf("Shutting down\n");
145         return 0;
146 }
147
148 static const char *guess_repo_name(const char *path)
149 {
150         static char pathbuf[PATH_MAX];
151         char *last_slash;
152         int len;
153
154         if(!realpath(path, pathbuf)) {
155                 goto fail;
156         }
157         len = strlen(pathbuf);
158
159         if(len > 0 && pathbuf[len - 1] == '/') {
160                 pathbuf[--len] = 0;
161         }
162         if(len <= 0) goto fail;
163
164         if((last_slash = strrchr(pathbuf, '/'))) {
165                 return last_slash + 1;
166         }
167         return pathbuf;
168
169 fail:
170         fprintf(stderr, "failed to resolve path %s while determining the repo name. Please specify an explicit name with -id\n", repo_path);
171         return 0;
172 }
173
174 static void sighandler(int s)
175 {
176         quit = 1;
177 }
178
179 static void print_usage(const char *argv0)
180 {
181         printf("Usage: %s [options] [repo path]\n", argv0);
182         printf("options:\n");
183         printf("  -id <name>: repo identifier (defaults to directory name)\n");
184         printf("  -h,-help: print help and exit\n");
185 }
186
187 static int parse_args(int argc, char **argv)
188 {
189         int i;
190
191         for(i=1; i<argc; i++) {
192                 if(argv[i][0] == '-') {
193                         if(strcmp(argv[i], "-id") == 0) {
194                                 repo_name = argv[++i];
195                         } else if(strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "-help") == 0) {
196                                 print_usage(argv[0]);
197                                 exit(0);
198                         } else {
199                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
200                                 return -1;
201                         }
202                 } else {
203                         if(repo_path) {
204                                 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
205                                 return -1;
206                         }
207                         repo_path = argv[i];
208                 }
209         }
210
211         if(!repo_path) repo_path = ".";
212         return 0;
213 }