~ubuntu-branches/ubuntu/wily/python-oslo.rootwrap/wily-proposed

« back to all changes in this revision

Viewing changes to oslo_rootwrap/daemon.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand, James Page, Thomas Goirand
  • Date: 2015-09-27 18:45:12 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20150927184512-kkl2oi0zrspeus7n
Tags: 2.3.0-1
[ James Page ]
* Update Vcs fields for new git repository locations.
* Fix typo in transitional package descriptions (LP: #1471561).

[ Thomas Goirand ]
* New upstream release.
* Fixed (build-)depends for this release.
* Do not a run test failing in Python 3.4:
  test_rootwrap.RootwrapTestCase.test_KillFilter_renamed_exe
* python-oslo-rootwrap (transition package) in priority extra.

Show diffs side-by-side

added added

removed removed

Lines of Context:
90
90
        manager_cls = get_manager_class(config, filters)
91
91
        manager = manager_cls(address=socket_path)
92
92
        server = manager.get_server()
93
 
        # allow everybody to connect to the socket
94
 
        rw_rw_rw_ = (stat.S_IRUSR | stat.S_IWUSR |
95
 
                     stat.S_IRGRP | stat.S_IWGRP |
96
 
                     stat.S_IROTH | stat.S_IWOTH)
97
 
        os.chmod(socket_path, rw_rw_rw_)
98
93
        try:
99
 
            # In Python 3 we have to use buffer to push in bytes directly
100
 
            stdout = sys.stdout.buffer
101
 
        except AttributeError:
102
 
            stdout = sys.stdout
103
 
        stdout.write(socket_path.encode('utf-8'))
104
 
        stdout.write(b'\n')
105
 
        stdout.write(bytes(server.authkey))
106
 
        sys.stdin.close()
107
 
        sys.stdout.close()
108
 
        sys.stderr.close()
109
 
        # Gracefully shutdown on INT or TERM signals
110
 
        stop = functools.partial(daemon_stop, server)
111
 
        signal.signal(signal.SIGTERM, stop)
112
 
        signal.signal(signal.SIGINT, stop)
113
 
        LOG.info("Starting rootwrap daemon main loop")
114
 
        server.serve_forever()
 
94
            # allow everybody to connect to the socket
 
95
            rw_rw_rw_ = (stat.S_IRUSR | stat.S_IWUSR |
 
96
                         stat.S_IRGRP | stat.S_IWGRP |
 
97
                         stat.S_IROTH | stat.S_IWOTH)
 
98
            os.chmod(socket_path, rw_rw_rw_)
 
99
            try:
 
100
                # In Python 3 we have to use buffer to push in bytes directly
 
101
                stdout = sys.stdout.buffer
 
102
            except AttributeError:
 
103
                stdout = sys.stdout
 
104
            stdout.write(socket_path.encode('utf-8'))
 
105
            stdout.write(b'\n')
 
106
            stdout.write(bytes(server.authkey))
 
107
            sys.stdin.close()
 
108
            sys.stdout.close()
 
109
            sys.stderr.close()
 
110
            # Gracefully shutdown on INT or TERM signals
 
111
            stop = functools.partial(daemon_stop, server)
 
112
            signal.signal(signal.SIGTERM, stop)
 
113
            signal.signal(signal.SIGINT, stop)
 
114
            LOG.info("Starting rootwrap daemon main loop")
 
115
            server.serve_forever()
 
116
        finally:
 
117
            conn = server.listener
 
118
            # This will break accept() loop with EOFError if it was not in the
 
119
            # main thread (as in Python 3.x)
 
120
            conn.close()
 
121
            # Closing all currently connected client sockets for reading to
 
122
            # break worker threads blocked on recv()
 
123
            for cl_conn in conn.get_accepted():
 
124
                try:
 
125
                    cl_conn.half_close()
 
126
                except Exception:
 
127
                    # Most likely the socket have already been closed
 
128
                    LOG.debug("Failed to close connection")
 
129
            LOG.info("Waiting for all client threads to finish.")
 
130
            for thread in threading.enumerate():
 
131
                if thread.daemon:
 
132
                    LOG.debug("Joining thread %s", thread)
 
133
                    thread.join()
115
134
    finally:
116
 
        conn = server.listener
117
 
        # This will break accept() loop with EOFError if it was not in the main
118
 
        # thread (as in Python 3.x)
119
 
        conn.close()
120
 
        # Closing all currently connected client sockets for reading to break
121
 
        # worker threads blocked on recv()
122
 
        for cl_conn in conn.get_accepted():
123
 
            try:
124
 
                cl_conn.half_close()
125
 
            except Exception:
126
 
                # Most likely the socket have already been closed
127
 
                LOG.debug("Failed to close connection")
128
 
        LOG.info("Waiting for all client threads to finish.")
129
 
        for thread in threading.enumerate():
130
 
            if thread.daemon:
131
 
                LOG.debug("Joining thread %s", thread)
132
 
                thread.join()
133
135
        LOG.debug("Removing temporary directory %s", temp_dir)
134
136
        shutil.rmtree(temp_dir)
135
137