summaryrefslogtreecommitdiffstats
path: root/syslogd.py
blob: 6fc6aa9021c303a91a4ca4c8a422e167b4ae4650 (plain)
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
# Simple syslog

import sys, os
from socket import *
from select import select

class Syslogd:

    def goSyslog(self, output, sockName):
	sock = socket(AF_UNIX, SOCK_STREAM)

	try:
	    os.unlink(sockName)
	except os.error:
	    pass

	sock.bind(sockName)
	acceptedFds = []
	sock.listen(5)

	while (1):
	    list = acceptedFds + [ sock ]
	    list = select(list, [], [])[0]
	    try:
		list.remove(sock)
		(fd, remoteAddr) = sock.accept()
		acceptedFds.append(fd)
	    except ValueError:
		pass

	    for fd in list:
		msg = fd.recv(50)
		if (msg):
		    output.write(msg)
		else:
		    acceptedFds.remove(fd)
		    fd.close()

    def __del__(self):
	self.kill()

    def kill(self):
	os.kill(self.child, 15)
	os.waitpid(self.child, 0)

    def __init__(self, root = "", output = sys.stdout, socket = "/dev/log"):
	output = output
	filename = root + socket;
	self.child = os.fork()
	if (not self.child):
	    self.goSyslog(output, filename)