File: endsession

package info (click to toggle)
epoptes 23.01-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,704 kB
  • sloc: python: 2,461; sh: 477; makefile: 11
file content (169 lines) | stat: -rwxr-xr-x 5,684 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
# This file is part of Epoptes, https://epoptes.org
# Copyright 2011-2018 the Epoptes team, see AUTHORS.
# SPDX-License-Identifier: GPL-3.0-or-later

usage() {
    printf "Usage: $0 --logout|--reboot|--shutdown\n%s" \
'
Request a logout, reboot or shutdown.
Currently it supports Gnome, KDE, XFCE and LXDE.
'
}

die() {
    printf "%s\n" "$@" >&2
    exit 1
}

is_root() {
    test $(id -u) -eq 0
}

is_ltsp() {
    test -n "$LTSP_CLIENT"
}

do_logout() {
    # Reset the xprop in case the user asks for a reboot, cancels it
    # (e.g. unsaved work), and then he asks for a logout instead.
    if [ $action = "logout" ] && is_ltsp; then
        # LTSP_LOGOUT_ACTION might not exist, but we don't care
        xprop -root -remove LTSP_LOGOUT_ACTION 2>/dev/null
    fi

    # Gnome
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
      org.gnome.SessionManager.Logout uint32:1 2>&1 && return

    # KDE
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout \
      int32:0 int32:0 int32:0 2>&1 && return

    # Mate
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.mate.SessionManager /org/mate/SessionManager \
      org.mate.SessionManager.Logout uint32:1 2>&1 && return

    # XFCE
    xfce4-session-logout --logout 2>&1 && return

    # LXDE
    test -n "$_LXSESSION_PID" && kill "$_LXSESSION_PID" && return

    # systemd
    test -n "$XDG_SESSION_ID" && \
    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.login1 /org/freedesktop/login1 \
      org.freedesktop.login1.Manager.TerminateSession string:"$XDG_SESSION_ID" \
      2>&1 && return

    die "I don't know how to logout in this environment"
}

do_reboot() {
    if is_root; then
        reboot && return
    elif is_ltsp; then
        # Notify ldm that we want to reboot after logoff
        xprop -root -f LTSP_LOGOUT_ACTION 8s -set LTSP_LOGOUT_ACTION REBOOT
        do_logout && return
    fi

    # Gnome
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
      org.gnome.SessionManager.RequestReboot 2>&1 && return

    # KDE
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout \
      int32:0 int32:1 int32:0 2>&1 && return

    # Mate
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.mate.SessionManager /org/mate/SessionManager \
      org.mate.SessionManager.RequestReboot 2>&1 && return

    # XFCE and LXDE
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.PowerManagement /org/freedesktop/PowerManagement \
      org.freedesktop.PowerManagement.Reboot 2>&1 && return

    # systemd
    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.login1 /org/freedesktop/login1 \
      org.freedesktop.login1.Manager.Reboot boolean:true 2>&1 && return

    # ConsoleKit is the last resort since it doesn't allow inhibiting
    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager \
      org.freedesktop.ConsoleKit.Manager.Restart 2>&1 && return

    die "I don't know how to reboot in this environment"
}

do_shutdown() {
    if is_root; then
        poweroff && return
    elif is_ltsp; then
        # Notify ldm that we want to poweroff after logoff
        xprop -root -f LTSP_LOGOUT_ACTION 8s -set LTSP_LOGOUT_ACTION HALT
        do_logout && return
    fi

    # Gnome
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.gnome.SessionManager /org/gnome/SessionManager \
      org.gnome.SessionManager.RequestShutdown 2>&1 && return

    # KDE
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout \
      int32:0 int32:2 int32:0 2>&1 && return

    # Mate
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.mate.SessionManager /org/mate/SessionManager \
      org.mate.SessionManager.RequestShutdown 2>&1 && return

    # XFCE and LXDE
    dbus-send --session --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.PowerManagement /org/freedesktop/PowerManagement \
      org.freedesktop.PowerManagement.Shutdown && return

    # systemd
    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.login1 /org/freedesktop/login1 \
      org.freedesktop.login1.Manager.PowerOff boolean:true 2>&1 && return

    # ConsoleKit is the last resort since it doesn't allow inhibiting
    dbus-send --system --type=method_call --print-reply --reply-timeout=2000 \
      --dest=org.freedesktop.ConsoleKit /org/freedesktop/ConsoleKit/Manager \
      org.freedesktop.ConsoleKit.Manager.Stop 2>&1 && return

    die "I don't know how to shutdown in this environment"
}

# main
# Hide all dbus output unless DEBUG is set.
test -z "$DEBUG" && exec >/dev/null

case "$1" in
    -l|--logout)
        action=logout
        ;;
    -r|--reboot)
        action=reboot
        ;;
    -s|--shutdown)
        action=shutdown
        ;;
    *)
        die "$(usage)"
        ;;
esac

do_$action