~package-import/ubuntu/natty/apport/defunct

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'''Python sys.excepthook hook to generate apport crash dumps.'''

# Copyright (c) 2006 - 2009 Canonical Ltd.
# Authors: Robert Collins <robert@ubuntu.com>
#          Martin Pitt <martin.pitt@ubuntu.com>
# 
# 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.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

import os
import sys

CONFIG = '/etc/default/apport'

def enabled():
    '''Return whether Apport should generate crash reports.'''

    import re
    try:
        conf = open(CONFIG).read()
        return re.search('^\s*enabled\s*=\s*0\s*$', conf, re.M) is None
    except IOError:
        # if the file does not exist, assume it's enabled
        return True

def apport_excepthook(exc_type, exc_obj, exc_tb):
    '''Catch an uncaught exception and make a traceback.'''

    # create and save a problem report. Note that exceptions in this code
    # are bad, and we probably need a per-thread reentrancy guard to
    # prevent that happening. However, on Ubuntu there should never be
    # a reason for an exception here, other than [say] a read only var
    # or some such. So what we do is use a try - finally to ensure that
    # the original excepthook is invoked, and until we get bug reports
    # ignore the other issues.

    # import locally here so that there is no routine overhead on python
    # startup time - only when a traceback occurs will this trigger.
    try:
        # ignore 'safe' exit types.
        if exc_type in (KeyboardInterrupt, ):
            return

        # do not do anything if apport was disabled
        if not enabled():
            return

        from cStringIO import StringIO
        import re, tempfile, traceback
        from apport.fileutils import likely_packaged

        # apport will look up the package from the executable path.
        try:
            binary = os.path.realpath(os.path.join(os.getcwdu(), sys.argv[0]))
        except (TypeError, AttributeError, IndexError):
            # the module has mutated sys.argv, plan B
            try:
                binary = os.readlink('/proc/%i/exe' % os.getpid())
            except OSError:
                return

        # for interactive python sessions, sys.argv[0] == ''; catch that and
        # other irregularities
        if not os.access(binary, os.X_OK) or not os.path.isfile(binary):
            return

        # filter out binaries in user accessible paths
        if not likely_packaged(binary):
            return

        import apport.report

        pr = apport.report.Report()
        # append a basic traceback. In future we may want to include
        # additional data such as the local variables, loaded modules etc.
        tb_file = StringIO()
        traceback.print_exception(exc_type, exc_obj, exc_tb, file=tb_file)
        pr['Traceback'] = tb_file.getvalue().strip()
        pr.add_proc_info()
        pr.add_user_info()
        # override the ExecutablePath with the script that was actually running.
        pr['ExecutablePath'] = binary
        try:
            pr['PythonArgs'] = '%r' % sys.argv
        except AttributeError:
            pass
        if pr.check_ignored():
            return
        mangled_program = re.sub('/', '_', binary)
        # get the uid for now, user name later
        user = os.getuid()
        pr_filename = '/var/crash/%s.%i.crash' % (mangled_program, user)
        if os.path.exists(pr_filename):
            if apport.fileutils.seen_report(pr_filename):
                # remove the old file, so that we can create the new one with
                # os.O_CREAT|os.O_EXCL
                os.unlink(pr_filename)
            else:
                # don't clobber existing report
                return
        report_file = os.fdopen(os.open(pr_filename,
            os.O_WRONLY|os.O_CREAT|os.O_EXCL, 0600), 'w')
        try:
            pr.write(report_file)
        finally:
            report_file.close()

    finally:
        # resume original processing to get the default behaviour,
        # but do not trigger an AttributeError on interpreter shutdown.
        if sys:
            sys.__excepthook__(exc_type, exc_obj, exc_tb)


def install():
    '''Install the python apport hook.'''

    sys.excepthook = apport_excepthook

#
# Unit test
#

