static char * FileGetTmpDir(Bool useConf) // IN: Use the config file? { char *dirName; char *edirName; /* Make several attempts to find a good temporary directory candidate */ if (useConf) { dirName = (char *)LocalConfig_GetString(NULL, "tmpDirectory"); edirName = FileTryDir(dirName); free(dirName); if (edirName != NULL) { return edirName; } } /* Posix_Getenv string must _not_ be freed */ edirName = FileTryDir(Posix_Getenv("TMPDIR")); if (edirName != NULL) { return edirName; } /* P_tmpdir is usually defined in <stdio.h> */ edirName = FileTryDir(P_tmpdir); if (edirName != NULL) { return edirName; } edirName = FileTryDir("/tmp"); if (edirName != NULL) { return edirName; } edirName = FileTryDir("~"); if (edirName != NULL) { return edirName; } dirName = File_Cwd(NULL); if (dirName != NULL) { edirName = FileTryDir(dirName); free(dirName); if (edirName != NULL) { return edirName; } } edirName = FileTryDir("/"); if (edirName != NULL) { return edirName; } Warning("%s: Couldn't get a temporary directory\n", __FUNCTION__); return NULL; }
int main(int argc, char *argv[], const char *envp[]) { int i; int ret = EXIT_FAILURE; char **argvCopy; GSource *src; Unicode_Init(argc, &argv, NULL); /* * ToolsCore_ParseCommandLine() uses g_option_context_parse(), which modifies * argv. We don't want that to happen, so we make a copy of the array and * use that as the argument instead. */ argvCopy = g_malloc(argc * sizeof *argvCopy); for (i = 0; i < argc; i++) { argvCopy[i] = argv[i]; } setlocale(LC_ALL, ""); VMTools_ConfigLogging(G_LOG_DOMAIN, NULL, FALSE, FALSE); VMTools_BindTextDomain(VMW_TEXT_DOMAIN, NULL, NULL); if (!ToolsCore_ParseCommandLine(&gState, argc, argvCopy)) { g_free(argvCopy); goto exit; } g_free(argvCopy); argvCopy = NULL; if (gState.pidFile != NULL) { /* * If argv[0] is not an absolute path, make it so; all other path * arguments should have been given as absolute paths if '--background' * was used, or things may not work as expected. */ if (!g_path_is_absolute(argv[0])) { gchar *abs = g_find_program_in_path(argv[0]); if (abs == NULL || strcmp(abs, argv[0]) == 0) { char *cwd = File_Cwd(NULL); g_free(abs); abs = g_strdup_printf("%s%c%s", cwd, DIRSEPC, argv[0]); vm_free(cwd); } argv[0] = abs; } /* * Need to remove --background from the command line or we'll get * into an infinite loop. ToolsCore_ParseCommandLine() already * validated that "-b" has an argument, so it's safe to assume the * data is there. */ for (i = 1; i < argc; i++) { size_t count = 0; if (strcmp(argv[i], "--background") == 0 || strcmp(argv[i], "-b") == 0) { count = 2; } else if (g_str_has_prefix(argv[i], "--background=")) { count = 1; } if (count) { memmove(argv + i, argv + i + count, (argc - i - count) * sizeof *argv); argv[argc - count] = NULL; break; } } if (!Hostinfo_Daemonize(argv[0], argv, HOSTINFO_DAEMONIZE_LOCKPID, gState.pidFile, NULL, 0)) { goto exit; } return 0; } ToolsCore_Setup(&gState); src = VMTools_NewSignalSource(SIGHUP); VMTOOLSAPP_ATTACH_SOURCE(&gState.ctx, src, ToolsCoreSigHUPCb, &gState, NULL); g_source_unref(src); src = VMTools_NewSignalSource(SIGINT); VMTOOLSAPP_ATTACH_SOURCE(&gState.ctx, src, ToolsCoreSigHandler, gState.ctx.mainLoop, NULL); g_source_unref(src); src = VMTools_NewSignalSource(SIGQUIT); VMTOOLSAPP_ATTACH_SOURCE(&gState.ctx, src, ToolsCoreSigHandler, gState.ctx.mainLoop, NULL); g_source_unref(src); src = VMTools_NewSignalSource(SIGTERM); VMTOOLSAPP_ATTACH_SOURCE(&gState.ctx, src, ToolsCoreSigHandler, gState.ctx.mainLoop, NULL); g_source_unref(src); src = VMTools_NewSignalSource(SIGUSR1); VMTOOLSAPP_ATTACH_SOURCE(&gState.ctx, src, ToolsCoreSigUsrHandler, NULL, NULL); g_source_unref(src); /* Ignore SIGUSR2 by default. */ signal(SIGUSR2, SIG_IGN); /* * Save the original environment so that we can safely spawn other * applications (since we may have to modify the original environment * to launch vmtoolsd successfully). */ gState.ctx.envp = System_GetNativeEnviron(envp); ret = ToolsCore_Run(&gState); if (gState.pidFile != NULL) { g_unlink(gState.pidFile); } exit: return ret; }