[PATCH] Documentation: push-pull commands into a separate category.
[alt-git.git] / daemon.c
blob315a74bf108dc648fd9b86b40a4257fe27df907e
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include <sys/socket.h>
4 #include <netinet/in.h>
6 static const char daemon_usage[] = "git-daemon [--port=n]";
8 static int upload(char *dir, int dirlen)
10 if (chdir(dir) < 0)
11 return -1;
12 chdir(".git");
15 * Security on the cheap.
17 * We want a readable HEAD, usable "objects" directory, and
18 * a "git-daemon-export-ok" flag that says that the other side
19 * is ok with us doing this.
21 if (access("git-daemon-export-ok", F_OK) ||
22 access("objects/00", X_OK) ||
23 access("HEAD", R_OK))
24 return -1;
26 /* git-upload-pack only ever reads stuff, so this is safe */
27 execlp("git-upload-pack", "git-upload-pack", ".", NULL);
28 return -1;
31 static int execute(char *line, int len)
33 if (!strncmp("git-upload-pack /", line, 17))
34 return upload(line + 16, len - 16);
36 fprintf(stderr, "got bad connection '%s'\n", line);
37 return -1;
40 static void handle(int incoming, struct sockaddr_in *addr, int addrlen)
42 static char line[1000];
43 int len;
45 if (fork()) {
46 close(incoming);
47 return;
50 dup2(incoming, 0);
51 dup2(incoming, 1);
52 close(incoming);
53 len = packet_read_line(0, line, sizeof(line));
55 if (len && line[len-1] == '\n')
56 line[--len] = 0;
58 exit(execute(line, len));
61 static int serve(int port)
63 int sockfd;
64 struct sockaddr_in addr;
66 sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
67 if (sockfd < 0)
68 die("unable to open socket (%s)", strerror(errno));
69 memset(&addr, 0, sizeof(addr));
70 addr.sin_port = htons(port);
71 addr.sin_family = AF_INET;
72 if (bind(sockfd, (void *)&addr, sizeof(addr)) < 0)
73 die("unable to bind to port %d (%s)", port, strerror(errno));
74 if (listen(sockfd, 5) < 0)
75 die("unable to listen to port %d (%s)", port, strerror(errno));
77 for (;;) {
78 struct sockaddr_in in;
79 socklen_t addrlen = sizeof(in);
80 int incoming = accept(sockfd, (void *)&in, &addrlen);
82 if (incoming < 0) {
83 switch (errno) {
84 case EAGAIN:
85 case EINTR:
86 case ECONNABORTED:
87 continue;
88 default:
89 die("accept returned %s", strerror(errno));
92 handle(incoming, &in, addrlen);
96 int main(int argc, char **argv)
98 int port = DEFAULT_GIT_PORT;
99 int i;
101 for (i = 1; i < argc; i++) {
102 char *arg = argv[i];
104 if (!strncmp(arg, "--port=", 7)) {
105 char *end;
106 unsigned long n;
107 n = strtoul(arg+7, &end, 0);
108 if (arg[7] && !*end) {
109 port = n;
110 continue;
113 usage(daemon_usage);
116 return serve(port);