void AppsPageComponent::startOrFocusApp(AppIconButton* appButton) { if (debounce) return; bool shouldStart = true; int appIdx = runningAppsByButton[appButton]; bool hasLaunched = runningApps[appIdx] != nullptr; String windowId; if(hasLaunched) { const auto shellWords = split(appButton->shell, " "); const auto& cmdName = shellWords[0]; StringArray findCmd{"xdotool", "search", "--all", "--limit", "1", "--class", cmdName.toRawUTF8()}; ChildProcess findWindow; findWindow.start(findCmd); findWindow.waitForProcessToFinish(1000); windowId = findWindow.readAllProcessOutput().trimEnd(); // does xdotool find a window id? if so, we shouldn't start a new one shouldStart = (windowId.length() > 0) ? false : true; } if (shouldStart) { startApp(appButton); } else { focusApp(appButton, windowId); } };
void finish (bool shouldKill) { String result; Array<URL> selection; if (shouldKill) child.kill(); else result = child.readAllProcessOutput().trim(); if (result.isNotEmpty()) { StringArray tokens; if (selectMultipleFiles) tokens.addTokens (result, separator, "\""); else tokens.add (result); for (auto& token : tokens) selection.add (URL (File::getCurrentWorkingDirectory().getChildFile (token))); } if (! shouldKill) { child.waitForProcessToFinish (60 * 1000); owner.finished (selection); } }
void PowerPageComponent::buttonClicked(Button *button) { if (button == backButton) { getMainStack().popPage(PageStackComponent::kTransitionTranslateHorizontalLeft); } else if (button == powerOffButton) { showPowerSpinner(); child.start("systemctl poweroff"); } else if (button == rebootButton) { showPowerSpinner(); child.start("systemctl reboot"); } else if (button == sleepButton) { setSleep(); } else if (button == felButton) { getMainStack().pushPage(felPage, PageStackComponent::kTransitionTranslateHorizontalLeft); } else if(button == updateButton){ updateWindow->setVisible(true); resized(); //Downloading rev number information StringArray cmd{"wget", "-O", "version", "https://drive.google.com/uc?export=download&id=0B1jRc4IqT9kiNC12WVpoUUtCRUE"}; ChildProcess download; bool ok = download.start(cmd, ChildProcess::StreamFlags::wantStdErr); if(!ok) printf("Process not launched\n"); else printf("Process launched !\n"); String output = download.readAllProcessOutput(); updateWindow->setMessage("Download successful !"); } }
int main(int argc, char* argv[]) { LDEBUG("address of notification procedure entry: %p", ChildThread::processNotification) // send a notification to let the parent know where the notification instruction is ChildProcess::notifyParent(ParentNotification_Startup, (long) ChildThread::processNotification); // start the child process ChildProcess child; int ret; try { ret = child.main(argc, argv); } catch (Exception &e) { fprintf(stderr, "Exception occurred: %s\n", e.get_message().c_str()); e.print(2); exit(1); } catch (Exception *e) { fprintf(stderr, "Exception occurred: %s\n", e->get_message().c_str()); e->print(2); exit(1); } catch (string &s) { fprintf(stderr, "Exception occurred: %s\n", s.c_str()); exit(1); } catch (const char* str) { fprintf(stderr, "Exception occurred: %s\n", str); exit(1); } catch (...) { fprintf(stderr, "Exception occurred \n"); exit(1); } LDEBUG("Exit value: %i", ret) exit(ret); }
void AppsPageComponent::focusApp(AppIconButton* appButton, const String& windowId) { DBG("AppsPageComponent::focusApp - " << appButton->shell); String focusShell = "echo 'focus_client_by_window_id("+windowId+")' | awesome-client"; StringArray focusCmd{"sh", "-c", focusShell.toRawUTF8()}; ChildProcess focusWindow; focusWindow.start(focusCmd); };
bool FileChooser::isPlatformDialogAvailable() { ChildProcess child; const bool ok = child.start ("which zenity") && child.readAllProcessOutput().trim().isNotEmpty(); child.waitForProcessToFinish (60 * 1000); return ok; }
static bool exeIsAvailable (const char* const executable) { ChildProcess child; const bool ok = child.start ("which " + String (executable)) && child.readAllProcessOutput().trim().isNotEmpty(); child.waitForProcessToFinish (60 * 1000); return ok; }
void jojo_doBang (t_jojo *x, t_symbol *s, long argc, t_atom *argv) { ChildProcess process; process.start ("ls /tmp"); /* All the job (fork, execv) is done there. */ /* IPC implemented with a pipe. */ StringArray processResult (StringArray::fromLines (process.readAllProcessOutput())); for (int i = 0; i < processResult.size(); ++i) { post ("%s", processResult.getReference (i).toRawUTF8()); } }
static void reap_child(GPid pid, gint status, gpointer data) { ChildProcess *cp = data; if (cp->callback) { cp->callback(status, cp->data); } g_free(cp); clean_pid(); wm->mode = GNT_KP_MODE_NORMAL; endwin(); setup_io(); refresh(); refresh_screen(); }
bool runLameChildProcess (const TemporaryFile& tempMP3, const StringArray& processArgs) const { ChildProcess cp; if (cp.start (processArgs)) { const String childOutput (cp.readAllProcessOutput()); DBG (childOutput); ignoreUnused (childOutput); cp.waitForProcessToFinish (10000); return tempMP3.getFile().getSize() > 0; } return false; }
void UnityProjectBuilder::saveProject (const File& introjucerAppFile) { if (! (introjucerAppFile.exists() || unityProjectFile.existsAsFile())) return; logOutput("Resaving Introjucer project..."); StringArray args; args.add (getExeFromApp (introjucerAppFile).getFullPathName()); args.add ("--resave"); args.add (unityProjectFile.getFullPathName()); ChildProcess introjucerProcess; introjucerProcess.start (args); logOutput (introjucerProcess.readAllProcessOutput()); }
void timerCallback() override { if (! child.isRunning()) { stopTimer(); finish (false); } }
void runTest() { beginTest ("Child Processes"); #if JUCE_WINDOWS || JUCE_MAC || JUCE_LINUX ChildProcess p; #if JUCE_WINDOWS expect (p.start ("tasklist")); #else expect (p.start ("ls /")); #endif //String output (p.readAllProcessOutput()); //expect (output.isNotEmpty()); #endif }
void FileChooser::showPlatformDialog (Array<File>& results, const String& title, const File& file, const String& filters, bool isDirectory, bool /* selectsFiles */, bool isSave, bool /* warnAboutOverwritingExistingFiles */, bool selectMultipleFiles, FilePreviewComponent*) { const File previousWorkingDirectory (File::getCurrentWorkingDirectory()); StringArray args; String separator; // use kdialog for KDE sessions or if zenity is missing if (exeIsAvailable ("kdialog") && (isKdeFullSession() || ! exeIsAvailable ("zenity"))) addKDialogArgs (args, separator, title, file, filters, isDirectory, isSave, selectMultipleFiles); else addZenityArgs (args, separator, title, file, filters, isDirectory, isSave, selectMultipleFiles); args.add ("2>/dev/null"); // (to avoid logging info ending up in the results) ChildProcess child; if (child.start (args, ChildProcess::wantStdOut)) { const String result (child.readAllProcessOutput().trim()); if (result.isNotEmpty()) { StringArray tokens; if (selectMultipleFiles) tokens.addTokens (result, separator, "\""); else tokens.add (result); for (int i = 0; i < tokens.size(); ++i) results.add (File::getCurrentWorkingDirectory().getChildFile (tokens[i])); } child.waitForProcessToFinish (60 * 1000); } previousWorkingDirectory.setAsCurrentWorkingDirectory(); }
void FileChooser::showPlatformDialog (Array<File>& results, const String& title, const File& file, const String& filters, bool isDirectory, bool selectsFiles, bool isSave, bool warnAboutOverwritingExistingFiles, bool selectMultipleFiles, FilePreviewComponent* previewComponent) { const String separator (":"); String command ("zenity --file-selection"); if (title.isNotEmpty()) command << " --title=\"" << title << "\""; if (file != File::nonexistent) command << " --filename=\"" << file.getFullPathName () << "\""; if (isDirectory) command << " --directory"; if (isSave) command << " --save"; if (selectMultipleFiles) command << " --multiple --separator=" << separator; command << " 2>&1"; ChildProcess child; if (child.start (command)) { const String result (child.readAllProcessOutput().trim()); if (result.isNotEmpty()) { StringArray tokens; if (selectMultipleFiles) tokens.addTokens (result, separator, "\""); else tokens.add (result); for (int i = 0; i < tokens.size(); i++) results.add (File (tokens[i])); } child.waitForProcessToFinish (60 * 1000); } }
void FileChooser::showPlatformDialog (Array<File>& results, const String& title, const File& file, const String& /* filters */, bool isDirectory, bool /* selectsFiles */, bool isSave, bool /* warnAboutOverwritingExistingFiles */, bool selectMultipleFiles, FilePreviewComponent* /* previewComponent */) { String separator; StringArray args; const File previousWorkingDirectory (File::getCurrentWorkingDirectory()); const bool isKdeFullSession = SystemStats::getEnvironmentVariable ("KDE_FULL_SESSION", String::empty) .equalsIgnoreCase ("true"); if (exeIsAvailable ("kdialog") && (isKdeFullSession || ! exeIsAvailable ("zenity"))) { // use kdialog for KDE sessions or if zenity is missing args.add ("kdialog"); if (title.isNotEmpty()) args.add ("--title=" + title); if (selectMultipleFiles) { separator = "\n"; args.add ("--multiple"); args.add ("--separate-output"); args.add ("--getopenfilename"); } else { if (isSave) args.add ("--getsavefilename"); else if (isDirectory) args.add ("--getexistingdirectory"); else args.add ("--getopenfilename"); } String startPath; if (file.exists() || file.getParentDirectory().exists()) { startPath = file.getFullPathName(); } else { startPath = File::getSpecialLocation (File::userHomeDirectory).getFullPathName(); if (isSave) startPath += "/" + file.getFileName(); } args.add (startPath); } else { // zenity args.add ("zenity"); args.add ("--file-selection"); if (title.isNotEmpty()) args.add ("--title=" + title); if (selectMultipleFiles) { separator = ":"; args.add ("--multiple"); args.add ("--separator=" + separator); } else { if (isDirectory) args.add ("--directory"); if (isSave) args.add ("--save"); } if (file.isDirectory()) file.setAsCurrentWorkingDirectory(); else if (file.getParentDirectory().exists()) file.getParentDirectory().setAsCurrentWorkingDirectory(); else File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory(); if (! file.getFileName().isEmpty()) args.add ("--filename=" + file.getFileName()); } ChildProcess child; if (child.start (args)) { const String result (child.readAllProcessOutput().trim()); if (result.isNotEmpty()) { StringArray tokens; if (selectMultipleFiles) tokens.addTokens (result, separator, "\""); else tokens.add (result); for (int i = 0; i < tokens.size(); i++) results.add (File (tokens[i])); } child.waitForProcessToFinish (60 * 1000); } previousWorkingDirectory.setAsCurrentWorkingDirectory(); }
DWORD WINAPI ChildProcess::ListenerThreadStatic(LPVOID pVoidThis) { ChildProcess* pThis = static_cast<ChildProcess*>(pVoidThis); return pThis->ListenerThread(); }
//======================================================= // toggle manuals //======================================================= void CodeWindow::toggleManuals(String manual) { if(!showingHelp) if(manual=="Csound") { String helpDir; if(manual=="Csound") helpDir = appProperties->getUserSettings()->getValue("CsoundHelpDir", ""); if(helpDir.length()<2) CabbageUtils::showMessage("Please set the Csound manual directory in the Preference menu", &getLookAndFeel()); else{ CodeDocument::Position pos1, pos2; pos1 = textEditor->getDocument().findWordBreakBefore(textEditor->getCaretPos()); pos2 = textEditor->getDocument().findWordBreakAfter(textEditor->getCaretPos()); String opcode = csoundDoc.getTextBetween(pos1, pos2); URL urlCsound(helpDir+"/"+opcode.trim()+String(".html")); String urlCabbage; ChildProcess process; File temp1(urlCsound.toString(false)); if(temp1.exists()){ #ifdef LINUX if(!process.start("xdg-open "+urlCsound.toString(false).toUTF8())) CabbageUtils::showMessage("couldn't show file", &getLookAndFeel()); #else htmlHelp->goToURL(urlCsound.toString(false)); showingHelp = true; setContentNonOwned(nullptr, false); setContentNonOwned(htmlHelp, false); #endif } } } else{ String path; #if defined(LINUX) || defined(MACOSX) path = File::getSpecialLocation(File::currentExecutableFile).getParentDirectory().getFullPathName()+"/Docs/cabbage.html"; #else path = File::getSpecialLocation(File::currentExecutableFile).getParentDirectory().getFullPathName()+"\\Docs\\cabbage.html"; #endif if(!File(path).existsAsFile()) CabbageUtils::showMessage( "Could not find Cabbage manual. Make sure\n\ it is located in the Docs folder in the same\n\ directory as the main Cabbage executable", &getLookAndFeel()); else{ ChildProcess process; File temp1(path); if(temp1.exists()){ #ifdef LINUX if(!process.start("xdg-open "+path)) CabbageUtils::showMessage("couldn't show file", &getLookAndFeel()); #else htmlHelp->goToURL(path); showingHelp = true; setContentNonOwned(nullptr, false); setContentNonOwned(htmlHelp, false); #endif } } } else{
/// Function called when the child (finally) exits... FSTATIC void _childprocess_childexit(GPid pid, gint status, gpointer childprocess_object) { ChildProcess* self = CASTTOCLASS(ChildProcess, childprocess_object); gboolean signalled = WIFSIGNALED(status); int exitrc = 0; int signal = 0; gboolean logexit = FALSE; enum HowDied howwedied = NOT_EXITED; (void)pid; if (self->timeoutsrc_id > 0) { g_source_remove(self->timeoutsrc_id); DEBUGMSG3("%s.%d: Removed timeout %d for process with user_data = %p" , __FUNCTION__, __LINE__, self->timeoutsrc_id, self); self->timeoutsrc_id = 0; } // If it refused to die, then the status is invalid if ((guint)(self->child_state) >= DIMOF(signalmap)) { howwedied = EXITED_HUNG; }else if ((guint)self->child_state != CHILDSTATE_RUNNING) { // Then we tried to kill it... howwedied = EXITED_TIMEOUT; signal = signalled ? WTERMSIG(status) : 0; }else{ if (signalled) { signal = WTERMSIG(status); howwedied = EXITED_SIGNAL; }else{ exitrc = WEXITSTATUS(status); howwedied = (exitrc == 0 ? EXITED_ZERO : EXITED_NONZERO); } } switch (howwedied) { case EXITED_SIGNAL: /*FALLTHROUGH*/ case EXITED_TIMEOUT: /*FALLTHROUGH*/ case EXITED_HUNG: logexit = self->logmode > CHILD_NOLOG; break; case EXITED_NONZERO: logexit = self->logmode >= CHILD_LOGERRS; break; case EXITED_ZERO: logexit = self->logmode >= CHILD_LOGALL; break; default: // We'll never produce any other values above /*NOTREACHED*/ logexit = TRUE; break; } if (logexit) { switch (howwedied) { case EXITED_SIGNAL: g_warning("Child process [%s] died from signal %d%s." , self->loggingname, signal, WCOREDUMP(status) ? " (core dumped)" : ""); break; case EXITED_TIMEOUT: if (signalled) { g_warning("Child process [%s] timed out after %d seconds [signal %d%s]." , self->loggingname, self->timeout, signal , WCOREDUMP(status) ? " (core dumped)" : ""); }else{ g_warning("Child process [%s] timed out after %d seconds." , self->loggingname, self->timeout); } break; case EXITED_HUNG: g_warning("Child process [%s] timed out after %d seconds and could not be killed." , self->loggingname, self->timeout); break; case EXITED_NONZERO: g_message("Child process [%s] exited with return code %d." , self->loggingname, exitrc); break; case EXITED_ZERO: g_message("Child process [%s] exited normally.", self->loggingname); break; default:/*NOTREACHED*/ break; } } DEBUGMSG2("%s.%d: Exit happened howwedied:%d", __FUNCTION__, __LINE__ , howwedied); if (!self->stdout_src->atEOF) { //DEBUGMSG3("Child %d [%s] EXITED but output is not at EOF [fd%d]", pid //, self->loggingname, self->stdout_src->gfd.fd); self->stdout_src->readmore(self->stdout_src); } if (!self->stderr_src->baseclass.atEOF) { self->stderr_src->baseclass.readmore(&self->stderr_src->baseclass); } self->notify(self, howwedied, exitrc, signal, WCOREDUMP(status)); self->child_state = -1; DEBUGMSG5("%s.%d: UNREF child: %p", __FUNCTION__,__LINE__, self); UNREF(self); // Undo the REF(self) in our constructor }