/** * Initializes the networking interfaces * @arg config Takes the bloom server configuration * @arg mgr The filter manager to pass up to the connection handlers * @arg netconf Output. The configuration for the networking stack. */ int init_networking(statsite_config *config, statsite_networking **netconf_out) { // Initialize the netconf structure statsite_networking *netconf = calloc(1, sizeof(struct statsite_networking)); netconf->config = config; /** * Check if we can use kqueue instead of select. * By default, libev will not use kqueue since it only * works for sockets, which is all we need. */ int ev_mode = EVFLAG_AUTO; if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) { ev_mode = EVBACKEND_KQUEUE; } if (!ev_default_loop (ev_mode)) { syslog(LOG_CRIT, "Failed to initialize libev!"); free(netconf); return 1; } // Setup the stdin listener int res = setup_stdin_listener(netconf); if (res != 0) { free(netconf); return 1; } // Setup the TCP listener res = setup_tcp_listener(netconf); if (res != 0) { free(netconf); return 1; } // Setup the UDP listener res = setup_udp_listener(netconf); if (res != 0) { if (ev_is_active(&netconf->tcp_client)) { ev_io_stop(&netconf->tcp_client); close(netconf->tcp_client.fd); } free(netconf); return 1; } // Setup the timer ev_timer_init(&netconf->flush_timer, handle_flush_event, config->flush_interval, config->flush_interval); ev_timer_start(&netconf->flush_timer); // Prepare the conn handlers init_conn_handler(config); // Success! *netconf_out = netconf; return 0; }
/** * Initializes the networking interfaces * @arg config Takes the bloom server configuration * @arg mgr The filter manager to pass up to the connection handlers * @arg netconf Output. The configuration for the networking stack. */ int init_networking(bloom_config *config, bloom_filtmgr *mgr, bloom_networking **netconf_out) { // Make the netconf structure bloom_networking *netconf = calloc(1, sizeof(struct bloom_networking)); // Initialize netconf->config = config; netconf->mgr = mgr; netconf->workers = calloc(config->worker_threads, sizeof(worker_ev_userdata*)); if (!netconf->workers) { free(netconf); perror("Failed to calloc() for worker threads"); return 1; } // Setup the barrier if (barrier_init(&netconf->thread_barrier, config->worker_threads + 1)) { free(netconf->workers); free(netconf); return 1; } /** * Check if we can use kqueue instead of select. * By default, libev will not use kqueue since it has * certain limitations that select doesn't, but which * we don't need. */ int ev_mode = EVFLAG_AUTO; if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) { ev_mode = EVBACKEND_KQUEUE; } netconf->ev_mode = ev_mode; if (!(netconf->default_loop = ev_loop_new (ev_mode))) { syslog(LOG_CRIT, "Failed to initialize libev!"); free(netconf); return 1; } // Setup the TCP listener int res = setup_tcp_listener(netconf); if (res != 0) { free(netconf); return 1; } // Setup the UDP listener res = setup_udp_listener(netconf); if (res != 0) { ev_io_stop(netconf->default_loop, &netconf->tcp_client); close(netconf->tcp_client.fd); free(netconf); return 1; } // Prepare the conn handlers init_conn_handler(); // Success! *netconf_out = netconf; return 0; }
static int do_status(int type) { pid_t pid; int rv, status_lock_fd, ret; const char *reason = NULL; char lockfile_data[1024], *cp; ret = PCMK_OCF_NOT_RUNNING; rv = setup_config(type); if (rv) { reason = "Error reading configuration."; ret = PCMK_OCF_UNKNOWN_ERROR; goto quit; } if (!local) { reason = "No Service IP active here."; goto quit; } rv = _lockfile(O_RDWR, &status_lock_fd, &pid); if (status_lock_fd == -1) { reason = "No PID file."; goto quit; } if (rv == 0) { close(status_lock_fd); reason = "PID file not locked."; goto quit; } if (pid) { fprintf(stdout, "booth_lockpid=%d ", pid); fflush(stdout); } rv = read(status_lock_fd, lockfile_data, sizeof(lockfile_data) - 1); if (rv < 4) { close(status_lock_fd); reason = "Cannot read lockfile data."; ret = PCMK_LSB_UNKNOWN_ERROR; goto quit; } lockfile_data[rv] = 0; close(status_lock_fd); /* Make sure it's only a single line */ cp = strchr(lockfile_data, '\r'); if (cp) *cp = 0; cp = strchr(lockfile_data, '\n'); if (cp) *cp = 0; rv = setup_tcp_listener(1); if (rv == 0) { reason = "TCP port not in use."; goto quit; } fprintf(stdout, "booth_lockfile='%s' %s\n", cl.lockfile, lockfile_data); if (!daemonize) fprintf(stderr, "Booth at %s port %d seems to be running.\n", local->addr_string, booth_conf->port); return 0; quit: log_debug("not running: %s", reason); /* Ie. "DEBUG" */ if (!daemonize) fprintf(stderr, "not running: %s\n", reason); return ret; }
/** * Initializes the networking interfaces * @arg config Takes the statsite_proxy server configuration * @arg netconf Output. The configuration for the networking stack. * @arg proxy Pointer to proxy for routing metrics via consistent hashing */ int init_networking(statsite_proxy_config *config, statsite_proxy_networking **netconf_out, proxy *proxy) { // Make the netconf structure statsite_proxy_networking *netconf = calloc(1, sizeof(struct statsite_proxy_networking)); // Initialize netconf->events = NULL; netconf->config = config; netconf->proxy = proxy; netconf->should_run = 1; netconf->thread = NULL; /** * Check if we can use kqueue instead of select. * By default, libev will not use kqueue since it only * works for sockets, which is all we need. */ int ev_mode = EVFLAG_AUTO; if (ev_supported_backends () & ~ev_recommended_backends () & EVBACKEND_KQUEUE) { ev_mode = EVBACKEND_KQUEUE; } if (!ev_default_loop (ev_mode)) { syslog(LOG_CRIT, "Failed to initialize libev!"); free(netconf); return 1; } // Setup proxy connections int proxy_res = setup_proxy_connections(netconf); if (proxy_res != 0) { // free proxy connections stuff free(netconf); return 1; } // Setup the TCP listener int res = setup_tcp_listener(netconf); if (res != 0) { free(netconf); return 1; } // Setup the UDP listener res = setup_udp_listener(netconf); if (res != 0) { ev_io_stop(&netconf->tcp_client); close(netconf->tcp_client.fd); free(netconf); return 1; } // Setup the async handler ev_async_init(&netconf->loop_async, handle_async_event); ev_async_start(&netconf->loop_async); // Prepare the conn handlers init_conn_handler(config); // Success! *netconf_out = netconf; return 0; }
static int do_status(int type) { pid_t pid; int rv, lock_fd, ret; const char *reason = NULL; char lockfile_data[1024], *cp; ret = PCMK_OCF_NOT_RUNNING; /* TODO: query all, and return quit only if it's _cleanly_ not * running, ie. _neither_ of port/lockfile/process is available? * * Currently a single failure says "not running", even if "only" the * lockfile has been removed. */ rv = setup_config(type); if (rv) { reason = "Error reading configuration."; ret = PCMK_OCF_UNKNOWN_ERROR; goto quit; } if (!local) { reason = "No Service IP active here."; goto quit; } rv = _lockfile(O_RDWR, &lock_fd, &pid); if (rv == 0) { reason = "PID file not locked."; goto quit; } if (lock_fd == -1) { reason = "No PID file."; goto quit; } if (pid) { fprintf(stdout, "booth_lockpid=%d ", pid); fflush(stdout); } rv = read(lock_fd, lockfile_data, sizeof(lockfile_data) - 1); if (rv < 4) { reason = "Cannot read lockfile data."; ret = PCMK_LSB_UNKNOWN_ERROR; goto quit; } lockfile_data[rv] = 0; if (lock_fd != -1) close(lock_fd); /* Make sure it's only a single line */ cp = strchr(lockfile_data, '\r'); if (cp) *cp = 0; cp = strchr(lockfile_data, '\n'); if (cp) *cp = 0; rv = setup_tcp_listener(1); if (rv == 0) { reason = "TCP port not in use."; goto quit; } fprintf(stdout, "booth_lockfile='%s' %s\n", cl.lockfile, lockfile_data); if (daemonize) fprintf(stderr, "Booth at %s port %d seems to be running.\n", local->addr_string, booth_conf->port); return 0; quit: log_debug("not running: %s", reason); /* Ie. "DEBUG" */ if (daemonize) fprintf(stderr, "not running: %s\n", reason); return ret; }