コード例 #1
0
ファイル: tagscanner.cpp プロジェクト: gt945/gede
int TagScanner::scan(QString filepath, QList<Tag> *taglist)
{
    if(!m_ctagsExist)
        return 0;

    QString etagsCmd;
    etagsCmd = ETAGS_ARGS;
    etagsCmd += " ";
    etagsCmd += filepath;
    QString name = ETAGS_CMD;
    QStringList argList;
    argList = etagsCmd.split(' ',  QString::SkipEmptyParts);

    QByteArray stdoutContent;
    QByteArray stderrContent;
    int rc = execProgram(ETAGS_CMD, argList,
                            &stdoutContent,
                            &stderrContent);

    parseOutput(stdoutContent, taglist);

    // Display stderr
    QString all = stderrContent;
    if(!all.isEmpty())
    {
        QStringList outputList = all.split('\n', QString::SkipEmptyParts);
        for(int r = 0;r < outputList.size();r++)
        {
            errorMsg("%s", stringToCStr(outputList[r]));
        } 
    }

    return rc;
}
コード例 #2
0
static void
spawnProcessor(const char *  const progName,
               const char ** const argArray,
               int           const stdinFd,
               int *         const stdoutFdP,
               pid_t *       const pidP) {
/*----------------------------------------------------------------------------
   Create a process to run program 'progName' with arguments
   argArray[] (terminated by NULL element).  Pass file descriptor
   'stdinFd' to the shell as Standard Input.

   if 'stdoutFdP' is NULL, have that process write its Standard Output to
   the current process' Standard Output.

   If 'stdoutFdP' is non-NULL, set up a pipe and pass it to the new
   process as Standard Output.  Return as *stdoutFdP the file
   descriptor of the other end of that pipe, from which Caller can
   suck the program's Standard Output.
-----------------------------------------------------------------------------*/
    bool const pipeStdout = !!stdoutFdP;

    int stdoutpipe[2];
    pid_t rc;

    if (pipeStdout)
        pm_pipe(stdoutpipe);

    rc = fork();
    if (rc < 0) {
        pm_error("fork() of processor process failed.  errno=%d (%s)", 
                 errno, strerror(errno));
    } else if (rc == 0) {
        /* The program child */

        int stdoutFd;
        
        if (pipeStdout) {
            close(stdoutpipe[0]);
            stdoutFd = stdoutpipe[1];
        } else
            stdoutFd = STDOUT;

        closeUninheritableFds(stdinFd, stdoutFd);

        execProgram(progName, argArray, stdinFd, stdoutFd);

        close(stdinFd);
        close(stdoutpipe[1]);
        pm_error("INTERNAL ERROR: execProgram() returns.");
    } else {
        /* The parent */
        pid_t const processorpid = rc;

        if (pipeStdout) {
            close(stdoutpipe[1]);
            *stdoutFdP = stdoutpipe[0];
        }
        *pidP = processorpid;
    }
}
コード例 #3
0
ファイル: tagscanner.cpp プロジェクト: gt945/gede
void TagScanner::init()
{

    // Check if ctags exists?
    QStringList argList;
    argList.push_back("--version");
    QByteArray stdoutContent;
    int n = execProgram(ETAGS_CMD, argList, &stdoutContent, NULL);
    QStringList outputList = QString(stdoutContent).split('\n');
    for(int u = 0;u < outputList.size();u++)
    {
        debugMsg("ETAGS: %s", stringToCStr(outputList[u]));
    }
    if(n)
    {
        QString msg;

        msg.sprintf("Failed to start program '%s'\n", ETAGS_CMD);
        msg += "ctags can be installed on ubuntu/debian using command:\n";
        msg +  "\n";
        msg += " apt-get install exuberant-ctags";

        QMessageBox::warning(NULL,
                    "Failed to start ctags",
                    msg);
        m_ctagsExist = false;
    }
    else
        m_ctagsExist = true;
}
コード例 #4
0
ファイル: Process.cpp プロジェクト: sconger/libge
void Process::execProgram(const String programName,
                          const List<String>& args,
                          bool mergeOutput)
{
    List<String> env(0);
    execProgram(programName, args, env, mergeOutput);
}
コード例 #5
0
ファイル: engine.c プロジェクト: vrowland/midiSwitcher
void wipeBank( void )
{
	// clear config
	state.config = 0x000;
	execProgram();

	// write programs
	uint8_t bank = ( (state.program+1) / 10 ) * 10;
	uint8_t i;
	for (i=0; i<10; i++)
	{
		state.program = bank + i;
		storeProgram();
	}
}
コード例 #6
0
ファイル: libsystem.c プロジェクト: moseymosey/netpbm
static void
spawnProcessor(const char * const shellCommand, 
               int          const stdinFd,
               int *        const stdoutFdP,
               pid_t *      const pidP) {
/*----------------------------------------------------------------------------
   Create a process to run a shell that runs command 'shellCommand'.
   Pass file descriptor 'stdinFd' to the shell as Standard Input.
   Set up a pipe and pass it to the shell as Standard Output.  Return
   as *stdoutFdP the file descriptor of the other end of that pipe,
   from which Caller can suck the shell's Standard Output.
-----------------------------------------------------------------------------*/
    int stdoutpipe[2];
    pid_t processorpid;
        
    pipe(stdoutpipe);

    processorpid = fork();
    if (processorpid < 0) {
        pm_error("fork() of processor process failed.  errno=%d (%s)", 
                 errno, strerror(errno));
    } else if (processorpid == 0) {
        /* The second child */
        close(stdoutpipe[0]);

        execProgram(shellCommand, stdinFd, stdoutpipe[1]);

        close(stdinFd);
        close(stdoutpipe[1]);
        pm_error("INTERNAL ERROR: execProgram() returns.");
    } else {
        /* The parent */
        close(stdoutpipe[1]);
        *stdoutFdP = stdoutpipe[0];
        *pidP = processorpid;
    }
}
コード例 #7
0
ファイル: smallShell.c プロジェクト: robintillman/OP_L2
/* smallShell
 *
 * smallShell innehåller logiken för shellets funktionalitet som definierat
 * i laborationsbeskrivningen.
 *
 */
