struct task_t* task_dispatcher_enqueue_task(struct task_dispatcher_t* td, task_callback_pf callback, void* ctx, long delay, long interval) { struct task_t task = { .callback = callback, .ctx = ctx, .interval = interval, }; timestamp_set(&task.enqueue_time); timestamp_offset(&task.enqueue_time, &task.activate_time, delay); pthread_mutex_lock(&td->lock); struct task_t* queued_task = pool_get_next(td->tasks, NULL); struct task_t* after = NULL; while (queued_task != NULL && timestamp_diff(&queued_task->activate_time, &task.activate_time) <= 0) { after = queued_task; queued_task = pool_get_next(td->tasks, queued_task); } struct task_t* p = pool_insert(td->tasks, &task, after); LOGD("Enqueued task %p after %p", p, after); if (after == NULL) { pthread_cond_broadcast(&td->event); } pthread_mutex_unlock(&td->lock); return p; }
/* * Initialize database module * No function should be called before this */ db_con_t* db_init(const char* _url) { struct db_id* id; struct my_con* con; db_con_t* res; id = 0; res = 0; /* if called from PROC_MAIN, allow it only from mod_init( when pt==0)*/ if (is_main && fixup_complete){ LOG(L_ERR, "BUG: mysql: db_init: called from the main process," " ignoring...\n"); } if (!_url) { LOG(L_ERR, "db_init: Invalid parameter value\n"); return 0; } res = pkg_malloc(sizeof(db_con_t) + sizeof(struct my_con*)); if (!res) { LOG(L_ERR, "db_init: No memory left\n"); return 0; } memset(res, 0, sizeof(db_con_t) + sizeof(struct my_con*)); id = new_db_id(_url); if (!id) { LOG(L_ERR, "db_init: Cannot parse URL '%s'\n", _url); goto err; } /* Find the connection in the pool */ con = (struct my_con*)pool_get(id); if (!con) { DBG("db_init: Connection '%s' not found in pool\n", _url); /* Not in the pool yet */ con = new_connection(id); if (!con) { goto err; } pool_insert((struct pool_con*)con); } else { DBG("db_init: Connection '%s' found in pool\n", _url); } res->tail = (unsigned long)con; return res; err: if (id) free_db_id(id); if (res) pkg_free(res); return 0; }
/* * Initialize database module * No function should be called before this */ db_con_t* db_mysql_init(const char* _url) { struct db_id* id; struct my_con* con; db_con_t* res; id = 0; res = 0; if (!_url) { LM_ERR("invalid parameter value\n"); return 0; } res = pkg_malloc(sizeof(db_con_t) + sizeof(struct my_con*)); if (!res) { LM_ERR("no private memory left\n"); return 0; } memset(res, 0, sizeof(db_con_t) + sizeof(struct my_con*)); id = new_db_id(_url); if (!id) { LM_ERR("cannot parse URL '%s'\n", _url); goto err; } /* Find the connection in the pool */ con = (struct my_con*)pool_get(id); if (!con) { LM_DBG("connection '%s' not found in pool\n", _url); /* Not in the pool yet */ con = db_mysql_new_connection(id); if (!con) { goto err; } pool_insert((struct pool_con*)con); } else { LM_DBG("connection '%s' found in pool\n", _url); } res->tail = (unsigned long)con; return res; err: if (id) free_db_id(id); if (res) pkg_free(res); return 0; }
CK_RV C_UnwrapKey(CK_SESSION_HANDLE hSession, /* the session's handle */ CK_MECHANISM_PTR pMechanism, /* the unwrapping mechanism */ CK_OBJECT_HANDLE hUnwrappingKey, /* handle of the unwrapping key */ CK_BYTE_PTR pWrappedKey, /* the wrapped key */ CK_ULONG ulWrappedKeyLen, /* bytes length of wrapped key */ CK_ATTRIBUTE_PTR pTemplate, /* template for the new key */ CK_ULONG ulAttributeCount, /* # of attributes in template */ CK_OBJECT_HANDLE_PTR phKey) /* gets handle of recovered key */ { struct sc_pkcs11_session *session; struct sc_pkcs11_object *object, *result; int rv; rv = sc_pkcs11_lock(); if (rv != CKR_OK) return rv; rv = pool_find(&session_pool, hSession, (void**) &session); if (rv != CKR_OK) goto out; rv = pool_find(&session->slot->object_pool, hUnwrappingKey, (void**) &object); if (rv != CKR_OK) goto out; if (object->ops->sign == NULL_PTR) { rv = CKR_KEY_TYPE_INCONSISTENT; goto out; } rv = object->ops->unwrap_key(session, object, pMechanism, pWrappedKey, ulWrappedKeyLen, pTemplate, ulAttributeCount, (void **) &result); sc_debug(context, "Unwrapping result was %d\n", rv); if (rv == CKR_OK) rv = pool_insert(&session->slot->object_pool, result, phKey); out: sc_pkcs11_unlock(); return rv; }
int main(int argc , char *argv[]) { char *ip = argv[1]; /* local ip addr */ char *port = argv[2]; /* local port */ int sockfd , listenfd , accepfd , optval=1 ; struct sockaddr_in laddr, raddr; socklen_t rlen = sizeof(struct sockaddr); laddr.sin_family = AF_INET; laddr.sin_addr.s_addr = inet_addr(ip); laddr.sin_port = htons(atoi(port)); pthread_t tid ; if(argc < 2) { printf("argc < 2 \n"); return -1 ; } if (-1 == (listenfd = socket(AF_INET, SOCK_STREAM, 0))) /* /etc/protocol s lists "protocol <-> number" */ { printf("sockfd function is failed ,errno is :%s",strerror(errno)); return -1 ; } #if 1 /*eliminates "address already in use " error form bind */ if(-1==setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,(const void *)&optval ,sizeof(int))) { printf("setsockopt function is error , errno is : %s \n",strerror(errno)); return -1 ; } #endif /* bind, syscall : bind */ if (-1 == bind(listenfd, (struct sockaddr *)&laddr, sizeof(struct sockaddr))) { printf("bind function is error , errno is : %s \n",strerror(errno)); return -1 ; } if (-1 == listen(listenfd, 5)) /* "man 2 listen" for detail information about argument 2 (backlog). in linux kernel 3.x, it doesn't care the backlog */ { printf("listen function is error , errno is : %d \n",errno); return -1 ; } #if 1 int val ; if ((val = fcntl(listenfd , F_GETFL , 0)) == -1 ) { printf("fcntl is failed ,errno %d\n",errno); return -1; } if (fcntl(listenfd , F_SETFL , val|O_NONBLOCK) == -1 ) { printf("fcntl1 is failed \n"); return -1; } #endif init_pool(&sp,Num_slots); for(int i = 0 ;i < Num_Thread ; i++) pthread_create(&tid,NULL,my_thread,NULL); while(1) { if(-1 == (accepfd = accept(listenfd,(struct sockaddr *)&raddr,&rlen)) ) { if(errno != EWOULDBLOCK || errno != EAGAIN) { printf("accept function is error , errno is : %d \n",errno); return -1 ; } } else { printf("arrive \n"); pool_insert(accepfd , &sp); } } return 0 ; }
void* pool_add(struct pool_t* pool, void* item) { return pool_insert(pool, item, NULL); }