~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/daemon.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2008-03-10 06:05:21 UTC
  • mfrom: (151.1.7 clearer-argument-passing)
  • Revision ID: launchpad@pqm.canonical.com-20080310060521-29xe8hy1hmc3d1xl
[r=jamesh] show revisions which merge a change to a file when filtering on changes that touch files and don't reuse the 'file_id' argument name when doing so

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# daemon code from ASPN
 
2
 
 
3
import os
 
4
 
 
5
 
 
6
def daemonize(pidfile, home):
 
7
    """
 
8
    Detach this process from the controlling terminal and run it in the
 
9
    background as a daemon.
 
10
    """
 
11
 
 
12
    UMASK = 0
 
13
    WORKDIR = "/"
 
14
    REDIRECT_TO = getattr(os, 'devnull', '/dev/null')
 
15
    MAXFD = 1024
 
16
 
 
17
    try:
 
18
        pid = os.fork()
 
19
    except OSError, e:
 
20
        raise Exception("%s [%d]" % (e.strerror, e.errno))
 
21
 
 
22
    if (pid == 0):      # The first child.
 
23
        os.setsid()
 
24
 
 
25
        try:
 
26
            pid = os.fork()     # Fork a second child.
 
27
        except OSError, e:
 
28
            raise Exception, "%s [%d]" % (e.strerror, e.errno)
 
29
 
 
30
        if (pid == 0):  # The second child.
 
31
            os.chdir(WORKDIR)
 
32
            os.umask(UMASK)
 
33
        else:
 
34
            os._exit(0) # Exit parent (the first child) of the second child.
 
35
    else:
 
36
        os._exit(0)     # Exit parent of the first child.
 
37
 
 
38
    #import resource            # Resource usage information.
 
39
    #maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
 
40
    #if (maxfd == resource.RLIM_INFINITY):
 
41
    #    maxfd = MAXFD
 
42
 
 
43
    # Iterate through and close all file descriptors.
 
44
    #for fd in range(3, maxfd):
 
45
    #    try:
 
46
    #        os.close(fd)
 
47
    #    except OSError:        # ERROR, fd wasn't open to begin with (ignored)
 
48
    #        pass
 
49
 
 
50
    # This call to open is guaranteed to return the lowest file descriptor,
 
51
    # which will be 0 (stdin), since it was closed above.
 
52
    #os.open(REDIRECT_TO, os.O_RDWR)    # standard input (0)
 
53
 
 
54
    # Duplicate standard input to standard output and standard error.
 
55
    #os.dup2(0, 1)                      # standard output (1)
 
56
    #os.dup2(0, 2)                      # standard error (2)
 
57
 
 
58
    f = open(pidfile, 'w')
 
59
    f.write('%d\n' % (os.getpid(),))
 
60
    f.write('%s\n' % (home,))
 
61
    f.close()
 
62
 
 
63
 
 
64
def is_running(pidfile):
 
65
    try:
 
66
        f = open(pidfile, 'r')
 
67
    except IOError:
 
68
        return False
 
69
    pid = int(f.readline())
 
70
    f.close()
 
71
    try:
 
72
        os.kill(pid, 0)
 
73
    except OSError:
 
74
        # no such process
 
75
        return False
 
76
    return True