コード例 #1
0
 cf_uint32 Get()
 {
     return ReadPid();
 }
コード例 #2
0
ファイル: main.c プロジェクト: AkankshaGovil/Automation
int
main(int argc, char *argv[], char *envp[])
{
	int i, poolid, classid;
	ssize_t len;
	key_t	key;
	char *msg;

	//	Parse the options and configure appropriate values. 
	parse_opt(argc, argv);

	//	Read the configuration
	setConfigFile();
	myConfigServerType = CONFIG_SERPLEX_EXECD;
	DoConfig(ConfigureExecD);

	//	Daemonize
	if (execd_daemonize) {
		daemonize();
		(void) freopen( "/dev/null", "r", stdin );
		(void) freopen( "/var/log/iserverout.log", "a", stdout );
		(void) freopen( "/var/log/iservererr.log", "a", stderr ); 
		for (i = 3; i < MAXFD; i++) {
			close(i);	
		}
	}

	//	Start the signal handler thread.
	SignalInit();

	NetSyslogOpen(pname, NETLOG_ASYNC);
	sprintf(pidf, "%s/%s", PIDS_DIRECTORY, EXECD_PID_FILE); 
	execdpid = ReadPid(pidf);
 
	if ( execdpid > 0 ) { 
		if ((kill(execdpid, 0) == 0) || (errno != ESRCH)) { 
			NETERROR(MRSD, ("%s seems to be running already - exiting\n", basename(argv[0])));
			exit(0);
		} 
		else  /* Get rid of the leftover file */ 
       		UnlinkPid(pidf);
    }

	StorePid(pidf);

	NETINFOMSG(MEXECD, ("*** NexTone Cmd Execution Server started ***\n"));

	// 	Start the thread which sends message to pm
	Initpoll(EXECD_ID, SERPLEX_GID);
	Sendpoll(0); 
	ThreadLaunch((PFVP)SendPMPoll, NULL, 1);

	// 	Get the msg_q id. If it does not exist then create it
	if ((key = ftok(ISERVER_FTOK_PATH, ISERVER_EXECD_Q)) < 0) {
		NETERROR(MEXECD, ("ftok: %s\n", strerror(errno)));
		exit(0);
	}
	
	if (q_vget(key, 0, MAX_NUM_MSG, MAX_MSGLEN, &msgqid) < 0) {
		NETERROR(MEXECD, ("q_vget: %s\n", strerror(errno)));
		exit(0);
	}

#ifdef USE_SYS_POPEN	
	sys_utils_init();
#endif

	// Start a threadpool of worker threads
	poolid = ThreadPoolInit("execcmd", nthreads, PTHREAD_SCOPE_PROCESS, -1, 0);
	classid = ThreadAddPoolClass("execcmd", poolid, 0, 0);
	ThreadPoolStart(poolid);

	// msgqid has to be valid if we reached here
	for( ; ; ) {
		if (!(msg = malloc(MAX_MSGLEN))) {
			NETERROR(MEXECD, ("malloc: %s\n", strerror(errno)));
			sleep(5);   // sleep and try again
			continue;
		}
		if (q_vreceive(msgqid, msg, MAX_MSGLEN, SRVR_MSG_TYP, 0, &len) < 0) {
			NETERROR(MEXECD, ("q_vreceive: %s\n", strerror(errno)));
			// Check error types
			if (errno == EIDRM) {
				printf("execd: stop messing with my message queue\n");
				// somebody deleted the queue
				if (q_vget(key, 0, MAX_NUM_MSG, MAX_MSGLEN, &msgqid) < 0) {
					NETERROR(MEXECD, ("q_vget: %s\n", strerror(errno)));
					exit(0);
				}
			}
			free(msg);
			continue;
		}
		if (ThreadDispatch(poolid, classid, (PFVP)CmdWorker, msg, 1, 
			PTHREAD_SCOPE_PROCESS, SCHED_FIFO, 59) < 0) {
			NETERROR(MRSD, ("ThreadDispatch: No free thread\n"));
			// Send response in a new thread
			ThreadLaunch((PFVP)SendFailMsg, msg, 1);
		}
	}
}