• Main Page
  • Files
  • File List
  • File Members

/home/developer/harmattan/workspace/applauncherd-3.0.3+0m8/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 #include <malloc.h>
00025 
00026 #include "preload.h"
00027 
00028 typedef int (*entry_t)(int, char **);
00029 
00030 int g_debugPrinting;
00031 
00033 static void loadLibraries(const char * const libs[], unsigned int numLibs)
00034 {
00035     for (unsigned int i = 0; i < numLibs; i++)
00036     {
00037         const char * lib = libs[i];
00038         int len = strlen(lib);
00039         if (lib[0] != '#' && len > 1)
00040         {
00041             int flags = 0;
00042             int skipChar = 0;
00043 
00044             // "now"
00045             if (lib[0] == 'N')
00046             {
00047                 skipChar = 1;
00048                 flags = RTLD_NOW | RTLD_GLOBAL;
00049             }
00050             // "lazy"
00051             else if (lib[0] == 'L')
00052             {
00053                 skipChar = 1;
00054                 flags = RTLD_LAZY | RTLD_GLOBAL;
00055             }
00056             // "deep"
00057             else if (lib[0] == 'D')
00058             {
00059                 skipChar = 1;
00060                 flags = RTLD_DEEPBIND | RTLD_GLOBAL;
00061             }
00062             // "default"
00063             else
00064             {
00065                 skipChar = 0;
00066                 flags =  RTLD_NOW | RTLD_GLOBAL;
00067             }
00068 
00069             // Open the library. Print possible errors only in debug mode.
00070             dlerror();
00071 
00072             // coverity[leaked_storage : FALSE]
00073             if (!dlopen(lib + skipChar, flags) && g_debugPrinting)
00074             {
00075                 fprintf(stderr, "Warning: can't preload %s\n", lib + skipChar);
00076             }
00077         }
00078     }
00079 }
00080 
00089 static int invokeLauncherLib(int argc, char ** argv)
00090 {
00091     // Clear any existing error
00092     dlerror();
00093 
00094     void * handle = dlopen(LAUNCHER_LIBRARY, RTLD_LAZY | RTLD_LOCAL);
00095     if (handle)
00096     {
00097         char * error = NULL;
00098         
00099         // Clear any existing error
00100         dlerror();
00101         
00102         // Find out address of main
00103         entry_t entry = (entry_t)dlsym(handle, "main");
00104 
00105         // Check error        
00106         if ((error = dlerror()) != NULL)
00107         {
00108             fprintf(stderr, "%s\n", error);
00109             dlclose(handle);
00110             return 0;
00111         }
00112 
00113         entry(argc, argv);
00114         dlclose(handle);
00115 
00116         return 1;
00117     }
00118     else
00119     {
00120         fprintf(stderr, "%s\n", dlerror());
00121         return 0;
00122     }
00123     
00124     return 1;
00125 }
00126 
00128 int main(int argc, char ** argv)
00129 {
00130     // Exit if DISPLAY is missing. This would result in dying
00131     // boosters and applauncherd would keep on re-starting them.
00132     if (!getenv("DISPLAY"))
00133     {
00134         fprintf(stderr, "FATAL!!: DISPLAY environment variable not set.\n");
00135         return EXIT_FAILURE;
00136     }
00137 
00138     // Parse command line
00139     g_debugPrinting = 0;
00140 
00141     int helpWanted = 0;
00142     for (int i = 1; i < argc; ++i)
00143     {
00144         if (strcmp(argv[i], "--debug") == 0)
00145             g_debugPrinting = 1;
00146 
00147         if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
00148             helpWanted = 1;
00149     }
00150 
00151     // Set environment. Because applauncherd is usually a privileged
00152     // process, TMPDIR variable might be unset by the C library. In
00153     // case it's not set, we set it to /var/tmp, which usually is not
00154     // a RAM disk.
00155     setenv("TMPDIR", "/var/tmp", 0);
00156 
00157     // Since this application is classified as suid application due to it's credentials
00158     // the environment variables below are not processed by eglibc
00159     // so we're processing this to set correct memory options
00160     char* s = NULL;
00161     if((s = getenv("MALLOC_TRIM_THRESHOLD_")))
00162         mallopt(M_TRIM_THRESHOLD, atoi(s));
00163     if((s = getenv("MALLOC_TOP_PAD_")))
00164         mallopt(M_TOP_PAD, atoi(s));
00165     if((s = getenv("MALLOC_PERTURB_")))
00166         mallopt(M_PERTURB, atoi(s));
00167     if((s = getenv("MALLOC_MMAP_THRESHOLD_")))
00168         mallopt(M_MMAP_THRESHOLD, atoi(s));
00169     if((s = getenv("MALLOC_MMAP_MAX_")))
00170         mallopt(M_MMAP_MAX, atoi(s));
00171 
00172     // Preload libraries
00173     if (!helpWanted)
00174         loadLibraries(gLibs, sizeof(gLibs) / sizeof(char *));
00175 
00176     // Start the real applauncherd.
00177     if (!invokeLauncherLib(argc, argv))
00178     {
00179         fprintf(stderr, "FATAL!!: Failed to load the launcher library\n");
00180         return EXIT_FAILURE;
00181     }
00182 
00183 #ifdef WITH_COVERAGE
00184         __gcov_flush();
00185 #endif
00186    
00187     return EXIT_SUCCESS;
00188 }

Generated on Sat Jan 17 2015 19:23:27 for applauncherd by  doxygen 1.7.1