Linus Torvalds | eaa9491 | 2005-07-16 03:42:28 | [diff] [blame] | 1 | #include <signal.h> |
| 2 | #include <sys/wait.h> |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 3 | #include <sys/socket.h> |
Jason Riedy | 3cd6ecd | 2005-08-23 20:34:44 | [diff] [blame] | 4 | #include <sys/time.h> |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 5 | #include <sys/poll.h> |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 6 | #include <netdb.h> |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 7 | #include <netinet/in.h> |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 8 | #include <arpa/inet.h> |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 9 | #include <syslog.h> |
Randal L. Schwartz | 979e32f | 2005-10-25 23:29:09 | [diff] [blame] | 10 | #include "pkt-line.h" |
| 11 | #include "cache.h" |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 12 | |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 13 | static int log_syslog; |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 14 | static int verbose; |
| 15 | |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 16 | static const char daemon_usage[] = |
| 17 | "git-daemon [--verbose] [--syslog] [--inetd | --port=n] [--export-all]\n" |
| 18 | " [--timeout=n] [--init-timeout=n] [directory...]"; |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 19 | |
| 20 | /* List of acceptable pathname prefixes */ |
| 21 | static char **ok_paths = NULL; |
| 22 | |
| 23 | /* If this is set, git-daemon-export-ok is not required */ |
| 24 | static int export_all_trees = 0; |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 25 | |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 26 | /* Timeout, and initial timeout */ |
| 27 | static unsigned int timeout = 0; |
| 28 | static unsigned int init_timeout = 0; |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 29 | |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 30 | static void logreport(int priority, const char *err, va_list params) |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 31 | { |
| 32 | /* We should do a single write so that it is atomic and output |
| 33 | * of several processes do not get intermingled. */ |
| 34 | char buf[1024]; |
| 35 | int buflen; |
| 36 | int maxlen, msglen; |
| 37 | |
| 38 | /* sizeof(buf) should be big enough for "[pid] \n" */ |
Junio C Hamano | 1bedd4c | 2005-09-24 06:26:55 | [diff] [blame] | 39 | buflen = snprintf(buf, sizeof(buf), "[%ld] ", (long) getpid()); |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 40 | |
| 41 | maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */ |
| 42 | msglen = vsnprintf(buf + buflen, maxlen, err, params); |
| 43 | |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 44 | if (log_syslog) { |
| 45 | syslog(priority, "%s", buf); |
| 46 | return; |
| 47 | } |
| 48 | |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 49 | /* maxlen counted our own LF but also counts space given to |
| 50 | * vsnprintf for the terminating NUL. We want to make sure that |
| 51 | * we have space for our own LF and NUL after the "meat" of the |
| 52 | * message, so truncate it at maxlen - 1. |
| 53 | */ |
| 54 | if (msglen > maxlen - 1) |
| 55 | msglen = maxlen - 1; |
| 56 | else if (msglen < 0) |
| 57 | msglen = 0; /* Protect against weird return values. */ |
| 58 | buflen += msglen; |
| 59 | |
| 60 | buf[buflen++] = '\n'; |
| 61 | buf[buflen] = '\0'; |
| 62 | |
| 63 | write(2, buf, buflen); |
| 64 | } |
| 65 | |
Pavel Roskin | cdda474 | 2005-09-29 20:53:14 | [diff] [blame] | 66 | static void logerror(const char *err, ...) |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 67 | { |
| 68 | va_list params; |
| 69 | va_start(params, err); |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 70 | logreport(LOG_ERR, err, params); |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 71 | va_end(params); |
| 72 | } |
| 73 | |
Pavel Roskin | cdda474 | 2005-09-29 20:53:14 | [diff] [blame] | 74 | static void loginfo(const char *err, ...) |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 75 | { |
| 76 | va_list params; |
| 77 | if (!verbose) |
| 78 | return; |
| 79 | va_start(params, err); |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 80 | logreport(LOG_INFO, err, params); |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 81 | va_end(params); |
| 82 | } |
| 83 | |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 84 | static int path_ok(const char *dir) |
| 85 | { |
| 86 | const char *p = dir; |
| 87 | char **pp; |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 88 | int sl, ndot; |
| 89 | |
| 90 | /* The pathname here should be an absolute path. */ |
| 91 | if ( *p++ != '/' ) |
| 92 | return 0; |
| 93 | |
| 94 | sl = 1; ndot = 0; |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 95 | |
| 96 | for (;;) { |
| 97 | if ( *p == '.' ) { |
| 98 | ndot++; |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 99 | } else if ( *p == '\0' ) { |
| 100 | /* Reject "." and ".." at the end of the path */ |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 101 | if ( sl && ndot > 0 && ndot < 3 ) |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 102 | return 0; |
| 103 | |
| 104 | /* Otherwise OK */ |
| 105 | break; |
| 106 | } else if ( *p == '/' ) { |
| 107 | /* Refuse "", "." or ".." */ |
| 108 | if ( sl && ndot < 3 ) |
| 109 | return 0; |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 110 | sl = 1; |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 111 | ndot = 0; |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 112 | } else { |
| 113 | sl = ndot = 0; |
| 114 | } |
| 115 | p++; |
| 116 | } |
| 117 | |
| 118 | if ( ok_paths && *ok_paths ) { |
| 119 | int ok = 0; |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 120 | int dirlen = strlen(dir); |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 121 | |
| 122 | for ( pp = ok_paths ; *pp ; pp++ ) { |
| 123 | int len = strlen(*pp); |
| 124 | if ( len <= dirlen && |
| 125 | !strncmp(*pp, dir, len) && |
| 126 | (dir[len] == '/' || dir[len] == '\0') ) { |
| 127 | ok = 1; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if ( !ok ) |
| 133 | return 0; /* Path not in whitelist */ |
| 134 | } |
| 135 | |
| 136 | return 1; /* Path acceptable */ |
| 137 | } |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 138 | |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 139 | static int set_dir(const char *dir) |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 140 | { |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 141 | if (!path_ok(dir)) { |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 142 | errno = EACCES; |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 143 | return -1; |
| 144 | } |
| 145 | |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 146 | if ( chdir(dir) ) |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 147 | return -1; |
H. Peter Anvin | 47888f0 | 2005-09-27 15:49:40 | [diff] [blame] | 148 | |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 149 | /* |
| 150 | * Security on the cheap. |
| 151 | * |
Junio C Hamano | a935c39 | 2005-10-21 06:19:36 | [diff] [blame] | 152 | * We want a readable HEAD, usable "objects" directory, and |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 153 | * a "git-daemon-export-ok" flag that says that the other side |
| 154 | * is ok with us doing this. |
| 155 | */ |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 156 | if (!export_all_trees && access("git-daemon-export-ok", F_OK)) { |
| 157 | errno = EACCES; |
| 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | if (access("objects/", X_OK) || access("HEAD", R_OK)) { |
| 162 | errno = EINVAL; |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | /* If all this passed, we're OK */ |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int upload(char *dir) |
| 171 | { |
| 172 | /* Try paths in this order */ |
| 173 | static const char *paths[] = { "%s", "%s/.git", "%s.git", "%s.git/.git", NULL }; |
| 174 | const char **pp; |
| 175 | /* Enough for the longest path above including final null */ |
| 176 | int buflen = strlen(dir)+10; |
| 177 | char *dirbuf = xmalloc(buflen); |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 178 | /* Timeout as string */ |
| 179 | char timeout_buf[64]; |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 180 | |
| 181 | loginfo("Request for '%s'", dir); |
| 182 | |
| 183 | for ( pp = paths ; *pp ; pp++ ) { |
| 184 | snprintf(dirbuf, buflen, *pp, dir); |
| 185 | if ( !set_dir(dirbuf) ) |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | if ( !*pp ) { |
| 190 | logerror("Cannot set directory '%s': %s", dir, strerror(errno)); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 191 | return -1; |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 192 | } |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 193 | |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 194 | /* |
| 195 | * We'll ignore SIGTERM from now on, we have a |
| 196 | * good client. |
| 197 | */ |
| 198 | signal(SIGTERM, SIG_IGN); |
| 199 | |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 200 | snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout); |
| 201 | |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 202 | /* git-upload-pack only ever reads stuff, so this is safe */ |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 203 | execlp("git-upload-pack", "git-upload-pack", "--strict", timeout_buf, ".", NULL); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 204 | return -1; |
| 205 | } |
| 206 | |
Linus Torvalds | 7d80694 | 2005-07-15 16:27:05 | [diff] [blame] | 207 | static int execute(void) |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 208 | { |
Linus Torvalds | 7d80694 | 2005-07-15 16:27:05 | [diff] [blame] | 209 | static char line[1000]; |
| 210 | int len; |
| 211 | |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 212 | alarm(init_timeout ? init_timeout : timeout); |
Linus Torvalds | 7d80694 | 2005-07-15 16:27:05 | [diff] [blame] | 213 | len = packet_read_line(0, line, sizeof(line)); |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 214 | alarm(0); |
Linus Torvalds | 7d80694 | 2005-07-15 16:27:05 | [diff] [blame] | 215 | |
| 216 | if (len && line[len-1] == '\n') |
| 217 | line[--len] = 0; |
| 218 | |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 219 | if (!strncmp("git-upload-pack /", line, 17)) |
H. Peter Anvin | 3e04c62 | 2005-10-19 01:26:52 | [diff] [blame] | 220 | return upload(line+16); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 221 | |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 222 | logerror("Protocol error: '%s'", line); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 223 | return -1; |
| 224 | } |
| 225 | |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * We count spawned/reaped separately, just to avoid any |
| 229 | * races when updating them from signals. The SIGCHLD handler |
| 230 | * will only update children_reaped, and the fork logic will |
| 231 | * only update children_spawned. |
| 232 | * |
| 233 | * MAX_CHILDREN should be a power-of-two to make the modulus |
| 234 | * operation cheap. It should also be at least twice |
| 235 | * the maximum number of connections we will ever allow. |
| 236 | */ |
| 237 | #define MAX_CHILDREN 128 |
| 238 | |
| 239 | static int max_connections = 25; |
| 240 | |
| 241 | /* These are updated by the signal handler */ |
| 242 | static volatile unsigned int children_reaped = 0; |
Linus Torvalds | 4d8fa91 | 2005-08-01 19:11:53 | [diff] [blame] | 243 | static pid_t dead_child[MAX_CHILDREN]; |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 244 | |
| 245 | /* These are updated by the main loop */ |
| 246 | static unsigned int children_spawned = 0; |
| 247 | static unsigned int children_deleted = 0; |
| 248 | |
Linus Torvalds | 4d8fa91 | 2005-08-01 19:11:53 | [diff] [blame] | 249 | static struct child { |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 250 | pid_t pid; |
Junio C Hamano | 7fa0908 | 2005-09-11 20:58:41 | [diff] [blame] | 251 | int addrlen; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 252 | struct sockaddr_storage address; |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 253 | } live_child[MAX_CHILDREN]; |
| 254 | |
Junio C Hamano | 7fa0908 | 2005-09-11 20:58:41 | [diff] [blame] | 255 | static void add_child(int idx, pid_t pid, struct sockaddr *addr, int addrlen) |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 256 | { |
| 257 | live_child[idx].pid = pid; |
| 258 | live_child[idx].addrlen = addrlen; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 259 | memcpy(&live_child[idx].address, addr, addrlen); |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Walk from "deleted" to "spawned", and remove child "pid". |
| 264 | * |
| 265 | * We move everything up by one, since the new "deleted" will |
| 266 | * be one higher. |
| 267 | */ |
| 268 | static void remove_child(pid_t pid, unsigned deleted, unsigned spawned) |
| 269 | { |
| 270 | struct child n; |
| 271 | |
| 272 | deleted %= MAX_CHILDREN; |
| 273 | spawned %= MAX_CHILDREN; |
| 274 | if (live_child[deleted].pid == pid) { |
| 275 | live_child[deleted].pid = -1; |
| 276 | return; |
| 277 | } |
| 278 | n = live_child[deleted]; |
| 279 | for (;;) { |
| 280 | struct child m; |
| 281 | deleted = (deleted + 1) % MAX_CHILDREN; |
| 282 | if (deleted == spawned) |
| 283 | die("could not find dead child %d\n", pid); |
| 284 | m = live_child[deleted]; |
| 285 | live_child[deleted] = n; |
| 286 | if (m.pid == pid) |
| 287 | return; |
| 288 | n = m; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * This gets called if the number of connections grows |
| 294 | * past "max_connections". |
| 295 | * |
| 296 | * We _should_ start off by searching for connections |
| 297 | * from the same IP, and if there is some address wth |
| 298 | * multiple connections, we should kill that first. |
| 299 | * |
| 300 | * As it is, we just "randomly" kill 25% of the connections, |
| 301 | * and our pseudo-random generator sucks too. I have no |
| 302 | * shame. |
| 303 | * |
| 304 | * Really, this is just a place-holder for a _real_ algorithm. |
| 305 | */ |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 306 | static void kill_some_children(int signo, unsigned start, unsigned stop) |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 307 | { |
| 308 | start %= MAX_CHILDREN; |
| 309 | stop %= MAX_CHILDREN; |
| 310 | while (start != stop) { |
| 311 | if (!(start & 3)) |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 312 | kill(live_child[start].pid, signo); |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 313 | start = (start + 1) % MAX_CHILDREN; |
| 314 | } |
| 315 | } |
| 316 | |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 317 | static void check_max_connections(void) |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 318 | { |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 319 | for (;;) { |
Linus Torvalds | eaa9491 | 2005-07-16 03:42:28 | [diff] [blame] | 320 | int active; |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 321 | unsigned spawned, reaped, deleted; |
Linus Torvalds | eaa9491 | 2005-07-16 03:42:28 | [diff] [blame] | 322 | |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 323 | spawned = children_spawned; |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 324 | reaped = children_reaped; |
| 325 | deleted = children_deleted; |
| 326 | |
| 327 | while (deleted < reaped) { |
| 328 | pid_t pid = dead_child[deleted % MAX_CHILDREN]; |
| 329 | remove_child(pid, deleted, spawned); |
| 330 | deleted++; |
Linus Torvalds | eaa9491 | 2005-07-16 03:42:28 | [diff] [blame] | 331 | } |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 332 | children_deleted = deleted; |
| 333 | |
| 334 | active = spawned - deleted; |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 335 | if (active <= max_connections) |
| 336 | break; |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 337 | |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 338 | /* Kill some unstarted connections with SIGTERM */ |
| 339 | kill_some_children(SIGTERM, deleted, spawned); |
| 340 | if (active <= max_connections << 1) |
| 341 | break; |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 342 | |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 343 | /* If the SIGTERM thing isn't helping use SIGKILL */ |
| 344 | kill_some_children(SIGKILL, deleted, spawned); |
| 345 | sleep(1); |
| 346 | } |
| 347 | } |
| 348 | |
Junio C Hamano | 7fa0908 | 2005-09-11 20:58:41 | [diff] [blame] | 349 | static void handle(int incoming, struct sockaddr *addr, int addrlen) |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 350 | { |
| 351 | pid_t pid = fork(); |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 352 | char addrbuf[256] = ""; |
| 353 | int port = -1; |
Linus Torvalds | 02d57da | 2005-07-16 05:53:31 | [diff] [blame] | 354 | |
| 355 | if (pid) { |
| 356 | unsigned idx; |
| 357 | |
| 358 | close(incoming); |
| 359 | if (pid < 0) |
| 360 | return; |
| 361 | |
| 362 | idx = children_spawned % MAX_CHILDREN; |
| 363 | children_spawned++; |
| 364 | add_child(idx, pid, addr, addrlen); |
| 365 | |
| 366 | check_max_connections(); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
| 370 | dup2(incoming, 0); |
| 371 | dup2(incoming, 1); |
| 372 | close(incoming); |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 373 | |
| 374 | if (addr->sa_family == AF_INET) { |
| 375 | struct sockaddr_in *sin_addr = (void *) addr; |
| 376 | inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf)); |
| 377 | port = sin_addr->sin_port; |
| 378 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 379 | #ifndef NO_IPV6 |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 380 | } else if (addr->sa_family == AF_INET6) { |
| 381 | struct sockaddr_in6 *sin6_addr = (void *) addr; |
| 382 | |
| 383 | char *buf = addrbuf; |
| 384 | *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */ |
| 385 | inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1); |
| 386 | strcat(buf, "]"); |
| 387 | |
| 388 | port = sin6_addr->sin6_port; |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 389 | #endif |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 390 | } |
Petr Baudis | da38641 | 2005-09-24 14:13:03 | [diff] [blame] | 391 | loginfo("Connection from %s:%d", addrbuf, port); |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 392 | |
Linus Torvalds | 7d80694 | 2005-07-15 16:27:05 | [diff] [blame] | 393 | exit(execute()); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 394 | } |
| 395 | |
Linus Torvalds | eaa9491 | 2005-07-16 03:42:28 | [diff] [blame] | 396 | static void child_handler(int signo) |
| 397 | { |
| 398 | for (;;) { |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 399 | int status; |
| 400 | pid_t pid = waitpid(-1, &status, WNOHANG); |
Linus Torvalds | 66e631d | 2005-07-16 04:51:57 | [diff] [blame] | 401 | |
| 402 | if (pid > 0) { |
| 403 | unsigned reaped = children_reaped; |
| 404 | dead_child[reaped % MAX_CHILDREN] = pid; |
| 405 | children_reaped = reaped + 1; |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 406 | /* XXX: Custom logging, since we don't wanna getpid() */ |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 407 | if (verbose) { |
| 408 | char *dead = ""; |
| 409 | if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) |
| 410 | dead = " (with error)"; |
| 411 | if (log_syslog) |
| 412 | syslog(LOG_INFO, "[%d] Disconnected%s", pid, dead); |
| 413 | else |
| 414 | fprintf(stderr, "[%d] Disconnected%s\n", pid, dead); |
| 415 | } |
Linus Torvalds | eaa9491 | 2005-07-16 03:42:28 | [diff] [blame] | 416 | continue; |
| 417 | } |
| 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 422 | #ifndef NO_IPV6 |
| 423 | |
| 424 | static int socksetup(int port, int **socklist_p) |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 425 | { |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 426 | int socknum = 0, *socklist = NULL; |
| 427 | int maxfd = -1; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 428 | char pbuf[NI_MAXSERV]; |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 429 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 430 | struct addrinfo hints, *ai0, *ai; |
| 431 | int gai; |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 432 | |
| 433 | sprintf(pbuf, "%d", port); |
| 434 | memset(&hints, 0, sizeof(hints)); |
| 435 | hints.ai_family = AF_UNSPEC; |
| 436 | hints.ai_socktype = SOCK_STREAM; |
| 437 | hints.ai_protocol = IPPROTO_TCP; |
| 438 | hints.ai_flags = AI_PASSIVE; |
| 439 | |
| 440 | gai = getaddrinfo(NULL, pbuf, &hints, &ai0); |
| 441 | if (gai) |
| 442 | die("getaddrinfo() failed: %s\n", gai_strerror(gai)); |
| 443 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 444 | for (ai = ai0; ai; ai = ai->ai_next) { |
| 445 | int sockfd; |
| 446 | int *newlist; |
| 447 | |
| 448 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 449 | if (sockfd < 0) |
| 450 | continue; |
| 451 | if (sockfd >= FD_SETSIZE) { |
| 452 | error("too large socket descriptor."); |
| 453 | close(sockfd); |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | #ifdef IPV6_V6ONLY |
| 458 | if (ai->ai_family == AF_INET6) { |
| 459 | int on = 1; |
| 460 | setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, |
| 461 | &on, sizeof(on)); |
| 462 | /* Note: error is not fatal */ |
| 463 | } |
| 464 | #endif |
| 465 | |
| 466 | if (bind(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 467 | close(sockfd); |
| 468 | continue; /* not fatal */ |
| 469 | } |
| 470 | if (listen(sockfd, 5) < 0) { |
| 471 | close(sockfd); |
| 472 | continue; /* not fatal */ |
| 473 | } |
| 474 | |
| 475 | newlist = realloc(socklist, sizeof(int) * (socknum + 1)); |
| 476 | if (!newlist) |
| 477 | die("memory allocation failed: %s", strerror(errno)); |
| 478 | |
| 479 | socklist = newlist; |
| 480 | socklist[socknum++] = sockfd; |
| 481 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 482 | if (maxfd < sockfd) |
| 483 | maxfd = sockfd; |
| 484 | } |
| 485 | |
| 486 | freeaddrinfo(ai0); |
| 487 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 488 | *socklist_p = socklist; |
| 489 | return socknum; |
| 490 | } |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 491 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 492 | #else /* NO_IPV6 */ |
| 493 | |
| 494 | static int socksetup(int port, int **socklist_p) |
| 495 | { |
| 496 | struct sockaddr_in sin; |
| 497 | int sockfd; |
| 498 | |
| 499 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 500 | if (sockfd < 0) |
| 501 | return 0; |
| 502 | |
| 503 | memset(&sin, 0, sizeof sin); |
| 504 | sin.sin_family = AF_INET; |
| 505 | sin.sin_addr.s_addr = htonl(INADDR_ANY); |
| 506 | sin.sin_port = htons(port); |
| 507 | |
| 508 | if ( bind(sockfd, (struct sockaddr *)&sin, sizeof sin) < 0 ) { |
| 509 | close(sockfd); |
| 510 | return 0; |
| 511 | } |
| 512 | |
H. Peter Anvin | 1b4713f | 2005-09-30 17:47:50 | [diff] [blame] | 513 | *socklist_p = xmalloc(sizeof(int)); |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 514 | **socklist_p = sockfd; |
| 515 | } |
| 516 | |
| 517 | #endif |
| 518 | |
| 519 | static int service_loop(int socknum, int *socklist) |
| 520 | { |
| 521 | struct pollfd *pfd; |
| 522 | int i; |
| 523 | |
H. Peter Anvin | 1b4713f | 2005-09-30 17:47:50 | [diff] [blame] | 524 | pfd = xcalloc(socknum, sizeof(struct pollfd)); |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 525 | |
| 526 | for (i = 0; i < socknum; i++) { |
| 527 | pfd[i].fd = socklist[i]; |
| 528 | pfd[i].events = POLLIN; |
| 529 | } |
H. Peter Anvin | 9220282 | 2005-09-30 18:01:57 | [diff] [blame] | 530 | |
| 531 | signal(SIGCHLD, child_handler); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 532 | |
| 533 | for (;;) { |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 534 | int i; |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 535 | |
Jens Axboe | 7872e05 | 2005-10-20 07:52:32 | [diff] [blame] | 536 | if (poll(pfd, socknum, -1) < 0) { |
Junio C Hamano | 1eef0b3 | 2005-07-26 20:26:52 | [diff] [blame] | 537 | if (errno != EINTR) { |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 538 | error("poll failed, resuming: %s", |
Junio C Hamano | 1eef0b3 | 2005-07-26 20:26:52 | [diff] [blame] | 539 | strerror(errno)); |
| 540 | sleep(1); |
| 541 | } |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 542 | continue; |
| 543 | } |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 544 | |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 545 | for (i = 0; i < socknum; i++) { |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 546 | if (pfd[i].revents & POLLIN) { |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 547 | struct sockaddr_storage ss; |
H. Peter Anvin | 7626e49 | 2005-09-30 17:48:21 | [diff] [blame] | 548 | unsigned int sslen = sizeof(ss); |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 549 | int incoming = accept(pfd[i].fd, (struct sockaddr *)&ss, &sslen); |
YOSHIFUJI Hideaki | df076bd | 2005-07-23 08:24:59 | [diff] [blame] | 550 | if (incoming < 0) { |
| 551 | switch (errno) { |
| 552 | case EAGAIN: |
| 553 | case EINTR: |
| 554 | case ECONNABORTED: |
| 555 | continue; |
| 556 | default: |
| 557 | die("accept returned %s", strerror(errno)); |
| 558 | } |
| 559 | } |
| 560 | handle(incoming, (struct sockaddr *)&ss, sslen); |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 561 | } |
| 562 | } |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 566 | static int serve(int port) |
| 567 | { |
| 568 | int socknum, *socklist; |
Junio C Hamano | 4ae22d9 | 2005-10-21 06:21:50 | [diff] [blame] | 569 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 570 | socknum = socksetup(port, &socklist); |
| 571 | if (socknum == 0) |
| 572 | die("unable to allocate any listen sockets on port %u", port); |
Junio C Hamano | 4ae22d9 | 2005-10-21 06:21:50 | [diff] [blame] | 573 | |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 574 | return service_loop(socknum, socklist); |
Junio C Hamano | 4ae22d9 | 2005-10-21 06:21:50 | [diff] [blame] | 575 | } |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 576 | |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 577 | int main(int argc, char **argv) |
| 578 | { |
| 579 | int port = DEFAULT_GIT_PORT; |
Linus Torvalds | e64e1b7 | 2005-07-15 16:32:16 | [diff] [blame] | 580 | int inetd_mode = 0; |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 581 | int i; |
| 582 | |
| 583 | for (i = 1; i < argc; i++) { |
| 584 | char *arg = argv[i]; |
| 585 | |
| 586 | if (!strncmp(arg, "--port=", 7)) { |
| 587 | char *end; |
| 588 | unsigned long n; |
| 589 | n = strtoul(arg+7, &end, 0); |
| 590 | if (arg[7] && !*end) { |
| 591 | port = n; |
| 592 | continue; |
| 593 | } |
| 594 | } |
Linus Torvalds | e64e1b7 | 2005-07-15 16:32:16 | [diff] [blame] | 595 | if (!strcmp(arg, "--inetd")) { |
| 596 | inetd_mode = 1; |
| 597 | continue; |
| 598 | } |
Petr Baudis | f8ff0c0 | 2005-09-22 09:25:28 | [diff] [blame] | 599 | if (!strcmp(arg, "--verbose")) { |
| 600 | verbose = 1; |
| 601 | continue; |
| 602 | } |
Petr Baudis | 9048fe1 | 2005-09-24 14:13:01 | [diff] [blame] | 603 | if (!strcmp(arg, "--syslog")) { |
| 604 | log_syslog = 1; |
| 605 | openlog("git-daemon", 0, LOG_DAEMON); |
| 606 | continue; |
| 607 | } |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 608 | if (!strcmp(arg, "--export-all")) { |
| 609 | export_all_trees = 1; |
| 610 | continue; |
| 611 | } |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 612 | if (!strncmp(arg, "--timeout=", 10)) { |
| 613 | timeout = atoi(arg+10); |
| 614 | } |
H. Peter Anvin | 54e31a2 | 2005-10-21 01:34:58 | [diff] [blame] | 615 | if (!strncmp(arg, "--init-timeout=", 15)) { |
H. Peter Anvin | 960decc | 2005-10-19 21:27:01 | [diff] [blame] | 616 | init_timeout = atoi(arg+15); |
| 617 | } |
H. Peter Anvin | 4ae9568 | 2005-09-27 02:10:55 | [diff] [blame] | 618 | if (!strcmp(arg, "--")) { |
| 619 | ok_paths = &argv[i+1]; |
| 620 | break; |
| 621 | } else if (arg[0] != '-') { |
| 622 | ok_paths = &argv[i]; |
| 623 | break; |
| 624 | } |
Linus Torvalds | e64e1b7 | 2005-07-15 16:32:16 | [diff] [blame] | 625 | |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 626 | usage(daemon_usage); |
| 627 | } |
| 628 | |
lars.doelle@on-line.de | 7c3693f | 2005-09-08 01:50:01 | [diff] [blame] | 629 | if (inetd_mode) { |
| 630 | fclose(stderr); //FIXME: workaround |
Linus Torvalds | e64e1b7 | 2005-07-15 16:32:16 | [diff] [blame] | 631 | return execute(); |
Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 632 | } else { |
| 633 | return serve(port); |
lars.doelle@on-line.de | 7c3693f | 2005-09-08 01:50:01 | [diff] [blame] | 634 | } |
Linus Torvalds | a87e8be | 2005-07-14 02:45:26 | [diff] [blame] | 635 | } |