void VMPI_spyCallback() { if(mmgc_spy_signal) { VMPI_lockAcquire(&lock); if(mmgc_spy_signal) { mmgc_spy_signal = 0; void *pipe = OpenAndConnectToNamedPipe("MMgc_Spy"); spyStream = HandleToStream(pipe); GCAssert(spyStream != NULL); RedirectLogOutput(SpyLog); MMgc::GCHeap::GetGCHeap()->DumpMemoryInfo(); fflush(spyStream); CloseNamedPipe(pipe); RedirectLogOutput(NULL); spyStream = NULL; } VMPI_lockRelease(&lock); } }
void RunDaemonMode(int argc, const char **argv) { if (argc < 2 || argc > 3) { daemonUsage(); } printf("SNAP in daemon mode, waiting for commands to execute\n"); const char *pipeName = argc == 3 ? argv[2] : DEFAULT_NAMED_PIPE_NAME; CommandPipe = OpenNamedPipe(pipeName, true); if (NULL == CommandPipe) { WriteErrorMessage("Unable to open named pipe for command IO.\n"); soft_exit(1); } const size_t commandBufferSize = 10000; // Yes, this is fixed size, no it's not a buffer overflow. The named pipe reader just quits if it's too long. char commandBuffer[commandBufferSize]; // // Format of commands is argc (in ascii) followed by argc arguments, each in one line. // for (;;) { if (!ReadFromNamedPipe(CommandPipe, commandBuffer, commandBufferSize)) { CloseNamedPipe(CommandPipe); CommandPipe = NULL; WriteStatusMessage("Named pipe closed. Exiting\n"); soft_exit_no_print(0); } int argc = atoi(commandBuffer); if (0 == argc) { WriteErrorMessage("Expected argument count on named pipe, got '%s'; ignoring.\n", commandBuffer); } else { char **argv = new char*[argc]; for (int i = 0; i < argc; i++) { argv[i] = new char[commandBufferSize]; if (!ReadFromNamedPipe(CommandPipe, argv[i], commandBufferSize)) { CloseNamedPipe(CommandPipe); CommandPipe = NULL; WriteStatusMessage("Error reading argument #%d from named pipe.\n", i); soft_exit(1); } } // for each arg if (argc > 1 && strcmp(argv[1], "exit") == 0) { WriteStatusMessage("SNAP server exiting by request\n"); WriteToNamedPipe(CommandPipe, CommandExecutedString); soft_exit_no_print(1); } printf("Executing command: "); for (int i = 1; i < argc; i++) { printf("%s ", argv[i]); } printf("\n"); ProcessNonDaemonCommands(argc, (const char **) argv); printf("\n"); for (int i = 0; i < argc; i++) { delete[] argv[i]; argv[i] = NULL; } delete[] argv; argv = NULL; } WriteToNamedPipe(CommandPipe, CommandExecutedString); } }