Esempio n. 1
0
int cli_parse(const int argc, const char **argv)
{
  options_init();

  flag_t *flag;
  const char *arg;
  int i;
  for (arg = argv[i=1]; i < argc; arg = argv[++i]) {
    flag = NULL;

    if (flag_is_long_flag(arg)) {
      flag = flag_find_by_name(arg+2);
    }
    else if (flag_is_short_flag(arg)) {
      flag = flag_find(arg[1]);
    }
    else if (flag_is_filename(arg)) {
      flag = flag_find_by_name("in");
      options_set_with_arg(flag, arg);
      continue;
    }

    if (!flag) {
      log_error("unknown flag \'%s\'", arg);
      return 0;
    }
    else if (flag->args > 0) {
      if (i+1 < argc)
        options_set_with_arg(flag, argv[++i]);
      else {
        log_error("flag \'%s\' needs an argument", arg);
        return 0;
      }
    }
    else {
      options_set(flag);
    }
  }

  if (options->out_filename == NULL && options->in_filename == NULL) {
    log_error("at least one filename (input or output) has to be declared");
    return 0;
  }

  log_info("parsed arguments from command line");
  return 1;
}
Esempio n. 2
0
void options_parse(int argc, char** argv)
{
  if (argv_singleton != NULL)
    argv_singleton = NULL;
  options_t* o = options_new();

  // Parse the basic options.
  o->num_args = argc;
  o->args = polymec_malloc(sizeof(char*) * o->num_args);
  int first_named_value = -1;
  for (int i = 0; i < argc; ++i)
  {
    if (string_contains(argv[i], "=") && (first_named_value == -1))
      first_named_value = i;
    o->args[i] = string_dup(argv[i]);
  }

  // Now parse parameters.
  int i = (first_named_value == -1) ? 0 : first_named_value;
  while (i < argc)
  {
    // Parse a key=value pair.
    int pos = 0, length;
    char* token;
    if (string_next_token(argv[i], "=", &pos, &token, &length))
    {
      // We found a key with an '=' after it. What's the value?
      char key[length+1];
      strncpy(key, token, length);
      key[length] = '\0';
      if (string_next_token(argv[i], "=", &pos, &token, &length))
      {
        char value[length+1];
        strncpy(value, token, length);
        value[length] = '\0';
        options_set(o, key, value);
      }
    }

    // Next!
    ++i;
  }

  argv_singleton = o;
}
Esempio n. 3
0
impl::app::options_set
impl::app::specific_options(void)
    const
{
    return options_set();
}
Esempio n. 4
0
int
main(int argc, char **argv)
{
  const char *ptr;
  int server_mode = -1;
  int i;
  int rc = 0;
  FILE *xmlf;

#if !defined(HPUX)
  setlinebuf(stdout);
#endif

  ptr = strrchr(argv[0], '/');
  if (ptr == NULL) {
    strncpy(prog_name, argv[0], PROG_NAME_LEN);
  } else {
    strncpy(prog_name, ptr + 1, PROG_NAME_LEN);
  }

  pagesize = getpagesize();
  printf("pagesize = %d\n", pagesize);
  cache_line_size = OPT_CACHE_LINE_EST;

#ifdef TBB_KINFO
  int kinfo_fd = 0;
#endif /* TBB_KINFO */
#ifdef TBB_KINFO
  if (options.kinfo) {
    kinfo_fd = pre_kinfo_init();
  }
#endif /* TBB_KINFO */

  init_print_time(&tnow, &tprev);

  /* must happen before processing arguments */
  options_set();

  /* Process command line arguments */
  process_args(argc, argv);

  /* Check for conflicts in the specified cmd line options */
  check_options();

  printf("INIT START\n");

  num_idle = maxconns;
  printf("main: num_idle = %d\n", num_idle);
  printf("main: maxconns = %d\n", maxconns);
  printf("main: max_fds = %d\n", max_fds);

#ifdef DEBUG_ON
  printf("Debugging is on debugging messages mask = 0x%x\n", MSG_MASK);
#else
  printf("Compiled without debugging\n");
#endif /* DEBUG_ON */

  if (options.use_socket_aio) {
    // for now leave some room for a few open files and
    // a few listening sockets (unfortunately we need to call this before
    // we know how many listening sockets there will be
    // max_sock_sd = maxconns + 20;

    // For now we just pick a value that _SHOULD_ avoid
    // collisions with future tests.
    // Note that if there were sd/fd collisions things
    // _should_ likely work just fine but this may
    // make finding problems easier - since there is
    // potential to control which range of values the
    // underlying aio layer is returning.

#ifdef HAVE_AIO_LAYER
    /* These are just estimates !!! */
    int aio_lowest_sock_sd = OPT_AIO_LOWEST_SD;
    int aio_highest_sock_sd = (maxconns + aio_lowest_sock_sd - 1) +
      OPT_AIO_MAX_LISTENERS;
    max_sock_sd = aio_highest_sock_sd;

    if (max_sock_sd > max_fds) {
      printf("main: max_sock_sd = %d > max_fds = %d\n", max_sock_sd, max_fds);
      exit(1);
    }

    printf("main: aio_lowest_sock_sd = %d\n", aio_lowest_sock_sd);
    printf("main: aio_highest_sock_sd = %d\n", aio_highest_sock_sd);
    PRINT_TIME(NOFD, &tnow, &tprev, "main: calling aio_sock_init with "
      "max_sock_sd = %d", max_sock_sd);
    rc = aio_sock_init(aio_lowest_sock_sd, aio_highest_sock_sd);
    if (rc < 0) {
      printf("main: aio_sock_init failed rc = %d\n", rc);
      exit(1);
    }
#endif /* HAVE_AIO_LAYER */

  } else {
    max_sock_sd = max_fds;
  }

  printf("main: max_sock_sd = %d\n", max_sock_sd);
  printf("main: FD_SETSIZE = %d\n", FD_SETSIZE);
  printf("main: sizeof(fd_set) = %d\n", (int) sizeof(fd_set));


#ifdef TBB_KINFO
  if (options.kinfo) {
    kinfo_fd = pre_kinfo_init();
  }
#endif /* TBB_KINFO */

  rc = info_init();


  info_listener_init();

  /* must happen after processing arguments */
  init_app_queues();
  create_pidfile();
  options_valid();
  options_print();

  xmlf = fopen("options.xml", "w");
  if (!xmlf) {
    printf("main: failed to create options.xml\n");
    exit(1);
  }
  options_print_xml(xmlf);
  fclose(xmlf);

#ifndef AIO_WORKAROUND_BUGS
  print_extra_info();
#endif /* AIO_WORKAROUND_BUGS */

#ifdef HAVE_NETSTAT
  netstat_init();
#endif /* HAVE_NETSTAT */

  sock_special_init();

  /* initialize the event dispatch mechanism */
  if (options.use_poll) {
    PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling poll_loop_init");
    poll_loop_init();
#ifdef HAVE_EPOLL
  } else if (options.use_epoll || options.use_epoll2) {
    PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling epoll_loop_init");
    epoll_loop_init();
#endif /* HAVE_EPOLL */
#ifdef HAVE_AIO_LAYER
  } else if (options.use_socket_aio) {
    PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling aio_loop_init");
    aio_loop_init();
#endif /* HAVE_AIO_LAYER */
  } else if (!options.send_loop) {
    assert(options.send_loop == 0);
    PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling select_loop_init");
    select_loop_init();
  } else {
#ifdef SEND
    assert(options.send_loop == 1);
    PRINT_TIME(NOFD, &tnow, &tprev, "userver: calling send_loop_init");
    send_loop_init();
#endif
  }

  switch (options.get_connections) {
    case OPT_CONN_WITH_SELECT_POLL_EPOLL:
      server_mode = LISTENER_NOT_ASYNC_INIT;
      break;

#ifdef SEND
    case OPT_CONN_WITH_SEND_SELECT:
      server_mode = LISTENER_NOT_ASYNC_INIT | LISTENER_SEND_INIT;
      break;
#endif /* SEND */

    case OPT_CONN_WITH_SIGIO:
      server_mode = LISTENER_DO_ASYNC_INIT;
      break;

    case OPT_CONN_WITH_SEND_EVTS:
      server_mode = LISTENER_SEND_INIT;
      break;

    case OPT_CONN_WITH_AIO_ACCEPT:
      server_mode = LISTENER_AIO_INIT;
      break;

    default:
      printf("%s: options.get_connections = %d not handled\n",
        argv[0], options.get_connections);
      exit(1);
      break;
  } /* switch */

  printf("Calling server_init with mode = 0x%x = %d\n",
    server_mode, server_mode);
  server_init(server_mode);

  for (i = sock_listener_min; i <= sock_listener_max; i++) {
    if (sock_is_listener(i)) {
      printf("listen_sd = %d\n", i);
      if (i > max_sd) {
        max_sd = i;
      }
    }
  }

  switch (options.process_sds_order) {
    case OPT_PROCESS_SDS_LRU:
      lru_copy_init();
      break;

    case OPT_PROCESS_SDS_LIFO:
      q_init(max_fds + 1);
      if (options.get_connections == OPT_CONN_WITH_SELECT_POLL_EPOLL) {
        for (i = sock_listener_min; i <= sock_listener_max; i++) {
          if (sock_is_listener(i)) {
            q_add_to_front(i);
          }
        }
      }
      break;

    case OPT_PROCESS_SDS_FIFO:
      q_init(max_fds + 1);
      if (options.get_connections == OPT_CONN_WITH_SELECT_POLL_EPOLL) {
        for (i = sock_listener_min; i <= sock_listener_max; i++) {
          if (sock_is_listener(i)) {
            q_add_to_rear(i);
          }
        }
      }
      break;

    default:
      /* do nothing */
      break;
  }

  if (options.caching_on) {
    initCache(options.cache_table_size, options.cache_max_bytes,
      options.cache_max_file_size, options.cache_max_load_factor,
      options.cache_lock_pages);
#ifdef CACHE_MAPPED_NEW
    if (options.cache_warm) {
      cache_warm(options.cache_warm_file,
        (options.doc_root[0] != '\0') ? options.doc_root : NULL,
        options.cache_table_print);
    }
#endif
  }

  rusage_init();

  fork_servers(numprocs);
  return 0;
}