예제 #1
0
uint32_t ncs_sel_obj_create(NCS_SEL_OBJ *o_sel_obj)
{
	int s_pair[2];
	int flags = 0;

	osaf_mutex_lock_ordie(&s_cloexec_mutex);
	if (0 != socketpair(AF_UNIX, SOCK_STREAM, 0, s_pair)) {
		syslog(LOG_ERR, "%s: socketpair failed - %s", __FUNCTION__, strerror(errno));
		osaf_mutex_unlock_ordie(&s_cloexec_mutex);
		return NCSCC_RC_FAILURE;
	}

	flags = fcntl(s_pair[0], F_GETFD, 0);
	fcntl(s_pair[0], F_SETFD, (flags | FD_CLOEXEC));

	flags = fcntl(s_pair[1], F_GETFD, 0);
	fcntl(s_pair[1], F_SETFD, (flags | FD_CLOEXEC));

	osaf_mutex_unlock_ordie(&s_cloexec_mutex);

	if (s_pair[0] > s_pair[1]) {
		/* Ensure s_pair[1] is equal or greater */
		int temp = s_pair[0];
		s_pair[0] = s_pair[1];
		s_pair[1] = temp;
	}
	o_sel_obj->raise_obj = s_pair[0];
	o_sel_obj->rmv_obj = s_pair[1];
	/* Raising indications should be a non-blocking operation. Otherwise, 
	   it can lead to deadlocks among reader and writer applications. 
	 */
	flags = fcntl(o_sel_obj->raise_obj, F_GETFL, 0);
	if (fcntl(o_sel_obj->raise_obj, F_SETFL, (flags | O_NONBLOCK)) == -1) {
		syslog(LOG_ERR, "%s: fcntl failed - %s", __FUNCTION__, strerror(errno));
		(void) ncs_sel_obj_destroy(*o_sel_obj);
		return NCSCC_RC_FAILURE;
	}

	return NCSCC_RC_SUCCESS;
}
예제 #2
0
/**
 * The main routine for the IMM director daemon.
 * @param argc
 * @param argv
 * 
 * @return int
 */
int main(int argc, char *argv[])
{
	SaAisErrorT error;
	NCS_SEL_OBJ mbx_fd;
	struct pollfd fds[3];

	daemonize(argc, argv);

	if (immd_initialize() != NCSCC_RC_SUCCESS) {
		TRACE("initialize_immd failed");
		goto done;
	}

	/* Get file descriptor for mailbox */
	mbx_fd = ncs_ipc_get_sel_obj(&immd_cb->mbx);

	/* Set up all file descriptors to listen to */
	fds[FD_USR1].fd = immd_cb->usr1_sel_obj.rmv_obj;
	fds[FD_USR1].events = POLLIN;
	fds[FD_MBCSV].fd = immd_cb->mbcsv_sel_obj;
	fds[FD_MBCSV].events = POLLIN;
	fds[FD_MBX].fd = mbx_fd.rmv_obj;
	fds[FD_MBX].events = POLLIN;

	while (1) {
		int ret = poll(fds, 3, -1);

		if (ret == -1) {
			if (errno == EINTR)
				continue;

			LOG_ER("poll failed - %s", strerror(errno));
			break;
		}

		if (fds[FD_MBCSV].revents & POLLIN) {
			if (immd_mbcsv_dispatch(immd_cb) != NCSCC_RC_SUCCESS) {
				LOG_ER("MBCSv Dispatch Failed");
				break;
			}
		}

		if (fds[FD_MBX].revents & POLLIN)
			immd_process_evt();

		if (fds[FD_AMF].revents & POLLIN) {
			if (immd_cb->amf_hdl != 0) {
				error = saAmfDispatch(immd_cb->amf_hdl, SA_DISPATCH_ALL);
				if (error != SA_AIS_OK) {
					LOG_ER("saAmfDispatch failed: %u", error);
					break;
				}
			} else {
				TRACE("SIGUSR1 event rec");
				ncs_sel_obj_rmv_ind(immd_cb->usr1_sel_obj, TRUE, TRUE);
				ncs_sel_obj_destroy(immd_cb->usr1_sel_obj);

				if (immd_amf_init(immd_cb) != NCSCC_RC_SUCCESS)
					break;

				TRACE("AMF Initialization SUCCESS......");
				fds[FD_AMF].fd = immd_cb->amf_sel_obj;
			}
		}
	}

done:
	LOG_ER("Failed, exiting...");
	TRACE_LEAVE();
	exit(1);
}
예제 #3
0
/**
 * The main routine for the clms daemon.
 * @param argc
 * @param argv
 * 
 * @return int
 */
