Exemple #1
0
bool MonitorItem::docmd(const char* command)
{
   if (!doconnect()) {
      return false;
   }

   if (command[0] != 0) {
      writecmd(command);
   }

   emit clearText(get_name());
   bool jobRunning = false;

   while (1) {
      int stat;
      if ((stat = bnet_recv(d->DSock)) >= 0) {
         strip_trailing_newline(d->DSock->msg);
         QString msg = QString::fromUtf8(d->DSock->msg);
         emit appendText(QString::fromUtf8(get_name()), msg);
         if (d->type == R_CLIENT) {
             if (msg.contains("Job started:"))
                jobRunning = true;
         }
      } else if (stat == BNET_SIGNAL) {
         if (d->DSock->msglen == BNET_EOD) {
            // qDebug() << "<< EOD >>";
             if (d->type == R_CLIENT)
                emit jobIsRunning (jobRunning);
            return true;
         }
         else if (d->DSock->msglen == BNET_SUB_PROMPT) {
            // qDebug() << "<< PROMPT >>";
            return false;
         }
         else if (d->DSock->msglen == BNET_HEARTBEAT) {
            bnet_sig(d->DSock, BNET_HB_RESPONSE);
         }
         else {
            qDebug() << bnet_sig_to_ascii(d->DSock);
         }
      } else { /* BNET_HARDEOF || BNET_ERROR */
         d->DSock = NULL;
         d->state = MonitorItem::Error;
         emit statusChanged(get_name(), d->state);
         emit showStatusbarMessage("Error : BNET_HARDEOF or BNET_ERROR");
         //fprintf(stderr, "<< ERROR >>\n"));
         return false;
      } /* if ((stat = bnet_recv(d->DSock)) >= 0) */

      if (is_bnet_stop(d->DSock)) {
         d->DSock = NULL;
         d->state = MonitorItem::Error;
         emit statusChanged(get_name(), d->state);
         emit showStatusbarMessage("Error : Connection closed.");
         //fprintf(stderr, "<< STOP >>\n");
         return false;            /* error or term */
      } /* if (is_bnet_stop(d->DSock) */

   } /* while (1) */
}
Exemple #2
0
/*
 * Now talk to the SD and do what he says
 */
static void do_sd_commands(JCR *jcr)
{
   int i, status;
   bool found, quit;
   BSOCK *sd = jcr->store_bsock;

   sd->set_jcr(jcr);
   quit = false;
   while (!quit) {
      /*
       * Read command coming from the Storage daemon
       */
      status = sd->recv();
      if (is_bnet_stop(sd)) {         /* hardeof or error */
         break;                       /* connection terminated */
      }
      if (status <= 0) {
         continue;                    /* ignore signals and zero length msgs */
      }

      Dmsg1(110, "<stored: %s", sd->msg);
      found = false;
      for (i = 0; sd_cmds[i].cmd; i++) {
         if (bstrncmp(sd_cmds[i].cmd, sd->msg, strlen(sd_cmds[i].cmd))) {
            found = true;               /* indicate command found */
            jcr->errmsg[0] = 0;
            if (!sd_cmds[i].func(jcr)) {    /* do command */
               /*
                * Note sd->msg command may be destroyed by comm activity
                */
               if (!job_canceled(jcr)) {
                  if (jcr->errmsg[0]) {
                     Jmsg1(jcr, M_FATAL, 0, _("Command error with SD, hanging up. %s\n"),
                           jcr->errmsg);
                  } else {
                     Jmsg0(jcr, M_FATAL, 0, _("Command error with SD, hanging up.\n"));
                  }
                  jcr->setJobStatus(JS_ErrorTerminated);
               }
               quit = true;
            }
            break;
         }
      }

      if (!found) {                   /* command not found */
         if (!job_canceled(jcr)) {
            Jmsg1(jcr, M_FATAL, 0, _("SD command not found: %s\n"), sd->msg);
            Dmsg1(110, "<stored: Command not found: %s\n", sd->msg);
         }
         sd->fsend(serrmsg);
         break;
      }
   }
   sd->signal(BNET_TERMINATE);        /* signal to SD job is done */
}
Exemple #3
0
/*
 * Now talk to the FD and do what he says
 */
