1 # Copyright 2010-2013 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
9 # http://bugs.jython.org/issue1074
12 from portage import os
13 from _emerge.AbstractPollTask import AbstractPollTask
14 from portage.cache.mappings import slot_dict_class
16 class FifoIpcDaemon(AbstractPollTask):
18 __slots__ = ("input_fifo", "output_fifo",) + \
19 ("_files", "_reg_id",)
21 _file_names = ("pipe_in",)
22 _files_dict = slot_dict_class(_file_names, prefix="")
25 self._files = self._files_dict()
27 # File streams are in unbuffered mode since we do atomic
28 # read and write of whole pickles.
29 self._files.pipe_in = \
30 os.open(self.input_fifo, os.O_RDONLY|os.O_NONBLOCK)
32 # FD_CLOEXEC is enabled by default in Python >=3.4.
33 if sys.hexversion < 0x3040000 and fcntl is not None:
36 except AttributeError:
39 fcntl.fcntl(self._files.pipe_in, fcntl.F_SETFD,
40 fcntl.fcntl(self._files.pipe_in,
41 fcntl.F_GETFD) | fcntl.FD_CLOEXEC)
43 self._reg_id = self.scheduler.io_add_watch(
45 self._registered_events, self._input_handler)
47 self._registered = True
49 def _reopen_input(self):
51 Re-open the input stream, in order to suppress
52 POLLHUP events (bug #339976).
54 self.scheduler.source_remove(self._reg_id)
55 os.close(self._files.pipe_in)
56 self._files.pipe_in = \
57 os.open(self.input_fifo, os.O_RDONLY|os.O_NONBLOCK)
59 # FD_CLOEXEC is enabled by default in Python >=3.4.
60 if sys.hexversion < 0x3040000 and fcntl is not None:
63 except AttributeError:
66 fcntl.fcntl(self._files.pipe_in, fcntl.F_SETFD,
67 fcntl.fcntl(self._files.pipe_in,
68 fcntl.F_GETFD) | fcntl.FD_CLOEXEC)
70 self._reg_id = self.scheduler.io_add_watch(
72 self._registered_events, self._input_handler)
75 return self._registered
78 if self.returncode is None:
81 # notify exit listeners
85 if self.returncode is not None:
86 return self.returncode
88 if self.returncode is None:
89 self.returncode = os.EX_OK
90 return self.returncode
92 def _input_handler(self, fd, event):
93 raise NotImplementedError(self)
95 def _unregister(self):
97 Unregister from the scheduler and close open files.
100 self._registered = False
102 if self._reg_id is not None:
103 self.scheduler.source_remove(self._reg_id)
106 if self._files is not None:
107 for f in self._files.values():