diff --git a/ksmserver/main.cpp b/ksmserver/main.cpp index 0467a5d14..d72bc1859 100644 --- a/ksmserver/main.cpp +++ b/ksmserver/main.cpp @@ -1,351 +1,345 @@ /***************************************************************** ksmserver - the KDE session management server Copyright 2000 Matthias Ettrich Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "server.h" #include "startup.h" #include "shutdown.h" #include #include #include #include #include static const char version[] = "0.4"; static const char description[] = I18N_NOOP( "The reliable KDE session manager that talks the standard X11R6 \nsession management protocol (XSMP)." ); Display* dpy = nullptr; Colormap colormap = 0; Visual *visual = nullptr; extern KSMServer* the_server; void IoErrorHandler ( IceConn iceConn) { the_server->ioError( iceConn ); } bool writeTest(QByteArray path) { path += "/XXXXXX"; int fd = mkstemp(path.data()); if (fd == -1) return false; if (write(fd, "Hello World\n", 12) == -1) { int save_errno = errno; close(fd); unlink(path.data()); errno = save_errno; return false; } close(fd); unlink(path.data()); return true; } void checkComposite() { if( qgetenv( "KDE_SKIP_ARGB_VISUALS" ) == "1" ) return; // thanks to zack rusin and frederik for pointing me in the right direction // for the following bits of X11 code dpy = XOpenDisplay(nullptr); // open default display if (!dpy) { qCCritical(KSMSERVER) << "Cannot connect to the X server"; return; } int screen = DefaultScreen(dpy); int eventBase, errorBase; if (XRenderQueryExtension(dpy, &eventBase, &errorBase)) { int nvi; XVisualInfo templ; templ.screen = screen; templ.depth = 32; templ.c_class = TrueColor; XVisualInfo *xvi = XGetVisualInfo(dpy, VisualScreenMask | VisualDepthMask | VisualClassMask, &templ, &nvi); for (int i = 0; i < nvi; ++i) { XRenderPictFormat *format = XRenderFindVisualFormat(dpy, xvi[i].visual); if (format->type == PictTypeDirect && format->direct.alphaMask) { visual = xvi[i].visual; colormap = XCreateColormap(dpy, RootWindow(dpy, screen), visual, AllocNone); XFree(xvi); return; } } XFree(xvi); } XCloseDisplay( dpy ); dpy = nullptr; } void sanity_check( int argc, char* argv[] ) { QString msg; QByteArray path = qgetenv("HOME"); const QByteArray readOnly = qgetenv("KDE_HOME_READONLY"); if (path.isEmpty()) { msg = i18n("$HOME not set!"); } if (msg.isEmpty() && access(path.data(), W_OK)) { if (errno == ENOENT) msg = i18n("$HOME directory (%1) does not exist.", QFile::decodeName(path)); else if (readOnly.isEmpty()) msg = i18n("No write access to $HOME directory (%1).", QFile::decodeName(path)); } if (msg.isEmpty() && access(path.data(), R_OK)) { if (errno == ENOENT) msg = i18n("$HOME directory (%1) does not exist.", QFile::decodeName(path)); else msg = i18n("No read access to $HOME directory (%1).", QFile::decodeName(path)); } if (msg.isEmpty() && readOnly.isEmpty() && !writeTest(path)) { if (errno == ENOSPC) msg = i18n("$HOME directory (%1) is out of disk space.", QFile::decodeName(path)); else msg = i18n("Writing to the $HOME directory (%2) failed with " "the error '%1'", QString::fromLocal8Bit(strerror(errno)), QFile::decodeName(path)); } if (msg.isEmpty()) { path = getenv("ICEAUTHORITY"); if (path.isEmpty()) { path = qgetenv("HOME"); path += "/.ICEauthority"; } if (access(path.data(), W_OK) && (errno != ENOENT)) msg = i18n("No write access to '%1'.", QFile::decodeName(path)); else if (access(path.data(), R_OK) && (errno != ENOENT)) msg = i18n("No read access to '%1'.", QFile::decodeName(path)); } if (msg.isEmpty()) { path = getenv("KDETMP"); if (path.isEmpty()) path = "/tmp"; if (!writeTest(path)) { if (errno == ENOSPC) msg = i18n("Temp directory (%1) is out of disk space.", QFile::decodeName(path)); else msg = i18n("Writing to the temp directory (%2) failed with\n " "the error '%1'", QString::fromLocal8Bit(strerror(errno)), QFile::decodeName(path)); } } if (msg.isEmpty() && (path != "/tmp")) { path = "/tmp"; if (!writeTest(path)) { if (errno == ENOSPC) msg = i18n("Temp directory (%1) is out of disk space."); else msg = i18n("Writing to the temp directory (%2) failed with\n " "the error '%1'", QString::fromLocal8Bit(strerror(errno)), QFile::decodeName(path)); } } if (msg.isEmpty()) { path += "/.ICE-unix"; if (access(path.data(), W_OK) && (errno != ENOENT)) msg = i18n("No write access to '%1'."); else if (access(path.data(), R_OK) && (errno != ENOENT)) msg = i18n("No read access to '%1'."); } if (!msg.isEmpty()) { const QString msg_pre = i18n("The following installation problem was detected\n" "while trying to start KDE:") + QStringLiteral("\n\n "); const QString msg_post = i18n("\n\nKDE is unable to start.\n"); fputs(msg_pre.toUtf8().constData(), stderr); fprintf(stderr, "%s", msg.toUtf8().constData()); fputs(msg_post.toUtf8().constData(), stderr); QApplication a(argc, argv); const QString qmsg = msg_pre + msg + msg_post; KMessageBox::error(nullptr, qmsg, i18n("Plasma Workspace installation problem!")); exit(255); } } extern "C" Q_DECL_EXPORT int kdemain( int argc, char* argv[] ) { sanity_check(argc, argv); putenv((char*)"SESSION_MANAGER="); checkComposite(); // force xcb QPA plugin as ksmserver is very X11 specific const QByteArray origQpaPlatform = qgetenv("QT_QPA_PLATFORM"); qputenv("QT_QPA_PLATFORM", QByteArrayLiteral("xcb")); QQuickWindow::setDefaultAlphaBuffer(true); QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling); QApplication *a = new QApplication(argc, argv); // now the QPA platform is set, unset variable again to not launch apps with incorrect environment if (origQpaPlatform.isEmpty()) { qunsetenv("QT_QPA_PLATFORM"); } else { qputenv("QT_QPA_PLATFORM", origQpaPlatform); } QApplication::setApplicationName( QStringLiteral( "ksmserver") ); QApplication::setApplicationVersion( QString::fromLatin1( version ) ); QApplication::setOrganizationDomain( QStringLiteral( "kde.org") ); fcntl(ConnectionNumber(QX11Info::display()), F_SETFD, 1); a->setQuitOnLastWindowClosed(false); // #169486 QCommandLineParser parser; parser.setApplicationDescription(i18n(description)); parser.addHelpOption(); parser.addVersionOption(); QCommandLineOption restoreOption(QStringList() << QStringLiteral("r") << QStringLiteral("restore"), i18n("Restores the saved user session if available")); parser.addOption(restoreOption); QCommandLineOption wmOption(QStringList() << QStringLiteral("w") << QStringLiteral("windowmanager"), i18n("Starts in case no other window manager is \nparticipating in the session. Default is 'kwin'"), i18n("wm")); parser.addOption(wmOption); QCommandLineOption nolocalOption(QStringLiteral("nolocal"), i18n("Also allow remote connections")); parser.addOption(nolocalOption); QCommandLineOption lockscreenOption(QStringLiteral("lockscreen"), i18n("Starts the session in locked mode")); parser.addOption(lockscreenOption); QCommandLineOption noLockscreenOption(QStringLiteral("no-lockscreen"), i18n("Starts without lock screen support. Only needed if other component provides the lock screen.")); parser.addOption(noLockscreenOption); parser.process(*a); QString wm = parser.value(wmOption); bool only_local = !parser.isSet(nolocalOption); #ifndef HAVE__ICETRANSNOLISTEN /* this seems strange, but the default is only_local, so if !only_local * the option --nolocal was given, and we warn (the option --nolocal * does nothing on this platform, as here the default is reversed) */ if (!only_local) { qCWarning(KSMSERVER, "--nolocal is not supported on your platform. Sorry."); } only_local = false; #endif KSMServer::InitFlags flags = KSMServer::InitFlag::None; if (only_local) { flags |= KSMServer::InitFlag::OnlyLocal; } if (parser.isSet(lockscreenOption)) { flags |= KSMServer::InitFlag::ImmediateLockScreen; } if (parser.isSet(noLockscreenOption)) { flags |= KSMServer::InitFlag::NoLockScreen; } KSMServer *server = new KSMServer( wm, flags); auto startup = new Startup(server); new Shutdown(a); // for the KDE-already-running check in startkde KSelectionOwner kde_running( "_KDE_RUNNING", 0 ); kde_running.claim( false ); IceSetIOErrorHandler( IoErrorHandler ); KConfigGroup config(KSharedConfig::openConfig(), "General"); - int realScreenCount = ScreenCount( QX11Info::display() ); - bool screenCountChanged = - ( config.readEntry( "screenCount", realScreenCount ) != realScreenCount ); - QString loginMode = config.readEntry( "loginMode", "restorePreviousLogout" ); - if ( parser.isSet( restoreOption ) && ! screenCountChanged ) + if ( parser.isSet( restoreOption )) server->restoreSession( QStringLiteral( SESSION_BY_USER ) ); - else if ( loginMode == QStringLiteral( "default" ) || screenCountChanged ) - server->startDefaultSession(); else if ( loginMode == QStringLiteral( "restorePreviousLogout" ) ) server->restoreSession( QStringLiteral( SESSION_PREVIOUS_LOGOUT ) ); else if ( loginMode == QStringLiteral( "restoreSavedSession" ) ) server->restoreSession( QStringLiteral( SESSION_BY_USER ) ); else server->startDefaultSession(); startup->upAndRunning(QStringLiteral( "ksmserver" )); KDBusService service(KDBusService::Unique); int ret = a->exec(); kde_running.release(); // needs to be done before QApplication destruction delete a; return ret; } diff --git a/ksmserver/server.cpp b/ksmserver/server.cpp index d6a5a478f..d95fd3d38 100644 --- a/ksmserver/server.cpp +++ b/ksmserver/server.cpp @@ -1,1262 +1,1261 @@ /***************************************************************** ksmserver - the KDE session management server Copyright 2000 Matthias Ettrich Copyright 2005 Lubos Lunak relatively small extensions by Oswald Buddenhagen some code taken from the dcopserver (part of the KDE libraries), which is Copyright 1999 Matthias Ettrich Copyright 1999 Preston Brown Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ******************************************************************/ #include "server.h" #include "global.h" #include "client.h" #include "ksmserver_debug.h" #include "ksmserverinterfaceadaptor.h" #include "klocalizedstring.h" #include "kglobalaccel.h" #include #include // HAVE_LIMITS_H #include #include #include #include #include #ifdef HAVE_SYS_TIME_H #include #endif #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_LIMITS_H #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "kscreenlocker_interface.h" KSMServer* the_server = nullptr; KSMServer* KSMServer::self() { return the_server; } /*! Utility function to execute a command on the local machine. Used * to restart applications. */ KProcess* KSMServer::startApplication( const QStringList& cmd, const QString& clientMachine, const QString& userId, bool wm ) { QStringList command = cmd; if ( command.isEmpty() ) return nullptr; if ( !userId.isEmpty()) { struct passwd* pw = getpwuid( getuid()); if( pw != nullptr && userId != QString::fromLocal8Bit( pw->pw_name )) { command.prepend( QStringLiteral("--") ); command.prepend( userId ); command.prepend( QStringLiteral("-u") ); command.prepend( QStandardPaths::findExecutable(QStringLiteral("kdesu"))); } } if ( !clientMachine.isEmpty() && clientMachine != QStringLiteral("localhost") ) { command.prepend( clientMachine ); command.prepend( xonCommand ); // "xon" by default } // TODO this function actually should not use KProcess at all and use klauncher (kdeinit) instead. // Klauncher should also have support for tracking whether the launched process is still alive // or not, so this should be redone. For now, use KProcess for wm's, as they need to be tracked, // klauncher for the rest where ksmserver doesn't care. if( wm ) { KProcess* process = new KProcess( this ); *process << command; // make it auto-delete connect(process, static_cast(&KProcess::error), process, &KProcess::deleteLater); connect(process, static_cast(&KProcess::finished), process, &KProcess::deleteLater); process->start(); return process; } else { int n = command.count(); org::kde::KLauncher klauncher(QStringLiteral("org.kde.klauncher5"), QStringLiteral("/KLauncher"), QDBusConnection::sessionBus()); QString app = command[0]; QStringList argList; for ( int i=1; i < n; i++) argList.append( command[i]); klauncher.exec_blind(app, argList ); return nullptr; } } /*! Utility function to execute a command on the local machine. Used * to discard session data */ void KSMServer::executeCommand( const QStringList& command ) { if ( command.isEmpty() ) return; KProcess::execute( command ); } IceAuthDataEntry *authDataEntries = nullptr; static QTemporaryFile *remTempFile = nullptr; static IceListenObj *listenObjs = nullptr; int numTransports = 0; static bool only_local = 0; static Bool HostBasedAuthProc ( char* /*hostname*/) { if (only_local) return true; else return false; } Status KSMRegisterClientProc ( SmsConn /* smsConn */, SmPointer managerData, char * previousId ) { KSMClient* client = (KSMClient*) managerData; client->registerClient( previousId ); return 1; } void KSMInteractRequestProc ( SmsConn /* smsConn */, SmPointer managerData, int dialogType ) { the_server->interactRequest( (KSMClient*) managerData, dialogType ); } void KSMInteractDoneProc ( SmsConn /* smsConn */, SmPointer managerData, Bool cancelShutdown ) { the_server->interactDone( (KSMClient*) managerData, cancelShutdown ); } void KSMSaveYourselfRequestProc ( SmsConn smsConn , SmPointer /* managerData */, int saveType, Bool shutdown, int interactStyle, Bool fast, Bool global ) { if ( shutdown ) { the_server->shutdown( fast ? KWorkSpace::ShutdownConfirmNo : KWorkSpace::ShutdownConfirmDefault, KWorkSpace::ShutdownTypeDefault, KWorkSpace::ShutdownModeDefault ); } else if ( !global ) { SmsSaveYourself( smsConn, saveType, false, interactStyle, fast ); SmsSaveComplete( smsConn ); } // else checkpoint only, ksmserver does not yet support this // mode. Will come for KDE 3.1 } void KSMSaveYourselfPhase2RequestProc ( SmsConn /* smsConn */, SmPointer managerData ) { the_server->phase2Request( (KSMClient*) managerData ); } void KSMSaveYourselfDoneProc ( SmsConn /* smsConn */, SmPointer managerData, Bool success ) { the_server->saveYourselfDone( (KSMClient*) managerData, success ); } void KSMCloseConnectionProc ( SmsConn smsConn, SmPointer managerData, int count, char ** reasonMsgs ) { the_server->deleteClient( ( KSMClient* ) managerData ); if ( count ) SmFreeReasons( count, reasonMsgs ); IceConn iceConn = SmsGetIceConnection( smsConn ); SmsCleanUp( smsConn ); IceSetShutdownNegotiation (iceConn, False); IceCloseConnection( iceConn ); } void KSMSetPropertiesProc ( SmsConn /* smsConn */, SmPointer managerData, int numProps, SmProp ** props ) { KSMClient* client = ( KSMClient* ) managerData; for ( int i = 0; i < numProps; i++ ) { SmProp *p = client->property( props[i]->name ); if ( p ) { client->properties.removeAll( p ); SmFreeProperty( p ); } client->properties.append( props[i] ); if ( !qstrcmp( props[i]->name, SmProgram ) ) the_server->clientSetProgram( client ); } if ( numProps ) free( props ); } void KSMDeletePropertiesProc ( SmsConn /* smsConn */, SmPointer managerData, int numProps, char ** propNames ) { KSMClient* client = ( KSMClient* ) managerData; for ( int i = 0; i < numProps; i++ ) { SmProp *p = client->property( propNames[i] ); if ( p ) { client->properties.removeAll( p ); SmFreeProperty( p ); } } } void KSMGetPropertiesProc ( SmsConn smsConn, SmPointer managerData ) { KSMClient* client = ( KSMClient* ) managerData; SmProp** props = new SmProp*[client->properties.count()]; int i = 0; foreach( SmProp *prop, client->properties ) props[i++] = prop; SmsReturnProperties( smsConn, i, props ); delete [] props; } class KSMListener : public QSocketNotifier { public: KSMListener( IceListenObj obj ) : QSocketNotifier( IceGetListenConnectionNumber( obj ), QSocketNotifier::Read ) { listenObj = obj; } IceListenObj listenObj; }; class KSMConnection : public QSocketNotifier { public: KSMConnection( IceConn conn ) : QSocketNotifier( IceConnectionNumber( conn ), QSocketNotifier::Read ) { iceConn = conn; } IceConn iceConn; }; /* for printing hex digits */ static void fprintfhex (FILE *fp, unsigned int len, char *cp) { static const char hexchars[] = "0123456789abcdef"; for (; len > 0; len--, cp++) { unsigned char s = *cp; putc(hexchars[s >> 4], fp); putc(hexchars[s & 0x0f], fp); } } /* * We use temporary files which contain commands to add/remove entries from * the .ICEauthority file. */ static void write_iceauth (FILE *addfp, FILE *removefp, IceAuthDataEntry *entry) { fprintf (addfp, "add %s \"\" %s %s ", entry->protocol_name, entry->network_id, entry->auth_name); fprintfhex (addfp, entry->auth_data_length, entry->auth_data); fprintf (addfp, "\n"); fprintf (removefp, "remove protoname=%s protodata=\"\" netid=%s authname=%s\n", entry->protocol_name, entry->network_id, entry->auth_name); } #define MAGIC_COOKIE_LEN 16 Status SetAuthentication_local (int count, IceListenObj *listenObjs) { int i; for (i = 0; i < count; i ++) { char *prot = IceGetListenConnectionString(listenObjs[i]); if (!prot) continue; char *host = strchr(prot, '/'); char *sock = nullptr; if (host) { *host=0; host++; sock = strchr(host, ':'); if (sock) { *sock = 0; sock++; } } qCDebug(KSMSERVER) << "KSMServer: SetAProc_loc: conn " << (unsigned)i << ", prot=" << prot << ", file=" << sock; if (sock && !strcmp(prot, "local")) { chmod(sock, 0700); } IceSetHostBasedAuthProc (listenObjs[i], HostBasedAuthProc); free(prot); } return 1; } Status SetAuthentication (int count, IceListenObj *listenObjs, IceAuthDataEntry **authDataEntries) { QTemporaryFile addTempFile; remTempFile = new QTemporaryFile; if (!addTempFile.open() || !remTempFile->open()) return 0; if ((*authDataEntries = (IceAuthDataEntry *) malloc ( count * 2 * sizeof (IceAuthDataEntry))) == nullptr) return 0; FILE *addAuthFile = fopen(QFile::encodeName(addTempFile.fileName()).constData(), "r+"); FILE *remAuthFile = fopen(QFile::encodeName(remTempFile->fileName()).constData(), "r+"); for (int i = 0; i < numTransports * 2; i += 2) { (*authDataEntries)[i].network_id = IceGetListenConnectionString (listenObjs[i/2]); (*authDataEntries)[i].protocol_name = (char *) "ICE"; (*authDataEntries)[i].auth_name = (char *) "MIT-MAGIC-COOKIE-1"; (*authDataEntries)[i].auth_data = IceGenerateMagicCookie (MAGIC_COOKIE_LEN); (*authDataEntries)[i].auth_data_length = MAGIC_COOKIE_LEN; (*authDataEntries)[i+1].network_id = IceGetListenConnectionString (listenObjs[i/2]); (*authDataEntries)[i+1].protocol_name = (char *) "XSMP"; (*authDataEntries)[i+1].auth_name = (char *) "MIT-MAGIC-COOKIE-1"; (*authDataEntries)[i+1].auth_data = IceGenerateMagicCookie (MAGIC_COOKIE_LEN); (*authDataEntries)[i+1].auth_data_length = MAGIC_COOKIE_LEN; write_iceauth (addAuthFile, remAuthFile, &(*authDataEntries)[i]); write_iceauth (addAuthFile, remAuthFile, &(*authDataEntries)[i+1]); IceSetPaAuthData (2, &(*authDataEntries)[i]); IceSetHostBasedAuthProc (listenObjs[i/2], HostBasedAuthProc); } fclose(addAuthFile); fclose(remAuthFile); QString iceAuth = QStandardPaths::findExecutable(QStringLiteral("iceauth")); if (iceAuth.isEmpty()) { qCWarning(KSMSERVER, "KSMServer: could not find iceauth"); return 0; } KProcess p; p << iceAuth << QStringLiteral("source") << addTempFile.fileName(); p.execute(); return (1); } /* * Free up authentication data. */ void FreeAuthenticationData(int count, IceAuthDataEntry *authDataEntries) { /* Each transport has entries for ICE and XSMP */ if (only_local) return; for (int i = 0; i < count * 2; i++) { free (authDataEntries[i].network_id); free (authDataEntries[i].auth_data); } free (authDataEntries); QString iceAuth = QStandardPaths::findExecutable(QStringLiteral("iceauth")); if (iceAuth.isEmpty()) { qCWarning(KSMSERVER, "KSMServer: could not find iceauth"); return; } if (remTempFile) { KProcess p; p << iceAuth << QStringLiteral("source") << remTempFile->fileName(); p.execute(); } delete remTempFile; remTempFile = nullptr; } static int Xio_ErrorHandler( Display * ) { qCWarning(KSMSERVER, "ksmserver: Fatal IO error: client killed"); // Don't do anything that might require the X connection if (the_server) { KSMServer *server = the_server; the_server = nullptr; server->cleanUp(); // Don't delete server!! } exit(0); // Don't report error, it's not our fault. return 0; // Bogus return value, notreached } void KSMServer::setupXIOErrorHandler() { XSetIOErrorHandler(Xio_ErrorHandler); } static int wake_up_socket = -1; static void sighandler(int sig) { if (sig == SIGHUP) { signal(SIGHUP, sighandler); return; } char ch = 0; (void)::write(wake_up_socket, &ch, 1); } void KSMWatchProc ( IceConn iceConn, IcePointer client_data, Bool opening, IcePointer* watch_data) { KSMServer* ds = ( KSMServer*) client_data; if (opening) { *watch_data = (IcePointer) ds->watchConnection( iceConn ); } else { ds->removeConnection( (KSMConnection*) *watch_data ); } } static Status KSMNewClientProc ( SmsConn conn, SmPointer manager_data, unsigned long* mask_ret, SmsCallbacks* cb, char** failure_reason_ret) { *failure_reason_ret = nullptr; void* client = ((KSMServer*) manager_data )->newClient( conn ); cb->register_client.callback = KSMRegisterClientProc; cb->register_client.manager_data = client; cb->interact_request.callback = KSMInteractRequestProc; cb->interact_request.manager_data = client; cb->interact_done.callback = KSMInteractDoneProc; cb->interact_done.manager_data = client; cb->save_yourself_request.callback = KSMSaveYourselfRequestProc; cb->save_yourself_request.manager_data = client; cb->save_yourself_phase2_request.callback = KSMSaveYourselfPhase2RequestProc; cb->save_yourself_phase2_request.manager_data = client; cb->save_yourself_done.callback = KSMSaveYourselfDoneProc; cb->save_yourself_done.manager_data = client; cb->close_connection.callback = KSMCloseConnectionProc; cb->close_connection.manager_data = client; cb->set_properties.callback = KSMSetPropertiesProc; cb->set_properties.manager_data = client; cb->delete_properties.callback = KSMDeletePropertiesProc; cb->delete_properties.manager_data = client; cb->get_properties.callback = KSMGetPropertiesProc; cb->get_properties.manager_data = client; *mask_ret = SmsRegisterClientProcMask | SmsInteractRequestProcMask | SmsInteractDoneProcMask | SmsSaveYourselfRequestProcMask | SmsSaveYourselfP2RequestProcMask | SmsSaveYourselfDoneProcMask | SmsCloseConnectionProcMask | SmsSetPropertiesProcMask | SmsDeletePropertiesProcMask | SmsGetPropertiesProcMask; return 1; } #ifdef HAVE__ICETRANSNOLISTEN extern "C" int _IceTransNoListen(const char * protocol); #endif KSMServer::KSMServer( const QString& windowManager, InitFlags flags ) : wmProcess( nullptr ) , sessionGroup( QStringLiteral( "" ) ) , logoutEffectWidget( nullptr ) , sockets{ -1, -1 } { if (!flags.testFlag(InitFlag::NoLockScreen)) { ScreenLocker::KSldApp::self()->initialize(); if (flags.testFlag(InitFlag::ImmediateLockScreen)) { ScreenLocker::KSldApp::self()->lock(ScreenLocker::EstablishLock::Immediate); } } if(::socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sockets) != 0) qFatal("Could not create socket pair, error %d (%s)", errno, strerror(errno)); wake_up_socket = sockets[0]; QSocketNotifier* n = new QSocketNotifier(sockets[1], QSocketNotifier::Read, this); qApp->connect(n, &QSocketNotifier::activated, &QApplication::quit); new KSMServerInterfaceAdaptor( this ); QDBusConnection::sessionBus().registerObject(QStringLiteral("/KSMServer"), this); the_server = this; clean = false; state = Idle; saveSession = false; wmPhase1WaitingCount = 0; KConfigGroup config(KSharedConfig::openConfig(), "General"); clientInteracting = nullptr; xonCommand = config.readEntry( "xonCommand", "xon" ); selectWm( windowManager ); only_local = flags.testFlag(InitFlag::OnlyLocal); #ifdef HAVE__ICETRANSNOLISTEN if (only_local) _IceTransNoListen("tcp"); #else only_local = false; #endif char errormsg[256]; if (!SmsInitialize ( (char*) KSMVendorString, (char*) KSMReleaseString, KSMNewClientProc, (SmPointer) this, HostBasedAuthProc, 256, errormsg ) ) { qCWarning(KSMSERVER, "KSMServer: could not register XSM protocol"); } if (!IceListenForConnections (&numTransports, &listenObjs, 256, errormsg)) { qCWarning(KSMSERVER, "KSMServer: Error listening for connections: %s", errormsg); qCWarning(KSMSERVER, "KSMServer: Aborting."); exit(1); } { // publish available transports. QByteArray fName = QFile::encodeName(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + QDir::separator() + QStringLiteral("KSMserver")); qCDebug(KSMSERVER) << fName; QString display = QString::fromLocal8Bit(::getenv("DISPLAY")); // strip the screen number from the display display.remove(QRegExp(QStringLiteral("\\.[0-9]+$"))); int i; while( (i = display.indexOf(QLatin1Char(':'))) >= 0) display[i] = '_'; while( (i = display.indexOf(QLatin1Char('/'))) >= 0) display[i] = '_'; fName += '_'+display.toLocal8Bit(); FILE *f; f = ::fopen(fName.data(), "w+"); if (!f) { qCWarning(KSMSERVER, "KSMServer: cannot open %s: %s", fName.data(), strerror(errno)); qCWarning(KSMSERVER, "KSMServer: Aborting."); exit(1); } char* session_manager = IceComposeNetworkIdList(numTransports, listenObjs); fprintf(f, "%s\n%i\n", session_manager, getpid()); fclose(f); setenv( "SESSION_MANAGER", session_manager, true ); // Pass env. var to kdeinit. org::kde::KLauncher klauncher( QStringLiteral( "org.kde.klauncher5" ), QStringLiteral( "/KLauncher" ), QDBusConnection::sessionBus()); klauncher.setLaunchEnv( QStringLiteral( "SESSION_MANAGER" ), QString::fromLocal8Bit( (const char*) session_manager ) ); free(session_manager); } if (only_local) { if (!SetAuthentication_local(numTransports, listenObjs)) qFatal("KSMSERVER: authentication setup failed."); } else { if (!SetAuthentication(numTransports, listenObjs, &authDataEntries)) qFatal("KSMSERVER: authentication setup failed."); } IceAddConnectionWatch (KSMWatchProc, (IcePointer) this); KSMListener* con; for ( int i = 0; i < numTransports; i++) { fcntl( IceGetListenConnectionNumber( listenObjs[i] ), F_SETFD, FD_CLOEXEC ); con = new KSMListener( listenObjs[i] ); listener.append( con ); connect(con, &KSMListener::activated, this, &KSMServer::newConnection); } signal(SIGHUP, sighandler); signal(SIGTERM, sighandler); signal(SIGINT, sighandler); signal(SIGPIPE, SIG_IGN); connect(&protectionTimer, &QTimer::timeout, this, &KSMServer::protectionTimeout); connect(&restoreTimer, &QTimer::timeout, this, &KSMServer::tryRestoreNext); connect(qApp, &QApplication::aboutToQuit, this, &KSMServer::cleanUp); } KSMServer::~KSMServer() { qDeleteAll( listener ); the_server = nullptr; cleanUp(); } void KSMServer::cleanUp() { if (clean) return; clean = true; IceFreeListenObjs (numTransports, listenObjs); wake_up_socket = -1; ::close(sockets[1]); ::close(sockets[0]); sockets[0] = -1; sockets[1] = -1; QByteArray fName = QFile::encodeName(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + QLatin1Char('/') + QStringLiteral("KSMserver")); QString display = QString::fromLocal8Bit(::getenv("DISPLAY")); // strip the screen number from the display display.remove(QRegExp(QStringLiteral("\\.[0-9]+$"))); int i; while( (i = display.indexOf(QLatin1Char(':'))) >= 0) display[i] = '_'; while( (i = display.indexOf(QLatin1Char('/'))) >= 0) display[i] = '_'; fName += '_'+display.toLocal8Bit(); ::unlink(fName.data()); FreeAuthenticationData(numTransports, authDataEntries); signal(SIGTERM, SIG_DFL); signal(SIGINT, SIG_DFL); } void* KSMServer::watchConnection( IceConn iceConn ) { KSMConnection* conn = new KSMConnection( iceConn ); connect(conn, &KSMConnection::activated, this, &KSMServer::processData); return (void*) conn; } void KSMServer::removeConnection( KSMConnection* conn ) { delete conn; } /*! Called from our IceIoErrorHandler */ void KSMServer::ioError( IceConn /*iceConn*/ ) { } void KSMServer::processData( int /*socket*/ ) { IceConn iceConn = ((KSMConnection*)sender())->iceConn; IceProcessMessagesStatus status = IceProcessMessages( iceConn, nullptr, nullptr ); if ( status == IceProcessMessagesIOError ) { IceSetShutdownNegotiation( iceConn, False ); QList::iterator it = clients.begin(); QList::iterator const itEnd = clients.end(); while ( ( it != itEnd ) && *it && ( SmsGetIceConnection( ( *it )->connection() ) != iceConn ) ) ++it; if ( ( it != itEnd ) && *it ) { SmsConn smsConn = (*it)->connection(); deleteClient( *it ); SmsCleanUp( smsConn ); } (void) IceCloseConnection( iceConn ); } } KSMClient* KSMServer::newClient( SmsConn conn ) { KSMClient* client = new KSMClient( conn ); clients.append( client ); return client; } void KSMServer::deleteClient( KSMClient* client ) { if ( !clients.contains( client ) ) // paranoia return; clients.removeAll( client ); clientsToKill.removeAll( client ); clientsToSave.removeAll( client ); if ( client == clientInteracting ) { clientInteracting = nullptr; handlePendingInteractions(); } delete client; if ( state == Shutdown || state == Checkpoint || state == ClosingSubSession ) completeShutdownOrCheckpoint(); if ( state == Killing ) completeKilling(); else if ( state == KillingSubSession ) completeKillingSubSession(); if ( state == KillingWM ) completeKillingWM(); } void KSMServer::newConnection( int /*socket*/ ) { IceAcceptStatus status; IceConn iceConn = IceAcceptConnection( ((KSMListener*)sender())->listenObj, &status); if( iceConn == nullptr ) return; IceSetShutdownNegotiation( iceConn, False ); IceConnectStatus cstatus; while ((cstatus = IceConnectionStatus (iceConn))==IceConnectPending) { (void) IceProcessMessages( iceConn, nullptr, nullptr ); } if (cstatus != IceConnectAccepted) { if (cstatus == IceConnectIOError) qCDebug(KSMSERVER) << "IO error opening ICE Connection!"; else qCDebug(KSMSERVER) << "ICE Connection rejected!"; (void )IceCloseConnection (iceConn); return; } // don't leak the fd fcntl( IceConnectionNumber(iceConn), F_SETFD, FD_CLOEXEC ); } QString KSMServer::currentSession() { if ( sessionGroup.startsWith( QLatin1String( "Session: " ) ) ) return sessionGroup.mid( 9 ); return QStringLiteral( "" ); // empty, not null, since used for KConfig::setGroup } void KSMServer::discardSession() { KConfigGroup config(KSharedConfig::openConfig(), sessionGroup ); int count = config.readEntry( "count", 0 ); foreach ( KSMClient *c, clients ) { QStringList discardCommand = c->discardCommand(); if ( discardCommand.isEmpty()) continue; // check that non of the old clients used the exactly same // discardCommand before we execute it. This used to be the // case up to KDE and Qt < 3.1 int i = 1; while ( i <= count && config.readPathEntry( QStringLiteral("discardCommand") + QString::number(i), QStringList() ) != discardCommand ) i++; if ( i <= count ) executeCommand( discardCommand ); } } void KSMServer::storeSession() { KSharedConfig::Ptr config = KSharedConfig::openConfig(); config->reparseConfiguration(); // config may have changed in the KControl module KConfigGroup generalGroup(config, "General"); excludeApps = generalGroup.readEntry( "excludeApps" ).toLower() .split( QRegExp( QStringLiteral("[,:]") ), QString::SkipEmptyParts ); KConfigGroup configSessionGroup(config, sessionGroup); int count = configSessionGroup.readEntry( "count", 0 ); for ( int i = 1; i <= count; i++ ) { QStringList discardCommand = configSessionGroup.readPathEntry( QLatin1String("discardCommand") + QString::number(i), QStringList() ); if ( discardCommand.isEmpty()) continue; // check that non of the new clients uses the exactly same // discardCommand before we execute it. This used to be the // case up to KDE and Qt < 3.1 QList::iterator it = clients.begin(); QList::iterator const itEnd = clients.end(); while ( ( it != itEnd ) && *it && (discardCommand != ( *it )->discardCommand() ) ) ++it; if ( ( it != itEnd ) && *it ) continue; executeCommand( discardCommand ); } config->deleteGroup( sessionGroup ); //### does not work with global config object... KConfigGroup cg( config, sessionGroup); count = 0; if (state != ClosingSubSession) { // put the wm first foreach ( KSMClient *c, clients ) if ( c->program() == wm ) { clients.removeAll( c ); clients.prepend( c ); break; } } foreach ( KSMClient *c, clients ) { int restartHint = c->restartStyleHint(); if (restartHint == SmRestartNever) continue; QString program = c->program(); QStringList restartCommand = c->restartCommand(); if (program.isEmpty() && restartCommand.isEmpty()) continue; if (state == ClosingSubSession && ! clientsToSave.contains(c)) continue; // 'program' might be (mostly) fullpath, or (sometimes) just the name. // 'name' is just the name. QFileInfo info(program); const QString& name = info.fileName(); if ( excludeApps.contains(program.toLower()) || excludeApps.contains(name.toLower()) ) { continue; } count++; QString n = QString::number(count); cg.writeEntry( QStringLiteral("program")+n, program ); cg.writeEntry( QStringLiteral("clientId")+n, c->clientId() ); cg.writeEntry( QStringLiteral("restartCommand")+n, restartCommand ); cg.writePathEntry( QStringLiteral("discardCommand")+n, c->discardCommand() ); cg.writeEntry( QStringLiteral("restartStyleHint")+n, restartHint ); cg.writeEntry( QStringLiteral("userId")+n, c->userId() ); cg.writeEntry( QStringLiteral("wasWm")+n, isWM( c )); } cg.writeEntry( "count", count ); KConfigGroup cg2( config, "General"); - cg2.writeEntry( "screenCount", ScreenCount(QX11Info::display())); storeLegacySession(config.data()); config->sync(); } QStringList KSMServer::sessionList() { QStringList sessions( QStringLiteral( "default" ) ); KSharedConfig::Ptr config = KSharedConfig::openConfig(); const QStringList groups = config->groupList(); for ( QStringList::ConstIterator it = groups.constBegin(); it != groups.constEnd(); ++it ) if ( (*it).startsWith( QLatin1String( "Session: " ) ) ) sessions << (*it).mid( 9 ); return sessions; } bool KSMServer::isWM( const KSMClient* client ) const { return isWM( client->program()); } bool KSMServer::isWM( const QString& program ) const { // Strip possible paths, so that even /usr/bin/kwin is recognized as kwin. QString wmName = wm.mid( wm.lastIndexOf( QDir::separator()) + 1 ); QString programName = program.mid( program.lastIndexOf( QDir::separator()) + 1 ); return programName == wmName; } bool KSMServer::defaultSession() const { return sessionGroup.isEmpty(); } // selection logic: // - $KDEWM is set - use that // - a wm is selected using the kcm - use that // - if that fails, just use KWin void KSMServer::selectWm( const QString& kdewm ) { wm = QStringLiteral( KWIN_BIN ); // defaults wmCommands = ( QStringList() << QStringLiteral( KWIN_BIN ) ); if( !kdewm.isEmpty()) { wmCommands = ( QStringList() << kdewm ); wm = kdewm; return; } KConfigGroup config(KSharedConfig::openConfig(), "General"); QString cfgwm = config.readEntry( "windowManager", "kwin" ); KDesktopFile file( QStandardPaths::AppDataLocation, QStringLiteral( "windowmanagers/" ) + cfgwm + QStringLiteral( ".desktop" ) ); if( file.noDisplay()) return; if( !file.tryExec()) return; QString testexec = file.desktopGroup().readEntry( "X-KDE-WindowManagerTestExec" ); if( !testexec.isEmpty()) { KProcess proc; proc.setShellCommand( testexec ); if( proc.execute() != 0 ) return; } QStringList cfgWmCommands = KShell::splitArgs( file.desktopGroup().readEntry( "Exec" )); if( cfgWmCommands.isEmpty()) return; QString smname = file.desktopGroup().readEntry( "X-KDE-WindowManagerId" ); // ok wm = smname.isEmpty() ? cfgwm : smname; wmCommands = cfgWmCommands; } void KSMServer::wmChanged() { KSharedConfig::openConfig()->reparseConfiguration(); selectWm( QStringLiteral( "" ) ); } void KSMServer::setupShortcuts() { if (KAuthorized::authorize( QStringLiteral( "logout" ))) { KActionCollection* actionCollection = new KActionCollection(this); QAction* a; a = actionCollection->addAction(QStringLiteral("Log Out")); a->setText(i18n("Log Out")); KGlobalAccel::self()->setShortcut(a, QList() << Qt::ALT+Qt::CTRL+Qt::Key_Delete); connect(a, &QAction::triggered, this, &KSMServer::defaultLogout); a = actionCollection->addAction(QStringLiteral("Log Out Without Confirmation")); a->setText(i18n("Log Out Without Confirmation")); KGlobalAccel::self()->setShortcut(a, QList() << Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_Delete); connect(a, &QAction::triggered, this, &KSMServer::logoutWithoutConfirmation); a = actionCollection->addAction(QStringLiteral("Halt Without Confirmation")); a->setText(i18n("Halt Without Confirmation")); KGlobalAccel::self()->setShortcut(a, QList() << Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_PageDown); connect(a, &QAction::triggered, this, &KSMServer::haltWithoutConfirmation); a = actionCollection->addAction(QStringLiteral("Reboot Without Confirmation")); a->setText(i18n("Reboot Without Confirmation")); KGlobalAccel::self()->setShortcut(a, QList() << Qt::ALT+Qt::CTRL+Qt::SHIFT+Qt::Key_PageUp); connect(a, &QAction::triggered, this, &KSMServer::rebootWithoutConfirmation); } } /*! Restores the previous session. Ensures the window manager is running (if specified). */ void KSMServer::restoreSession( const QString &sessionName ) { if( state != Idle ) return; #ifdef KSMSERVER_STARTUP_DEBUG1 t.start(); #endif state = LaunchingWM; qCDebug(KSMSERVER) << "KSMServer::restoreSession " << sessionName; KSharedConfig::Ptr config = KSharedConfig::openConfig(); sessionGroup = QStringLiteral("Session: ") + sessionName; KConfigGroup configSessionGroup( config, sessionGroup); int count = configSessionGroup.readEntry( "count", 0 ); appsToStart = count; // find all commands to launch the wm in the session QList wmStartCommands; if ( !wm.isEmpty() ) { for ( int i = 1; i <= count; i++ ) { QString n = QString::number(i); if ( isWM( configSessionGroup.readEntry( QStringLiteral("program")+n, QString())) ) { wmStartCommands << configSessionGroup.readEntry( QStringLiteral("restartCommand")+n, QStringList() ); } } } if( wmStartCommands.isEmpty()) // otherwise use the configured default wmStartCommands << wmCommands; launchWM( wmStartCommands ); } void KSMServer::launchWM( const QList< QStringList >& wmStartCommands ) { assert( state == LaunchingWM ); if (!(qEnvironmentVariableIsSet("WAYLAND_DISPLAY") || qEnvironmentVariableIsSet("WAYLAND_SOCKET"))) { // when we have a window manager, we start it first and give // it some time before launching other processes. Results in a // visually more appealing startup. wmProcess = startApplication( wmStartCommands[ 0 ], QString(), QString(), true ); connect( wmProcess, SIGNAL(error(QProcess::ProcessError)), SLOT(wmProcessChange())); connect( wmProcess, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(wmProcessChange())); } emit windowManagerLoaded(); } void KSMServer::wmProcessChange() { if( state != LaunchingWM ) { // don't care about the process when not in the wm-launching state anymore wmProcess = nullptr; return; } if( wmProcess->state() == QProcess::NotRunning ) { // wm failed to launch for some reason, go with kwin instead qCWarning(KSMSERVER) << "Window manager" << wm << "failed to launch"; if( wm == QStringLiteral( KWIN_BIN ) ) return; // uhoh, kwin itself failed qCDebug(KSMSERVER) << "Launching KWin"; wm = QStringLiteral( KWIN_BIN ); wmCommands = ( QStringList() << QStringLiteral( KWIN_BIN ) ); // launch it launchWM( QList< QStringList >() << wmCommands ); return; } } /*! Starts the default session. Currently, that's the window manager only (if specified). */ void KSMServer::startDefaultSession() { if( state != Idle ) return; state = LaunchingWM; #ifdef KSMSERVER_STARTUP_DEBUG1 t.start(); #endif sessionGroup = QString(); launchWM( QList< QStringList >() << wmCommands ); } void KSMServer::restoreSubSession( const QString& name ) { sessionGroup = QStringLiteral( "SubSession: " ) + name; KConfigGroup configSessionGroup( KSharedConfig::openConfig(), sessionGroup); int count = configSessionGroup.readEntry( "count", 0 ); appsToStart = count; lastAppStarted = 0; lastIdStarted.clear(); state = RestoringSubSession; tryRestoreNext(); } void KSMServer::clientSetProgram( KSMClient* client ) { if( client->program() == wm ) { emit windowManagerLoaded(); } } void KSMServer::clientRegistered( const char* previousId ) { if ( previousId && lastIdStarted == QString::fromLocal8Bit( previousId ) ) tryRestoreNext(); } void KSMServer::tryRestoreNext() { if( state != Restoring && state != RestoringSubSession ) return; restoreTimer.stop(); KConfigGroup config(KSharedConfig::openConfig(), sessionGroup ); while ( lastAppStarted < appsToStart ) { lastAppStarted++; QString n = QString::number(lastAppStarted); QString clientId = config.readEntry( QStringLiteral("clientId")+n, QString() ); bool alreadyStarted = false; foreach ( KSMClient *c, clients ) { if ( QString::fromLocal8Bit( c->clientId() ) == clientId ) { alreadyStarted = true; break; } } if ( alreadyStarted ) continue; QStringList restartCommand = config.readEntry( QStringLiteral("restartCommand")+n, QStringList() ); if ( restartCommand.isEmpty() || (config.readEntry( QStringLiteral("restartStyleHint")+n, 0 ) == SmRestartNever)) { continue; } if ( isWM( config.readEntry( QStringLiteral("program")+n, QString())) ) continue; // wm already started if( config.readEntry( QStringLiteral( "wasWm" )+n, false )) continue; // it was wm before, but not now, don't run it (some have --replace in command :( ) startApplication( restartCommand, config.readEntry( QStringLiteral("clientMachine")+n, QString() ), config.readEntry( QStringLiteral("userId")+n, QString() )); lastIdStarted = clientId; if ( !lastIdStarted.isEmpty() ) { restoreTimer.setSingleShot( true ); restoreTimer.start( 2000 ); return; // we get called again from the clientRegistered handler } } //all done appsToStart = 0; lastIdStarted.clear(); if (state == Restoring) { emit sessionRestored(); } else { //subsession emit subSessionOpened(); } state = Idle; } void KSMServer::startupDone() { state = Idle; } void KSMServer::defaultLogout() { shutdown(KWorkSpace::ShutdownConfirmYes, KWorkSpace::ShutdownTypeDefault, KWorkSpace::ShutdownModeDefault); } void KSMServer::logoutWithoutConfirmation() { shutdown(KWorkSpace::ShutdownConfirmNo, KWorkSpace::ShutdownTypeNone, KWorkSpace::ShutdownModeDefault); } void KSMServer::haltWithoutConfirmation() { shutdown(KWorkSpace::ShutdownConfirmNo, KWorkSpace::ShutdownTypeHalt, KWorkSpace::ShutdownModeDefault); } void KSMServer::rebootWithoutConfirmation() { shutdown(KWorkSpace::ShutdownConfirmNo, KWorkSpace::ShutdownTypeReboot, KWorkSpace::ShutdownModeDefault); } void KSMServer::openSwitchUserDialog() { //this method exists only for compatibility. Users should ideally call this directly OrgKdeScreensaverInterface iface(QStringLiteral("org.freedesktop.ScreenSaver"), QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus()); iface.SwitchUser(); }