if __name__ == '__main__':
    import unittest, tempfile, subprocess, os.path, stat
    import apport.fileutils, problem_report

    class _T(unittest.TestCase):
        def test_env(self):
            '''Check the test environment.'''

            self.assertEqual(apport.fileutils.get_all_reports(), [],
                'No crash reports already present')

        def _test_crash(self, extracode='', scriptname=None):
            '''Create a test crash.'''

            # put the script into /var/crash, since that isn't ignored in the
            # hook
            if scriptname:
                script = scriptname
                fd = os.open(scriptname, os.O_CREAT|os.O_WRONLY)
            else:
                (fd, script) = tempfile.mkstemp(dir=apport.fileutils.report_dir)
            try:
                os.write(fd, '''#!/usr/bin/python
def func(x):
    raise Exception, 'This should happen.'

%s
func(42)
''' % extracode)
                os.close(fd)
                os.chmod(script, 0755)

                p = subprocess.Popen([script, 'testarg1', 'testarg2'],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                err = p.communicate()[1]
                self.assertEqual(p.returncode, 1,
                    'crashing test python program exits with failure code')
                self.assert_('Exception: This should happen.' in err)
                self.failIf('OSError' in err, err)
            finally:
                os.unlink(script)

            return script

        def test_general(self):
            '''general operation of the Python crash hook.'''

            script = self._test_crash()

            # did we get a report?
            reports = apport.fileutils.get_new_reports()
            pr = None
            try:
                self.assertEqual(len(reports), 1, 'crashed Python program produced a report')
                self.assertEqual(stat.S_IMODE(os.stat(reports[0]).st_mode),
                    0600, 'report has correct permissions')

                pr = problem_report.ProblemReport()
                pr.load(open(reports[0]))
            finally:
                for r in reports:
                    os.unlink(r)

            # check report contents
            expected_keys = ['InterpreterPath', 'PythonArgs',
                'Traceback', 'ProblemType', 'ProcEnviron', 'ProcStatus',
                'ProcCmdline', 'Date', 'ExecutablePath', 'ProcMaps',
                'UserGroups']
            self.assert_(set(expected_keys).issubset(set(pr.keys())),
                'report has necessary fields')
            self.assert_('bin/python' in pr['InterpreterPath'])
            self.assertEqual(pr['ExecutablePath'], script)
            self.assertEqual(pr['PythonArgs'], "['%s', 'testarg1', 'testarg2']" % script)
            self.assert_(pr['Traceback'].startswith('Traceback'))
            self.assert_("func\n    raise Exception, 'This should happen." in pr['Traceback'])

        def test_existing(self):
            '''Python crash hook overwrites seen existing files.'''

            script = self._test_crash()

            # did we get a report?
            to_del = set()
            try:
                reports = apport.fileutils.get_new_reports()
                to_del.update(reports)
                self.assertEqual(len(reports), 1, 'crashed Python program produced a report')
                self.assertEqual(stat.S_IMODE(os.stat(reports[0]).st_mode),
                    0600, 'report has correct permissions')

                # touch report -> "seen" case
                apport.fileutils.mark_report_seen(reports[0])

                reports = apport.fileutils.get_new_reports()
                to_del.update(reports)
                self.assertEqual(len(reports), 0)

                script = self._test_crash(scriptname=script)
                reports = apport.fileutils.get_new_reports()
                to_del.update(reports)
                self.assertEqual(len(reports), 1)

                # "unseen" case
                script = self._test_crash(scriptname=script)
                reports = apport.fileutils.get_new_reports()
                self.assertEqual(len(reports), 1)
                to_del.update(reports)
            finally:
                for r in to_del:
                    os.unlink(r)

        def test_no_argv(self):
            '''with zapped sys.argv.'''

            self._test_crash('import sys\nsys.argv = None')

            # did we get a report?
            reports = apport.fileutils.get_new_reports()
            pr = None
            try:
                self.assertEqual(len(reports), 1, 'crashed Python program produced a report')
                self.assertEqual(stat.S_IMODE(os.stat(reports[0]).st_mode),
                    0600, 'report has correct permissions')

                pr = problem_report.ProblemReport()
                pr.load(open(reports[0]))
            finally:
                for r in reports:
                    os.unlink(r)

            # check report contents
            expected_keys = ['InterpreterPath',
                'Traceback', 'ProblemType', 'ProcEnviron', 'ProcStatus',
                'ProcCmdline', 'Date', 'ExecutablePath', 'ProcMaps',
                'UserGroups']
            self.assert_(set(expected_keys).issubset(set(pr.keys())),
                'report has necessary fields')
            self.assert_('bin/python' in pr['InterpreterPath'])
            self.assert_(pr['Traceback'].startswith('Traceback'))

        def _assert_no_reports(self):
            '''Assert that there are no crash reports.'''

            reports = apport.fileutils.get_new_reports()
            try:
                self.assertEqual(len(reports), 0,
                    'no crash reports present (cwd: %s)' % os.getcwd())
            finally:
                # clean up in case we fail
                for r in reports:
                    pass
                    #os.unlink(r)

        def test_interactive(self):
            '''interactive Python sessions never generate a report.'''

            orig_cwd = os.getcwd()
            try:
                for d in ('/tmp', '/usr/local', '/usr'):
                    os.chdir(d)
                    p = subprocess.Popen(['python'], stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    (out, err) = p.communicate('raise ValueError')
                    assert p.returncode != 0
                    assert out == ''
                    assert 'ValueError' in err
                    self._assert_no_reports()
            finally:
                os.chdir(orig_cwd)

        def test_ignoring(self):
            '''the Python crash hook respects the ignore list.'''

            # put the script into /var/crash, since that isn't ignored in the
            # hook
            (fd, script) = tempfile.mkstemp(dir=apport.fileutils.report_dir)
            ifpath = os.path.expanduser(apport.report._ignore_file)
            orig_ignore_file = None
            try:
                os.write(fd, '''#!/usr/bin/python
def func(x):
    raise Exception, 'This should happen.'

func(42)
''')
                os.close(fd)
                os.chmod(script, 0755)

                # move aside current ignore file
                if os.path.exists(ifpath):
                    orig_ignore_file = ifpath + '.apporttest'
                    os.rename(ifpath, orig_ignore_file)

                # ignore
                r = apport.report.Report()
                r['ExecutablePath'] = script
                r.mark_ignore()
                r = None

                p = subprocess.Popen([script, 'testarg1', 'testarg2'],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                err = p.communicate()[1]
                self.assertEqual(p.returncode, 1,
                    'crashing test python program exits with failure code')
                self.assert_('Exception: This should happen.' in err)

            finally:
                os.unlink(script)
                # clean up our ignore file
                if os.path.exists(ifpath):
                    os.unlink(ifpath)
                if orig_ignore_file:
                    os.rename(orig_ignore_file, ifpath)

            # did we get a report?
            reports = apport.fileutils.get_new_reports()
            pr = None
            try:
                self.assertEqual(len(reports), 0)
            finally:
                for r in reports:
                    os.unlink(r)

    unittest.main()