Exemple #1
0
/********************************************************************//**
 * MAIN
 */
int main (int argc, char* argv[])
{
    int ret = 0;

    if (argc < 2) {
        ret = ERR_NOT_ENOUGH_ARGUMENTS;
    } else {
        /* **********************************************************************
         * TODO WEEK 08: THIS PART SHALL BE REVISED THEN (WEEK 09) EXTENDED.
         * **********************************************************************
         */
        argc--; argv++; // skips command call name
        if (!strcmp("list", argv[0])) {
            if (argc < 2) {
                ret = ERR_NOT_ENOUGH_ARGUMENTS;
            } else {
                ret = do_list_cmd(argv[1]);
            }
        } else if (!strcmp("create", argv[0])) {
            if (argc < 2) {
                ret = ERR_NOT_ENOUGH_ARGUMENTS;
            } else {
                ret = do_create_cmd(argv[1]);
            }
        } else if (!strcmp("delete", argv[0])) {
            if (argc < 3) {
                ret = ERR_NOT_ENOUGH_ARGUMENTS;
            } else {
                ret = do_delete_cmd(argv[1], argv[2]);
            }
        } else if (!strcmp("help", argv[0])) {
            ret = help();
        } else {
            ret = ERR_INVALID_COMMAND;
        }
    }

    if (ret) {
        fprintf(stderr, "ERROR: %s\n", ERROR_MESSAGES[ret]);
        (void)help();
    }

    return ret;
}
int main(int argc, char *argv[])
{
   int rc;
   FILE *fs = NULL;
   int32_t error_code;

#ifdef HAVE_LOCALE_H
   setlocale(LC_ALL, "");
#endif

   /* Log initially to stderr */
   log.OpenLog(stderr, LOG_ERR);
   /* parse the command line */
   if ((error_code = parse_cmdline(argc, argv)) != 0) {
      print_help();
      return 1;
   }
   /* Check for --version flag */
   if (cmdl.print_version) {
      print_version();
      return 0;
   }
   /* Check for --help flag */
   if (cmdl.print_help) {
      print_help();
      return 0;
   }
   /* Read vchanger config file */
   if (!conf.Read(cmdl.config_file)) {
      return 1;
   }
   /* User:group from cmdline overrides config file values */
   if (cmdl.runas_user.size()) conf.user = cmdl.runas_user;
   if (cmdl.runas_group.size()) conf.group = cmdl.runas_group;
   /* Pool from cmdline overrides config file */
   if (!cmdl.pool.empty()) conf.def_pool = cmdl.pool;
   /* If root, try to run as configured user:group */
   rc = drop_privs(conf.user.c_str(), conf.group.c_str());
   if (rc) {
      fprintf(stderr, "Error %d attempting to run as user '%s'", rc, conf.user.c_str());
      return 1;
   }
   /* Start logging to log file specified in configuration file */
   if (!conf.logfile.empty()) {
      fs = fopen(conf.logfile.c_str(), "a");
      if (fs == NULL) {
         fprintf(stderr, "Error opening opening log file\n");
         return 1;
      }
      log.OpenLog(fs, conf.log_level);
   }
   /* Validate and commit configuration parameters */
   if (!conf.Validate()) {
      return 1;
   }
   /* Initialize changer. A lock file is created to serialize access
    * to the changer. As a result, changer initialization may block
    * for up to 30 seconds, and may fail if a timeout is reached */
   if (changer.Initialize()) {
      fprintf(stderr, "%s\n", changer.GetErrorMsg());
      return 1;
   }

   /* Perform command */
   switch (cmdl.command) {
   case CMD_LIST:
      log.Debug("==== preforming LIST command pid=%d", getpid());
      error_code = do_list_cmd();
      break;
   case CMD_SLOTS:
      log.Debug("==== preforming SLOTS command pid=%d", getpid());
      error_code = do_slots_cmd();
      break;
   case CMD_LOAD:
      log.Debug("==== preforming LOAD command pid=%d", getpid());
      error_code = do_load_cmd();
      break;
   case CMD_UNLOAD:
      log.Debug("==== preforming UNLOAD command pid=%d", getpid());
      error_code = do_unload_cmd();
      break;
   case CMD_LOADED:
      log.Debug("==== preforming LOADED command pid=%d", getpid());
      error_code = do_loaded_cmd();
      break;
   case CMD_LISTALL:
      log.Debug("==== preforming LISTALL command pid=%d", getpid());
      error_code = do_list_all();
      break;
   case CMD_LISTMAGS:
      log.Debug("==== preforming LISTMAGS command pid=%d", getpid());
      error_code = do_list_magazines();
      break;
   case CMD_CREATEVOLS:
      log.Debug("==== preforming CREATEVOLS command pid=%d", getpid());
      error_code = do_create_vols();
      break;
   case CMD_REFRESH:
      log.Debug("==== preforming REFRESH command pid=%d", getpid());
      error_code = 0;
      log.Info("  SUCCESS pid=%d", getpid());
      break;
   }

   /* If there was an error, then exit */
   if (error_code) {
      changer.Unlock();
      return error_code;
   }

   /* If not updating Bacula, then exit */
   if (conf.bconsole.empty()) {
      /* Bacula interaction via bconsole is disabled, so log warnings */
      if (changer.NeedsUpdate())
         log.Error("WARNING! 'update slots' needed in bconsole pid=%d", getpid());
      if (changer.NeedsLabel())
         log.Error("WARNING! 'label barcodes' needed in bconsole pid=%d", getpid());
      changer.Unlock();
      return 0;
   }

   /* Update Bacula via bconsole */
#ifndef HAVE_WINDOWS_H
   changer.UpdateBacula();
#else
   /* Auto-update of bacula not working for Windows */
   if (changer.NeedsUpdate())
      log.Error("WARNING! 'update slots' needed in bconsole");
   if (changer.NeedsLabel())
      log.Error("WARNING! 'label barcodes' needed in bconsole");
   return 0;
#endif

   changer.Unlock();
   return 0;
}
Exemple #3
0
/* Do short or summary listing */
int list_cmd(UAContext *ua, const char *cmd)
{
   return do_list_cmd(ua, cmd, HORZ_LIST);
}
Exemple #4
0
/* Do long or full listing */
int llist_cmd(UAContext *ua, const char *cmd)
{
   return do_list_cmd(ua, cmd, VERT_LIST);
}