int main(int argc, char *argv[])
{
	NCS_SEL_OBJ mbx_fd;
	SaAisErrorT error = SA_AIS_OK;
	uint32_t rc;
	osaf_cluster = NULL;
	int term_fd;
	int timeout = -1;

	daemonize(argc, argv);

	if (clms_init() != NCSCC_RC_SUCCESS) {
		LOG_ER("clms_init failed");
		goto done;
	}

	mbx_fd = ncs_ipc_get_sel_obj(&clms_cb->mbx);
	daemon_sigterm_install(&term_fd);

	/* Set up all file descriptors to listen to */
	fds[FD_TERM].fd = term_fd;
	fds[FD_TERM].events = POLLIN;
	fds[FD_AMF].fd = clms_cb->nid_started ?
		usr1_sel_obj.rmv_obj : clms_cb->amf_sel_obj;
	fds[FD_AMF].events = POLLIN;
	fds[FD_MBCSV].fd = clms_cb->mbcsv_sel_obj;
	fds[FD_MBCSV].events = POLLIN;
	fds[FD_MBX].fd = mbx_fd.rmv_obj;
	fds[FD_MBX].events = POLLIN;
	fds[FD_IMM].fd = clms_cb->imm_sel_obj;
	fds[FD_IMM].events = POLLIN;

#ifdef ENABLE_AIS_PLM
	fds[FD_PLM].fd = clms_cb->plm_sel_obj;
	fds[FD_PLM].events = POLLIN;
#endif

	while (1) {

		if (clms_cb->rtu_pending == true) {
			TRACE("There is an IMM task to be tried again. setting poll time out to 500");
			timeout = 500;
		} else {
			timeout = -1;
		}

		if ((clms_cb->immOiHandle != 0) && (clms_cb->is_impl_set == true)) {
			fds[FD_IMM].fd = clms_cb->imm_sel_obj;
			fds[FD_IMM].events = POLLIN;
			nfds = NUM_FD;
		} else {
			nfds = NUM_FD - 1;
		}
		int ret = poll(fds, nfds, timeout);

		if (ret == -1) {
			if (errno == EINTR)
				continue;

			LOG_ER("poll failed - %s", strerror(errno));
			break;
		}

		if (ret == 0) {
			/* Process any/all pending RTAttribute updates to IMM */
			TRACE("poll time out processing pending updates");
			clms_retry_pending_rtupdates();
			continue;
		}
 
		if (fds[FD_TERM].revents & POLLIN) {
			daemon_exit();
		}

		if (fds[FD_AMF].revents & POLLIN) {
			if (clms_cb->amf_hdl != 0) {
				if ((error = saAmfDispatch(clms_cb->amf_hdl, SA_DISPATCH_ALL)) != SA_AIS_OK) {
					LOG_ER("saAmfDispatch failed: %u", error);
					break;
				}
			} else {
				TRACE("SIGUSR1 event rec");
				ncs_sel_obj_rmv_ind(usr1_sel_obj, true, true);
				ncs_sel_obj_destroy(usr1_sel_obj);

				if (clms_amf_init(clms_cb) != NCSCC_RC_SUCCESS) {
					LOG_ER("AMF Initialization failed");
					break;
				}

				TRACE("AMF Initialization SUCCESS......");
				fds[FD_AMF].fd = clms_cb->amf_sel_obj;
			}

		}

		if (fds[FD_MBCSV].revents & POLLIN) {
			if ((rc = clms_mbcsv_dispatch(clms_cb->mbcsv_hdl)) != NCSCC_RC_SUCCESS) {
				LOG_ER("MBCSv Dispatch Failed");
				break;
			}
		}

		if (fds[FD_MBX].revents & POLLIN) {
			clms_process_mbx(&clms_cb->mbx);
		}
#ifdef ENABLE_AIS_PLM
		/*Incase the Immnd restart is not supported fully,have to reint imm - TO Be Done */
		if (clms_cb->reg_with_plm == SA_TRUE){
			if (fds[FD_PLM].revents & POLLIN) {
				if ((error = saPlmDispatch(clms_cb->plm_hdl, SA_DISPATCH_ALL)) != SA_AIS_OK) {
					LOG_ER("saPlmDispatch FAILED: %u", error);
					break;
				}
			}
		}
#endif

		if (clms_cb->immOiHandle && fds[FD_IMM].revents & POLLIN) {
			if ((error = saImmOiDispatch(clms_cb->immOiHandle, SA_DISPATCH_ALL)) != SA_AIS_OK) {
				if (error == SA_AIS_ERR_BAD_HANDLE) {
					TRACE("main :saImmOiDispatch returned BAD_HANDLE");

					/* 
					 * Invalidate the IMM OI handle, this info is used in other
					 * locations. E.g. giving TRY_AGAIN responses to a create and
					 * close app stream requests. That is needed since the IMM OI
					 * is used in context of these functions.
					 * 
					 * Also closing the handle. Finalize is ok with a bad handle
					 * that is bad because it is stale and this actually clears
					 * the handle from internal agent structures.  In any case
					 * we ignore the return value from Finalize here.
					 */
					saImmOiFinalize(clms_cb->immOiHandle);
					clms_cb->immOiHandle = 0;
					clms_cb->is_impl_set = false;

					/* Initiate IMM reinitializtion in the background */
					clm_imm_reinit_bg(clms_cb);


				} else if (error != SA_AIS_OK) {
					LOG_ER("saImmOiDispatch FAILED: %u", error);
					break;
				}
			}
		}
		/* Retry any pending updates */
		if (clms_cb->rtu_pending == true)
			clms_retry_pending_rtupdates();
	} /* End while (1) */

 done:
	LOG_ER("Failed, exiting...");
	TRACE_LEAVE();
	exit(1);
}
예제 #4
0
int main(int argc, char *argv[])
{
	uint32_t rc;
	nfds_t nfds = 4;
	struct pollfd fds[nfds + RDA_MAX_CLIENTS];
	int i, ret;
	NCS_SEL_OBJ mbx_sel_obj;
	RDE_RDA_CB *rde_rda_cb = &rde_cb->rde_rda_cb;
	int term_fd;

	daemonize(argc, argv);

	if (initialize_rde() != NCSCC_RC_SUCCESS)
		goto init_failed;

	mbx_sel_obj = ncs_ipc_get_sel_obj(&rde_cb->mbx);

	if ((rc = discover_peer(mbx_sel_obj.rmv_obj)) == NCSCC_RC_FAILURE)
		goto init_failed;

	if ((rc = determine_role(mbx_sel_obj.rmv_obj)) == NCSCC_RC_FAILURE)
		goto init_failed;

	/* If AMF started register immediately */
	if (!rde_cb->rde_amf_cb.nid_started &&
		(rc = rde_amf_init(&rde_cb->rde_amf_cb)) != NCSCC_RC_SUCCESS) {
		goto init_failed;
	}

	if (rde_cb->rde_amf_cb.nid_started &&
		nid_notify("RDE", rc, NULL) != NCSCC_RC_SUCCESS) {
		LOG_ER("nid_notify failed");
		goto done;
	}

	daemon_sigterm_install(&term_fd);

	fds[FD_TERM].fd = term_fd;
	fds[FD_TERM].events = POLLIN;

	/* USR1/AMF fd */
	fds[FD_AMF].fd = rde_cb->rde_amf_cb.nid_started ?
		usr1_sel_obj.rmv_obj : rde_cb->rde_amf_cb.amf_fd;
	fds[FD_AMF].events = POLLIN;

	/* Mailbox */
	fds[FD_MBX].fd = mbx_sel_obj.rmv_obj;
	fds[FD_MBX].events = POLLIN;

	/* RDA server socket */
	fds[FD_RDA_SERVER].fd = rde_cb->rde_rda_cb.fd;
	fds[FD_RDA_SERVER].events = POLLIN;

	while (1) {
		ret = poll(fds, nfds, -1);

		if (ret == -1) {
			if (errno == EINTR)
				continue;
			
			LOG_ER("poll failed - %s", strerror(errno));
			break;
		}

		if (fds[FD_TERM].revents & POLLIN) {
			daemon_exit();
		}

		if (fds[FD_AMF].revents & POLLIN) {
			if (rde_cb->rde_amf_cb.amf_hdl != 0) {
				SaAisErrorT error;
				TRACE("AMF event rec");
				if ((error = saAmfDispatch(rde_cb->rde_amf_cb.amf_hdl, SA_DISPATCH_ALL)) != SA_AIS_OK) {
					LOG_ER("saAmfDispatch failed: %u", error);
					goto done;
				}
			} else {
				TRACE("SIGUSR1 event rec");
				ncs_sel_obj_destroy(&usr1_sel_obj);
				
				if (rde_amf_init(&rde_cb->rde_amf_cb) != NCSCC_RC_SUCCESS)
					goto done;
				
				fds[FD_AMF].fd = rde_cb->rde_amf_cb.amf_fd;
			}
		}

		if (fds[FD_MBX].revents & POLLIN)
			handle_mbx_event();

		if (fds[FD_RDA_SERVER].revents & POLLIN) {
			int newsockfd;

			newsockfd = accept(rde_rda_cb->fd, (struct sockaddr *)NULL, NULL);
			if (newsockfd < 0) {
				LOG_ER("accept FAILED %s", strerror(errno));
				goto done;
			}

			/* Add the new client fd to client-list	*/
			rde_rda_cb->clients[rde_rda_cb->client_count].is_async = false;
			rde_rda_cb->clients[rde_rda_cb->client_count].fd = newsockfd;
			rde_rda_cb->client_count++;

			/* Update poll fd selection */
			fds[nfds].fd = newsockfd;
			fds[nfds].events = POLLIN;
			nfds++;

			TRACE("accepted new client, fd=%d, idx=%d, nfds=%lu", newsockfd, rde_rda_cb->client_count, nfds);
		}

		for (i = FD_CLIENT_START; i < nfds; i++) {
			if (fds[i].revents & POLLIN) {
				int client_disconnected = 0;
				TRACE("received msg on fd %u", fds[i].fd);
				rde_rda_client_process_msg(rde_rda_cb, fd_to_client_ixd(fds[i].fd), &client_disconnected);
				if (client_disconnected) {
					/* reinitialize the fd array & nfds */
					nfds = FD_CLIENT_START;
					for (i = 0; i < rde_rda_cb->client_count; i++, nfds++) {
						fds[i + FD_CLIENT_START].fd = rde_rda_cb->clients[i].fd;
						fds[i + FD_CLIENT_START].events = POLLIN;
					}
					TRACE("client disconnected, fd array reinitialized, nfds=%lu", nfds);
					break;
				}
			}
		}
	}

 init_failed:
	if (rde_cb->rde_amf_cb.nid_started &&
		nid_notify("RDE", NCSCC_RC_FAILURE, NULL) != NCSCC_RC_SUCCESS) {
		LOG_ER("nid_notify failed");
		rc = NCSCC_RC_FAILURE;
	}

 done:
	syslog(LOG_ERR, "Exiting...");
	exit(1);
}