void
smallShell()
{
    char arg[70] = ""; /* Hanterare för argument */
    signal(SIGINT, signalHandler); /* Hantera CTRL-C */

    int isBackground;
    while(1) /* Evighetsloop då programmet endast ska avlutas vid "exit" */    
    {
        printf("==>> "); /* Skriv ut prompttecken */
        
        /* Rensa zombieprocesser */
		bgChildProcesses = zombieFilter(bgChildProcesses, &terminationCheck);
        
        fgets(arg, 70, stdin); /* Hämta argument från stdin till arg */
        char **args = splitList(arg, 0); /* Dela in indatat i lista */

        char *command = args[0]; /* Det första argumentet är kommandot */
        if(command != NULL)
        {
            /* Kolla om inbyggt kommando */
            if(!strcmp(command, "exit")) {
                free(args);
                return;
            } else if(!strcmp(command, "cd")) {
                if(chdir(args[1])) {
                    chdir(getenv("HOME"));
                }
            } else { /* Icke inbyggt kommando */
				execProgram(args);
            }
        }
        
        free(args);
    } 
}
コード例 #8
0
ファイル: engine.c プロジェクト: vrowland/midiSwitcher
void wipeProgram( void )
{
	state.config = 0x000;
	execProgram();
	storeProgram();
}
コード例 #9
0
ファイル: AgentWatcher.cpp プロジェクト: jlund/passenger
    /**
     * Starts the agent process. May throw arbitrary exceptions.
     */
    virtual pid_t start() {
        this_thread::disable_interruption di;
        this_thread::disable_syscall_interruption dsi;
        string exeFilename = getExeFilename();
        SocketPair fds;
        int e, ret;
        pid_t pid;

        /* Create feedback fd for this agent process. We'll send some startup
         * arguments to this agent process through this fd, and we'll receive
         * startup information through it as well.
         */
        fds = createUnixSocketPair();

        pid = syscalls::fork();
        if (pid == 0) {
            // Child

            /* Make sure file descriptor FEEDBACK_FD refers to the newly created
             * feedback fd (fds[1]) and close all other file descriptors.
             * In this child process we don't care about the original FEEDBACK_FD
             * (which is the Watchdog's communication channel to the agents starter.)
             *
             * fds[1] is guaranteed to be != FEEDBACK_FD because the watchdog
             * is started with FEEDBACK_FD already assigned.
             */
            syscalls::close(fds[0]);

            if (syscalls::dup2(fds[1], FEEDBACK_FD) == -1) {
                /* Something went wrong, report error through feedback fd. */
                e = errno;
                try {
                    writeArrayMessage(fds[1],
                                      "system error before exec",
                                      "dup2() failed",
                                      toString(e).c_str(),
                                      NULL);
                    _exit(1);
                } catch (...) {
                    fprintf(stderr, "Passenger Watchdog: dup2() failed: %s (%d)\n",
                            strerror(e), e);
                    fflush(stderr);
                    _exit(1);
                }
            }

            closeAllFileDescriptors(FEEDBACK_FD);

            /* Become the process group leader so that the watchdog can kill the
             * agent as well as all its descendant processes. */
            setpgid(getpid(), getpid());

            setOomScore(oldOomScore);

            try {
                execProgram();
            } catch (...) {
                fprintf(stderr, "PassengerWatchdog: execProgram() threw an exception\n");
                fflush(stderr);
                _exit(1);
            }
            e = errno;
            try {
                writeArrayMessage(FEEDBACK_FD,
                                  "exec error",
                                  toString(e).c_str(),
                                  NULL);
            } catch (...) {
                fprintf(stderr, "Passenger Watchdog: could not execute %s: %s (%d)\n",
                        exeFilename.c_str(), strerror(e), e);
                fflush(stderr);
            }
            _exit(1);
        } else if (pid == -1) {
            // Error
            e = errno;
            throw SystemException("Cannot fork a new process", e);
        } else {
            // Parent
            FileDescriptor feedbackFd = fds[0];
            vector<string> args;

            fds[1].close();
            this_thread::restore_interruption ri(di);
            this_thread::restore_syscall_interruption rsi(dsi);
            ScopeGuard failGuard(boost::bind(killAndWait, pid));

            /* Send startup arguments. Ignore EPIPE and ECONNRESET here
             * because the child process might have sent an feedback message
             * without reading startup arguments.
             */
            try {
                sendStartupArguments(pid, feedbackFd);
            } catch (const SystemException &ex) {
                if (ex.code() != EPIPE && ex.code() != ECONNRESET) {
                    throw SystemException(string("Unable to start the ") + name() +
                                          ": an error occurred while sending startup arguments",
                                          ex.code());
                }
            }

            // Now read its feedback.
            try {
                ret = readArrayMessage(feedbackFd, args);
            } catch (const SystemException &e) {
                if (e.code() == ECONNRESET) {
                    ret = false;
                } else {
                    throw SystemException(string("Unable to start the ") + name() +
                                          ": unable to read its startup information",
                                          e.code());
                }
            }
            if (!ret) {
                this_thread::disable_interruption di2;
                this_thread::disable_syscall_interruption dsi2;
                int status;

                /* The feedback fd was prematurely closed for an unknown reason.
                 * Did the agent process crash?
                 *
                 * We use timedWaitPid() here because if the process crashed
                 * because of an uncaught exception, the file descriptor
                 * might be closed before the process has printed an error
                 * message, so we give it some time to print the error
                 * before we kill it.
                 */
                ret = timedWaitPid(pid, &status, 5000);
                if (ret == 0) {
                    /* Doesn't look like it; it seems it's still running.
                     * We can't do anything without proper feedback so kill
                     * the agent process and throw an exception.
                     */
                    failGuard.runNow();
                    throw RuntimeException(string("Unable to start the ") + name() +
                                           ": it froze and reported an unknown error during its startup");
                } else if (ret != -1 && WIFSIGNALED(status)) {
                    /* Looks like a crash which caused a signal. */
                    throw RuntimeException(string("Unable to start the ") + name() +
                                           ": it seems to have been killed with signal " +
                                           getSignalName(WTERMSIG(status)) + " during startup");
                } else if (ret == -1) {
                    /* Looks like it exited after detecting an error. */
                    throw RuntimeException(string("Unable to start the ") + name() +
                                           ": it seems to have crashed during startup for an unknown reason");
                } else {
                    /* Looks like it exited after detecting an error, but has an exit code. */
                    throw RuntimeException(string("Unable to start the ") + name() +
                                           ": it seems to have crashed during startup for an unknown reason, "
                                           "with exit code " + toString(WEXITSTATUS(status)));
                }
            }

            if (args[0] == "system error before exec") {
                throw SystemException(string("Unable to start the ") + name() +
                                      ": " + args[1], atoi(args[2]));
            } else if (args[0] == "exec error") {
                e = atoi(args[1]);
                if (e == ENOENT) {
                    throw RuntimeException(string("Unable to start the ") + name() +
                                           " because its executable (" + getExeFilename() + ") "
                                           "doesn't exist. This probably means that your "
                                           "Phusion Passenger installation is broken or "
                                           "incomplete. Please reinstall Phusion Passenger");
                } else {
                    throw SystemException(string("Unable to start the ") + name() +
                                          " because exec(\"" + getExeFilename() + "\") failed",
                                          atoi(args[1]));
                }
            } else if (!processStartupInfo(pid, feedbackFd, args)) {
                throw RuntimeException(string("The ") + name() +
                                       " sent an unknown startup info message '" +
                                       args[0] + "'");
            }

            lock_guard<boost::mutex> l(lock);
            this->feedbackFd = feedbackFd;
            this->pid = pid;
            failGuard.clear();
            return pid;
        }
    }
