This file is indexed.

/usr/lib/python3/dist-packages/spoon/server.py is in python3-spoon 1.0.6-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
"""Exposes UDP/TCP servers that can handle requests and
can be stopped gracefully or reloaded.
"""

from __future__ import absolute_import

import os
import errno
import socket
import signal
import logging
import threading
try:
    import socketserver
except ImportError:
    import SocketServer as socketserver


def _eintr_retry(func, *args):
    """restart a system call interrupted by EINTR"""
    while True:
        try:
            return func(*args)
        except OSError as e:
            if e.args[0] != errno.EINTR:
                raise


class _Gulp(object):
    """Handle a single request."""

    def handle(self):
        """Get the command from the client and pass it to the
        correct handler.
        """
        raise NotImplementedError()


class _StreamRequestHandler(socketserver.StreamRequestHandler, object):
    """Converted to newstyle class."""


class _DatagramRequestHandler(socketserver.DatagramRequestHandler, object):
    """Converted to newstyle class."""


class TCPGulp(_Gulp, _StreamRequestHandler):
    """Handle a single TCP request."""


class UDPGulp(_Gulp, _DatagramRequestHandler):
    """Handle a single UDP request."""


class _TCPServer(socketserver.TCPServer, object):
    """Converted to newstyle class."""


class _UDPServer(socketserver.UDPServer, object):
    """Converted to newstyle class."""


class _SpoonMixIn(object):
    """A server that consumes Gulps in a single thread and
    single process.
    """
    server_logger = "spoon-server"
    handler_klass = TCPGulp
    # Custom signal handling
    signal_reload = signal.SIGUSR1
    signal_shutdown = signal.SIGTERM
    # Socket options.
    ipv6_only = False
    allow_reuse_address = True
    # Command line defaults
    command_line_defaults = {
        "port": 5000,
        "interface": "::0",
        "pid_file": None,
        "log_file": None,
        "sentry_dsn": None,
        "spork": None,
    }

    def __init__(self, address):
        self.log = logging.getLogger(self.server_logger)
        self.socket = None
        if ":" in address[0]:
            self.address_family = socket.AF_INET6
        else:
            self.address_family = socket.AF_INET
        self.log.debug("Listening on %s", address)

        super(_SpoonMixIn, self).__init__(address, self.handler_klass,
                                          bind_and_activate=False)
        self.load_config()
        self._setup_socket()

        # Finally, set signals
        if self.signal_reload is not None:
            signal.signal(self.signal_reload, self.reload_handler)
        if self.signal_shutdown is not None:
            signal.signal(self.signal_shutdown, self.shutdown_handler)

    def _setup_socket(self):
        self.socket = socket.socket(self.address_family, self.socket_type)
        if self.allow_reuse_address:
            self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        if not self.ipv6_only:
            try:
                self.socket.setsockopt(socket.IPPROTO_IPV6,
                                       socket.IPV6_V6ONLY, 0)
            except (AttributeError, socket.error) as e:
                self.log.debug("Unable to set IPV6_V6ONLY to false %s", e)
        self.server_bind()
        self.server_activate()

    def serve_forever(self, poll_interval=0.1):
        super(_SpoonMixIn, self).serve_forever(poll_interval=poll_interval)

    def load_config(self):
        """Reads the configuration files, this is called when
        the reload handler is received.

        Can be reimplemented.
        """

    def shutdown_handler(self, *args, **kwargs):
        """Handler for the SIGTERM signal. This should be used to kill the
        daemon and ensure proper clean-up.
        """
        self.log.info("SIGTERM received. Shutting down.")
        t = threading.Thread(target=self.shutdown)
        t.start()

    def reload_handler(self, *args, **kwargs):
        """Handler for the SIGUSR1 signal. This should be used to reload
        the configuration files.
        """
        self.log.info("SIGUSR1 received. Reloading configuration.")
        t = threading.Thread(target=self.load_config)
        t.start()

    def handle_error(self, request, client_address):
        self.log.error("Error while processing request from: %s",
                       client_address, exc_info=True)


class _SporkMixIn(_SpoonMixIn):
    """The same as Spoon, but allows consuming Gulps with more than
    one spoon by pre-forking when starting the server.

    The parent Spoon process will then wait for all his child
    process to complete.
    """
    prefork = 4

    def __init__(self, address):
        """The same as Server.__init__ but requires a list of databases
        instead of a single database connection.
        """
        self.pids = None
        _SpoonMixIn.__init__(self, address)

    def serve_forever(self, poll_interval=0.1):
        """Fork the current process and wait for all children to finish."""
        if self.prefork is None or self.prefork <= 1:
            return super(_SporkMixIn, self).serve_forever(
                poll_interval=poll_interval)
        pids = []
        for dummy in range(self.prefork):
            pid = os.fork()
            if not pid:
                super(_SporkMixIn, self).serve_forever(
                    poll_interval=poll_interval)
                os._exit(0)
            else:
                self.log.info("Forked worker %s", pid)
                pids.append(pid)
        self.pids = pids
        for pid in self.pids:
            _eintr_retry(os.waitpid, pid, 0)

    def shutdown(self):
        """If this is the parent process send the TERM signal to all children,
        else call the super method.
        """
        for pid in self.pids or ():
            os.kill(pid, self.signal_shutdown)
        if self.pids is None:
            super(_SporkMixIn, self).shutdown()

    def load_config(self):
        """If this is the parent process send the USR1 signal to all children,
        else call the super method.
        """
        for pid in self.pids or ():
            os.kill(pid, self.signal_reload)
        if self.pids is None:
            super(_SporkMixIn, self).load_config()


class TCPSpoon(_SpoonMixIn, _TCPServer):
    """A TCP Socket server that handles everything in a
    single process.
    """


class TCPSpork(_SporkMixIn, _TCPServer):
    """A TCP Socket server that pre-forks a number of child
    processes.
    """


class UDPSpoon(_SpoonMixIn, _UDPServer):
    """A UDP Socket server that handles everything in a
    single process.
    """


class UDPSpork(_SporkMixIn, _UDPServer):
    """A UDP Socket server that pre-forks a number of child
    processes.
    """