Exemple #1
0
int main(int argc, char *argv[])
{
	int listenfd, c;
	struct sockaddr_in  servaddr;

	int connfd;
	struct sockaddr_in  cliaddr;

	char *pidfile_path;
	int given_pid;

	pid_t childpid;
	socklen_t clilen;

	given_pid = 0;
	pidfile_path = (char)'\0';

	while ((c = getopt(argc, argv, "p:")) != EOF) {
		switch(c) {
			case 'p':
			    pidfile_path = optarg;
			    given_pid = 1;
			    break;
		}
	}

	daemonInit(message(MSG_DAEMON_NAME), SYSLOG_FACILITY);

	listenfd = socket(AF_INET, SOCK_STREAM, 0);

	memset((void *) &servaddr, '\0', (size_t) sizeof(servaddr));

	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(0x7F000001);
	/*servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");*/
	servaddr.sin_port = htons(SERVER_LISTEN_PORT);

	if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {
		say(message(MSG_ERROR_BIND), strerror(errno));
		exit(errno);
	}

	if (listen(listenfd, MAX_LISTENQ) < 0) {
		say(message(MSG_ERROR_LISTEN), strerror(errno));
		exit(errno);
	}

	say("%s", message(MSG_DAEMON_VER));

	tv_rcv = (struct timeval *) calloc(1, sizeof(struct timeval));
	tv_snd = (struct timeval *) calloc(1, sizeof(struct timeval));

	memset(tv_rcv, '\0', sizeof(struct timeval));
	memset(tv_snd, '\0', sizeof(struct timeval));

	tv_rcv -> tv_sec = 30;
	tv_rcv -> tv_usec = 0;

	tv_snd -> tv_sec = 30;
	tv_snd -> tv_usec = 0;

	signal(SIGCHLD, sigChild);
	signal(SIGPIPE, sigPipe);

	if(given_pid) {
		FILE *file = fopen(pidfile_path, "w");
		fprintf(file, "%ld", (long)getpid());
		fclose(file);
	}

	for (;;) {
		memset((void *) &cliaddr, '\0', sizeof(cliaddr));
		clilen = (socklen_t) sizeof(cliaddr);

		if ((connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen)) < 0) {
			if (errno == EINTR) {
				continue;
			} else {
				say(message(MSG_ERROR_ACCEPT), strerror(errno));
				exit(errno);
			}
		}

		setsockopt(connfd, SOL_SOCKET, SO_RCVTIMEO, tv_rcv, sizeof(struct timeval));
		setsockopt(connfd, SOL_SOCKET, SO_SNDTIMEO, tv_snd, sizeof(struct timeval));

		memset(client_ip, '\0', MAX_MSG_SIZE);

		inet_ntop(AF_INET, &cliaddr.sin_addr, client_ip, MAX_MSG_SIZE);

		if ( ( childpid = fork() ) == 0) {
			char *nmb = calloc(50, sizeof(char));

			close(listenfd);

			childpid = getpid();

			sprintf(nmb, "%d", childpid);

			say(message(MSG_START_CHILD), nmb);

			takeConnection(connfd);
            free(nmb);

			exit(0);
		}

		close(connfd);
	}

	closelog();

	return (NO_ERROR);
}
/*
 * daemon entry
 *
 * parse arguments
 * create resources to talk to child
 * if !foreground
 *    daemonize, run as child
 * open proxy driver
 * install door in proxy driver
 * create socket
 * if server-side
 *     bind socket
 * if !foreground
 *    inform parent things aok
 * if parent
 *    exit(SMF_EXIT_OK)
 * if server-side
 *    accept
 * if client-side
 *    connect
 * send hello
 * recv hello
 * loop on recieve
 * XXX anyway to check in envp that we are started by SMF?
 */
