File indexing completed on 2024-04-28 16:45:13

0001 /*
0002 SPDX-FileCopyrightText: 2011 Martin Gräßlin <mgraesslin@kde.org>
0003 SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
0004 
0005 SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 #include <KLocalizedString>
0008 
0009 #include <QCommandLineParser>
0010 #include <QDateTime>
0011 #include <QSessionManager>
0012 #include <QSurfaceFormat>
0013 
0014 #include <iostream>
0015 
0016 #include <signal.h>
0017 
0018 #include "greeterapp.h"
0019 
0020 #include <config-kscreenlocker.h>
0021 #include <kscreenlocker_greet_logging.h>
0022 
0023 #if HAVE_SYS_PRCTL_H
0024 #include <sys/prctl.h>
0025 #endif
0026 #if HAVE_SYS_PROCCTL_H
0027 #include <sys/procctl.h>
0028 #include <unistd.h>
0029 #endif
0030 
0031 #include <KSignalHandler>
0032 #include <LayerShellQt/Shell>
0033 
0034 static void signalHandler(int signum)
0035 {
0036     ScreenLocker::UnlockApp *instance = qobject_cast<ScreenLocker::UnlockApp *>(QCoreApplication::instance());
0037 
0038     if (!instance) {
0039         return;
0040     }
0041 
0042     switch (signum) {
0043     case SIGTERM:
0044         // exit gracefully to not leave behind screensaver processes (bug#224200)
0045         // return exit code 1 to indicate that a valid password was not entered,
0046         // to prevent circumventing the password input by sending a SIGTERM
0047 
0048         qCDebug(KSCREENLOCKER_GREET) << "Greeter received SIGTERM. Will exit with error.";
0049         instance->exit(1);
0050         break;
0051     case SIGUSR1:
0052         qCDebug(KSCREENLOCKER_GREET) << "Greeter received SIGUSR1. Will lock immediately.";
0053         instance->lockImmediately();
0054         break;
0055     }
0056 }
0057 
0058 int main(int argc, char *argv[])
0059 {
0060     LayerShellQt::Shell::useLayerShell();
0061 
0062     // disable ptrace on the greeter
0063 #if HAVE_PR_SET_DUMPABLE
0064     prctl(PR_SET_DUMPABLE, 0);
0065 #endif
0066 #if HAVE_PROC_TRACE_CTL
0067     int mode = PROC_TRACE_CTL_DISABLE;
0068     procctl(P_PID, getpid(), PROC_TRACE_CTL, &mode);
0069 #endif
0070 
0071     qCDebug(KSCREENLOCKER_GREET) << "Greeter is starting up.";
0072 
0073     KLocalizedString::setApplicationDomain("kscreenlocker_greet");
0074 
0075     // explicitly disable input methods on x11 as it makes it impossible to unlock, see BUG 306932
0076     // but explicitly set on screen keyboard such as maliit is allowed
0077     // on wayland, let the compositor take care of the input method
0078     if (!qEnvironmentVariableIsSet("WAYLAND_DISPLAY") && !qEnvironmentVariableIsSet("WAYLAND_SOCKET")
0079         && qgetenv("QT_IM_MODULE") != QByteArrayLiteral("maliit")) {
0080         qputenv("QT_IM_MODULE", QByteArrayLiteral("qtvirtualkeyboard"));
0081     }
0082 
0083     // Suppresses modal warnings about unwritable configuration files which may render the system inaccessible
0084     qputenv("KDE_HOME_READONLY", "1");
0085     // Disable QML caching to prevent cache corruption in full or near-full disk scenarios.
0086     // https://bugs.kde.org/show_bug.cgi?id=471952
0087     // https://bugreports.qt.io/browse/QTBUG-117130
0088     qputenv("QML_DISABLE_DISK_CACHE", "1");
0089 
0090     auto format = QSurfaceFormat::defaultFormat();
0091     format.setOption(QSurfaceFormat::ResetNotification);
0092     QSurfaceFormat::setDefaultFormat(format);
0093 
0094     ScreenLocker::UnlockApp app(argc, argv);
0095 
0096     KSignalHandler::self()->watchSignal(SIGTERM);
0097     KSignalHandler::self()->watchSignal(SIGUSR1);
0098 
0099     // only connect signal handler once we can actual handle the signal properly
0100     QObject::connect(KSignalHandler::self(), &KSignalHandler::signalReceived, &app, &signalHandler);
0101 
0102     app.setQuitOnLastWindowClosed(false);
0103     QCoreApplication::setApplicationName(QStringLiteral("kscreenlocker_greet"));
0104     QCoreApplication::setApplicationVersion(QStringLiteral("0.1"));
0105     QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
0106 
0107     // disable session management for the greeter
0108     auto disableSessionManagement = [](QSessionManager &sm) {
0109         sm.setRestartHint(QSessionManager::RestartNever);
0110     };
0111     QObject::connect(&app, &QGuiApplication::commitDataRequest, disableSessionManagement);
0112     QObject::connect(&app, &QGuiApplication::saveStateRequest, disableSessionManagement);
0113 
0114     QCommandLineParser parser;
0115     parser.setApplicationDescription(i18n("Greeter for the KDE Plasma Workspaces Screen locker"));
0116     parser.addHelpOption();
0117     parser.addVersionOption();
0118 
0119     QCommandLineOption testingOption(QStringLiteral("testing"), i18n("Starts the greeter in testing mode"));
0120 
0121     QCommandLineOption themeOption(QStringLiteral("theme"),
0122                                    i18n("Starts the greeter with the selected theme (only in Testing mode)"),
0123                                    QStringLiteral("theme"),
0124                                    QStringLiteral(""));
0125 
0126     QCommandLineOption immediateLockOption(QStringLiteral("immediateLock"), i18n("Lock immediately, ignoring any grace time etc."));
0127     QCommandLineOption graceTimeOption(QStringLiteral("graceTime"),
0128                                        i18n("Delay till the lock user interface gets shown in milliseconds."),
0129                                        QStringLiteral("milliseconds"),
0130                                        QStringLiteral("0"));
0131     QCommandLineOption nolockOption(QStringLiteral("nolock"), i18n("Don't show any lock user interface."));
0132     QCommandLineOption switchUserOption(QStringLiteral("switchuser"), i18n("Default to the switch user UI."));
0133 
0134     QCommandLineOption waylandFdOption(QStringLiteral("ksldfd"), i18n("File descriptor for connecting to ksld."), QStringLiteral("fd"));
0135 
0136     parser.addOption(testingOption);
0137     parser.addOption(themeOption);
0138     parser.addOption(immediateLockOption);
0139     parser.addOption(graceTimeOption);
0140     parser.addOption(nolockOption);
0141     parser.addOption(switchUserOption);
0142     parser.addOption(waylandFdOption);
0143     parser.process(app);
0144 
0145     if (parser.isSet(testingOption)) {
0146         app.setTesting(true);
0147         app.setImmediateLock(true);
0148 
0149         // parse theme option
0150         const QString theme = parser.value(themeOption);
0151         if (!theme.isEmpty()) {
0152             app.setTheme(theme);
0153         }
0154 
0155         // allow ptrace if testing is enabled
0156 #if HAVE_PR_SET_DUMPABLE
0157         prctl(PR_SET_DUMPABLE, 1);
0158 #endif
0159 #if HAVE_PROC_TRACE_CTL
0160         int mode = PROC_TRACE_CTL_ENABLE;
0161         procctl(P_PID, getpid(), PROC_TRACE_CTL, &mode);
0162 #endif
0163     } else {
0164         app.setImmediateLock(parser.isSet(immediateLockOption));
0165     }
0166     app.setNoLock(parser.isSet(nolockOption));
0167 
0168     bool ok = false;
0169     int graceTime = parser.value(graceTimeOption).toInt(&ok);
0170     if (ok) {
0171         app.setGraceTime(graceTime);
0172     }
0173 
0174     if (parser.isSet(switchUserOption)) {
0175         app.setDefaultToSwitchUser(true);
0176     }
0177 
0178     if (parser.isSet(waylandFdOption)) {
0179         ok = false;
0180         const int fd = parser.value(waylandFdOption).toInt(&ok);
0181         if (ok) {
0182             app.setKsldSocket(fd);
0183         }
0184     }
0185 
0186     app.initialViewSetup();
0187 
0188     // This allow ksmserver to know when the application has actually finished setting itself up.
0189     // Crucial for blocking until it is ready, ensuring locking happens before sleep, e.g.
0190     std::cout << "Locked at " << QDateTime::currentDateTime().toSecsSinceEpoch() << std::endl;
0191 
0192     return app.exec();
0193 }