コード例 #10
0
ファイル: process.c プロジェクト: freeVM/freeVM
/**
  * Create a System Process with the specified
  * environment and arguments 
  */
JNIEXPORT jlongArray JNICALL
Java_org_apache_harmony_luni_internal_process_SystemProcess_createImpl (JNIEnv * env, jclass clazz,
            jobject recv,
            jobjectArray arg1,
            jobjectArray arg2,
            jbyteArray dir)
{
  jbyteArray envString;
  jlongArray pVals = NULL;
  jlong npVals[4];
  char *envArray[256];
  char *command[256];
  int i, retVal;
  IDATA pHandle, inHandle, outHandle, errHandle;
  int envLength, commandLineLength, len;
  char *workingDir = NULL;
  PORT_ACCESS_FROM_ENV (env);

  /* validate sizes */
  commandLineLength = (*env)->GetArrayLength (env, arg1);
  envLength = (*env)->GetArrayLength (env, arg2);
  if (commandLineLength >= 255)
    {
      throwJavaIoIOException(env, "Too many arguments");
      return NULL;
    }
  if (envLength >= 255)
    {
      throwJavaIoIOException(env, "Too many environment arguments");
      return NULL;
    }

  memset (command, 0, sizeof (command));
  memset (envArray, 0, sizeof (envArray));

  /* Get the command string and arguments */
  /* convert java.lang.String into C char* */
  for (i = commandLineLength; --i >= 0;)
    {
      jbyteArray element = (*env)->GetObjectArrayElement (env, arg1, i);
      len = (*env)->GetArrayLength (env, element);
      command[i] = jclmem_allocate_memory (env, len + 1);
      if (command[i] == NULL)
        {
          throwNewOutOfMemoryError (env, "");
          goto failed;
        }
      (*env)->GetByteArrayRegion (env, element, 0, len, (jbyte *)command[i]);
      command[i][len] = 0;
    }
  if (envLength)
    for (i = 0; i < envLength; i++)
      {
        envString = (*env)->GetObjectArrayElement (env, arg2, i);
        len = (*env)->GetArrayLength (env, envString);
        envArray[i] = jclmem_allocate_memory (env, len + 1);
        if (envArray[i] == NULL)
          {
            throwNewOutOfMemoryError (env, "");
            goto failed;
          }
        (*env)->GetByteArrayRegion (env, envString, 0, len, (jbyte *)envArray[i]);
        envArray[i][len] = 0;
      }
  /* NULL terminate for UNIX (does work on windows too; in fact, it doesn't care) */
  command[commandLineLength] = NULL;
  envArray[envLength] = NULL;

  if (dir != NULL)
    {
      jsize dirLength = (*env)->GetArrayLength (env, dir);

      workingDir = jclmem_allocate_memory (env, dirLength + 1);
      if (workingDir)
        {
          (*env)->GetByteArrayRegion (env, dir, 0, dirLength,
            (jbyte *) workingDir);
          workingDir[dirLength] = '\0';
        }
    }

  /*
   *  now call execProgram.  Any non-zero return code 
   *  indicates some kind of failure
   */
   
  retVal = execProgram (env, recv,
      command, commandLineLength, envArray, envLength,
      workingDir, &pHandle, &inHandle, &outHandle,
      &errHandle);

  if (workingDir)
    {
      jclmem_free_memory (env, workingDir);
    }

  if (retVal)
    {
        char errMsg[256];

        /* Failed to exec program */
        
        switch(retVal) {
        case 1001 :
            sprintf(errMsg, "Unable to start program : %s", "fork() failed with errno = EOMEM");
            break;
        case 1002 : 
            sprintf(errMsg, "Unable to start program : %s", "fork() failed with errno = EAGAIN");
            break;
        default:
            sprintf(errMsg, "Unable to start program : %s", "unknown");
            break;
        }

        throwJavaIoIOException(env, errMsg);
        goto failed;
    }

  pVals = (*env)->NewLongArray (env, 4);

  if (pVals)
    {
      npVals[0] = (jlong) pHandle;
      npVals[1] = (jlong) inHandle;
      npVals[2] = (jlong) outHandle;
      npVals[3] = (jlong) errHandle;
      (*env)->SetLongArrayRegion (env, pVals, 0, 4, (jlong *) (&npVals));
    }

failed:

  for (i = 0; i < envLength; i++)
    {
      if (envArray[i])
        jclmem_free_memory (env, envArray[i]);
    }
  for (i = commandLineLength; --i >= 0;)
    {
      if (command[i])
        jclmem_free_memory (env, command[i]);
    }

  return pVals;
}
コード例 #11
0
ファイル: Process.cpp プロジェクト: sconger/libge
void Process::execProgram(const String programName,
                          const List<String>& args,
                          const List<String>& env)
{
    execProgram(programName, args, env, false);
}
コード例 #12
0
ファイル: Process.cpp プロジェクト: sconger/libge
void Process::execProgram(const String programName,
                          const List<String>& args)
{
    List<String> env(0);
    execProgram(programName, args, env, false);
}