/* Module functions */ static int init(const struct mpdcron_config *conf, GKeyFile *fd) { GError *error; g_debug("Initializing"); /* Load configuration */ if (file_load(conf, fd) < 0) return MPDCRON_INIT_FAILURE; /* Initialize database */ error = NULL; if (!db_init(globalconf.dbpath, true, false, &error)) { g_critical("Failed to initialize database `%s': %s", globalconf.dbpath, error->message); g_error_free(error); file_cleanup(); return MPDCRON_INIT_FAILURE; } /* Initialize, bind and start the server */ server_init(); for (unsigned int i = 0; globalconf.addrs[i] != NULL; i++) { if (strncmp(globalconf.addrs[i], "any", 4) == 0) server_bind(NULL, globalconf.port); else if (globalconf.addrs[i][0] == '/') server_bind(globalconf.addrs[i], -1); else server_bind(globalconf.addrs[i], globalconf.port); } server_start(); timer = g_timer_new(); return MPDCRON_INIT_SUCCESS; }
void dsme_dbus_bind_methods(bool* bound_already, const dsme_dbus_binding_t* bindings, const char* service, const char* interface) { if (bound_already && !*bound_already) { const dsme_dbus_binding_t* binding = bindings; while (binding && binding->method) { if (!server_bind(server_instance(), binding->method, service, interface, binding->name, 0)) { dsme_log(LOG_ERR, "D-Bus binding for '%s' failed", binding->name); // TODO: roll back the ones that succeeded and break? } ++binding; } *bound_already = true; } }
void server_run (server_t * s, const char * port) { int i; server_bind(s, port); server_listen(s); // Clear the master and temp sets FD_ZERO(&(s->master)); FD_ZERO(&(s->read_fds)); // Add the listener to the master set FD_SET(s->listener, &(s->master)); // Keep track of the biggest file descriptor s->fdmax = s->listener; // so far, it's this one while (1) { s->read_fds = s->master; if (select(s->fdmax + 1, &(s->read_fds), NULL, NULL, NULL) == -1) { exit(1); } for (i = 0; i <= s->fdmax; i ++) { if (FD_ISSET(i, &(s->read_fds))) { if (i == s->listener) { // Handle new connection server_accept(s); } else { // Handle data from a client robot_recv(s->robots[i]); } } } } }
int main(int ac, char *av[]) { int c, i; u_config_t *cfg = NULL, *vhost; while ((c = getopt(ac, av, "b:hf:Rs:v")) != -1) { switch (c) { case 'b': if (sscanf(optarg, "%zu", &g_ctx.bsz) != 1) usage(av[0]); break; case 'f': g_ctx.conf = optarg; break; case 'v': g_ctx.verbose = true; break; case 's': if (sscanf(optarg, "%lld", (long long *)&g_ctx.sep.tv_sec) != 1) usage(av[0]); break; case 'R': g_ctx.rel_refs = true; break; case 'h': default: usage(av[0]); } } /* Load configuration from file. */ dbg_err_ifm(u_config_load_from_file(g_ctx.conf, &cfg), "error loading %s", g_ctx.conf); /* Initialize libevent and evcoap machinery. */ dbg_err_ifm(server_init(), "evcoap initialization failed"); /* Bind configured addresses. */ dbg_err_ifm(server_bind(cfg), "server socket setup failed"); /* Setup configured virtual hosts. */ for (i = 0; (vhost = u_config_get_child_n(cfg, "vhost", i)) != NULL; ++i) dbg_err_ifm(vhost_setup(vhost), "configuration error"); dbg_err_ifm(i == 0, "no origins configured"); #if 0 /* Attach create() as the URI fallback handler. */ dbg_err_ifm(ec_register_fb(g_ctx.coap, create, NULL), "error registering fallback"); #endif dbg_err_ifm(server_run(), "server run failed"); return EXIT_SUCCESS; err: if (cfg) u_config_free(cfg); server_term(); return EXIT_FAILURE; }
static int main_loop (void *arg) { ICI *d = arg; struct sockaddr_in addr; int rv = 0; #ifndef HAVE_WINDOWS signal(SIGPIPE, SIG_IGN); #endif if ((d->fd = create_server_socket()) < 0) {rv = -1; goto daemon_end;} server_sockaddr(d, &addr); if(server_bind(d, addr)) {rv = -1; goto daemon_end;} if (d->uid || d->gid) { if (drop_privileges(d->uid, d->gid)) {rv = -1; goto daemon_end;} } if (access(d->docroot, R_OK)) { dlog(DLOG_CRIT, "SRV: can not read document-root (permission denied)\n"); rv = -1; goto daemon_end; } global_shutdown = 0; #ifdef CATCH_SIGNALS signal(SIGHUP, catchsig); signal(SIGINT, catchsig); #endif #ifdef USAGE_FREQUENCY_STATISTICS d->stat_start = time(NULL); #endif while(d->run && !global_shutdown) { fd_set rfds; struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(d->fd, &rfds); // select() returns 0 on timeout, -1 on error. if((select(d->fd+1, &rfds, NULL, NULL, &tv))<0) { dlog(DLOG_WARNING, "SRV: unable to select the socket: %s\n", strerror(errno)); if (errno != EINTR) { rv = -1; goto daemon_end; } else { continue; } } char *rh = NULL; unsigned short rp = 0; int s = -1; if(FD_ISSET(d->fd, &rfds)) { s = accept_connection(d, &rh, &rp); } else { d->age++; #ifdef USAGE_FREQUENCY_STATISTICS /* may not be accurate, select() may skip a second once in a while */ d->req_stats[time(NULL) % FREQ_LEN] = 0; #endif } if (s >= 0) { start_child(d, s, rh, rp); d->age=0; #ifdef USAGE_FREQUENCY_STATISTICS d->stat_count++; d->req_stats[time(NULL) % FREQ_LEN]++; #endif continue; // no need to check age. } if (d->timeout > 0 && d->age > d->timeout) { dlog(DLOG_INFO, "SRV: no request since %d seconds shutting down.\n", d->age); global_shutdown = 1; } } #ifdef CATCH_SIGNALS signal(SIGHUP, SIG_DFL); signal(SIGINT, SIG_DFL); #endif /* wait until all connections are closed */ int timeout = 31; if (d->num_clients > 0) dlog(DLOG_INFO, "SRV: server shutdown procedure: waiting up to %i sec for clients to disconnect..\n", timeout-1); #ifdef VERBOSE_SHUTDOWN printf("\n"); #endif while (d->num_clients> 0 && --timeout > 0) { #ifdef VERBOSE_SHUTDOWN if (timeout%3 == 0) printf("SRV: shutdown timeout (%i) \r", timeout); fflush(stdout); #endif mymsleep(1000); } #ifdef VERBOSE_SHUTDOWN printf("\n"); #endif if (d->num_clients > 0) { dlog(DLOG_WARNING, "SRV: Terminating with %d active connections.\n", d->num_clients); } else { dlog(DLOG_INFO, "SRV: Closed all connections.\n"); } daemon_end: close(d->fd); dlog(DLOG_CRIT, "SRV: server shut down.\n"); d->run = 0; if (d->local_addr) free(d->local_addr); pthread_mutex_destroy(&d->lock); free(d); #ifdef HAVE_WINDOWS WSACleanup(); #endif return(rv); }