~guadalinex-members/casper-guada/casper-guada-jdev

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
#!/usr/bin/python

import os
import sys
import subprocess
import time
import signal
import errno
import imp
import grp

sys.path.insert(0, '/usr/lib/ubiquity')

import ubiquity.frontend
import pwd

from ubiquity import osextras

background = '/usr/share/backgrounds/guadalinex_v6_bg.png'

class XStartupError(EnvironmentError):
    pass

class MissingProgramError(EnvironmentError):
    pass

class DM:
    def __init__(self, vt, display):
        self.vt = vt
        self.display = display
        self.server_started = False

        fp = open('/etc/casper.conf', 'r')
        try:
            for line in fp:
                if line.startswith('#'):
                    continue
                line = line.split()
                if not line:
                    continue
                if line[0] == 'export':
                    w = line[1].split('=')
                    if w[0] == 'USERNAME':
                        self.username = w[1].strip('"')
        except IOError:
            import syslog
            syslog.syslog('Unable to determine the username from /etc/casper.conf, using the default.')
            self.username = 'ubuntu'
        finally:
            fp.close()
        self.uid, self.gid = pwd.getpwnam(self.username)[2:4]
        self.homedir = pwd.getpwnam(self.username)[5]
        self.uid = int(self.uid)
        self.gid = int(self.gid)
        self.groups = []
        for g in grp.getgrall():
            if self.username in g[3] or g[0] == self.username:
                self.groups.append(g[2])

        # Look for a frontend module; we won't actually use it (yet), but
        # this lets us find out which window manager etc. to launch. Be
        # careful that importing this here will cause the underlying library
        # to try to talk to the X server, which won't go well.
        frontend_names = ['mythbuntu_ui', 'gtk_ui', 'kde_ui','guada_ui']
        self.frontend = None
        for f in frontend_names:
            try:
                imp.find_module(f, ubiquity.frontend.__path__)
                self.frontend = f
                break
            except ImportError:
                pass
        else:
            raise AttributeError, ('No frontend available; tried %s' %
                                   ', '.join(frontend_names))

    def sigusr1_handler(self, signum, frame):
        self.server_started = True

    def drop_privileges(self):
        os.setgroups(self.groups)
        os.setgid(self.gid)
        os.setuid(self.uid)

    def server_preexec(self):
        signal.signal(signal.SIGUSR1, signal.SIG_IGN)

    def run(self, *program):
        null = open('/dev/null', 'w')
        if not os.path.exists('/var/log/installer'):
            os.makedirs('/var/log/installer')
        logfile = open('/var/log/installer/dm', 'w')

        signal.signal(signal.SIGUSR1, self.sigusr1_handler)
        signal.signal(signal.SIGTTIN, signal.SIG_IGN)
        signal.signal(signal.SIGTTOU, signal.SIG_IGN)

        servercommand = ['X', '-br', '-ac', '-noreset', self.vt, self.display]
        if self.frontend == 'mythbuntu_ui':
            servercommand.append('-dpi')
            servercommand.append('100')

        server = subprocess.Popen(servercommand, stdin=null, stdout=logfile, stderr=logfile, preexec_fn=self.server_preexec)

        os.environ['DISPLAY'] = self.display
        os.environ['HOME'] = self.homedir

        # Really we should select on a pipe or something, but it's not worth
        # the effort for now.
        timeout = 60
        while not self.server_started:
            status = server.poll()
            if type(status) is int and status != 0:
                raise XStartupError, "X server exited with return code " + str(status)
            if timeout == 0:
                raise XStartupError, "X server failed to start after 60 seconds"
            time.sleep(1)
            timeout -= 1

        # Session bus, apparently needed by most interfaces now
        if ('DBUS_SESSION_BUS_ADDRESS' not in os.environ and
            osextras.find_on_path('dbus-launch')):
            dbus_subp = subprocess.Popen(
                ['dbus-launch', '--exit-with-session'],
                stdin=null, stdout=subprocess.PIPE, stderr=logfile,
                preexec_fn=self.drop_privileges)
            for line in dbus_subp.stdout:
                try:
                    name, value = line.rstrip('\n').split('=', 1)
                    os.environ[name] = value
                except ValueError:
                    pass
            dbus_subp.wait()

        gconfd_running = False
        if ((self.frontend == 'gtk_ui' or self.frontend == 'mythbuntu_ui') and
            osextras.find_on_path('gconftool-2')):
            subprocess.call(['gconftool-2', '--spawn'],
                            stdin=null, stdout=logfile, stderr=logfile,
                            preexec_fn=self.drop_privileges)
            gconfd_running = True

        extras = []
        if self.frontend == 'gtk_ui' or self.frontend == 'mythbuntu_ui':
            gconf_dir = ('xml:readwrite:%s' %
                os.path.expanduser('~%s/.gconf' %
                self.username))
            accessibility = 'false'
            if osextras.find_on_path('gconftool-2'):
                subprocess.call(
                    ['gconftool-2', '--config-source', gconf_dir,
                     '--type', 'int', '--set',
                     '/apps/metacity/general/num_workspaces', '1'],
                    stdin=null, stdout=logfile, stderr=logfile,
                    preexec_fn=self.drop_privileges)
                subp = subprocess.Popen(
                    ['gconftool-2', '--config-source', gconf_dir,
                     '--get', '/desktop/gnome/interface/accessibility'],
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                    preexec_fn=self.drop_privileges)
                accessibility = subp.communicate()[0].rstrip('\n')
            # Accessibility infrastructure
            if accessibility == 'true':
                if os.path.exists('/usr/lib/at-spi/at-spi-registryd'):
                    extras.append(subprocess.Popen(['/usr/lib/at-spi/at-spi-registryd'],
                        stdin=null, stdout=logfile, stderr=logfile))
                    os.environ['GTK_MODULES'] = 'gail:atk-bridge'

            maybe_drop_privileges = {}
            if accessibility != 'true':
                maybe_drop_privileges['preexec_fn'] = self.drop_privileges

            # Don't show the background image in v1 accessibility profile. It
            # is not shown in the GNOME desktop after all.
            fp = open('/proc/cmdline', 'r')
            if os.access(background, os.R_OK) and not 'access=v1' in fp.readline():
                import gtk
                pixbuf = gtk.gdk.pixbuf_new_from_file(background)
                root = gtk.gdk.get_default_root_window()
                (root_width, root_height) = root.get_size()
                scaled = pixbuf.scale_simple(root_width, root_height, gtk.gdk.INTERP_BILINEAR)
                (pixmap, mask) = scaled.render_pixmap_and_mask(0)
                root.set_back_pixmap(pixmap, False)
                root.clear()
                gtk.gdk.flush()
            fp.close()

        if self.frontend in ('gtk_ui', 'mythbuntu_ui'):
            if gconfd_running and osextras.find_on_path('metacity'):
                wm = subprocess.Popen(['metacity', '--sm-disable'],
                    stdin=null, stdout=logfile, stderr=logfile,
                    **maybe_drop_privileges)
            elif osextras.find_on_path('xfwm4'):
                wm = subprocess.Popen('xfwm4',
                    stdin=null, stdout=logfile, stderr=logfile,
                    **maybe_drop_privileges)
            elif osextras.find_on_path('matchbox-window-manager'):
                wm = subprocess.Popen('matchbox-window-manager',
                    stdin=null, stdout=logfile, stderr=logfile,
                    **maybe_drop_privileges)
            else:
                raise MissingProgramError, \
                    "No window manager found (tried metacity, xfwm4, matchbox-window-manager)"

            if os.path.exists('/usr/lib/gnome-settings-daemon/gnome-settings-daemon'):
                extras.append(subprocess.Popen(['/usr/lib/gnome-settings-daemon/gnome-settings-daemon'],
                    stdin=null, stdout=logfile, stderr=logfile, **maybe_drop_privileges))

            # Accessibility tools
            if accessibility == 'true':
                fp = open('/proc/cmdline', 'r')
                if 'access=m2' in fp.readline():
                    if osextras.find_on_path('onboard'):
                        extras.append(subprocess.Popen(['onboard'],
                            stdin=null, stdout=logfile, stderr=logfile))
                else:
                    if osextras.find_on_path('orca'):
                        time.sleep(15)
                        extras.append(subprocess.Popen(['orca', '-sudo', self.username, '-n'],
                            stdin=null, stdout=logfile, stderr=logfile))
                fp.close()
        elif self.frontend == 'kde_ui':
            extras.append(subprocess.Popen(['kwriteconfig', '--file', 'kwinrc', '--group', 'Compositing', '--key', 'Enabled', 'false'], stdin=null, stdout=logfile, stderr=logfile, preexec_fn=self.drop_privileges))
            wm = subprocess.Popen('kwin', stdin=null, stdout=logfile,
                stderr=logfile, preexec_fn=self.drop_privileges)

        greeter = subprocess.Popen(program, stdin=null, stdout=logfile, stderr=logfile)
        ret = greeter.wait()

        def sigalrm_handler(signum, frame):
            os.kill(wm.pid, signal.SIGKILL)
            for extra in extras:
                os.kill(extra.pid, signal.SIGKILL)

        os.kill(wm.pid, signal.SIGTERM)
        for extra in extras:
            os.kill(extra.pid, signal.SIGTERM)
        if gconfd_running:
            subprocess.call(['gconftool-2', '--shutdown'],
                            stdin=null, stdout=logfile, stderr=logfile,
                            preexec_fn=self.drop_privileges)
        signal.signal(signal.SIGALRM, sigalrm_handler)
        signal.alarm(1) # low patience with WMs failing to exit on demand
        processes = set(extras)
        processes.add(wm)
        while processes:
            done = set()
            for process in processes:
                try:
                    process.wait()
                    done.add(process)
                except OSError, e:
                    if e.errno == errno.EINTR:
                        continue
                    raise
            processes -= done
        signal.alarm(0)
        os.kill(server.pid, signal.SIGTERM)
        server.wait()

        if ret is not None and ret >= 0:
            return ret
        else:
            return 1

if len(sys.argv) < 3:
    sys.stderr.write('Usage: ubiquity-dm <vt[1-N]> <:[0-N]> <program> [<arguments>]\n')
    sys.exit(1)

vt, display = sys.argv[1:3]
try:
    dm = DM(vt, display)
except XStartupError:
    sys.exit(1)
proc = None
if dm.frontend == 'gtk_ui' or dm.frontend == 'mythbuntu_ui':
    proc = ['/etc/init.d/gdm', 'start']
elif dm.frontend == 'kde_ui':
    proc = ['/etc/init.d/kdm', 'start']
ret = dm.run(*sys.argv[3:])
if proc:
    subprocess.Popen(proc)
sys.exit(ret)