Ejemplo n.º 1
0
Archivo: cli.c Proyecto: yubo/bird
static void
cli_event(void *data)
{
  struct cli *c = data;
  int err;

  while (c->ring_read != c->ring_write &&
      c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
    cli_copy_message(c);

  if (c->tx_pos)
    ;
  else if (c->cont)
    c->cont(c);
  else
    {
      err = cli_get_command(c);
      if (!err)
	return;
      if (err < 0)
	cli_printf(c, 9000, "Command too long");
      else
	cli_command(c);
    }

  cli_write_trigger(c);
}
Ejemplo n.º 2
0
STATIC int
do_script(CLI_CTX ctx, bool_t halt_on_error)
{
   GIO *gio = ctx->gio;
   GIO *cmd = gio->next;      /* GIO for echoing commands */
   char buf[MAXCMD+1];
   char *cp = buf;
   int err = 0;
   int line_no = 0;

   while ((err = gio_in(gio, cp, 1)) > 0)
   {
      if (*cp++ == '\n')
      {
         *(cp - 1) = NUL;
         line_no++;

         /* echo the input line */
         if (gio->flags & GIO_F_ECHO)
            gio_printf(cmd, "%s%s\n", ctx->prompt, buf);

         err = cli_command(ctx, buf);
         if (err != 0)
         {
            /* display the error message on the console */
            cli_get_errstr(err, buf);
            gio_out(cmd, buf, strlen(buf));
         }

         if ((err < 0) && halt_on_error)
         {
            dprintf("Script error at line: %d\n", line_no);
            return (err);
         }
         cp = buf;
      }
   }
   if (err == 0)
   {    
      if (cp != buf)
      {
         /* Execute last command which was terminated by a NUL */
         *cp = NUL;
         line_no++;

         /* echo the input line */
         if (gio->flags & GIO_F_ECHO)
            gio_printf(cmd, "%s%s\n", ctx->prompt, buf);

         err = cli_command(ctx, buf);
         if (err != 0)
         {
            /* display the error message on the console */
            cli_get_errstr(err, buf);
            gio_out(cmd, buf, strlen(buf));
         }

         if ((err < 0) && halt_on_error)
         {
            dprintf("Script error at line: %d\n", line_no);
            return (err);
         }
      }
   }
   else
      dprintf("READRC ERR: err=%d\n", err);

   return (err);
}
Ejemplo n.º 3
0
int main(int argc, char **argv)
{
	int i;
	int fanotify_fd, ret;
	struct backend *be = NULL;
	struct stat stbuf;

	logfd = stdout;
	memset(frontend_prefix, 0x0, FILENAME_MAX);

	while ((i = getopt(argc, argv, "b:c:d:m:n:o:p:su:")) != -1) {
		switch (i) {
		case 'b':
			be = new_backend(optarg);
			if (!be) {
				err("Invalid backend '%s'\n", optarg);
				return EINVAL;
			}
			break;
		case 'd':
			if (stat(optarg, &stbuf) < 0 ||
			    !S_ISDIR(stbuf.st_mode)) {
				err("Frontend prefix %s is not a directory",
				    optarg);
				return EINVAL;
			}
			strncpy(frontend_prefix, optarg, FILENAME_MAX);
			break;
		case 'c':
			return cli_command(CLI_CHECK, optarg);
			break;
		case 'm':
			ret = cli_command(CLI_CHECK, optarg);
			if (ret)
				return ret;
			ret = cli_command(CLI_MIGRATE, optarg);
			if (ret)
				return ret;
			/* Fallthrough */
		case 'n':
			return cli_command(CLI_MONITOR, optarg);
			break;
		case 'o':
			if (!be) {
				err("No backend selected");
				return EINVAL;
			}
			if (parse_backend_options(be, optarg) < 0) {
				err("Invalid backend option '%s'", optarg);
				return EINVAL;
			}
			break;
		case 'p':
			log_priority = strtoul(optarg, NULL, 10);
			if (log_priority > LOG_DEBUG) {
				err("Invalid logging priority %d (max %d)",
				    log_priority, LOG_DEBUG);
				exit(1);
			}
			break;
		case 's':
			return cli_command(CLI_SHUTDOWN, NULL);
			break;
		case 'u':
			ret = cli_command(CLI_CHECK, optarg);
			if (ret && ret != ENOENT)
				return ret;
			ret = cli_command(CLI_SETUP, optarg);
			if (ret)
				return ret;
			return cli_command(CLI_MONITOR, optarg);
			break;
		default:
			fprintf(stderr, "usage: %s [-d <dir>]\n", argv[0]);
			return EINVAL;
		}
	}
	if (optind < argc) {
		fprintf(stderr, "usage: %s [-b file] [-p <dir>]\n", argv[0]);
		return EINVAL;
	}

	signal_set(SIGINT, sigend);
	signal_set(SIGTERM, sigend);

	fanotify_fd = fanotify_init(FAN_CLASS_PRE_CONTENT, O_RDWR);
	if (fanotify_fd < 0) {
		fprintf(stderr, "cannot start fanotify, error %d\n",
			errno);
		return errno;
	}

	daemon_thr = pthread_self();

	watcher_thr = start_watcher(be, fanotify_fd);
	if (!watcher_thr)
		return errno;

	cli_thr = start_cli(be, fanotify_fd);
	if (!cli_thr) {
		stop_watcher(watcher_thr);
		return ENOMEM;
	}

	pthread_cond_wait(&exit_cond, &exit_mutex);

	stop_cli(cli_thr);
	stop_watcher(watcher_thr);

	return 0;
}