daemon.py 3.1 KB
Newer Older
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
#
#

# Copyright (C) 2006, 2007, 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Module with helper classes and functions for daemons"""


import select
import signal
import errno

from ganeti import utils


class Mainloop(object):
  """Generic mainloop for daemons

  """
  def __init__(self):
    self._io_wait = []
    self._signal_wait = []
39
    self.sigchld_handler = None
Guido Trotter's avatar
Guido Trotter committed
40
41
    self.sigterm_handler = None
    self.quit = False
42
43
44
45
46
47
48
49

  def Run(self):
    # TODO: Does not yet support adding new event sources while running
    poller = select.poll()
    for (owner, fd, conditions) in self._io_wait:
      poller.register(fd, conditions)

    self.sigchld_handler = utils.SignalHandler([signal.SIGCHLD])
Guido Trotter's avatar
Guido Trotter committed
50
    self.sigterm_handler = utils.SignalHandler([signal.SIGTERM])
51
    try:
Guido Trotter's avatar
Guido Trotter committed
52
      while not self.quit:
53
        try:
54
          io_events = poller.poll()
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        except select.error, err:
          # EINTR can happen when signals are sent
          if err.args and err.args[0] in (errno.EINTR,):
            io_events = None
          else:
            raise

        if io_events:
          # Check for I/O events
          for (evfd, evcond) in io_events:
            for (owner, fd, conditions) in self._io_wait:
              if fd == evfd and evcond & conditions:
                owner.OnIO(fd, evcond)

        # Check whether signal was raised
        if self.sigchld_handler.called:
          for owner in self._signal_wait:
            owner.OnSignal(signal.SIGCHLD)
          self.sigchld_handler.Clear()
Guido Trotter's avatar
Guido Trotter committed
74
75
76
77

        if self.sigterm_handler.called:
          self.quit = True
          self.sigterm_handler.Clear()
78
79
80
    finally:
      self.sigchld_handler.Reset()
      self.sigchld_handler = None
Guido Trotter's avatar
Guido Trotter committed
81
82
83
      self.sigterm_handler.Reset()
      self.sigterm_handler = None

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

  def RegisterIO(self, owner, fd, condition):
    """Registers a receiver for I/O notifications

    The receiver must support a "OnIO(self, fd, conditions)" function.

    @type owner: instance
    @param owner: Receiver
    @type fd: int
    @param fd: File descriptor
    @type condition: int
    @param condition: ORed field of conditions to be notified
                      (see select module)

    """
    self._io_wait.append((owner, fd, condition))

  def RegisterSignal(self, owner):
    """Registers a receiver for signal notifications

    The receiver must support a "OnSignal(self, signum)" function.

    @type owner: instance
    @param owner: Receiver

    """
    self._signal_wait.append(owner)