Modify tizen coding style
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / scd_launcher.cc
index 2959b19..029e93c 100644 (file)
 using namespace std;
 
 
-
 bool isDebugMode = false;
-char *coreclr_gdbjit[PATH_MAX];
-char *root_path;
+char *coreclrGdbjit[PATH_MAX];
+char *rootPath;
 
 bool service_app_create(void *data)
 {
-  // Todo: add your code here.
-  return true;
+       // Todo: add your code here.
+       return true;
 }
 
 void service_app_terminate(void *data)
 {
-  // Todo: add your code here.
+       // Todo: add your code here.
 
-  return;
+       return;
 }
 
-void service_app_control(app_control_h app_control, void *data)
+void service_app_control(app_control_h appControl, void *data)
 {
-  // Get _DEBUG_ value
-  char* buf[82];
-  int ret = app_control_get_extra_data(app_control, "_SCD_DEBUG_", buf);
-
-  if (ret == APP_CONTROL_ERROR_NONE) {
-    if(strcmp(*buf, "1") == 0) {
-      isDebugMode = true;
-    }
-  }
-  if (isDebugMode) {
-    ret = app_control_get_extra_data(app_control, "_SCD_CORECLR_GDBJIT_", coreclr_gdbjit);
-  }
-
-  service_app_exit();
-  return;
+       // Get _DEBUG_ value
+       char* buf[82];
+       int ret = app_control_get_extra_data(appControl, "_SCD_DEBUG_", buf);
+
+       if (ret == APP_CONTROL_ERROR_NONE)
+               if (strcmp(*buf, "1") == 0)
+                       isDebugMode = true;
+
+       if (isDebugMode)
+               ret = app_control_get_extra_data(appControl, "_SCD_CORECLR_GDBJIT_", coreclrGdbjit);
+
+       service_app_exit();
+       return;
 }
 
 
@@ -94,576 +91,490 @@ void service_app_control(app_control_h app_control, void *data)
 
 #ifndef SUCCEEDED
 #define SUCCEEDED(Status) ((Status) >= 0)
-#endif // !SUCCEEDED
+#endif  // !SUCCEEDED
 
-static const char * const coreClrDll = "libcoreclr.so";
+static const char * const __CORECLR_DLL = "libcoreclr.so";
 // Name of the environment variable controlling server GC.
 // If set to 1, server GC is enabled on startup. If 0, server GC is
 // disabled. Server GC is off by default.
-static const char* serverGcVar = "CORECLR_SERVER_GC";
+static const char* __SERVER_GC_VAR = "CORECLR_SERVER_GC";
 #define symlinkEntrypointExecutable "/proc/self/exe"
 
-bool GetEntrypointExecutableAbsolutePath(std::string& entrypointExecutable)
+bool getEntrypointExecutableabsolutePath(std::string& entrypointExecutable)
 {
-  bool result = false;
-
-  entrypointExecutable.clear();
-
-  // Get path to the executable for the current process using
-  // platform specific means.
-
-  // On Linux, fetch the entry point EXE absolute path, inclusive of filename.
-  char exe[PATH_MAX];
-  ssize_t res = readlink(symlinkEntrypointExecutable, exe, PATH_MAX - 1);
-  if (res != -1)
-    {
-      exe[res] = '\0';
-      entrypointExecutable.assign(exe);
-      result = true;
-    }
-  else
-    {
-      result = false;
-    }
-
-  return result;
-}
+       bool result = false;
 
-bool GetAbsolutePath(const char* path, std::string& absolutePath)
-{
-  bool result = false;
+       entrypointExecutable.clear();
 
-  char realPath[PATH_MAX];
-  if (realpath(path, realPath) != nullptr && realPath[0] != '\0')
-    {
-      absolutePath.assign(realPath);
-      // realpath should return canonicalized path without the trailing slash
-      assert(absolutePath.back() != '/');
+       // Get path to the executable for the current process using
+       // platform specific means.
 
-      result = true;
-    }
+       // On Linux, fetch the entry point EXE absolute path, inclusive of filename.
+       char exe[PATH_MAX];
+       ssize_t res = readlink(symlinkEntrypointExecutable, exe, PATH_MAX - 1);
+       if (res != -1) {
+               exe[res] = '\0';
+               entrypointExecutable.assign(exe);
+               result = true;
+       } else {
+               result = false;
+       }
 
-  return result;
+       return result;
 }
 
