File: logpipe.c

package info (click to toggle)
uwsgi 2.0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,564 kB
  • sloc: ansic: 87,066; python: 7,004; cpp: 1,133; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (63 lines) | stat: -rw-r--r-- 1,186 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <uwsgi.h>

/*

	The pipe logger

	Author: INADA Naoki

	every log line is sent to the stdin of an external process	

	Example:

		req-logger = pipe:/usr/local/bin/mylogger

*/

static ssize_t uwsgi_pipe_logger(struct uwsgi_logger *ul, char *message, size_t len) {
	if (!ul->configured) {
		if (ul->arg) {
			int pipefd[2];
			// retry later...
			if (pipe(pipefd) < 0) return -1;
			pid_t pid = fork();
			if (pid < 0) return -1;
			if (pid > 0) {
				close(pipefd[0]);
				ul->fd = pipefd[1];
			}
			else {
				// child
				if (setsid() < 0) {
					uwsgi_error("setsid()");
					exit(1);
				}
				close(pipefd[1]);
				dup2(pipefd[0], STDIN_FILENO);
				close(pipefd[0]);
				uwsgi_exec_command_with_args(ul->arg);
				exit(1);	// if here something seriously failed
			}
		}

		ul->configured = 1;
	}

	int err = write(ul->fd, message, len);
	// on failed writes, re-configure the logger
	if (err <= 0) {
		close(ul->fd);
		ul->configured = 0;
		return err;
	}
	return 0;
}

static void uwsgi_pipe_logger_register() {
	uwsgi_register_logger("pipe", uwsgi_pipe_logger);
}

struct uwsgi_plugin logpipe_plugin = {
	.name = "logpipe",
	.on_load = uwsgi_pipe_logger_register,
};