示例#1
0
static void performCommand(char *acLine, DynArray_T oHistoryList, 
                           char *pcProgName)

/* Expand any !commandprefix in acLine. Insert acLine into 
   oHistoryList iff the expanding succeeds and acLine does not consist
   of entirely whitespace characters. Lexically and syntactically 
   analyze acLine. Execute acLine if no errors are found. It is a 
   checked runtime error for acLine, oHistory, or pcProgName to be
   NULL. */

{
   char *pcTemp;
   Command_T oCommand;
   DynArray_T oTokens;
   int iSuccessful;

   assert(acLine != NULL);
   assert(oHistoryList != NULL);
   assert(pcProgName != NULL);

   if(histHasCommandPrefix(acLine))
   {
      iSuccessful = histExpandLine(acLine, oHistoryList, pcProgName);
      if(iSuccessful)
         printf("%s\n", acLine);
      else
         return;
   }
   oTokens = DynArray_new(0);
   iSuccessful = lexLine(acLine, oTokens, pcProgName);
   if(DynArray_getLength(oTokens) > 0)
   {
      /* Allocate memory to store command in oHistoryList iff 
         command does not consist of entirely whitespace
         characters. */
      pcTemp = (char*)malloc(strlen(acLine) + 1);
      assert(pcTemp != NULL);
      strcpy(pcTemp, acLine);
      DynArray_add(oHistoryList, pcTemp);

      if(iSuccessful)
      {
         oCommand = Command_new();
         iSuccessful = parseToken(oTokens, oCommand, pcProgName);
         if(iSuccessful)
            execute(oCommand, oHistoryList, pcProgName);
         Command_free(oCommand, NULL);
      }
   }
   DynArray_map(oTokens, Token_free, NULL);
   DynArray_free(oTokens);
}
示例#2
0
文件: control.c 项目: Nejuf/monit
static int _commandExecute(Service_T S, command_t c, char *msg, int msglen, int64_t *timeout) {
        ASSERT(S);
        ASSERT(c);
        ASSERT(msg);
        msg[0] = 0;
        int status = -1;
        Command_T C = NULL;
        TRY
        {
                // May throw exception if the program doesn't exist (was removed while Monit was up)
                C = Command_new(c->arg[0], NULL);
        }
        ELSE
        {
                snprintf(msg, msglen, "Program %s failed: %s", c->arg[0], Exception_frame.message);
        }
        END_TRY;
        if (C) {
                for (int i = 1; i < c->length; i++)
                        Command_appendArgument(C, c->arg[i]);
                if (c->has_uid)
                        Command_setUid(C, c->uid);
                if (c->has_gid)
                        Command_setGid(C, c->gid);
                Command_setEnv(C, "MONIT_DATE", Time_string(Time_now(), (char[26]){}));
                Command_setEnv(C, "MONIT_SERVICE", S->name);
                Command_setEnv(C, "MONIT_HOST", Run.system->name);
                Command_setEnv(C, "MONIT_EVENT", c == S->start ? "Started" : c == S->stop ? "Stopped" : "Restarted");
                Command_setEnv(C, "MONIT_DESCRIPTION", c == S->start ? "Started" : c == S->stop ? "Stopped" : "Restarted");
                if (S->type == Service_Process) {
                        Command_vSetEnv(C, "MONIT_PROCESS_PID", "%d", Util_isProcessRunning(S, false));
                        Command_vSetEnv(C, "MONIT_PROCESS_MEMORY", "%ld", S->inf->priv.process.mem_kbyte);
                        Command_vSetEnv(C, "MONIT_PROCESS_CHILDREN", "%d", S->inf->priv.process.children);
                        Command_vSetEnv(C, "MONIT_PROCESS_CPU_PERCENT", "%d", S->inf->priv.process.cpu_percent);
                }
                Process_T P = Command_execute(C);
                Command_free(&C);
                if (P) {
                        do {
                                Time_usleep(RETRY_INTERVAL);
                                *timeout -= RETRY_INTERVAL;
                        } while ((status = Process_exitStatus(P)) < 0 && *timeout > 0 && ! (Run.flags & Run_Stopped));
                        if (*timeout <= 0)
                                snprintf(msg, msglen, "Program %s timed out", c->arg[0]);
                        int n, total = 0;
                        char buf[STRLEN];
                        do {
                                if ((n = _getOutput(Process_getErrorStream(P), buf, sizeof(buf))) <= 0)
                                        n = _getOutput(Process_getInputStream(P), buf, sizeof(buf));
                                if (n > 0) {
                                        buf[n] = 0;
                                        DEBUG("%s", buf);
                                        // Report the first message (override existing plain timeout message if some program output is available)
                                        if (! total)
                                                snprintf(msg, msglen, "%s: %s%s", c->arg[0], *timeout <= 0 ? "Program timed out -- " : "", buf);
                                        total += n;
                                }
                        } while (n > 0 && Run.debug && total < 2048); // Limit the debug output (if the program will have endless output, such as 'yes' utility, we have to stop at some point to not spin here forever)
                        Process_free(&P); // Will kill the program if still running
                }
        }
示例#3
0
文件: gc.c 项目: AsydSolutions/monit
static void _gc_service(Service_T *s) {

        ASSERT(s&&*s);

        if ((*s)->program) {
                if ((*s)->program->P)
                        Process_free(&(*s)->program->P);
                if ((*s)->program->C)
                        Command_free(&(*s)->program->C);
                if ((*s)->program->args)
                        gccmd(&(*s)->program->args);
                StringBuffer_free(&((*s)->program->output));
                FREE((*s)->program);
        }

        if((*s)->portlist)
                _gcppl(&(*s)->portlist);

        if((*s)->filesystemlist)
                _gcfilesystem(&(*s)->filesystemlist);

        if((*s)->icmplist)
                _gcicmp(&(*s)->icmplist);

        if((*s)->maillist)
                gc_mail_list(&(*s)->maillist);

        if((*s)->resourcelist)
                _gcpql(&(*s)->resourcelist);

        if((*s)->inf)
                _gc_inf(&(*s)->inf);

        if((*s)->timestamplist)
                _gcptl(&(*s)->timestamplist);

        if((*s)->actionratelist)
                _gcparl(&(*s)->actionratelist);

        if((*s)->sizelist)
                _gcso(&(*s)->sizelist);

        if((*s)->matchlist)
                _gcmatch(&(*s)->matchlist);

        if((*s)->matchignorelist)
                _gcmatch(&(*s)->matchignorelist);

        if((*s)->checksum)
                _gcchecksum(&(*s)->checksum);

        if((*s)->perm)
                _gcperm(&(*s)->perm);

        if ((*s)->statuslist)
                _gcstatus(&(*s)->statuslist);

        if ((*s)->every.type == EVERY_CRON || (*s)->every.type == EVERY_NOTINCRON)
                FREE((*s)->every.spec.cron);

        if((*s)->uid)
                _gcuid(&(*s)->uid);

        if((*s)->euid)
                _gcuid(&(*s)->euid);

        if((*s)->gid)
                _gcgid(&(*s)->gid);

        if((*s)->pidlist)
                _gcpid(&(*s)->pidlist);

        if((*s)->ppidlist)
                _gcppid(&(*s)->ppidlist);

        if((*s)->dependantlist)
                _gcpdl(&(*s)->dependantlist);

        if((*s)->start)
                gccmd(&(*s)->start);

        if((*s)->stop)
                gccmd(&(*s)->stop);

        if((*s)->action_DATA)
                _gc_eventaction(&(*s)->action_DATA);

        if((*s)->action_EXEC)
                _gc_eventaction(&(*s)->action_EXEC);

        if((*s)->action_INVALID)
                _gc_eventaction(&(*s)->action_INVALID);

        if((*s)->action_NONEXIST)
                _gc_eventaction(&(*s)->action_NONEXIST);

        if((*s)->action_FSFLAG)
                _gc_eventaction(&(*s)->action_FSFLAG);

        if((*s)->action_MONIT_START)
                _gc_eventaction(&(*s)->action_MONIT_START);

        if((*s)->action_MONIT_STOP)
                _gc_eventaction(&(*s)->action_MONIT_STOP);

        if((*s)->action_MONIT_RELOAD)
                _gc_eventaction(&(*s)->action_MONIT_RELOAD);

        if((*s)->action_ACTION)
                _gc_eventaction(&(*s)->action_ACTION);

        if((*s)->eventlist)
                gc_event(&(*s)->eventlist);

        FREE((*s)->name);
        FREE((*s)->path);

        (*s)->next = NULL;

        FREE(*s);

}
int main(int argc, char *argv[])
{
   char *pcLine;
   DynArray_T oTokens;
   DynArray_T oArgs;
   int iArgLength;
   int i;
   Command_T oCommand;

   pcPgmName = argv[0];

   /* Write to stdout a prompt consisting of a percent sign 
      and a space. */
   fprintf(stdout, "%% ");

   /* Read a line (that is, an array of characters) from stdin. */
   while ((pcLine = Token_readLine(stdin)) != NULL)
   {
      /* Write that line (array of characters) to stdout, and 
         flush the stdout buffer. */
      fprintf(stdout, "%s\n", pcLine);
      fflush(stdout);

      /* Pass the line (array of characters) to your lexical 
         analyzer to create a DynArray object containing tokens. */
      oTokens = Lex_analyze(pcLine);

      oCommand = Syn_analyze(oTokens);
      if (oCommand != NULL) {
         if (Command_getName(oCommand) != NULL)
            fprintf(stdout, "Command name: %s\n", 
               Command_getName(oCommand));
         if (Command_getArgs(oCommand) != NULL) {
            oArgs = Command_getArgs(oCommand);
            iArgLength = DynArray_getLength(oArgs);
            for (i = 0; i < iArgLength; i++) {
               fprintf(stdout, "Command arg: %s\n",
                  (char *)DynArray_get(oArgs, i));
            }
         }
         if (Command_getStdIn(oCommand) != NULL) {
            fprintf(stdout, "Command stdin: %s\n", 
               Command_getStdIn(oCommand));
         }
         if (Command_getStdOut(oCommand) != NULL) {
            fprintf(stdout, "Command stdout: %s\n", 
               Command_getStdOut(oCommand));
         }
         Command_free(oCommand);
      }   

      if (oTokens != NULL) {
         Token_freeTokens(oTokens);
         DynArray_free(oTokens);
      }  
      
      fprintf(stdout, "%% ");
      free(pcLine);
   }
   fprintf(stdout, "\n");

   return 0;
}