• Main Page
  • Files
  • File List
  • File Members

/home/bifh6/cs2009q3-i386/work/applauncherd-0.30.5+rq730927+0m6/src/launcher/launcher.c

Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (directui@nokia.com)
00006 **
00007 ** This file is part of applauncherd
00008 **
00009 ** If you have questions regarding the use of this file, please contact
00010 ** Nokia at directui@nokia.com.
00011 **
00012 ** This library is free software; you can redistribute it and/or
00013 ** modify it under the terms of the GNU Lesser General Public
00014 ** License version 2.1 as published by the Free Software Foundation
00015 ** and appearing in the file LICENSE.LGPL included in the packaging
00016 ** of this file.
00017 **
00018 ****************************************************************************/
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <dlfcn.h>
00023 #include <string.h>
00024 
00025 #include "preload.h"
00026 
00027 typedef int (*entry_t)(int, char **);
00028 
00029 int g_debugPrinting;
00030 
00032 static void loadLibraries(const char * const libs[], unsigned int numLibs)
00033 {
00034     for (unsigned int i = 0; i < numLibs; i++)
00035     {
00036         const char * lib = libs[i];
00037         int len = strlen(lib);
00038         if (lib[0] != '#' && len > 1)
00039         {
00040             int flags = 0;
00041             int skipChar = 0;
00042 
00043             // "now"
00044             if (lib[0] == 'N')
00045             {
00046                 skipChar = 1;
00047                 flags = RTLD_NOW | RTLD_GLOBAL;
00048             }
00049             // "lazy"
00050             else if (lib[0] == 'L')
00051             {
00052                 skipChar = 1;
00053                 flags = RTLD_LAZY | RTLD_GLOBAL;
00054             }
00055             // "deep"
00056             else if (lib[0] == 'D')
00057             {
00058                 skipChar = 1;
00059                 flags = RTLD_DEEPBIND | RTLD_GLOBAL;
00060             }
00061             // "default"
00062             else
00063             {
00064                 skipChar = 0;
00065                 flags =  RTLD_NOW | RTLD_GLOBAL;
00066             }
00067 
00068             // Open the library. Print possible errors only in debug mode.
00069             dlerror();
00070 
00071             // coverity[leaked_storage : FALSE]
00072             if (!dlopen(lib + skipChar, flags) && g_debugPrinting)
00073             {
00074                 fprintf(stderr, "Warning: can't preload %s\n", lib + skipChar);
00075             }
00076         }
00077     }
00078 }
00079 
00088 static int invokeLauncherLib(int argc, char ** argv)
00089 {
00090     // Clear any existing error
00091     dlerror();
00092 
00093     void * handle = dlopen(LAUNCHER_LIBRARY, RTLD_LAZY | RTLD_LOCAL);
00094     if (handle)
00095     {
00096         char * error = NULL;
00097         
00098         // Clear any existing error
00099         dlerror();
00100         
00101         // Find out address of main
00102         entry_t entry = (entry_t)dlsym(handle, "main");
00103 
00104         // Check error        
00105         if ((error = dlerror()) != NULL)
00106         {
00107             fprintf(stderr, "%s\n", error);
00108             dlclose(handle);
00109             return 0;
00110         }
00111 
00112         entry(argc, argv);
00113         dlclose(handle);
00114 
00115         return 1;
00116     }
00117     else
00118     {
00119         fprintf(stderr, "%s\n", dlerror());
00120         return 0;
00121     }
00122     
00123     return 1;
00124 }
00125 
00127 int main(int argc, char ** argv)
00128 {
00129     // Exit if DISPLAY is missing. This would result in dying
00130     // boosters and applauncherd would keep on re-starting them.
00131     if (!getenv("DISPLAY"))
00132     {
00133         fprintf(stderr, "FATAL!!: DISPLAY environment variable not set.\n");
00134         return EXIT_FAILURE;
00135     }
00136 
00137     // Parse command line
00138     g_debugPrinting = 0;
00139 
00140     int helpWanted = 0;
00141     for (int i = 1; i < argc; ++i)
00142     {
00143         if (strcmp(argv[i], "--debug") == 0)
00144             g_debugPrinting = 1;
00145 
00146         if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
00147             helpWanted = 1;
00148     }
00149 
00150     // Set environment. Because applauncherd is usually a privileged
00151     // process, TMPDIR variable might be unset by the C library. In
00152     // case it's not set, we set it to /var/tmp, which usually is not
00153     // a RAM disk.
00154     setenv("TMPDIR", "/var/tmp", 0);
00155 
00156     // Preload libraries
00157     if (!helpWanted)
00158         loadLibraries(gLibs, sizeof(gLibs) / sizeof(char *));
00159 
00160     // Start the real applauncherd.
00161     if (!invokeLauncherLib(argc, argv))
00162     {
00163         fprintf(stderr, "FATAL!!: Failed to load the launcher library\n");
00164         return EXIT_FAILURE;
00165     }
00166    
00167     return EXIT_SUCCESS;
00168 }

Generated on Mon Jul 4 2011 14:23:01 for applauncherd by  doxygen 1.7.1