[PATCH] Verbose git-daemon logging
This patch makes git-daemon --verbose log some useful things on stderr - in particular connects, disconnects and upload requests, and in such a way to be able to trace a particular session. Some more errors are now also logged (even when --verbose is not passed). It is still not perfect since messages produced by the non-daemon-specific code are obviously not formatted properly. [jc: With minor fix up in the log line truncation, and use of write(2) as suggested by Linus.] Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>
Petr Baudis committed
Sep 22, 2005 at 11:25 UTC
f8ff0c0641a14770a2214fffbd4271b1ea3a0d61
1 file changed
+90
-4
daemon.c
+90
-4
@@ -6,13 +6,70 @@
6
#include <sys/time.h>
7
#include <netdb.h>
8
#include <netinet/in.h>
9
+#include <arpa/inet.h>
10
+
11
+static int verbose;
12
+
13
+static const char daemon_usage[] = "git-daemon [--verbose] [--inetd | --port=n]";
14
+
15
+
16
+static void logreport(const char *err, va_list params)
17
+{
18
+ /* We should do a single write so that it is atomic and output
19
+ * of several processes do not get intermingled. */
20
+ char buf[1024];
21
+ int buflen;
22
+ int maxlen, msglen;
23
+
24
+ /* sizeof(buf) should be big enough for "[pid] \n" */
25
+ buflen = snprintf(buf, sizeof(buf), "[%d] ", getpid());
26
+
27
+ maxlen = sizeof(buf) - buflen - 1; /* -1 for our own LF */
28
+ msglen = vsnprintf(buf + buflen, maxlen, err, params);
29
+
30
+ /* maxlen counted our own LF but also counts space given to
31
+ * vsnprintf for the terminating NUL. We want to make sure that
32
+ * we have space for our own LF and NUL after the "meat" of the
33
+ * message, so truncate it at maxlen - 1.
34
+ */
35
+ if (msglen > maxlen - 1)
36
+ msglen = maxlen - 1;
37
+ else if (msglen < 0)
38
+ msglen = 0; /* Protect against weird return values. */
39
+ buflen += msglen;
40
+
41
+ buf[buflen++] = '\n';
42
+ buf[buflen] = '\0';
43
+
44
+ write(2, buf, buflen);
45
+}
46
+
47
+void logerror(const char *err, ...)
48
+{
49
+ va_list params;
50
+ va_start(params, err);
51
+ logreport(err, params);
52
+ va_end(params);
53
+}
54
+
55
+void lognotice(const char *err, ...)
56
+{
57
+ va_list params;
58
+ if (!verbose)
59
+ return;
60
+ va_start(params, err);
61
+ logreport(err, params);
62
+ va_end(params);
63
+}
64
10
-static const char daemon_usage[] = "git-daemon [--inetd | --port=n]";
65
66
static int upload(char *dir, int dirlen)
67
{
14
- if (chdir(dir) < 0)
68
+ lognotice("Request for '%s'", dir);
69
+ if (chdir(dir) < 0) {
70
+ logerror("Cannot chdir('%s'): %s", dir, strerror(errno));
71
return -1;
72
+ }
73
chdir(".git");
74
75
/*
@@ -24,8 +81,10 @@ static int upload(char *dir, int dirlen)
81
*/
82
if (access("git-daemon-export-ok", F_OK) ||
83
access("objects/00", X_OK) ||
27
- access("HEAD", R_OK))
84
+ access("HEAD", R_OK)) {
85
+ logerror("Not a valid gitd-enabled repository: '%s'", dir);
86
return -1;
87
+ }
88
89
/*
90
* We'll ignore SIGTERM from now on, we have a
@@ -51,7 +110,7 @@ static int execute(void)
110
if (!strncmp("git-upload-pack /", line, 17))
111
return upload(line + 16, len - 16);
112
54
- fprintf(stderr, "got bad connection '%s'\n", line);
113
+ logerror("Protocol error: '%s'", line);
114
return -1;
115
}
116
@@ -181,6 +240,8 @@ static void check_max_connections(void)
240
static void handle(int incoming, struct sockaddr *addr, int addrlen)
241
{
242
pid_t pid = fork();
243
+ char addrbuf[256] = "";
244
+ int port = -1;
245
246
if (pid) {
247
unsigned idx;
@@ -200,6 +261,24 @@ static void handle(int incoming, struct sockaddr *addr, int addrlen)
261
dup2(incoming, 0);
262
dup2(incoming, 1);
263
close(incoming);
264
+
265
+ if (addr->sa_family == AF_INET) {
266
+ struct sockaddr_in *sin_addr = (void *) addr;
267
+ inet_ntop(AF_INET, &sin_addr->sin_addr, addrbuf, sizeof(addrbuf));
268
+ port = sin_addr->sin_port;
269
+
270
+ } else if (addr->sa_family == AF_INET6) {
271
+ struct sockaddr_in6 *sin6_addr = (void *) addr;
272
+
273
+ char *buf = addrbuf;
274
+ *buf++ = '['; *buf = '\0'; /* stpcpy() is cool */
275
+ inet_ntop(AF_INET6, &sin6_addr->sin6_addr, buf, sizeof(addrbuf) - 1);
276
+ strcat(buf, "]");
277
+
278
+ port = sin6_addr->sin6_port;
279
+ }
280
+ lognotice("Connection from %s:%d", addrbuf, port);
281
+
282
exit(execute());
283
}
284
@@ -212,6 +291,9 @@ static void child_handler(int signo)
291
unsigned reaped = children_reaped;
292
dead_child[reaped % MAX_CHILDREN] = pid;
293
children_reaped = reaped + 1;
294
+ /* XXX: Custom logging, since we don't wanna getpid() */
295
+ if (verbose)
296
+ fprintf(stderr, "[%d] Disconnected\n", pid);
297
continue;
298
}
299
break;
@@ -349,6 +431,10 @@ int main(int argc, char **argv)
431
inetd_mode = 1;
432
continue;
433
}
434
+ if (!strcmp(arg, "--verbose")) {
435
+ verbose = 1;
436
+ continue;
437
+ }
438
439
usage(daemon_usage);
440
}