void slayer_server_stats_timer_thread(apr_pool_t *mpool,slayer_server_stats_t *stats) { apr_threadattr_t *thread_attr; apr_thread_t *thread; apr_threadattr_create(&thread_attr,mpool); apr_threadattr_detach_set(thread_attr,1); // detach apr_threadattr_stacksize_set(thread_attr,4096*10); apr_thread_create(&thread,thread_attr,slayer_server_stats_timer_thread_run,stats,mpool); }
int openenv(DB_ENV **pdb_env,const char *home,const char *data_dir, const char *log_dir,FILE *err_file, int cachesize,unsigned int flag,apr_pool_t *p) { int rv = -1; if((rv = db_env_create(pdb_env,0)) != 0){ return rv; } if((rv = (*pdb_env)->set_cachesize((*pdb_env),0,cachesize,0)) != 0){ return rv; } if((rv = (*pdb_env)->set_data_dir((*pdb_env),data_dir)) != 0){ return rv; } if((rv = (*pdb_env)->set_lg_dir((*pdb_env),log_dir)) != 0){ return rv; } if((rv = (*pdb_env)->set_lk_detect((*pdb_env),DB_LOCK_DEFAULT)) != 0){ return rv; } if((rv = (*pdb_env)->set_tx_max((*pdb_env),1024)) != 0){ return rv; } if((rv = (*pdb_env)->set_lg_bsize((*pdb_env),cachesize)) != 0){ return rv; } if((rv =(*pdb_env)->set_flags((*pdb_env), DB_TXN_NOSYNC,1) !=0)) { return rv; } if((rv = (*pdb_env)->open((*pdb_env),home,flag,0)) != 0){ (*pdb_env)->close((*pdb_env),0); return rv; } if((rv=(*pdb_env)->log_set_config((*pdb_env),DB_LOG_AUTO_REMOVE,1) != 0)) { return rv; } apr_threadattr_create(&thread_attr,p); apr_threadattr_detach_set(thread_attr,1); rv = apr_thread_create(&chkpnt_threadid,thread_attr,chkpnt_thread,*pdb_env,p); if(rv != APR_SUCCESS){ return -1; } return 0; }
static int selinux_handler(request_rec *r) { apr_threadattr_t *thread_attr; apr_thread_t *thread; apr_status_t rv, thread_rv; /* * If the hook is invoked under the worker context, * we simply skips this module. */ if (am_worker) return DECLINED; apr_threadattr_create(&thread_attr, r->pool); /* 0 means PTHREAD_CREATE_JOINABLE */ apr_threadattr_detach_set(thread_attr, 0); rv = apr_thread_create(&thread, thread_attr, selinux_worker_handler, r, r->pool); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r, "Unable to launch a one-time worker thread"); return HTTP_INTERNAL_SERVER_ERROR; } rv = apr_thread_join(&thread_rv, thread); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, errno, r, "Unable to join the one-time worker thread"); /* kill itself to clean up the thread */ r->connection->aborted = 1; return HTTP_INTERNAL_SERVER_ERROR; } return thread_rv; }
SWITCH_DECLARE(switch_status_t) switch_threadattr_detach_set(switch_threadattr_t *attr, int32_t on) { return apr_threadattr_detach_set(attr, on); }
void * CALLBACK mosquitto_on_connect(const WebSocketServer *server) { MosquittoData *dib = NULL; apr_sockaddr_t *sa; if ((server != NULL) && (server->version == WEBSOCKET_SERVER_VERSION_1)) { /* Get access to the request_rec strucure for this connection */ request_rec *r = server->request(server); if (r != NULL) { apr_pool_t *pool = NULL; size_t i, count = server->protocol_count(server); /* Only support "mqtt" */ for (i = 0; i < count; i++) { const char *protocol = server->protocol_index(server, i); if ((protocol != NULL) && (strncmp(protocol, "mqtt",4) == 0)) { /* If the client can speak the protocol, set it in the response */ server->protocol_set(server, protocol); break; } } /* If the protocol negotiation worked, create a new memory pool */ if ((i < count) && (apr_pool_create(&pool, r->pool) == APR_SUCCESS)) { /* Allocate memory to hold mosquitto state */ if ((dib = (MosquittoData *) apr_palloc(pool, sizeof(MosquittoData))) != NULL) { apr_thread_t *thread = NULL; apr_threadattr_t *thread_attr = NULL; dib->server = server; dib->pool = pool; dib->thread = NULL; dib->counter = 0; dib->active = 1; request_rec *r = server->request(server); mosquitto_cfg* dir = ap_get_module_config(r->per_dir_config, &mod_websocket_mosquitto) ; int rv = apr_sockaddr_info_get(&sa,dir->broker,APR_UNSPEC,atoi(dir->port),APR_IPV6_ADDR_OK ,pool); if (rv) ap_log_error(APLOG_MARK, APLOG_CRIT,0,NULL,"apr_sockaddr_info_get failed #%x",rv); else ap_log_error(APLOG_MARK, APLOG_DEBUG,0,NULL,"Address family %s",sa->family == APR_INET6 ? "IPv6" : "IPv4"); rv = apr_socket_create(&dib->sockfd,sa->family, SOCK_STREAM, APR_PROTO_TCP,pool); if (rv) ap_log_error(APLOG_MARK, APLOG_CRIT,0,NULL,"apr_socket_create failed #%x",rv); rv = apr_socket_connect(dib->sockfd,sa); if (rv) ap_log_error(APLOG_MARK, APLOG_CRIT,0,NULL,"apr_socket_connect failed #%x",rv); /* Create a non-detached thread that will perform the work */ if ((apr_threadattr_create(&thread_attr, pool) == APR_SUCCESS) && (apr_threadattr_detach_set(thread_attr, 0) == APR_SUCCESS) && (apr_thread_create(&thread, thread_attr, mosquitto_run, dib, pool) == APR_SUCCESS)) { dib->thread = thread; /* Success */ pool = NULL; } else { dib = NULL; } } if (pool != NULL) { apr_pool_destroy(pool); } } } } return dib; }
static int process_security_handler(request_rec *r) { int i; const char *extension; apr_threadattr_t *thread_attr; apr_thread_t *thread; apr_status_t status, thread_status; int enable = 0; int name_len = 0; process_security_config_t *conf = ap_get_module_config(r->server->module_config, &process_security_module); if (thread_on) return DECLINED; if (conf->all_ext_enable) { enable = ON; } else { for (i = 0; i < conf->extensions->nelts; i++) { extension = ((char **)conf->extensions->elts)[i]; name_len = strlen(r->filename) - strlen(extension); if (name_len >= 0 && strcmp(&r->filename[name_len], extension) == 0) enable = ON; } } if (!enable) return DECLINED; apr_threadattr_create(&thread_attr, r->pool); apr_threadattr_detach_set(thread_attr, 0); status = apr_thread_create(&thread , thread_attr , process_security_thread_handler , r , r->pool); if (status != APR_SUCCESS) { ap_log_error (APLOG_MARK , APLOG_ERR , 0 , NULL , "%s ERROR %s: Unable to create a thread" , MODULE_NAME , __func__ ); return HTTP_INTERNAL_SERVER_ERROR; } status = apr_thread_join(&thread_status, thread); if (status != APR_SUCCESS) { ap_log_error (APLOG_MARK , APLOG_ERR , 0 , NULL , "%s ERROR %s: Unable to join a thread" , MODULE_NAME , __func__ ); r->connection->aborted = 1; return HTTP_INTERNAL_SERVER_ERROR; } return thread_status; }
static int build_startup_data(const WebSocketServer *server) { apr_pool_t *main_pool = NULL; apr_pool_t *stateful_session_pool = NULL; apr_thread_t *thread = NULL; apr_threadattr_t *thread_attr = NULL; apr_thread_mutex_t *mutex = NULL; request_rec *r = server->request(server); // create a pool for our translator data // Do not use r->pool as the parent, since r->pool will be freed // when the current client disconnects. if (apr_pool_create(&main_pool, NULL) != APR_SUCCESS) { osrfLogError(OSRF_LOG_MARK, "WS Unable to create apr_pool"); return 1; } trans = (osrfWebsocketTranslator*) apr_palloc(main_pool, sizeof(osrfWebsocketTranslator)); if (trans == NULL) { osrfLogError(OSRF_LOG_MARK, "WS Unable to create translator"); return 1; } trans->server = server; trans->main_pool = main_pool; trans->osrf_router = osrfConfigGetValue(NULL, "/router_name"); trans->osrf_domain = osrfConfigGetValue(NULL, "/domain"); // opensrf session / recipient cache trans->stateful_session_cache = apr_hash_make(trans->main_pool); if (trans->stateful_session_cache == NULL) { osrfLogError(OSRF_LOG_MARK, "WS unable to create session cache"); return 1; } // opensrf session / recipient string pool; cleared regularly // the only data entering this pools are the session strings. if (apr_pool_create(&stateful_session_pool, trans->main_pool) != APR_SUCCESS) { osrfLogError(OSRF_LOG_MARK, "WS Unable to create apr_pool"); return NULL; } trans->stateful_session_pool = stateful_session_pool; if (apr_thread_mutex_create( &mutex, APR_THREAD_MUTEX_UNNESTED, trans->main_pool) != APR_SUCCESS) { osrfLogError(OSRF_LOG_MARK, "WS unable to create thread mutex"); return 1; } trans->mutex = mutex; // responder thread if ( (apr_threadattr_create(&thread_attr, trans->main_pool) == APR_SUCCESS) && (apr_threadattr_detach_set(thread_attr, 0) == APR_SUCCESS) && (apr_thread_create(&thread, thread_attr, osrf_responder_thread_main, trans, trans->main_pool) == APR_SUCCESS)) { trans->responder_thread = thread; } else { osrfLogError(OSRF_LOG_MARK, "WS unable to create responder thread"); return 1; } // idle timeout thread thread = NULL; // reset thread_attr = NULL; // reset if ( (apr_threadattr_create(&thread_attr, trans->main_pool) == APR_SUCCESS) && (apr_threadattr_detach_set(thread_attr, 0) == APR_SUCCESS) && (apr_thread_create(&thread, thread_attr, osrf_idle_timeout_thread_main, trans, trans->main_pool) == APR_SUCCESS)) { osrfLogDebug(OSRF_LOG_MARK, "WS created idle timeout thread"); trans->idle_timeout_thread = thread; } else { osrfLogError(OSRF_LOG_MARK, "WS unable to create idle timeout thread"); return 1; } return APR_SUCCESS; }
void *CALLBACK tcp_proxy_on_connect(const WebSocketServer * server) { TcpProxyData *tpd = NULL; if ((server != NULL) && (server->version == WEBSOCKET_SERVER_VERSION_1)) { /* Get access to the request_rec strucure for this connection */ request_rec *r = server->request(server); APACHELOG(APLOG_DEBUG, r, "tcp_proxy_on_connect starting"); if (r != NULL) { apr_pool_t *pool = NULL; size_t i = 0, count = server->protocol_count(server); websocket_tcp_proxy_config_rec *conf = (websocket_tcp_proxy_config_rec *) ap_get_module_config(r-> per_dir_config, &websocket_tcp_proxy_module); const char *requiredprotocol = conf ? conf->protocol : NULL; if (requiredprotocol) { for (i = 0; i < count; i++) { const char *protocol = server->protocol_index(server, i); if (protocol && (strcmp(protocol, requiredprotocol) == 0)) { /* If the client can speak the protocol, set it in the response */ server->protocol_set(server, protocol); break; } } } else { count = 1; /* ensure i<count */ } /* If the protocol negotiation worked, create a new memory pool */ if ((i < count) && (apr_pool_create(&pool, r->pool) == APR_SUCCESS)) { APACHELOG(APLOG_DEBUG, r, "tcp_proxy_on_connect protocol correct"); /* Allocate memory to hold the tcp proxy state */ if ((tpd = (TcpProxyData *) apr_palloc(pool, sizeof(TcpProxyData))) != NULL) { apr_thread_t *thread = NULL; apr_threadattr_t *thread_attr = NULL; tpd->server = server; tpd->pool = pool; tpd->thread = NULL; tpd->tcpsocket = NULL; tpd->active = 1; tpd->base64 = 0; tpd->timeout = 30; tpd->port = "echo"; tpd->host = "127.0.0.1"; tpd->initialdata = NULL; websocket_tcp_proxy_config_rec *conf = (websocket_tcp_proxy_config_rec *) ap_get_module_config(r->per_dir_config, &websocket_tcp_proxy_module); if (conf) { tpd->base64 = conf->base64; tpd->timeout = conf->timeout; if (conf->host) tpd->host = apr_pstrdup(pool, conf->host); if (conf->port) tpd->port = apr_pstrdup(pool, conf->port); APACHELOG(APLOG_DEBUG, r, "tcp_proxy_on_connect: base64 is %d", conf->base64); } else { APACHELOG(APLOG_DEBUG, r, "tcp_proxy_on_connect: no config"); } /* Check we can authenticate the incoming user (this is a hook for others to add to) * Check we can connect * And if we have initial data to send, then send that */ if ((APR_SUCCESS == tcp_proxy_do_authenticate(r, tpd, pool)) && (APR_SUCCESS == tcp_proxy_do_tcp_connect(r, tpd, pool)) && (APR_SUCCESS == tcp_proxy_send_initial_data(r, tpd, pool))) { /* Create a non-detached thread that will perform the work */ if ((apr_threadattr_create(&thread_attr, pool) == APR_SUCCESS) && (apr_threadattr_detach_set(thread_attr, 0) == APR_SUCCESS) && (apr_thread_create (&thread, thread_attr, tcp_proxy_run, tpd, pool) == APR_SUCCESS)) { tpd->thread = thread; /* Success */ pool = NULL; } else { tpd = NULL; } } else tpd = NULL; } if (pool != NULL) { apr_pool_destroy(pool); } } } } return tpd; }