void do_fd_commands(JCR *jcr)
{
   int i;
   bool found, quit;
   BSOCK *fd = jcr->file_bsock;

   fd->set_jcr(jcr);
   for (quit=false; !quit;) {
      int status;

      /* Read command coming from the File daemon */
      status = fd->recv();
      if (is_bnet_stop(fd)) {         /* hardeof or error */
         break;                       /* connection terminated */
      }
      if (status <= 0) {
         continue;                    /* ignore signals and zero length msgs */
      }
      Dmsg1(110, "<filed: %s", fd->msg);
      found = false;
      for (i=0; fd_cmds[i].cmd; i++) {
         if (bstrncmp(fd_cmds[i].cmd, fd->msg, strlen(fd_cmds[i].cmd))) {
            found = true;               /* indicate command found */
            jcr->errmsg[0] = 0;
            if (!fd_cmds[i].func(jcr)) {    /* do command */
               /* Note fd->msg command may be destroyed by comm activity */
               if (!job_canceled(jcr)) {
                  if (jcr->errmsg[0]) {
                     Jmsg1(jcr, M_FATAL, 0, _("Command error with FD, hanging up. %s\n"),
                           jcr->errmsg);
                  } else {
                     Jmsg0(jcr, M_FATAL, 0, _("Command error with FD, hanging up.\n"));
                  }
                  jcr->setJobStatus(JS_ErrorTerminated);
               }
               quit = true;
            }
            break;
         }
      }
      if (!found) {                   /* command not found */
         if (!job_canceled(jcr)) {
            Jmsg1(jcr, M_FATAL, 0, _("FD command not found: %s\n"), fd->msg);
            Dmsg1(110, "<filed: Command not found: %s\n", fd->msg);
         }
         fd->fsend(ferrmsg);
         break;
      }
   }
   fd->signal(BNET_TERMINATE);        /* signal to FD job is done */
}
/*
 * Handle Director User Agent commands
 */
static void *handle_UA_client_request(void *arg)
{
   int status;
   UAContext *ua;
   JCR *jcr;
   BSOCK *user = (BSOCK *)arg;

   pthread_detach(pthread_self());

   jcr = new_control_jcr("-Console-", JT_CONSOLE);

   ua = new_ua_context(jcr);
   ua->UA_sock = user;
   set_jcr_in_tsd(INVALID_JCR);

   user->recv();             /* Get first message */
   if (!authenticate_user_agent(ua)) {
      goto getout;
   }

   while (!ua->quit) {
      if (ua->api) {
         user->signal(BNET_MAIN_PROMPT);
      }

      status = user->recv();
      if (status >= 0) {
         pm_strcpy(ua->cmd, ua->UA_sock->msg);
         parse_ua_args(ua);

         if (ua->argc > 0 && ua->argk[0][0] == '.') {
            do_a_dot_command(ua);
         } else {
            do_a_command(ua);
         }

         dequeue_messages(ua->jcr);

         if (!ua->quit) {
            if (console_msg_pending && acl_access_ok(ua, Command_ACL, "messages")) {
               if (ua->auto_display_messages) {
                  pm_strcpy(ua->cmd, "messages");
                  qmessages_cmd(ua, ua->cmd);
                  ua->user_notified_msg_pending = false;
               } else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
                  if (ua->api) {
                     user->signal(BNET_MSGS_PENDING);
                  } else {
                     bsendmsg(ua, _("You have messages.\n"));
                  }
                  ua->user_notified_msg_pending = true;
               }
            }
            if (!ua->api) {
               user->signal(BNET_EOD); /* send end of command */
            }
         }
      } else if (is_bnet_stop(user)) {
         ua->quit = true;
      } else { /* signal */
         user->signal(BNET_POLL);
      }
   }

