Example #1
0
int main(int argc, char *argv[])
{
    unsigned int d;
    in_port_t server_port = 21U;

#ifdef HAVE_SETLOCALE
# ifdef LC_MESSAGES
    (void) setlocale(LC_MESSAGES, "");
# endif
# ifdef LC_CTYPE
    (void) setlocale(LC_CTYPE, "");
# endif
# ifdef LC_COLLATE
    (void) setlocale(LC_COLLATE, "");
# endif
#endif    
    
    if (argc == 2) {
        server_port = (in_port_t) strtoul(argv[1], NULL, 10);
    }
    d = daemons(server_port);
    printf("%u\n%u\n%s\n%s\n", d, d, uptime(), name());
    
    return 0;
}
Example #2
0
/*
 *  stress_daemon()
 *	stress by multiple daemonizing forks
 */
int stress_daemon(
	uint64_t *const counter,
	const uint32_t instance,
	const uint64_t max_ops,
	const char *name)
{
	int fds[2];
	pid_t pid;

	(void)instance;

	if (pipe(fds) < 0) {
		pr_fail_dbg(name, "pipe");
		return EXIT_FAILURE;
	}
	pid = fork();
	if (pid < 0) {
		pr_fail_dbg(name, "fork");
		(void)close(fds[0]);
		(void)close(fds[1]);
		return EXIT_FAILURE;
	} else if (pid == 0) {
		/* Children */
		(void)close(fds[0]);
		daemons(fds[1]);
		(void)close(fds[1]);
	} else {
		/* Parent */
		(void)close(fds[1]);
		do {
			ssize_t n;
			char buf[1];

			n = read(fds[0], buf, sizeof(buf));
			if (n < 0) {
				(void)close(fds[0]);
				if (errno != EINTR) {
					pr_dbg(stderr, "read failed: errno=%d (%s)\n",
						errno, strerror(errno));
				}
				break;
			}
			(*counter)++;
		} while (opt_do_run && (!max_ops || *counter < max_ops));
	}

	return EXIT_SUCCESS;
}
Example #3
0
int main(int argc,char **argv)
{
	printf("Http server welcome you!\n");
    tpool_create(10);
	if(1!=argc)
	getoption(argc,argv);//It's hard to learn how to use it  
	if(NULL==_log)
		logfd=open(DEFAULTLOG,O_WRONLY | O_APPEND | O_CREAT);
	else
		logfd=open(_log,O_WRONLY | O_CREAT | O_APPEND);
	
	daytime();
	int sockfd,sockfds;
	if(daemon_check)
		daemons();
    ssl_init(ctx);
    signal(SIGPIPE,SIG_IGN);
	signal(SIGCHLD, SIG_IGN);
	sockfd=make_socket(sockfd);
    sockfds=make_socket_ssl(sockfds);
	if(sockfd<0||sockfds<0)
		errorfunc("sockfd error!");
	int addrlen = 1;  
    setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&addrlen,sizeof(addrlen));//set the port quickly reuse
    setsockopt(sockfds,SOL_SOCKET,SO_REUSEADDR,&addrlen,sizeof(addrlen));
    struct epoll_event events[MAXEVENTS];//question
	int epollfd=epoll_create(MAXEVENTS);
	addfd(epollfd,sockfd,0);
    addfd(epollfd,sockfds,0);
	chdir("/home/wangyao/web");
	while(1)
	{
		int ret=epoll_wait(epollfd,events,MAXEVENTS,-1);//epoll func should be use in here
		if(ret<0)
			errorfunc("epoll_wait error!");
		lt(events,ret,epollfd,sockfd,sockfds);
	}
    close(sockfds);
	close(sockfd);
    close(epollfd);
    sleep(10);
    tpool_destroy();
    SSL_CTX_free(ctx);         
	exit(0);
}