Beispiel #1
0
/* main loop of the daemon*/
int
init_start ()
{
	int 			ssock;
	struct sockaddr_in 	saddr;
	GIOChannel* 		sch;
	
	/* register pid */
	if (cl_lock_pidfile(PID_FILE) < 0) {
		quorum_log(LOG_ERR, "already running: [pid %d]."
		,	 cl_read_pidfile(PID_FILE));
		quorum_log(LOG_ERR, "Startup aborted (already running)."
				  "Shutting down.");
		exit(100);
	}
	register_pid(FALSE, sigterm_action);

	/* enable coredumps */
	quorum_log(LOG_DEBUG, "Enabling coredumps");
 	cl_cdtocoredir();
	cl_enable_coredumps(TRUE);	
	cl_set_all_coredump_signal_handlers();
	
	/* initialize gnutls */
	initialize_tls_global();
	
	/* enable dynamic up/down debug level */
	G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR1, 
				 sig_handler, NULL, NULL);
	G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGUSR2, 
				 sig_handler, NULL, NULL);
	G_main_add_SignalHandler(G_PRIORITY_HIGH, SIGHUP, 
				 sig_handler, NULL, NULL);
		
	/* create the mainloop */
	mainloop = g_main_new(FALSE);

	/* create the protocal table */
	protocols = g_hash_table_new(g_str_hash, g_str_equal);
		
	/* create server socket */
	ssock = socket(AF_INET, SOCK_STREAM, 0);
	if (ssock == -1) {
		quorum_log(LOG_ERR, "Can not create server socket."
				  "Shutting down.");
		exit(100);
	}
	/* bind server socket*/
	memset(&saddr, '\0', sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = INADDR_ANY;
	saddr.sin_port = htons(PORT);
	if (bind(ssock, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
		quorum_log(LOG_ERR, "Can not bind server socket."
				  "Shutting down.");
		exit(100);
	}
	if (listen(ssock, 10) == -1) {
		quorum_log(LOG_ERR, "Can not start listen."
				"Shutting down.");
		exit(100);
	}	

	/* create source for server socket and add to the mainloop */
	sch = g_io_channel_unix_new(ssock);
	g_io_add_watch(sch, G_IO_IN|G_IO_ERR|G_IO_HUP, on_listen, NULL);
	
	/* run the mainloop */
	quorum_log(LOG_DEBUG, "main: run the loop...");
	quorum_log(LOG_INFO, "Started.");

	g_main_run(mainloop);

	/* exit, clean the pid file */
	if (cl_unlock_pidfile(PID_FILE) == 0) {
		quorum_log(LOG_DEBUG, "[%s] stopped", QUORUMD);
	}

	return 0;
}
Beispiel #2
0
int
main(int argc, char *argv[])
{
   	int rc; 
   	int retval = 0;  
   	const char* conf_file = CONFIG_FILE;
	int pid;
	cl_cdtocoredir();
	
	if(argc == 2){
		conf_file = argv[1];
	}else if(argc > 2){
		printf("Usage: %s [config_file]\n", cmdname);
		exit(LSB_EXIT_NOTCONFIGED);
	}

   	cl_log_enable_stderr(TRUE);

	pid = cl_read_pidfile(PIDFILE);
	if (pid > 0 && pid != getpid()){
		cl_log(LOG_INFO, "recovermgrd is already running[%d]",
		       pid);
		return 0;
	}
   	
	cl_log_set_entity(argv[0]);
   	cl_log_set_facility(LOG_USER);
   	cl_log(LOG_INFO, "Starting %s", argv[0]);
   	signal(SIGCHLD, sigchld_handler);
	
	if(parseConfigFile(conf_file) == FALSE){
		exit(LSB_EXIT_NOTCONFIGED);
	};

   /* make self a daemon */
#ifndef DEBUG
   	daemon(0,0);
#else  
   	printf("Debug mode -- non daemon\n");
#endif

	if (cl_lock_pidfile(PIDFILE) < 0){
		
		cl_log(LOG_INFO, "recoverymgrd is already running[%d]",
		       cl_read_pidfile(PIDFILE));
		return 0;
	}

   	/* register with apphbd as a client and send pings */
   	retval = register_hb();
   	if (0 != retval)
   	{
     		cl_perror("Error registering -- is apphbd running??");
     		exit(retval);
   	}
   
   	create_connection();   

   	/* unregister and shutdown */
   	rc = apphb_unregister();
   	if (rc < 0) 
		{
      		cl_perror("apphb_unregister failure");
      		exit(3);
   	}
	
	cl_unlock_pidfile(PIDFILE);
   	return 0; 
}