KInit
klauncher_main.cpp
Go to the documentation of this file.
00001 /* 00002 This file is part of the KDE libraries 00003 Copyright (c) 1999 Waldo Bastian <bastian@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <config.h> 00021 00022 #include <unistd.h> 00023 #include <fcntl.h> 00024 00025 #include "klauncher.h" 00026 #include <kcomponentdata.h> 00027 #include "kcrash.h" 00028 #include "kdebug.h" 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <signal.h> 00032 #include <klocale.h> 00033 #include <kde_file.h> 00034 00035 #include "klauncher_cmds.h" 00036 #include <QtCore/QCoreApplication> 00037 00038 #ifndef USE_KPROCESS_FOR_KIOSLAVES 00039 static int sigpipe[ 2 ]; 00040 static void sig_handler(int sig_num) 00041 { 00042 // No recursion 00043 KDE_signal( SIGHUP, SIG_IGN); 00044 KDE_signal( SIGTERM, SIG_IGN); 00045 fprintf(stderr, "klauncher: Exiting on signal %d\n", sig_num); 00046 char tmp = 'x'; 00047 write( sigpipe[ 1 ], &tmp, 1 ); 00048 } 00049 #endif 00050 00051 #if defined(Q_OS_DARWIN) || defined (Q_OS_MAC) 00052 #include <kkernel_mac.h> 00053 #endif 00054 00055 extern "C" KDE_EXPORT int kdemain( int argc, char**argv ) 00056 { 00057 #ifndef Q_WS_WIN 00058 // Started via kdeinit. 00059 int launcherFd; 00060 if (argc != 2 || memcmp(argv[1], "--fd=", 5) || !(launcherFd = atoi(argv[1] + 5))) 00061 { 00062 fprintf(stderr, "%s", i18n("klauncher: This program is not supposed to be started manually.\n" 00063 "klauncher: It is started automatically by kdeinit4.\n").toLocal8Bit().data()); 00064 return 1; 00065 } 00066 #endif 00067 00068 #if defined(Q_OS_DARWIN) || defined (Q_OS_MAC) 00069 mac_initialize_dbus(); 00070 #endif 00071 00072 KComponentData componentData("klauncher", "kdelibs4"); 00073 KGlobal::locale(); 00074 00075 // WABA: Make sure not to enable session management. 00076 putenv(strdup("SESSION_MANAGER=")); 00077 00078 // We need a QCoreApplication to get a DBus event loop 00079 QCoreApplication app(argc, argv); 00080 app.setApplicationName( componentData.componentName() ); 00081 00082 int maxTry = 3; 00083 while(true) 00084 { 00085 QString service(QLatin1String("org.kde.klauncher")); // same as ktoolinvocation.cpp 00086 if (!QDBusConnection::sessionBus().isConnected()) { 00087 kWarning() << "No DBUS session-bus found. Check if you have started the DBUS server."; 00088 return 1; 00089 } 00090 QDBusReply<QDBusConnectionInterface::RegisterServiceReply> reply = 00091 QDBusConnection::sessionBus().interface()->registerService(service); 00092 if (!reply.isValid()) 00093 { 00094 kWarning() << "DBUS communication problem!"; 00095 return 1; 00096 } 00097 if (reply == QDBusConnectionInterface::ServiceRegistered) 00098 break; 00099 00100 if (--maxTry == 0) 00101 { 00102 kWarning() << "Another instance of klauncher is already running!"; 00103 return 1; 00104 } 00105 00106 // Wait a bit... 00107 kWarning() << "Waiting for already running klauncher to exit."; 00108 sleep(1); 00109 00110 // Try again... 00111 } 00112 00113 #ifndef USE_KPROCESS_FOR_KIOSLAVES 00114 KLauncher *launcher = new KLauncher(launcherFd); 00115 #else 00116 KLauncher *launcher = new KLauncher(); 00117 #endif 00118 QDBusConnection::sessionBus().registerObject(QString::fromLatin1("/"), launcher); 00119 00120 #ifndef USE_KPROCESS_FOR_KIOSLAVES 00121 if (pipe(sigpipe) != 0) { 00122 perror("klauncher: pipe failed."); 00123 return 1; 00124 } 00125 QSocketNotifier* signotif = new QSocketNotifier( sigpipe[ 0 ], QSocketNotifier::Read, launcher ); 00126 QObject::connect( signotif, SIGNAL( activated( int )), launcher, SLOT( destruct())); 00127 KCrash::setEmergencySaveFunction(sig_handler); 00128 KDE_signal( SIGHUP, sig_handler); 00129 KDE_signal( SIGPIPE, SIG_IGN); 00130 KDE_signal( SIGTERM, sig_handler); 00131 #endif 00132 00133 return app.exec(); 00134 }
KDE 4.6 API Reference