+bool getabsolutePath(const char* path, std::string& absolutePath)
+{
+       bool result = false;
 
+       char realPath[PATH_MAX];
+       if (realpath(path, realPath) != nullptr && realPath[0] != '\0') {
+               absolutePath.assign(realPath);
+               // realpath should return canonicalized path without the trailing slash
+               assert(absolutePath.back() != '/');
 
-bool GetDirectory(const char* absolutePath, std::string& directory)
-{
-  directory.assign(absolutePath);
-  size_t lastSlash = directory.rfind('/');
-  if (lastSlash != std::string::npos)
-    {
-      directory.erase(lastSlash);
-      return true;
-    }
-
-  return false;
+               result = true;
+       }
+
+       return result;
 }
 
-bool GetClrFilesAbsolutePath(const char* currentExePath, const char* clrFilesPath, std::string& clrFilesAbsolutePath)
+bool getDirectory(const char* absolutePath, std::string& directory)
 {
-  std::string clrFilesRelativePath;
-  const char* clrFilesPathLocal = clrFilesPath;
-  if (clrFilesPathLocal == nullptr)
-    {
-      // There was no CLR files path specified, use the folder of the corerun/coreconsole
-      if (!GetDirectory(currentExePath, clrFilesRelativePath))
-        {
-          perror("Failed to get directory from argv[0]");
-          return false;
-        }
-
-      clrFilesPathLocal = clrFilesRelativePath.c_str();
-
-      // TODO: consider using an env variable (if defined) as a fall-back.
-      // The windows version of the corerun uses core_root env variable
-    }
-
-  if (!GetAbsolutePath(clrFilesPathLocal, clrFilesAbsolutePath))
-    {
-      perror("Failed to convert CLR files path to absolute path");
-      return false;
-    }
-
-  return true;
+       directory.assign(absolutePath);
+       size_t lastSlash = directory.rfind('/');
+       if (lastSlash != std::string::npos) {
+               directory.erase(lastSlash);
+               return true;
+       }
+
+       return false;
 }
 
-void AddFilesFromDirectoryToTpaList(const char* directory, std::string& tpaList)
+bool getClrFilesabsolutePath(const char* currentExePath, const char* clrFilesPath, std::string& clrFilesAbsolutePath)
 {
-  const char * const tpaExtensions[] = {
-    ".ni.dll",      // Probe for .ni.dll first so that it's preferred if ni and il coexist in the same dir
-    ".dll",
-    ".ni.exe",
-    ".exe",
-  };
-
-  DIR* dir = opendir(directory);
-  if (dir == nullptr)
-    {
-      return;
-    }
-
-  std::set<std::string> addedAssemblies;
-
-  // Walk the directory for each extension separately so that we first get files with .ni.dll extension,
-  // then files with .dll extension, etc.
-  for (unsigned int extIndex = 0; extIndex < sizeof(tpaExtensions) / sizeof(tpaExtensions[0]); extIndex++)
-    {
-      const char* ext = tpaExtensions[extIndex];
-      int extLength = strlen(ext);
-
-      struct dirent* entry;
-
-      // For all entries in the directory
-      while ((entry = readdir(dir)) != nullptr)
-        {
-          // We are interested in files only
-          switch (entry->d_type)
-            {
-            case DT_REG:
-              break;
-
-              // Handle symlinks and file systems that do not support d_type
-            case DT_LNK:
-            case DT_UNKNOWN:
-              {
-                std::string fullFilename;
-
-                fullFilename.append(directory);
-                fullFilename.append("/");
-                fullFilename.append(entry->d_name);
-
-                struct stat sb;
-                if (stat(fullFilename.c_str(), &sb) == -1)
-                  {
-                    continue;
-                  }
-
-                if (!S_ISREG(sb.st_mode))
-                  {
-                    continue;
-                  }
-              }
-              break;
-
-            default:
-              continue;
-            }
-
-          std::string filename(entry->d_name);
-
-          // Check if the extension matches the one we are looking for
-          int extPos = filename.length() - extLength;
-          if ((extPos <= 0) || (filename.compare(extPos, extLength, ext) != 0))
-            {
-              continue;
-            }
-
-          std::string filenameWithoutExt(filename.substr(0, extPos));
-
-          // Make sure if we have an assembly with multiple extensions present,
-          // we insert only one version of it.
-          if (addedAssemblies.find(filenameWithoutExt) == addedAssemblies.end())
-            {
-              addedAssemblies.insert(filenameWithoutExt);
-
-              tpaList.append(directory);
-              tpaList.append("/");
-              tpaList.append(filename);
-              tpaList.append(":");
-            }
-        }
-
-      // Rewind the directory stream to be able to iterate over it for the next extension
-      rewinddir(dir);
-    }
-
-  closedir(dir);
+       std::string clrFilesRelativePath;
+       const char* clrFilesPathLocal = clrFilesPath;
+       if (clrFilesPathLocal == nullptr) {
+               // There was no CLR files path specified, use the folder of the corerun/coreconsole
+               if (!getDirectory(currentExePath, clrFilesRelativePath)) {
+                       perror("Failed to get directory from argv[0]");
+                       return false;
+               }
+
+               clrFilesPathLocal = clrFilesRelativePath.c_str();
+
+               // TODO: consider using an env variable (if defined) as a fall-back.
+               // The windows version of the corerun uses core_root env variable
+       }
+
+       if (!getabsolutePath(clrFilesPathLocal, clrFilesAbsolutePath)) {
+               perror("Failed to convert CLR files path to absolute path");
+               return false;
+       }
+
+       return true;
 }
 
+void addFilesFromDirectoryToTpaList(const char* directory, std::string& tpaList)
+{
+       const char * const tpaExtensions[] = {
+               ".ni.dll",  // Probe for .ni.dll first so that it's preferred if ni and il coexist in the same dir
+               ".dll",
+               ".ni.exe",
+               ".exe",
+       };
+
+       DIR* dir = opendir(directory);
+       if (dir == nullptr)
+               return;
+
+       std::set<std::string> addedAssemblies;
+
+       // Walk the directory for each extension separately so that we first get files with .ni.dll extension,
+       // then files with .dll extension, etc.
+       for (unsigned int extIndex = 0; extIndex < sizeof(tpaExtensions) / sizeof(tpaExtensions[0]); extIndex++) {
+               const char* ext = tpaExtensions[extIndex];
+               int extLength = strlen(ext);
+
+               struct dirent* entry;
+
+               // For all entries in the directory
+               while ((entry = readdir(dir)) != nullptr) {
+                       // We are interested in files only
+                       switch (entry->d_type) {
+                               case DT_REG:
+                                       break;
+
+                                       // Handle symlinks and file systems that do not support d_type
+                               case DT_LNK:
+                               case DT_UNKNOWN:
+                               {
+                                       std::string fullFileName;
+
+                                       fullFileName.append(directory);
+                                       fullFileName.append("/");
+                                       fullFileName.append(entry->d_name);
+
+                                       struct stat sb;
+                                       if (stat(fullFileName.c_str(), &sb) == -1)
+                                               continue;
+
+                                       if (!S_ISREG(sb.st_mode))
+                                               continue;
+                               }
+                                       break;
+
+                               default:
+                                       continue;
+                       }
+
+                       std::string fileName(entry->d_name);
+
+                       // Check if the extension matches the one we are looking for
+                       int extPos = fileName.length() - extLength;
+                       if ((extPos <= 0) || (fileName.compare(extPos, extLength, ext) != 0))
+                               continue;
+
+                       std::string fileNameWithoutExt(fileName.substr(0, extPos));
+
+                       // Make sure if we have an assembly with multiple extensions present,
+                       // we insert only one version of it.
+                       if (addedAssemblies.find(fileNameWithoutExt) == addedAssemblies.end()) {
+                               addedAssemblies.insert(fileNameWithoutExt);
+
+                               tpaList.append(directory);
+                               tpaList.append("/");
+                               tpaList.append(fileName);
+                               tpaList.append(":");
+                       }
+               }
+
+               // Rewind the directory stream to be able to iterate over it for the next extension
+               rewinddir(dir);
+       }
+
+       closedir(dir);
+}
 
-
-int ExecuteManagedAssembly(
-                           const char* currentExeAbsolutePath,
-                           const char* clrFilesAbsolutePath,
-                           const char* managedAssemblyAbsolutePath,
-                           int managedAssemblyArgc,
-                           const char** managedAssemblyArgv)
+int executeManagedAssembly(const char* currentExeAbsolutePath,
+                                                                       const char* clrFilesAbsolutePath,
+                                                                       const char* managedAssemblyAbsolutePath,
+                                                                       int managedAssemblyArgc,
+                                                                       const char** managedAssemblyArgv)
 {
-  // Indicates failure
-  int exitCode = -1;
+       // Indicates failure
+       int exitCode = -1;
 
 #ifdef _ARM_
-  // libunwind library is used to unwind stack frame, but libunwind for ARM
-  // does not support ARM vfpv3/NEON registers in DWARF format correctly.
-  // Therefore let's disable stack unwinding using DWARF information
-  // See https://github.com/dotnet/coreclr/issues/6698
-  //
-  // libunwind use following methods to unwind stack frame.
-  // UNW_ARM_METHOD_ALL          0xFF
-  // UNW_ARM_METHOD_DWARF        0x01
-  // UNW_ARM_METHOD_FRAME        0x02
-  // UNW_ARM_METHOD_EXIDX        0x04
-  putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
-#endif // _ARM_
-
-  std::string coreClrDllPath(clrFilesAbsolutePath);
-  coreClrDllPath.append("/");
-  coreClrDllPath.append(coreClrDll);
-
-  if (coreClrDllPath.length() >= PATH_MAX)
-    {
-      fprintf(stderr, "Absolute path to libcoreclr.so too long\n");
-      return -1;
-    }
-
-  // Get just the path component of the managed assembly path
-  std::string appPath;
-  GetDirectory(managedAssemblyAbsolutePath, appPath);
-
-  std::string tpaList;
-  // Construct native search directory paths
-  std::string nativeDllSearchDirs(appPath);
-  char *coreLibraries = getenv("CORE_LIBRARIES");
-  if (coreLibraries)
-    {
-      nativeDllSearchDirs.append(":");
-      nativeDllSearchDirs.append(coreLibraries);
-      if (std::strcmp(coreLibraries, clrFilesAbsolutePath) != 0)
-        {
-          AddFilesFromDirectoryToTpaList(coreLibraries, tpaList);
-        }
-    }
-  nativeDllSearchDirs.append(":");
-  nativeDllSearchDirs.append(clrFilesAbsolutePath);
-
-  AddFilesFromDirectoryToTpaList(clrFilesAbsolutePath, tpaList);
-
-  void* coreclrLib = dlopen(coreClrDllPath.c_str(), RTLD_NOW | RTLD_LOCAL);
-  if (coreclrLib != nullptr)
-    {
-      coreclr_initialize_ptr initializeCoreCLR = (coreclr_initialize_ptr)dlsym(coreclrLib, "coreclr_initialize");
-      coreclr_execute_assembly_ptr executeAssembly = (coreclr_execute_assembly_ptr)dlsym(coreclrLib, "coreclr_execute_assembly");
-      coreclr_shutdown_ptr shutdownCoreCLR = (coreclr_shutdown_ptr)dlsym(coreclrLib, "coreclr_shutdown");
-
-      if (initializeCoreCLR == nullptr)
-        {
-          fprintf(stderr, "Function coreclr_initialize not found in the libcoreclr.so\n");
-        }
-      else if (executeAssembly == nullptr)
-        {
-          fprintf(stderr, "Function coreclr_execute_assembly not found in the libcoreclr.so\n");
-        }
-      else if (shutdownCoreCLR == nullptr)
-        {
-          fprintf(stderr, "Function coreclr_shutdown not found in the libcoreclr.so\n");
-        }
-      else
-        {
-          // Check whether we are enabling server GC (off by default)
-          const char* useServerGc = std::getenv(serverGcVar);
-          if (useServerGc == nullptr)
-            {
-              useServerGc = "0";
-            }
-
-          // CoreCLR expects strings "true" and "false" instead of "1" and "0".
-          useServerGc = std::strcmp(useServerGc, "1") == 0 ? "true" : "false";
-
-          // Allowed property names:
-          // APPBASE
-          // - The base path of the application from which the exe and other assemblies will be loaded
-          //
-          // TRUSTED_PLATFORM_ASSEMBLIES
-          // - The list of complete paths to each of the fully trusted assemblies
-          //
-          // APP_PATHS
-          // - The list of paths which will be probed by the assembly loader
-          //
-          // APP_NI_PATHS
-          // - The list of additional paths that the assembly loader will probe for ngen images
-          //
-          // NATIVE_DLL_SEARCH_DIRECTORIES
-          // - The list of paths that will be probed for native DLLs called by PInvoke
-          //
-          const char *propertyKeys[] = {
-            "TRUSTED_PLATFORM_ASSEMBLIES",
-            "APP_PATHS",
-            "APP_NI_PATHS",
-            "NATIVE_DLL_SEARCH_DIRECTORIES",
-            "System.GC.Server",
-          };
-          const char *propertyValues[] = {
-            // TRUSTED_PLATFORM_ASSEMBLIES
-            tpaList.c_str(),
-            // APP_PATHS
-            appPath.c_str(),
-            // APP_NI_PATHS
-            appPath.c_str(),
-            // NATIVE_DLL_SEARCH_DIRECTORIES
-            nativeDllSearchDirs.c_str(),
-            // System.GC.Server
-            useServerGc,
-          };
-
-          void* hostHandle;
-          unsigned int domainId;
-
-          int st = initializeCoreCLR(
-                                     currentExeAbsolutePath,
-                                     "unixcorerun",
-                                     sizeof(propertyKeys) / sizeof(propertyKeys[0]),
-                                     propertyKeys,
-                                     propertyValues,
-                                     &hostHandle,
-                                     &domainId);
-
-          if (!SUCCEEDED(st))
-            {
-              fprintf(stderr, "coreclr_initialize failed - status: 0x%08x\n", st);
-              exitCode = -1;
-            }
-          else
-            {
-              st = executeAssembly(
-                                   hostHandle,
-                                   domainId,
-                                   managedAssemblyArgc,
-                                   managedAssemblyArgv,
-                                   managedAssemblyAbsolutePath,
-                                   (unsigned int*)&exitCode);
-
-              if (!SUCCEEDED(st))
-                {
-                  fprintf(stderr, "coreclr_execute_assembly failed - status: 0x%08x\n", st);
-                  exitCode = -1;
-                }
-
-              st = shutdownCoreCLR(hostHandle, domainId);
-              if (!SUCCEEDED(st))
-                {
-                  fprintf(stderr, "coreclr_shutdown failed - status: 0x%08x\n", st);
-                  exitCode = -1;
-                }
-            }
-        }
-
-      if (dlclose(coreclrLib) != 0)
-        {
-          fprintf(stderr, "Warning - dlclose failed\n");
-        }
-    }
-  else
-    {
-      const char* error = dlerror();
-      fprintf(stderr, "dlopen failed to open the libcoreclr.so with error %s\n", error);
-    }
-
-  return exitCode;
+       // libunwind library is used to unwind stack frame, but libunwind for ARM
+       // does not support ARM vfpv3/NEON registers in DWARF format correctly.
+       // Therefore let's disable stack unwinding using DWARF information
+       // See https://github.com/dotnet/coreclr/issues/6698
+       //
+       // libunwind use following methods to unwind stack frame.
+       // UNW_ARM_METHOD_ALL                   0xFF
+       // UNW_ARM_METHOD_DWARF                 0x01
+       // UNW_ARM_METHOD_FRAME                 0x02
+       // UNW_ARM_METHOD_EXIDX                 0x04
+       putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
+#endif  // _ARM_
+
+       std::string coreClrDllPath(clrFilesAbsolutePath);
+       coreClrDllPath.append("/");
+       coreClrDllPath.append(__CORECLR_DLL);
+
+       if (coreClrDllPath.length() >= PATH_MAX) {
+               fprintf(stderr, "Absolute path to libcoreclr.so too long\n");
+               return -1;
+       }
+
+       // Get just the path component of the managed assembly path
+       std::string appPath;
+       getDirectory(managedAssemblyAbsolutePath, appPath);
+
+       std::string tpaList;
+       // Construct native search directory paths
+       std::string nativeDllSearchDirs(appPath);
+       char *coreLibraries = getenv("CORE_LIBRARIES");
+       if (coreLibraries) {
+               nativeDllSearchDirs.append(":");
+               nativeDllSearchDirs.append(coreLibraries);
+               if (std::strcmp(coreLibraries, clrFilesAbsolutePath) != 0)
+                       addFilesFromDirectoryToTpaList(coreLibraries, tpaList);
+       }
+       nativeDllSearchDirs.append(":");
+       nativeDllSearchDirs.append(clrFilesAbsolutePath);
+
+       addFilesFromDirectoryToTpaList(clrFilesAbsolutePath, tpaList);
+
+       void* __coreclrLib = dlopen(coreClrDllPath.c_str(), RTLD_NOW | RTLD_LOCAL);
+       if (__coreclrLib != nullptr) {
+               coreclr_initialize_ptr initializeCoreCLR = (coreclr_initialize_ptr)dlsym(__coreclrLib, "coreclr_initialize");
+               coreclr_execute_assembly_ptr executeAssembly = (coreclr_execute_assembly_ptr)dlsym(__coreclrLib, "coreclr_execute_assembly");
+               coreclr_shutdown_ptr shutdownCoreCLR = (coreclr_shutdown_ptr)dlsym(__coreclrLib, "coreclr_shutdown");
+
+               if (initializeCoreCLR == nullptr) {
+                       fprintf(stderr, "Function coreclr_initialize not found in the libcoreclr.so\n");
+               } else if (executeAssembly == nullptr) {
+                       fprintf(stderr, "Function coreclr_execute_assembly not found in the libcoreclr.so\n");
+               } else if (shutdownCoreCLR == nullptr) {
+                       fprintf(stderr, "Function coreclr_shutdown not found in the libcoreclr.so\n");
+               } else {
+                       // Check whether we are enabling server GC (off by default)
+                       const char* useServerGc = std::getenv(__SERVER_GC_VAR);
+                       if (useServerGc == nullptr)
+                               useServerGc = "0";
+
+                       // CoreCLR expects strings "true" and "false" instead of "1" and "0".
+                       useServerGc = std::strcmp(useServerGc, "1") == 0 ? "true" : "false";
+
+                       // Allowed property names:
+                       // APPBASE
+                       // - The base path of the application from which the exe and other assemblies will be loaded
+                       //
+                       // TRUSTED_PLATFORM_ASSEMBLIES
+                       // - The list of complete paths to each of the fully trusted assemblies
+                       //
+                       // APP_PATHS
+                       // - The list of paths which will be probed by the assembly loader
+                       //
+                       // APP_NI_PATHS
+                       // - The list of additional paths that the assembly loader will probe for ngen images
+                       //
+                       // NATIVE_DLL_SEARCH_DIRECTORIES
+                       // - The list of paths that will be probed for native DLLs called by PInvoke
+                       //
+                       const char *propertyKeys[] = {
+                               "TRUSTED_PLATFORM_ASSEMBLIES",
+                               "APP_PATHS",
+                               "APP_NI_PATHS",
+                               "NATIVE_DLL_SEARCH_DIRECTORIES",
+                               "System.GC.Server",
+                       };
+                       const char *propertyValues[] = {
+                               // TRUSTED_PLATFORM_ASSEMBLIES
+                               tpaList.c_str(),
+                               // APP_PATHS
+                               appPath.c_str(),
+                               // APP_NI_PATHS
+                               appPath.c_str(),
+                               // NATIVE_DLL_SEARCH_DIRECTORIES
+                               nativeDllSearchDirs.c_str(),
+                               // System.GC.Server
+                               useServerGc,
+                       };
+
+                       void* hostHandle;
+                       unsigned int domainId;
+
+                       int st = initializeCoreCLR(currentExeAbsolutePath,
+                                                                        "unixcorerun",
+                                                                        sizeof(propertyKeys) / sizeof(propertyKeys[0]),
+                                                                        propertyKeys,
+                                                                        propertyValues,
+                                                                        &hostHandle,
+                                                                        &domainId);
+
+                       if (!SUCCEEDED(st)) {
+                               fprintf(stderr, "coreclr_initialize failed - status: 0x%08x\n", st);
+                               exitCode = -1;
+                       } else {
+                               st = executeAssembly(hostHandle,
+                                                                        domainId,
+                                                                        managedAssemblyArgc,
+                                                                        managedAssemblyArgv,
+                                                                        managedAssemblyAbsolutePath,
+                                                                        (unsigned int*)&exitCode);
+
+                               if (!SUCCEEDED(st)) {
+                                       fprintf(stderr, "coreclr_execute_assembly failed - status: 0x%08x\n", st);
+                                       exitCode = -1;
+                               }
+
+                               st = shutdownCoreCLR(hostHandle, domainId);
+                               if (!SUCCEEDED(st)) {
+                                       fprintf(stderr, "coreclr_shutdown failed - status: 0x%08x\n", st);
+                                       exitCode = -1;
+                               }
+                       }
+               }
+
+               if (dlclose(__coreclrLib) != 0) {
+                       fprintf(stderr, "Warning - dlclose failed\n");
+               }
+       } else {
+               const char* error = dlerror();
+               fprintf(stderr, "dlopen failed to open the libcoreclr.so with error %s\n", error);
+       }
+
+       return exitCode;
 }
 
 
 // Display the help text
-void DisplayUsage()
+void displayUsage()
 {
-  fprintf(
-          stderr,
-          "Runs executables on CoreCLR\n\n"
-          "Usage: <program> [OPTIONS] [ARGUMENTS]\n"
-          "Runs <program>.dll on CoreCLR.\n\n"
-          "Options:\n"
-          "-_c  path to libcoreclr.so and the managed CLR assemblies.\n"
-          "-_h  show this help message. \n");
+       fprintf(stderr,
+                       "Runs executables on CoreCLR\n\n"
+                       "Usage: <program> [OPTIONS] [ARGUMENTS]\n"
+                       "Runs <program>.dll on CoreCLR.\n\n"
+                       "Options:\n"
+                       "-_c    path to libcoreclr.so and the managed CLR assemblies.\n"
+                       "-_h    show this help message. \n");
 }
 
 // Parse the command line arguments
-bool ParseArguments(
-                    const int argc,
-                    const char* argv[],
-                    const char** clrFilesPath,
-                    int* managedAssemblyArgc,
-                    const char*** managedAssemblyArgv)
+bool parseArguments(const int argc,
+                                               const char* argv[],
+                                               const char** clrFilesPath,
+                                               int* managedAssemblyArgc,
+                                               const char*** managedAssemblyArgv)
 {
-  bool success = true;
-
-  *clrFilesPath = nullptr;
-  *managedAssemblyArgv = nullptr;
-  *managedAssemblyArgc = 0;
-
-  for (int i = 1; i < argc; i++)
-    {
-      // Check for options. Options to the Unix coreconsole are prefixed with '-_' to match the convention
-      // used in the Windows version of coreconsole.
-      if (strncmp(argv[i], "-_", 2) == 0)
-        {
-          // Path to the libcoreclr.so and the managed CLR assemblies
-          if (strcmp(argv[i], "-_c") == 0)
-            {
-              i++;
-              if (i < argc)
-                {
-                  *clrFilesPath = argv[i];
-                }
-              else
-                {
-                  fprintf(stderr, "Option %s: missing path\n", argv[i - 1]);
-                  success = false;
-                  break;
-                }
-            }
-          else if (strcmp(argv[i], "-_h") == 0)
-            {
-              DisplayUsage();
-              success = false;
-              break;
-            }
-          else
-            {
-              fprintf(stderr, "Unknown option %s\n", argv[i]);
-              success = false;
-              break;
-            }
-        }
-      else
-        {
-          // We treat everything starting from the first non-option argument as arguments
-          // to the managed assembly.
-          *managedAssemblyArgc = argc - i;
-          if (*managedAssemblyArgc != 0)
-            {
-              *managedAssemblyArgv = &argv[i];
-            }
-
-          break;
-        }
-    }
-
-  return success;
+       bool success = true;
+
+       *clrFilesPath = nullptr;
+       *managedAssemblyArgv = nullptr;
+       *managedAssemblyArgc = 0;
+
+       for (int i = 1; i < argc; i++) {
+               // Check for options. Options to the Unix coreconsole are prefixed with '-_' to match the convention
+               // used in the Windows version of coreconsole.
+               if (strncmp(argv[i], "-_", 2) == 0) {
+                       // Path to the libcoreclr.so and the managed CLR assemblies
+                       if (strcmp(argv[i], "-_c") == 0) {
+                               i++;
+                               if (i < argc) {
+                                       *clrFilesPath = argv[i];
+                               } else {
+                                       fprintf(stderr, "Option %s: missing path\n", argv[i - 1]);
+                                       success = false;
+                                       break;
+                               }
+                       } else if (strcmp(argv[i], "-_h") == 0) {
+                               displayUsage();
+                               success = false;
+                               break;
+                       } else {
+                               fprintf(stderr, "Unknown option %s\n", argv[i]);
+                               success = false;
+                               break;
+                       }
+               } else {
+                       // We treat everything starting from the first non-option argument as arguments
+                       // to the managed assembly.
+                       *managedAssemblyArgc = argc - i;
+                       if (*managedAssemblyArgc != 0)
+                               *managedAssemblyArgv = &argv[i];
+
+                       break;
+               }
+       }
+
+       return success;
 }
 
 int main(const int argc, const char* argv[])
 {
-  /// service_app_begin
-  char *root_path = getenv("AUL_ROOT_PATH");
-  char *second_pass = getenv("SECONDPASS");
-
-  // This routine check whether _SCD_DEBUG_ flag is set(1) or not.
-  // In second pass, this routine is skipped.
-  if (second_pass == NULL) {
-    // run service_app routine to extract _SCD_DEBUG_
-    char ad[50] = {0,};
-    service_app_lifecycle_callback_s event_callback;
-    event_callback.create = service_app_create;
-    event_callback.terminate = service_app_terminate;
-    event_callback.app_control = service_app_control;
-    // FIXME: casting of argv is safe ?
-    service_app_main(argc, (char**)argv, &event_callback, ad);
-
-    char buf[PATH_MAX];
-    vector<const char*> vargs;
-    int status = 0;
-
-    if (isDebugMode) {
-      dlog_print(DLOG_INFO,"dotnet","debugmode on\n");
-      setenv("CORECLR_GDBJIT", *coreclr_gdbjit, 1);
-
-      string cur_path(getenv("PATH"));
-      string new_path("/home/owner/share/tmp/sdk_tools/lldb/bin:");
-      new_path.append(cur_path);
-      setenv("PATH", new_path.c_str(), 1);
-
-      vargs.push_back("/home/owner/share/tmp/sdk_tools/lldb/bin/lldb-server");
-      vargs.push_back("g");
-      vargs.push_back("--platform=host");
-      vargs.push_back("*:1234");
-      vargs.push_back("--");
-    }
-    snprintf(buf, sizeof(buf), "%s/bin/%s", root_path, basename(root_path));
-    vargs.push_back(buf);
-
-    // Pass app argument to lldb-server as it is
-    for (int i=1; i<argc; i++) {
-      vargs.push_back(argv[i]);
-    }
-
-    const char** new_args = &vargs[0];
-    setenv("SECONDPASS", "1", 1);
-    status = execvp(new_args[0], (char *const *)new_args);
-    if (status == -1) {
-      dlog_print(DLOG_ERROR,"dotnet","execvp error");
-    }
-    dlog_print(DLOG_INFO,"dotnet","something wrong errno: %d\n",errno);
-  }
-  /// service_app_end
-
-
-
-  // Make sure we have a full path for argv[0].
-  std::string argv0AbsolutePath;
-  std::string entryPointExecutablePath;
-
-  if (!GetEntrypointExecutableAbsolutePath(entryPointExecutablePath))
-    {
-      perror("Could not get full path to current executable");
-      return -1;
-    }
-
-  if (!GetAbsolutePath(entryPointExecutablePath.c_str(), argv0AbsolutePath))
-    {
-      perror("Could not normalize full path to current executable");
-      return -1;
-    }
-
-  // We will try to load the managed assembly with the same name as this executable
-  // but with the .dll extension.
-  std::string programPath(argv0AbsolutePath);
-  programPath.append(".dll");
-  const char* managedAssemblyAbsolutePath = programPath.c_str();
-
-  // Check if the specified managed assembly file exists
-  struct stat sb;
-  if (stat(managedAssemblyAbsolutePath, &sb) == -1)
-    {
-      perror("Managed assembly not found");
-      return -1;
-    }
-
-  // Verify that the managed assembly path points to a file
-  if (!S_ISREG(sb.st_mode))
-    {
-      fprintf(stderr, "The specified managed assembly is not a file\n");
-      return -1;
-    }
-
-  const char* clrFilesPath;
-  const char** managedAssemblyArgv;
-  int managedAssemblyArgc;
-
-  if (!ParseArguments(
-                      argc,
-                      argv,
-                      &clrFilesPath,
-                      &managedAssemblyArgc,
-                      &managedAssemblyArgv
-                      ))
-    {
-      // Invalid command line
-      return -1;
-    }
-
-  std::string clrFilesAbsolutePath;
-  if(!GetClrFilesAbsolutePath(argv0AbsolutePath.c_str(), clrFilesPath, clrFilesAbsolutePath))
-    {
-      return -1;
-    }
-
-  int exitCode = ExecuteManagedAssembly(
-                                        argv0AbsolutePath.c_str(),
-                                        clrFilesAbsolutePath.c_str(),
-                                        managedAssemblyAbsolutePath,
-                                        managedAssemblyArgc,
-                                        managedAssemblyArgv);
-
-  return exitCode;
+       /// service_app_begin
+       char *rootPath = getenv("AUL_ROOT_PATH");
+       char *secondPass = getenv("SECONDPASS");
+
+       // This routine check whether _SCD_DEBUG_ flag is set(1) or not.
+       // In second pass, this routine is skipped.
+       if (secondPass == NULL) {
+               // run service_app routine to extract _SCD_DEBUG_
+               char ad[50] = {0, };
+               service_app_lifecycle_callback_s eventCallback;
+               eventCallback.create = service_app_create;
+               eventCallback.terminate = service_app_terminate;
+               eventCallback.app_control = service_app_control;
+               // FIXME: casting of argv is safe ?
+               service_app_main(argc, (char**)argv, &eventCallback, ad);
+
+               char buf[PATH_MAX];
+               vector<const char*> vargs;
+               int status = 0;
+
+               if (isDebugMode) {
+                       dlog_print(DLOG_INFO, "dotnet", "debugmode on\n");
+                       setenv("CORECLR_GDBJIT", *coreclrGdbjit, 1);
+
+                       string curPath(getenv("PATH"));
+                       string newPath("/home/owner/share/tmp/sdk_tools/lldb/bin:");
+                       newPath.append(curPath);
+                       setenv("PATH", newPath.c_str(), 1);
+
+                       vargs.push_back("/home/owner/share/tmp/sdk_tools/lldb/bin/lldb-server");
+                       vargs.push_back("g");
+                       vargs.push_back("--platform=host");
+                       vargs.push_back("*:1234");
+                       vargs.push_back("--");
+               }
+               snprintf(buf, sizeof(buf), "%s/bin/%s", rootPath, basename(rootPath));
+               vargs.push_back(buf);
+
+               // Pass app argument to lldb-server as it is
+               for (int i = 1; i < argc; i++)
+                       vargs.push_back(argv[i]);
+
+               const char** newArgs = &vargs[0];
+               setenv("SECONDPASS", "1", 1);
+               status = execvp(newArgs[0], (char *const *)newArgs);
+               if (status == -1)
+                       dlog_print(DLOG_ERROR, "dotnet", "execvp error");
+               dlog_print(DLOG_INFO, "dotnet", "something wrong errno: %d\n", errno);
+       }
+       /// service_app_end
+
+       // Make sure we have a full path for argv[0].
+       std::string argv0AbsolutePath;
+       std::string entryPointExecutablePath;
+
+       if (!getEntrypointExecutableabsolutePath(entryPointExecutablePath)) {
+               perror("Could not get full path to current executable");
+               return -1;
+       }
+
+       if (!getabsolutePath(entryPointExecutablePath.c_str(), argv0AbsolutePath)) {
+               perror("Could not normalize full path to current executable");
+               return -1;
+       }
+
+       // We will try to load the managed assembly with the same name as this executable
+       // but with the .dll extension.
+       std::string programPath(argv0AbsolutePath);
+       programPath.append(".dll");
+       const char* managedAssemblyAbsolutePath = programPath.c_str();
+
+       // Check if the specified managed assembly file exists
+       struct stat sb;
+       if (stat(managedAssemblyAbsolutePath, &sb) == -1) {
+               perror("Managed assembly not found");
+               return -1;
+       }
+
+       // Verify that the managed assembly path points to a file
+       if (!S_ISREG(sb.st_mode)) {
+               fprintf(stderr, "The specified managed assembly is not a file\n");
+               return -1;
+       }
+
+       const char* clrFilesPath;
+       const char** managedAssemblyArgv;
+       int managedAssemblyArgc;
+       if (!parseArguments(argc, argv, &clrFilesPath, &managedAssemblyArgc, &managedAssemblyArgv))
+               return -1;
+
+       std::string clrFilesAbsolutePath;
+       if (!getClrFilesabsolutePath(argv0AbsolutePath.c_str(), clrFilesPath, clrFilesAbsolutePath))
+               return -1;
+
+       int exitCode = executeManagedAssembly(argv0AbsolutePath.c_str(),
+                                                                                       clrFilesAbsolutePath.c_str(),
+                                                                                       managedAssemblyAbsolutePath,
+                                                                                       managedAssemblyArgc,
+                                                                                       managedAssemblyArgv);
+
+       return exitCode;
 }