/*Initialization of group*/ void buxtond_set_group(char *group, char *layer) { client_connection(); save_errno(); if(_layer){ free(_layer); } if(_group){ free(_group); } _layer = strdup(layer); if(!_layer){ printf("Layer assignment failed. Aborting operation.\n"); return; } _group = strdup(group); if(!_group){ printf("Group assignment failed. Aborting operation.\n"); return; } BuxtonKey g = buxton_key_create(_group, NULL, _layer, STRING); int status; if (buxton_create_group(client, g, cg_cb, &status, true)){ printf("Create group call failed.\n"); } else { printf("Switched to group: %s, layer: %s.\n", buxton_key_get_group(g), buxton_key_get_layer(g)); errno = saved_errno; } buxton_key_free(g); }
void buxtond_remove_group(BuxtonKey group) { client_connection(); if (buxton_remove_group(client, group, rg_cb, NULL, true)){ printf("Remove group call failed.\n"); } }
void buxtond_remove_group2(char *group_name, char *layer) { client_connection(); BuxtonKey group = buxton_group_create(group_name, layer); if (buxton_remove_group(client, group, rg_cb, NULL, true)){ printf("Remove group call failed.\n"); } buxton_key_free(group); }
void prepare_process_sock (connection* ptr, int type) { if (type == 0) { get_socket_ID(&(SOCK_CAST(ptr->connection_params)->init_socket)); client_connection(SOCK_CAST(ptr->connection_params)->init_socket); } else { get_socket_ID(&(SOCK_CAST(ptr->connection_params)->server_socket)); server_connection(ptr); } }
/*create a buxtond side group*/ void buxtond_create_group(BuxtonKey group) { client_connection(); if (buxton_create_group(client, group, NULL, NULL, true)){ printf("Create group call failed.\n"); buxton_key_free(group); return; } /*TODO*/ //buxton_key_free(group); }
BuxtonKey buxtond_create_group2(char *group_name, char *layer) { client_connection(); BuxtonKey group = buxton_key_create(group_name, NULL, layer, STRING); if (buxton_create_group(client, group, NULL, NULL, true)){ printf("Create group call failed.\n"); buxton_key_free(group); return; } return group; /*Buxton free key? TODO*/ }
int main(int argc, char *argv[]) { int sockfd; int retcode = EXIT_SUCCESS; // read program options if (get_options(argc, argv, &my_opt) == 0) { print_usage(my_opt.progname); exit(EXIT_FAILURE); } /* end if */ // set the time zone (TZ) to GMT in order to // ignore any other local time zone that would // interfere with correct time string parsing setenv("TZ", "GMT", 1); tzset(); /* printf("Server Port: %d\n", my_opt.server_port); //printf("Server Address: %d\n", my_opt.server_addr); printf("Progname: %s\n", my_opt.progname); printf("Root Dir: %s\n", my_opt.root_dir); printf("Log File: %s\n", my_opt.log_filename); */ // do some checks and initialisations... open_logfile(&my_opt); check_root_dir(&my_opt); install_signal_handlers(); init_logging_semaphore(); // TODO: start the server and handle clients... // here, as an example, show how to interact with the // condition set by the signal handler above //printf("[%d] Starting server '%s'...\n", getpid(), my_opt.progname); server_running = true; prog_options_t *opt = &my_opt; struct sockaddr_in* struct_port = (struct sockaddr_in*) opt->server_addr->ai_addr; sockfd = server_init(ntohs(struct_port->sin_port)); //printf("Port: %i \n", sockfd); while(server_running) { client_connection(sockfd); //pause(); } /* end while */ printf("[%d] Good Bye...\n", getpid()); exit(retcode); } /* end of main */
int main() { int to_client; int from_client; //char buffer[100]; while (1) { printf("<server> waiting for connection\n"); to_client = server_handshake( &from_client ); client_connection( to_client, from_client ); close( to_client ); } return 0; }
void buxtond_set_bool(char *key, bool value) { /*make sure client connection is open*/ client_connection(); /*create key*/ BuxtonKey _key = buxton_key_create(_group, strdup(key), _layer, BOOLEAN); /*Return value and status*/ struct vstatus ret; ret.type = BOOLEAN; ret.bval = value; save_errno(); if(buxton_set_value(client, _key, &value, bs_cb, &ret, true)){ printf("Set bool call failed.\n"); } if (!ret.status){ errno = EACCES; } else { errno = saved_errno; } buxton_key_free(_key); }
bool buxtond_get_bool(char *key) { /*make sure client connection is open*/ client_connection(); /*create key*/ BuxtonKey _key = buxton_key_create(_group, strdup(key), _layer, BOOLEAN); /*return value*/ struct vstatus ret; ret.type = BOOLEAN; save_errno(); /*get value*/ if (buxton_get_value(client, _key, bgb_cb, &ret, true)){ printf("Get bool call failed.\n"); } if (!ret.status){ errno = EACCES; } else { errno = saved_errno; } buxton_key_free(_key); return ret.bval; }
void parcelport::send_pending_parcels_trampoline( naming::locality const& locality_id) { parcelport_connection_ptr client_connection( connection_cache_.get(locality_id)); // If another thread was faster ... try again if (!client_connection) return; std::vector<parcel> parcels; std::vector<write_handler_type> handlers; typedef pending_parcels_map::iterator iterator; util::spinlock::scoped_lock l(mtx_); iterator it = pending_parcels_.find(locality_id); if (it != pending_parcels_.end()) { std::swap(parcels, it->second.first); std::swap(handlers, it->second.second); } if (!parcels.empty() && !handlers.empty()) { // create a new thread which sends parcels that might still be pending hpx::applier::register_thread_nullary( HPX_STD_BIND(&parcelport::send_pending_parcels, this, client_connection, boost::move(parcels), boost::move(handlers)), "send_pending_parcels"); } else { BOOST_ASSERT(locality_id == client_connection->destination()); connection_cache_.reclaim(locality_id, client_connection); } }
void numbers(void){ int now = glutGet(GLUT_ELAPSED_TIME); int elapsedMilliseconds = now - lastFrameTime; float elapsedTime = elapsedMilliseconds / 1000.0f; lastFrameTime = now; float h = elapsedTime; client_update(tclient, h); while (enet_host_service (enet_client, & event, 10) > 0 && client_connection(tclient)) { switch (event.type) { case ENET_EVENT_TYPE_RECEIVE: client_process_packets(tclient, &event); break; case ENET_EVENT_TYPE_DISCONNECT: client_disconnect(tclient); break; } } }
void buxtond_set_int32(char *key, int32_t value) { /*make sure client connection is open*/ client_connection(); /*check if a key has been created*/ /*create key */ BuxtonKey _key = buxton_key_create(_group, strdup(key), _layer, INT32); /*Return value and status*/ struct vstatus ret; ret.type = INT32; ret.i32val = value; save_errno(); /*call buxton_set_value for type INT32*/ if (buxton_set_value(client, _key, &value, bs_cb, &ret, true)){ printf("Set int32_t call failed.\n"); return; } if (!ret.status){ errno = EACCES; } else { errno = saved_errno; } buxton_key_free(_key); }
parcelport_connection_ptr parcelport::create_connection( naming::locality const& l, error_code& ec) { boost::asio::io_service& io_service = io_service_pool_.get_io_service(); // The parcel gets serialized inside the connection constructor, no // need to keep the original parcel alive after this call returned. parcelport_connection_ptr client_connection( new parcelport_connection(io_service, here_, l, data_buffer_cache_, parcels_sent_, ++connection_count_)); // Connect to the target locality, retry if needed boost::system::error_code error = boost::asio::error::try_again; for (std::size_t i = 0; i < HPX_MAX_NETWORK_RETRIES; ++i) { try { naming::locality::iterator_type end = connect_end(l); for (naming::locality::iterator_type it = connect_begin(l, io_service); it != end; ++it) { boost::asio::ip::tcp::endpoint const& ep = *it; std::string fullname(ep.address().to_string() + "." + boost::lexical_cast<std::string>(ep.port())); parcelset::shmem::data_window& w = client_connection->window(); w.close(); w.connect(fullname, error); if (!error) break; } if (!error) break; // wait for a really short amount of time this_thread::suspend(); } catch (boost::system::system_error const& e) { client_connection->window().close(); client_connection.reset(); HPX_THROWS_IF(ec, network_error, "shmem::parcelport::get_connection", e.what()); return client_connection; } } if (error) { client_connection->window().close(); client_connection.reset(); hpx::util::osstream strm; strm << error.message() << " (while trying to connect to: " << l << ")"; HPX_THROWS_IF(ec, network_error, "shmem::parcelport::get_connection", hpx::util::osstream_get_string(strm)); return client_connection; } if (&ec != &throws) ec = make_success_code(); return client_connection; }
parcelport_connection_ptr parcelport::create_connection( naming::locality const& l, error_code& ec) { boost::asio::io_service& io_service = io_service_pool_.get_io_service(); // The parcel gets serialized inside the connection constructor, no // need to keep the original parcel alive after this call returned. parcelport_connection_ptr client_connection(new parcelport_connection( io_service, l, parcels_sent_)); // Connect to the target locality, retry if needed boost::system::error_code error = boost::asio::error::try_again; for (std::size_t i = 0; i < HPX_MAX_NETWORK_RETRIES; ++i) { try { naming::locality::iterator_type end = connect_end(l); for (naming::locality::iterator_type it = connect_begin(l, io_service); it != end; ++it) { boost::asio::ip::tcp::endpoint const& ep = *it; parcelset::ibverbs::client_context& ctx = client_connection->context(); ctx.close(); ctx.connect(ep, error); if (!error) break; } if (!error) break; // wait for a really short amount of time if (hpx::threads::get_self_ptr()) { this_thread::suspend(); } else { boost::this_thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds( HPX_NETWORK_RETRIES_SLEEP)); } } catch (boost::system::system_error const& e) { client_connection->context().close(); client_connection.reset(); HPX_THROWS_IF(ec, network_error, "ibverbs::parcelport::get_connection", e.what()); return client_connection; } } if (error) { client_connection->context().close(); client_connection.reset(); hpx::util::osstream strm; strm << error.message() << " (while trying to connect to: " << l << ")"; HPX_THROWS_IF(ec, network_error, "ibverbs::parcelport::get_connection", hpx::util::osstream_get_string(strm)); return client_connection; } if (&ec != &throws) ec = make_success_code(); return client_connection; }
static bool run_daemon() { int fd; struct sockaddr_un addr; fd = socket(AF_LOCAL, SOCK_STREAM, 0); if (fd < 0) { LOGE("Failed to create socket: %s", strerror(errno)); return false; } auto close_fd = util::finally([&] { close(fd); }); char abs_name[] = "\0mbtool.daemon"; size_t abs_name_len = sizeof(abs_name) - 1; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; memcpy(addr.sun_path, abs_name, abs_name_len); // Calculate correct length so the trailing junk is not included in the // abstract socket name socklen_t addr_len = offsetof(struct sockaddr_un, sun_path) + abs_name_len; if (bind(fd, (struct sockaddr *) &addr, addr_len) < 0) { LOGE("Failed to bind socket: %s", strerror(errno)); LOGE("Is another instance running?"); return false; } if (listen(fd, 3) < 0) { LOGE("Failed to listen on socket: %s", strerror(errno)); return false; } // Let parent process know that we're ready if we're forking the daemon if (send_ok_to_pipe) { ssize_t n = write(pipe_fds[1], "", 1); close(pipe_fds[1]); if (n < 0) { LOGE("Failed to send OK to parent process"); return false; } } else if (sigstop_when_ready) { kill(getpid(), SIGSTOP); } // Eat zombies! // SIG_IGN reaps zombie processes (it's not just a dummy function) struct sigaction sa; sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) < 0) { LOGE("Failed to set SIGCHLD handler: %s", strerror(errno)); return false; } LOGD("Socket ready, waiting for connections"); int client_fd; while ((client_fd = accept(fd, nullptr, nullptr)) >= 0) { pid_t child_pid = fork(); if (child_pid < 0) { LOGE("Failed to fork: %s", strerror(errno)); } else if (child_pid == 0) { if (!no_unshare) { if (unshare(CLONE_NEWNS) < 0) { LOGE("unshare() failed: %s", strerror(errno)); _exit(127); } if (mount("", "/", "", MS_PRIVATE | MS_REC, "") < 0) { LOGE("Failed to set private mount propagation: %s", strerror(errno)); _exit(127); } } // Change the process name so --replace doesn't kill existing // connections if (!util::set_process_title_v( nullptr, "mbtool connection initializing")) { LOGE("Failed to set process title: %s", strerror(errno)); _exit(127); } // Restore default SIGCHLD handler struct sigaction sa; sa.sa_handler = SIG_DFL; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) < 0) { LOGE("Failed to set default SIGCHLD handler: %s", strerror(errno)); _exit(127); } // Don't need the listening socket fd close(fd); bool ret = client_connection(client_fd); close(client_fd); _exit(ret ? EXIT_SUCCESS : EXIT_FAILURE); } close(client_fd); } if (client_fd < 0) { LOGE("Failed to accept connection on socket: %s", strerror(errno)); return false; } return true; }
static bool run_daemon(void) { int fd; struct sockaddr_un addr; fd = socket(AF_LOCAL, SOCK_STREAM, 0); if (fd < 0) { LOGE("Failed to create socket: %s", strerror(errno)); return false; } auto close_fd = util::finally([&] { close(fd); }); char abs_name[] = "\0mbtool.daemon"; size_t abs_name_len = sizeof(abs_name) - 1; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; memcpy(addr.sun_path, abs_name, abs_name_len); // Calculate correct length so the trailing junk is not included in the // abstract socket name socklen_t addr_len = offsetof(struct sockaddr_un, sun_path) + abs_name_len; if (bind(fd, (struct sockaddr *) &addr, addr_len) < 0) { LOGE("Failed to bind socket: %s", strerror(errno)); LOGE("Is another instance running?"); return false; } if (listen(fd, 3) < 0) { LOGE("Failed to listen on socket: %s", strerror(errno)); return false; } // Eat zombies! // SIG_IGN reaps zombie processes (it's not just a dummy function) struct sigaction sa; sa.sa_handler = SIG_IGN; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; if (sigaction(SIGCHLD, &sa, 0) < 0) { LOGE("Failed to set SIGCHLD handler: %s", strerror(errno)); return false; } LOGD("Socket ready, waiting for connections"); int client_fd; while ((client_fd = accept(fd, nullptr, nullptr)) >= 0) { pid_t child_pid = fork(); if (child_pid < 0) { LOGE("Failed to fork: %s", strerror(errno)); } else if (child_pid == 0) { bool ret = client_connection(client_fd); close(client_fd); _exit(ret ? EXIT_SUCCESS : EXIT_FAILURE); } close(client_fd); } if (client_fd < 0) { LOGE("Failed to accept connection on socket: %s", strerror(errno)); return false; } return true; }
/*create a client side group TODO: create BuxtonGroup type*/ BuxtonKey buxton_group_create(char *name, char *layer) { client_connection(); BuxtonKey ret = buxton_key_create(name, NULL, layer, STRING); return ret; }