source: trunk/debathena/config/cluster-login-config/debian/getty.debathena @ 24678

Revision 24678, 3.3 KB checked in by broder, 14 years ago (diff)
In cluster-login-config: * If we are unable to find and activate the user's session using ConsoleKit, assume it is on tty7. This will happen on systems using legacy GDM before the user has logged in.
Line 
1#!/usr/bin/python
2"""getty replacement for Debathena
3
4Debathena clusters only allow logins through X, in order to avoid a
5user logging into a tty, then switching away from that tty, and
6walking away from their session.
7
8This replacement for getty clears the screen, and offers to switch the
9user back to their session if they press any key.
10"""
11
12
13import curses
14import getopt
15import os
16import pwd
17import subprocess
18import sys
19
20import dbus
21
22
23CK_NAME = 'org.freedesktop.ConsoleKit'
24CK_MANAGER_PATH = '/org/freedesktop/ConsoleKit/Manager'
25CK_MANAGER_IFACE = 'org.freedesktop.ConsoleKit.Manager'
26CK_SESSION_IFACE = 'org.freedesktop.ConsoleKit.Session'
27KIOSK_USER = None
28
29
30def find_tty(args):
31    """Find the tty in a set of getty arguments.
32
33    Given the command line arguments one might pass to getty, find and
34    return the argument that is the tty to use.
35
36    find_tty uses the getopt option specifier and tty identifying
37    logic from util-linux 2.13.1.1.
38
39    Note that all other arguments will be ignored, making this getty
40    useless for gettys that are not on normal ttys.
41    """
42    opts, args = getopt.getopt(args, '8I:LH:f:hil:mt:wUn')
43
44    # Accept both "tty baudrate" and "baudrate tty"
45    if '0' <= args[0][0] <= '9':
46        return args[1]
47    else:
48        return args[0]
49
50
51def activate_session():
52    """Seek out and activate what should be the current session.
53
54    Using ConsoleKit, activate_session first looks to see if there is
55    an active kiosk-mode browsing session, and if so, switches to
56    that. Otherwise, it picks the first local session it can find and
57    assumes that's the user's session.
58
59    With the exception of the kiosk-mode session, it should be
60    impossible to have more than one session running simultaneously,
61    so blindly using the first session shouldn't be a problem.
62    """
63    global KIOSK_USER
64    if KIOSK_USER is None:
65        try:
66            KIOSK_USER = pwd.getpwnam('kiosk@mit').pw_uid
67        except KeyError:
68            # There is no kiosk user
69            KIOSK_USER = False
70
71    bus = dbus.SystemBus()
72    manager = dbus.Interface(bus.get_object(CK_NAME, CK_MANAGER_PATH),
73                             CK_MANAGER_IFACE)
74
75    session = None
76    if KIOSK_USER:
77        sessions = manager.GetSessionsForUnixUser(KIOSK_USER)
78        if sessions:
79            session = dbus.Interface(bus.get_object(CK_NAME, sessions[0]),
80                                     CK_SESSION_IFACE)
81    if not session:
82        # Find the first session that identifies itself as local
83        sessions = manager.GetSessions()
84        for s in sessions:
85            session = dbus.Interface(bus.get_object(CK_NAME, s),
86                                     CK_SESSION_IFACE)
87            if session.IsLocal():
88               break
89        else:
90            session = None
91
92    if session:
93        session.Activate()
94    else:
95        subprocess.call(['chvt', '7'])
96
97
98def main():
99    tty_name = find_tty(sys.argv[1:])
100    tty = open(os.path.join('/dev', tty_name), 'a+')
101    # We want to set TERM for a tty, not for whatever TERM was set to
102    # on getty's controlling terminal.
103    curses.setupterm("linux", tty.fileno())
104
105    CLEAR = curses.tigetstr('clear')
106
107    while True:
108        tty.write(CLEAR)
109        tty.write('Please press Enter to return to your session\n')
110        tty.readline()
111
112        activate_session()
113
114
115if __name__ == '__main__':
116    main()
Note: See TracBrowser for help on using the repository browser.