static ret_t spawn_init (void) { int re; int fd; int mem_len; /* Names */ mem_len = sizeof("/cherokee-spawner-XXXXXXX"); spawn_shared_name = malloc (mem_len); snprintf (spawn_shared_name, mem_len, "/cherokee-spawner-%d", getpid()); /* Create the shared memory */ fd = shm_open (spawn_shared_name, O_RDWR | O_EXCL | O_CREAT, 0600); if (fd < 0) { return ret_error; } re = ftruncate (fd, SPAWN_SHARED_LEN); if (re < 0) { close (fd); return ret_error; } spawn_shared = mmap (0, SPAWN_SHARED_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (spawn_shared == MAP_FAILED) { close (fd); spawn_shared = NULL; return ret_error; } memset (spawn_shared, 0, SPAWN_SHARED_LEN); close (fd); /* Semaphore */ spawn_shared_sem = sem_new(); if (spawn_shared_sem < 0) { return ret_error; } if (worker_uid != NULL) { sem_chmod (spawn_shared_sem, worker_uid); } /* Thread */ re = pthread_create (&spawn_thread, NULL, spawn_thread_func, NULL); if (re != 0) { PRINT_MSG_S ("(critical) Couldn't spawning thread..\n"); return ret_error; } return ret_ok; }
/** * Initializes the worker structures, like the task queue. */ void worker_init() { tasks = shm_malloc(sizeof(task_queue_t)); tasks->lock = lock_alloc(); tasks->lock = lock_init(tasks->lock); sem_new(tasks->empty,0); sem_new(tasks->full,1); tasks->start = 0; tasks->end = 0; tasks->max = config->queue_length; tasks->queue = shm_malloc(tasks->max*sizeof(task_t)); if (!tasks->queue) { LOG_NO_MEM("shm",tasks->max*sizeof(task_t)); goto out_of_memory; } memset(tasks->queue,0,tasks->max*sizeof(task_t)); callbacks = shm_malloc(sizeof(cdp_cb_list_t)); if (!callbacks) goto out_of_memory; callbacks->head = 0; callbacks->tail = 0; return; out_of_memory: if (tasks){ if (tasks->lock) { lock_destroy(tasks->lock); lock_dealloc(&(tasks->lock)); } sem_free(tasks->full); sem_free(tasks->empty); if (tasks->queue) shm_free(tasks->queue); shm_free(tasks); } if (callbacks) shm_free(callbacks); }
/** * Send a AAAMessage synchronously. * This blocks until a response is received or a transactional time-out happens. * @param message - the request to be sent * @param peer_id - FQDN of the peer to send * @returns 1 on success, 0 on failure * \todo remove peer_id and add Realm routing * \todo replace the busy-waiting lock in here with one that does not consume CPU */ AAAMessage* AAASendRecvMessageToPeer(AAAMessage *message, str *peer_id) { peer *p; gen_sem_t *sem; cdp_trans_t *t; AAAMessage *ans; p = get_peer_by_fqdn(peer_id); if (!p) { LOG(L_ERR,"ERROR:AAASendRecvMessageToPeer(): Peer unknown %.*s\n",peer_id->len,peer_id->s); goto error; } if (p->state!=I_Open && p->state!=R_Open){ LOG(L_ERR,"ERROR:AAASendRecvMessageToPeer(): Peer not connected to %.*s\n",peer_id->len,peer_id->s); goto error; } if (is_req(message)){ sem_new(sem,0); t = cdp_add_trans(message,sendrecv_cb,(void*)sem,config->transaction_timeout,0); // if (!peer_send_msg(p,message)) { if (!sm_process(p,Send_Message,message,0,0)){ sem_free(sem); goto error; } /* block until callback is executed */ while(sem_get(sem)<0){ if (shutdownx&&(*shutdownx)) goto error; LOG(L_WARN,"WARN:AAASendRecvMessageToPeer(): interrupted by signal or something > %s\n",strerror(errno)); } sem_free(sem); ans = t->ans; cdp_free_trans(t); return ans; } else { LOG(L_ERR,"ERROR:AAASendRecvMessageToPeer(): can't add wait for answer to answer.\n"); goto error; } error: out_of_memory: AAAFreeMessage(&message); return 0; }
int init_cdp_cb_event_list() { cdp_event_list = shm_malloc(sizeof (cdp_cb_event_list_t)); if (!cdp_event_list) { LM_ERR("No more SHM mem\n"); return 0; } memset(cdp_event_list, 0, sizeof (cdp_cb_event_list_t)); cdp_event_list->lock = lock_alloc(); if (!cdp_event_list->lock) { LM_ERR("failed to create cdp event list lock\n"); return 0; } cdp_event_list->lock = lock_init(cdp_event_list->lock); sem_new(cdp_event_list->empty, 0); //pre-locked - as we assume list is empty at start return 1; }
int init_reginfo_event_list() { if (reginfo_event_list) return 1; reginfo_event_list = shm_malloc(sizeof(reginfo_event_list_t)); if (!reginfo_event_list) { LM_ERR("No more SHM mem\n"); return 0; } memset(reginfo_event_list, 0, sizeof(reginfo_event_list_t)); reginfo_event_list->lock = lock_alloc(); if (!reginfo_event_list->lock) { LM_ERR("failed to create reginfo event list lock\n"); return 0; } reginfo_event_list->lock = lock_init(reginfo_event_list->lock); reginfo_event_list->size = 0; sem_new(reginfo_event_list->empty, 0); //pre-locked - as we assume list is empty at start return 1; }
/*---------------------------------------------------------------------------*/ usys stdio_init (void) { usys stat ; usys id ; /*********/ /* Input */ /*********/ /* Create an empty counting semaphore of buffer size for the receiver */ stat = sem_new (BUF_SIZE, 0, "STDIO: rx_sem", &rx_sem) ; if (stat != GOOD) { printf ("ERR: (STDIO) Creating rx_sem. Stat = %d\n", stat) ; /* Graceful Exit <-- DO */ return BAD ; } rx_p = rx_c = 0 ; /**********/ /* Output */ /**********/ /* STD-Output Task */ stat = task_new (2, 0, 1024, 0,0, stdout_task, 0,0,0, "stdout_task", &id) ; if (stat != GOOD) { printf ("ERR: (STDIO) Creating stdout_task. Stat = %d\n", stat) ; /* Graceful Exit <-- DO */ return BAD ; } /* Create a full binary semaphore for putchar() */ stat = sem_new (1, 1, "STDIO: out_free_sem", &putchar_sem) ; if (stat != GOOD) { printf ("ERR: (STDIO) Creating putchar_sem. Stat = %d\n", stat) ; /* Graceful Exit <-- DO */ return BAD ; } /* Create a empty binary semaphore to wake-up the stdout_task() */ stat = sem_new (BUF_SIZE, 0, "STDIO: tx_task_sem", &tx_task_sem) ; if (stat != GOOD) { printf ("ERR: Cannot Create STDIO: tx_task_sem. Stat = %d\n", stat) ; /* Graceful Exit <-- DO */ return BAD ; } /* Create a full counting semaphore of buffer size for the transmitter */ stat = sem_new (BUF_SIZE, BUF_SIZE, "STDIO: tx_buf_sem", &tx_buf_sem) ; if (stat != GOOD) { printf ("ERR: (STDIO) Creating out_buf_sem. Stat = %d\n", stat) ; /* Graceful Exit <-- DO */ return BAD ; } tx_p = tx_c = 0 ; /*********/ /* Misc. */ /*********/ /* Initialize the stdio interface structure */ memset (&stdio_link, 0, sizeof (link_t)) ; stdio_link.rx_func = rx_func ; return GOOD ; } /* End of function stdio_init() */