int
main(int argc, char *argv[])
{
	struct	sockaddr_in sin;
	int	rc;
	struct sigaction 	act;
	sigset_t		sigmask;
	int	c;
	int	node = 0;
	int	node_override = 0;
	int	server_node = 0;
	int	server_match = 0;
	extern char *optarg;
	int stmf_ret;

	(void) setlocale(LC_ALL, "");
	openlog("stmfproxy", LOG_PID, LOG_DAEMON);
	(void) setlogmask(LOG_UPTO(LOG_INFO));

	while ((c = getopt(argc, argv, "dfn:")) != -1) {
		switch (c) {
			case 'd':
				(void) setlogmask(LOG_UPTO(LOG_DEBUG));
				log_debug = 1;
				break;
			case 'f':
				fore_ground = 1;
				break;
			case 'n':
				node_override = 1;
				node = atoi(optarg);
				break;
			default:
				/*
				 * Should never happen from smf
				 */
				(void) fprintf(stderr, USAGE, argv[0]);
				exit(SMF_EXIT_ERR_CONFIG);
				break;
		}
	}
	/*
	 * After the options, only the server argument should remain.
	 */
	if (optind != argc-1) {
		(void) fprintf(stderr, USAGE, argv[0]);
		exit(SMF_EXIT_ERR_CONFIG);
	}
	(void) strcpy(aluaNode, argv[optind]);
	syslog(LOG_DAEMON|LOG_DEBUG, "aluaNode %s", aluaNode);
	if (gethostname(myNode, 255)) {
		perror("gethostname");
		exit(1);
	}
	if ((inet_aton(aluaNode, &sin.sin_addr)) == 0) {
		/*
		 * Not ipaddr, try hostname match.
		 */
		server_match = (strcmp(aluaNode, myNode)) ? 0 : 1;
	} else {
		/*
		 * see if this is our ip address
		 */
		(void) fprintf(stderr, "Sorry, cannot use ip adress format\n");
	}
	if (server_match) {
		server_node = 1;
		if (!node_override)
			node = 1;
	}


	/*
	 * Allow SIGQUIT, SIGINT and SIGTERM signals to terminate us
	 */
	act.sa_handler = killHandler;
	(void) sigemptyset(&act.sa_mask);
	act.sa_flags = 0;

	/* Install the signal handler */
	(void) sigaction(SIGQUIT, &act, NULL);
	(void) sigaction(SIGINT, &act, NULL);
	(void) sigaction(SIGTERM, &act, NULL);
	(void) sigaction(SIGHUP, &act, NULL);

	/* block all signals */
	(void) sigfillset(&sigmask);

	/* unblock SIGQUIT, SIGINT, SIGTERM */
	(void) sigdelset(&sigmask, SIGQUIT);
	(void) sigdelset(&sigmask, SIGINT);
	(void) sigdelset(&sigmask, SIGTERM);
	(void) sigdelset(&sigmask, SIGHUP);

	(void) sigprocmask(SIG_SETMASK, &sigmask, NULL);

	/* time to go backstage */
	daemonInit();

	if ((rc = open_proxy_driver()) != 0)
		daemon_fini(rc);

	if ((rc = stmf_proxy_transport_init("sockets", &pt_ops)) != 0)
		daemon_fini(rc);

	/*
	 * Establish connection
	 *
	 * At this point, the parent has exited and the service
	 * is online. But there are no real proxy services until
	 * this connect call succeeds. That could take a long time if
	 * the peer node is down.
	 */
	t_handle = pt_ops->stmf_proxy_connect(server_node, aluaNode);
	if (t_handle == NULL) {
		syslog(LOG_DAEMON|LOG_WARNING,
		    "socket() call failed: %d", errno);
		exit(1);
	}

	/* The first message is a greeting */
	(void) postMsg((uint_t)strlen(myNode)+1, (uchar_t *)myNode);
	/* Read the greeting from peer node */
	relay_peer_msg();
	/*
	 * Set the alua state in stmf. No need to keep
	 * the device open since the proxy driver has a reference.
	 */
	stmf_ret = stmfSetAluaState(B_TRUE, node);
	if (stmf_ret != STMF_STATUS_SUCCESS) {
		syslog(LOG_DAEMON|LOG_CRIT, "stmf ioctl failed - %x", stmf_ret);
		exit(1);
	}

	/* service is online */
	daemon_fini(0);

	/*
	 * Loop relaying data from the peer daemon to the local kernel.
	 * Data coming from the local kernel is handled asynchronously
	 * by the door server.
	 */
	for (;;) { /* loop forever */
		relay_peer_msg();
	}
}
Exemple #3
0
int initService(int argc, char *argv[])
{
    int i;
    char *optpattern = "hl:p:s:e";
    char *prog = argv[0];
    char *p = NULL;
    string logDir = "/tmp";
    int logLevel = Log::INFORMATION;
    int logMode = Log::DISABLE;
#if defined(_SCI_LINUX) || defined(__APPLE__)
    string pidDir = "/var/run/";
#else
    string pidDir = "/var/opt/";
#endif
    string logFile;

    extern char *optarg;
    extern int  optind;
    p = strrchr(prog, '/');
    if (p != NULL) 
        p++;
    else
        p = prog;
    
    while ((i = getopt(argc, argv, optpattern)) != EOF) {
        switch (i) {
            case 'l':
                logDir = optarg;
                break ;
            case 'p':
                pidDir = optarg;
                break;
            case 's':
                logLevel = atoi(optarg);
                break;
            case 'e':
                logMode = Log::ENABLE;
                break;
            case 'h':
                usage(p);
                exit(0);
                break;
        }
    }
    pidFile = pidDir + "/" + p + ".pid";
    if (checkPidFile(pidFile) < 0) {
        printf("%s is already running...\n", p);
        return -1;
    }
    if (getuid() != 0) {
        printf("Must running as root\n");
        return -1;
    }
    daemonInit();
    writePidFile(pidFile);

    logFile = string(p) + ".log"; 
    Log::getInstance()->init(logDir.c_str(), logFile.c_str(), logLevel, logMode);

    return 0;
}
Exemple #4
0
//-----------------------------------------------------------------------------
// Name: main()
// Desc: UNIX app start position
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
	LogString("Welcome to Army War Server v2.0");
	LogString("-------------------------------\n");

	if(argc > 1)
	{
		if(strcmp(argv[1], "-daemon") == 0)
		{
			daemonInit();
		}
	}

	// Ignore the SIGPIPE signal, so the program does not terminate if the
	// pipe gets broken
	signal(SIGPIPE, SIG_IGN);
	
	LogString("Init successful");

	game = new CArmyWarServer();
	game->InitNetwork();
	
	int time, oldTime, newTime;

	oldTime = game->networkServer->dreamSock->dreamSock_GetCurrentSystemTime();

	// App main loop
	try
	{
		if(runningDaemon)
		{
			// Keep server alive
			while(1)
			{
				do
				{
					newTime = game->networkServer->dreamSock->dreamSock_GetCurrentSystemTime();
					time = newTime - oldTime;
				} while (time < 1);

				game->Frame(time);

				oldTime = newTime;
			}
		}
		else
		{
			// Keep server alive (wait for keypress to kill it)
			while(keyPress() == -1)
			{
				do
				{
					newTime = game->networkServer->dreamSock->dreamSock_GetCurrentSystemTime();
					time = newTime - oldTime;
				} while (time < 1);

				game->Frame(time);

				oldTime = newTime;
			}
		}
	}
	catch(...)
	{
		game->networkServer->dreamSock->dreamSock_Shutdown();

		LogString("Unknown Exception caught in main loop");

		return -1;
	}

	LogString("Shutting down everything");

	game->networkServer->dreamSock->dreamSock_Shutdown();

	return 0;
}