klauncher_main.cpp

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 "kapplication.h"
00026 #include "klauncher.h"
00027 #include "kcmdlineargs.h"
00028 #include "kcrash.h"
00029 #include "kdebug.h"
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <signal.h>
00033 #include <qcstring.h>
00034 #include <klocale.h>
00035 
00036 #include "klauncher_cmds.h"
00037 
00038 static void sig_handler(int sig_num)
00039 {
00040    // No recursion
00041    signal( SIGHUP, SIG_IGN);
00042    signal( SIGTERM, SIG_IGN);
00043 fprintf(stderr, "klauncher: Exiting on signal %d\n", sig_num);
00044    KLauncher::destruct(255);
00045 }
00046 
00047 extern "C" KDE_EXPORT int kdemain( int argc, char**argv )
00048 {
00049    // Started via kdeinit.
00050    if (fcntl(LAUNCHER_FD, F_GETFD) == -1)
00051    {
00052       fprintf(stderr, "%s", i18n("klauncher: This program is not supposed to be started manually.\n"
00053                                  "klauncher: It is started automatically by kdeinit.\n").local8Bit().data());
00054       return 1;
00055    }
00056 
00057    QCString cname = KApplication::launcher();
00058    char *name = cname.data();
00059    KCmdLineArgs::init(argc, argv, name, "KLauncher", "A service launcher.",
00060                        "v1.0");
00061 
00062    KLauncher::addCmdLineOptions();
00063 
00064    // WABA: Make sure not to enable session management.
00065    putenv(strdup("SESSION_MANAGER="));
00066 
00067    // Allow the locale to initialize properly
00068    KLocale::setMainCatalogue("kdelibs");
00069 
00070    int maxTry = 3;
00071    while(true)
00072    {
00073       QCString dcopName = KApplication::dcopClient()->registerAs(name, false);
00074       if (dcopName.isEmpty())
00075       {
00076          kdWarning() << "DCOP communication problem!" << endl;
00077          return 1;
00078       }
00079       if (dcopName == cname)
00080          break; // Good!
00081 
00082       if (--maxTry == 0)
00083       {
00084          kdWarning() << "Another instance of klauncher is already running!" << endl;
00085          return 1;
00086       }
00087       
00088       // Wait a bit...
00089       kdWarning() << "Waiting for already running klauncher to exit." << endl;
00090       sleep(1);
00091 
00092       // Try again...
00093    }
00094    
00095    KLauncher *launcher = new KLauncher(LAUNCHER_FD);
00096    launcher->dcopClient()->setDefaultObject( name );
00097    launcher->dcopClient()->setDaemonMode( true );
00098 
00099    KCrash::setEmergencySaveFunction(sig_handler);
00100    signal( SIGHUP, sig_handler);
00101    signal( SIGPIPE, SIG_IGN);
00102    signal( SIGTERM, sig_handler);
00103 
00104    launcher->exec();
00105    return 0;
00106 }
00107 
KDE Home | KDE Accessibility Home | Description of Access Keys