KInit
klauncher_main.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 Q_WS_WIN
00039 static int sigpipe[ 2 ];
00040 static void sig_handler(int sig_num)
00041 {
00042
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 extern "C" KDE_EXPORT int kdemain( int argc, char**argv )
00052 {
00053 #ifndef Q_WS_WIN
00054
00055 int launcherFd;
00056 if (argc != 2 || memcmp(argv[1], "--fd=", 5) || !(launcherFd = atoi(argv[1] + 5)))
00057 {
00058 fprintf(stderr, "%s", i18n("klauncher: This program is not supposed to be started manually.\n"
00059 "klauncher: It is started automatically by kdeinit4.\n").toLocal8Bit().data());
00060 return 1;
00061 }
00062 #endif
00063
00064 KComponentData componentData("klauncher", "kdelibs4");
00065 KGlobal::locale();
00066
00067
00068 putenv(strdup("SESSION_MANAGER="));
00069
00070
00071 QCoreApplication app(argc, argv);
00072 app.setApplicationName( componentData.componentName() );
00073
00074 int maxTry = 3;
00075 while(true)
00076 {
00077 QString service(QLatin1String("org.kde.klauncher"));
00078 if (!QDBusConnection::sessionBus().isConnected()) {
00079 kWarning() << "No DBUS session-bus found. Check if you have started the DBUS server.";
00080 return 1;
00081 }
00082 QDBusReply<QDBusConnectionInterface::RegisterServiceReply> reply =
00083 QDBusConnection::sessionBus().interface()->registerService(service);
00084 if (!reply.isValid())
00085 {
00086 kWarning() << "DBUS communication problem!";
00087 return 1;
00088 }
00089 if (reply == QDBusConnectionInterface::ServiceRegistered)
00090 break;
00091
00092 if (--maxTry == 0)
00093 {
00094 kWarning() << "Another instance of klauncher is already running!";
00095 return 1;
00096 }
00097
00098
00099 kWarning() << "Waiting for already running klauncher to exit.";
00100 sleep(1);
00101
00102
00103 }
00104
00105 #ifndef Q_WS_WIN
00106 KLauncher *launcher = new KLauncher(launcherFd);
00107 #else
00108 KLauncher *launcher = new KLauncher();
00109 #endif
00110 QDBusConnection::sessionBus().registerObject(QString::fromLatin1("/"), launcher);
00111
00112 #ifndef Q_WS_WIN
00113 if (pipe(sigpipe) != 0) {
00114 perror("klauncher: pipe failed.");
00115 return 1;
00116 }
00117 QSocketNotifier* signotif = new QSocketNotifier( sigpipe[ 0 ], QSocketNotifier::Read, launcher );
00118 QObject::connect( signotif, SIGNAL( activated( int )), launcher, SLOT( destruct()));
00119 KCrash::setEmergencySaveFunction(sig_handler);
00120 KDE_signal( SIGHUP, sig_handler);
00121 KDE_signal( SIGPIPE, SIG_IGN);
00122 KDE_signal( SIGTERM, sig_handler);
00123 #endif
00124
00125 return app.exec();
00126 }