Exemple #1
0
void
klog_setup(klog_options_st *options, klog_metrics_st *metrics)
{
    size_t nbuf = KLOG_NBUF;
    char *filename = NULL;

    log_info("Set up the %s module", KLOG_MODULE_NAME);

    if (klog_init) {
        log_warn("%s has already been setup, overwrite", KLOG_MODULE_NAME);
        log_destroy(&klogger);
    }

    klog_metrics = metrics;

    if (options != NULL) {
        filename = option_str(&options->klog_file);
        klog_backup = option_str(&options->klog_backup);
        if (klog_backup != NULL) {
            size_t nbyte = strnlen(klog_backup, PATH_MAX + 1);
            if (nbyte > PATH_MAX) {
                log_crit("klog file path too long");
                goto error;
            }
            strncpy(backup_path, klog_backup, PATH_MAX);
            klog_backup = backup_path;
        }
        nbuf = option_uint(&options->klog_nbuf);
        klog_sample = option_uint(&options->klog_sample);
        if (klog_sample == 0) {
            log_crit("klog sample rate cannot be 0 - divide by zero");
            goto error;
        }
        klog_max =  option_uint(&options->klog_max);
    }

    if (filename == NULL) { /* no klog filename provided, do not log */
        klog_enabled = false;
        return;
    }

    klogger = log_create(filename, nbuf);
    if (klogger == NULL) {
        log_crit("Could not create klogger!");
        goto error;
    }

    klog_enabled = true;

    klog_init = true;

    return;

error:
    log_destroy(&klogger);
    exit(EX_CONFIG);
}
Exemple #2
0
void
request_setup(request_options_st *options, request_metrics_st *metrics)
{
    uint32_t max = REQ_POOLSIZE;

    log_info("set up the %s module", REQUEST_MODULE_NAME);

    if (request_init) {
        log_warn("%s has already been setup, overwrite", REQUEST_MODULE_NAME);
    }

    request_metrics = metrics;

    if (options != NULL) {
        max = option_uint(&options->request_poolsize);
    }
    request_pool_create(max);

    request_init = true;
}
Exemple #3
0
void
slab_setup(slab_options_st *options, slab_metrics_st *metrics)
{
    char *profile_str = SLAB_PROFILE;

    log_info("set up the %s module", SLAB_MODULE_NAME);

    if (slab_init) {
        log_warn("%s has already been set up, re-creating", SLAB_MODULE_NAME);
        slab_teardown();
    }

    log_verb("Slab header size: %d, item header size: %d", SLAB_HDR_SIZE,
            ITEM_HDR_SIZE);

    slab_metrics = metrics;

    if (options != NULL) {
        slab_size = option_uint(&options->slab_size);
        slab_mem = option_uint(&options->slab_mem);
        prealloc = option_bool(&options->slab_prealloc);
        evict_opt = option_uint(&options->slab_evict_opt);
        use_freeq = option_bool(&options->slab_use_freeq);
        profile_str = option_str(&options->slab_profile);
        item_min = option_uint(&options->slab_item_min);
        item_max = option_uint(&options->slab_item_max);
        item_growth = option_fpn(&options->slab_item_growth);
        max_ttl = option_uint(&options->slab_item_max_ttl);
        use_cas = option_bool(&options->slab_use_cas);
        hash_power = option_uint(&options->slab_hash_power);
    }

    hash_table = hashtable_create(hash_power);
    if (hash_table == NULL) {
        log_crit("Could not create hash table");
        goto error;
    }

    if (_slab_heapinfo_setup() != CC_OK) {
        log_crit("Could not setup slab heap info");
        goto error;
    }

    if (_slab_profile_setup(profile_str) != CC_OK) {
        log_crit("Could not setup slab profile");
        goto error;
    }

    if (_slab_slabclass_setup() != CC_OK) {
        log_crit("Could not setup slabclasses");
        goto error;
    }

    slab_init = true;

    return;

error:
    slab_teardown();
    exit(EX_CONFIG);
}
Exemple #4
0
void
core_admin_setup(admin_options_st *options)
{
    struct tcp_conn *c;
    struct timeout tick;
    char *host = ADMIN_HOST;
    char *port = ADMIN_PORT;
    int timeout = ADMIN_TIMEOUT;
    int nevent = ADMIN_NEVENT;
    uint64_t tick_ms = ADMIN_TW_TICK;
    size_t cap = ADMIN_TW_CAP;
    size_t ntick = ADMIN_TW_NTICK;

    log_info("set up the %s module", ADMIN_MODULE_NAME);

    if (admin_init) {
        log_warn("admin has already been setup, re-creating");
        core_admin_teardown();
    }

    if (options != NULL) {
        host = option_str(&options->admin_host);
        port = option_str(&options->admin_port);
        timeout = option_uint(&options->admin_timeout);
        nevent = option_uint(&options->admin_nevent);
        tick_ms = option_uint(&options->admin_tw_tick);
        cap = option_uint(&options->admin_tw_cap);
        ntick = option_uint(&options->admin_tw_ntick);
    }

    ctx->timeout = timeout;
    ctx->evb = event_base_create(nevent, _admin_event);
    if (ctx->evb == NULL) {
        log_crit("failed to set up admin thread; could not create event "
                 "base for control plane");
        goto error;
    }

    hdl->accept = (channel_accept_fn)tcp_accept;
    hdl->reject = (channel_reject_fn)tcp_reject;
    hdl->open = (channel_open_fn)tcp_listen;
    hdl->term = (channel_term_fn)tcp_close;
    hdl->recv = (channel_recv_fn)tcp_recv;
    hdl->send = (channel_send_fn)tcp_send;
    hdl->rid = (channel_id_fn)tcp_read_id;
    hdl->wid = (channel_id_fn)tcp_write_id;

    admin_sock = buf_sock_borrow();
    if (admin_sock == NULL) {
        log_crit("failed to set up admin thread; could not get buf_sock");
        goto error;
    }

    admin_sock->hdl = hdl;

    if (CC_OK != getaddr(&admin_ai, host, port)) {
        log_crit("failed to resolve address for admin host & port");
        goto error;
    }
    c = admin_sock->ch;
    if (!hdl->open(admin_ai, c)) {
        log_crit("admin connection setup failed");
        goto error;
    }
    c->level = CHANNEL_META;
    event_add_read(ctx->evb, hdl->rid(c), admin_sock);

    timeout_set_ms(&tick, tick_ms);
    tw = timing_wheel_create(&tick, cap, ntick);
    if (tw == NULL) {
        log_crit("create timing wheel failed");
        goto error;
    }
    timing_wheel_start(tw);

    admin_init = true;

    return;

error:
    core_admin_teardown();
    exit(EX_CONFIG);
}
Exemple #5
0
static void
setup(void)
{
    char *fname = NULL;
    uint64_t intvl;

    if (atexit(teardown) != 0) {
        log_stderr("cannot register teardown procedure with atexit()");
        exit(EX_OSERR); /* only failure comes from NOMEM */
    }

    /* Setup logging first */
    log_setup(&stats.log);
    if (debug_setup(&setting.debug) != CC_OK) {
        log_stderr("debug log setup failed");
        exit(EX_CONFIG);
    }

    /* setup top-level application options */
    if (option_bool(&setting.ds.daemonize)) {
        daemonize();
    }
    fname = option_str(&setting.ds.pid_filename);
    if (fname != NULL) {
        /* to get the correct pid, call create_pidfile after daemonize */
        create_pidfile(fname);
    }

    /* setup library modules */
    buf_setup(&setting.buf, &stats.buf);
    dbuf_setup(&setting.dbuf, &stats.dbuf);
    event_setup(&stats.event);
    sockio_setup(&setting.sockio, &stats.sockio);
    tcp_setup(&setting.tcp, &stats.tcp);
    timing_wheel_setup(&stats.timing_wheel);

    /* setup pelikan modules */
    time_setup(&setting.time);
    procinfo_setup(&stats.procinfo);
    request_setup(&setting.request, &stats.request);
    response_setup(&setting.response, &stats.response);
    parse_setup(&stats.parse_req, NULL);
    compose_setup(NULL, &stats.compose_rsp);
    slab_setup(&setting.slab, &stats.slab);
    process_setup(&setting.process, &stats.process);
    admin_process_setup();
    core_admin_setup(&setting.admin);
    core_server_setup(&setting.server, &stats.server);
    core_worker_setup(&setting.worker, &stats.worker);

    /* adding recurring events to maintenance/admin thread */
    intvl = option_uint(&setting.ds.dlog_intvl);
    if (core_admin_register(intvl, debug_log_flush, NULL) == NULL) {
        log_stderr("Could not register timed event to flush debug log");
        goto error;
    }

    return;

error:
    if (fname != NULL) {
        remove_pidfile(fname);
    }

    /* since we registered teardown with atexit, it'll be called upon exit */
    exit(EX_CONFIG);
}
Exemple #6
0
int main(int argc, char **argv)
{ 
  struct in_addr lease;
  struct dhcp_packet packet;
  unsigned char *p = packet.options;
  struct sockaddr_in dest;
  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  ssize_t rc;
  
  if (argc < 2)
    { 
      fprintf(stderr, "usage: dhcp_lease_time <address>\n");
      exit(1);
    }

  if (fd == -1)
    {
      perror("cannot create socket");
      exit(1);
    }
 
  lease.s_addr = inet_addr(argv[1]);
   
  memset(&packet, 0, sizeof(packet));
 
  packet.hlen = 0;
  packet.htype = 0;

  packet.op = BOOTREQUEST;
  packet.ciaddr = lease;
  packet.cookie = htonl(DHCP_COOKIE);

  *(p++) = OPTION_MESSAGE_TYPE;
  *(p++) = 1;
  *(p++) = DHCPINFORM;

  /* Explicitly request the lease time, it won't be sent otherwise:
     this is a dnsmasq extension, not standard. */
  *(p++) = OPTION_REQUESTED_OPTIONS;
  *(p++) = 1;
  *(p++) = OPTION_LEASE_TIME;
  
  *(p++) = OPTION_END;
 
  dest.sin_family = AF_INET; 
  dest.sin_addr.s_addr = inet_addr("127.0.0.1");
  dest.sin_port = ntohs(DHCP_SERVER_PORT);
  
  if (sendto(fd, &packet, sizeof(packet), 0, 
	     (struct sockaddr *)&dest, sizeof(dest)) == -1)
    {
      perror("sendto failed");
      exit(1);
    }

  alarm(3); /* noddy timeout. */

  rc = recv(fd, &packet, sizeof(packet), 0);
  
  if (rc < (ssize_t)(sizeof(packet) - sizeof(packet.options)))
    {
      perror("recv failed");
      exit(1);
    }

  if ((p = option_find(&packet, (size_t)rc, OPTION_LEASE_TIME, 4)))
    {
      unsigned int t = option_uint(p, 4);
      if (t == 0xffffffff)
	printf("infinite");
      else
	{
	  unsigned int x;
	  if ((x = t/86400))
	    printf("%ud", x);
	  if ((x = (t/3600)%24))
	    printf("%uh", x);
	  if ((x = (t/60)%60))
	    printf("%um", x);
	  if ((x = t%60))
	    printf("%us", x);
	}
      return 0;
    }

  return 1; /* no lease */
}
Exemple #7
0
void
core_server_setup(server_options_st *options, server_metrics_st *metrics)
{
    struct tcp_conn *c;
    char *host = SERVER_HOST;
    char *port = SERVER_PORT;
    int timeout = SERVER_TIMEOUT;
    int nevent = SERVER_NEVENT;

    log_info("set up the %s module", SERVER_MODULE_NAME);

    if (server_init) {
        log_warn("server has already been setup, re-creating");
        core_server_teardown();
    }

    server_metrics = metrics;

    if (options != NULL) {
        host = option_str(&options->server_host);
        port = option_str(&options->server_port);
        timeout = option_uint(&options->server_timeout);
        nevent = option_uint(&options->server_nevent);
    }

    ctx->timeout = timeout;
    ctx->evb = event_base_create(nevent, _server_event);
    if (ctx->evb == NULL) {
        log_crit("failed to setup server core; could not create event_base");
        goto error;
    }

    hdl->accept = (channel_accept_fn)tcp_accept;
    hdl->reject = (channel_reject_fn)tcp_reject;
    hdl->open = (channel_open_fn)tcp_listen;
    hdl->term = (channel_term_fn)tcp_close;
    hdl->recv = (channel_recv_fn)tcp_recv;
    hdl->send = (channel_send_fn)tcp_send;
    hdl->rid = (channel_id_fn)tcp_read_id;
    hdl->wid = (channel_id_fn)tcp_write_id;

    /**
     * Here we give server socket a buf_sock purely because it is difficult to
     * write code in the core event loop that would accommodate different types
     * of structs at the moment. However, this doesn't have to be the case in
     * the future. We can choose to wrap different types in a common header-
     * one that contains a type field and a pointer to the actual struct, or
     * define common fields, like how posix sockaddr structs are used.
     */
    server_sock = buf_sock_borrow();
    if (server_sock == NULL) {
        log_crit("failed to setup server core; could not get buf_sock");
        goto error;
    }

    server_sock->hdl = hdl;
    if (CC_OK != getaddr(&server_ai, host, port)) {
        log_crit("failed to resolve address for admin host & port");
        goto error;
    }

    c = server_sock->ch;
    if (!hdl->open(server_ai, c)) {
        log_crit("server connection setup failed");
        goto error;
    }
    c->level = CHANNEL_META;

    event_add_read(ctx->evb, hdl->rid(c), server_sock);

    server_init = true;

    return;

error:
    core_server_teardown();
    exit(EX_CONFIG);
}