getout:
   close_db(ua);
   free_ua_context(ua);
   free_jcr(jcr);
   user->close();
   delete user;

   return NULL;
}
Exemple #5
0
static void read_and_process_input(FILE *input, BSOCK *UA_sock)
{
   const char *prompt = "*";
   bool at_prompt = false;
   int tty_input = isatty(fileno(input));
   int status;
   btimer_t *tid = NULL;

   while (1) {
      if (at_prompt) {                /* don't prompt multiple times */
         prompt = "";
      } else {
         prompt = "*";
         at_prompt = true;
      }
      if (tty_input) {
         status = get_cmd(input, prompt, UA_sock, 30);
         if (usrbrk() == 1) {
            clrbrk();
         }
         if (usrbrk()) {
            break;
         }
      } else {
         /*
          * Reading input from a file
          */
         int len = sizeof_pool_memory(UA_sock->msg) - 1;
         if (usrbrk()) {
            break;
         }
         if (fgets(UA_sock->msg, len, input) == NULL) {
            status = -1;
         } else {
            sendit(UA_sock->msg);     /* echo to terminal */
            strip_trailing_junk(UA_sock->msg);
            UA_sock->msglen = strlen(UA_sock->msg);
            status = 1;
         }
      }
      if (status < 0) {
         break;                       /* error or interrupt */
      } else if (status == 0) {       /* timeout */
         if (bstrcmp(prompt, "*")) {
            tid = start_bsock_timer(UA_sock, timeout);
            UA_sock->fsend(".messages");
            stop_bsock_timer(tid);
         } else {
            continue;
         }
      } else {
         at_prompt = false;
         /*
          * @ => internal command for us
          */
         if (UA_sock->msg[0] == '@') {
            parse_args(UA_sock->msg, &args, &argc, argk, argv, MAX_CMD_ARGS);
            if (!do_a_command(input, UA_sock)) {
               break;
            }
            continue;
         }
         tid = start_bsock_timer(UA_sock, timeout);
         if (!UA_sock->send()) {      /* send command */
            stop_bsock_timer(tid);
            break;                    /* error */
         }
         stop_bsock_timer(tid);
      }

      if (bstrcmp(UA_sock->msg, ".quit") || bstrcmp(UA_sock->msg, ".exit")) {
         break;
      }

      tid = start_bsock_timer(UA_sock, timeout);
      while ((status = UA_sock->recv()) >= 0 ||
             ((status == BNET_SIGNAL) && (
              (UA_sock->msglen != BNET_EOD) &&
              (UA_sock->msglen != BNET_MAIN_PROMPT) &&
              (UA_sock->msglen != BNET_SUB_PROMPT)))) {
         if (status == BNET_SIGNAL) {
            if (UA_sock->msglen == BNET_START_RTREE) {
               file_selection = true;
            } else if (UA_sock->msglen == BNET_END_RTREE) {
               file_selection = false;
            }
            continue;
         }

         if (at_prompt) {
            if (!stop) {
               sendit("\n");
            }
            at_prompt = false;
         }

         /*
          * Suppress output if running in background or user hit ctl-c
          */
         if (!stop && !usrbrk()) {
            if (UA_sock->msg) {
               sendit(UA_sock->msg);
            }
         }
      }
      stop_bsock_timer(tid);

      if (usrbrk() > 1) {
         break;
      } else {
         clrbrk();
      }
      if (!stop) {
         fflush(stdout);
      }

      if (is_bnet_stop(UA_sock)) {
         break;                       /* error or term */
      } else if (status == BNET_SIGNAL) {
         if (UA_sock->msglen == BNET_SUB_PROMPT) {
            at_prompt = true;
         }
         Dmsg1(100, "Got poll %s\n", bnet_sig_to_ascii(UA_sock));
      }
   }
}