int
main(int *argc, char *argv[])
{
    int server_sock_fd;

    server_sock_fd = create_and_bind_socket();
    do_repeated_task(server_sock_fd);
    /*if ((ret = do_repeated_task()))
      goto out;

    out:
    cleanup_sock_file(ctx.sockfile);*/
    return 0;
}
int
main(int *argc, char *argv[])
{
  int ret, sockfd;

  if ((sockfd= create_and_bind_socket()) < 0) {
    fprintf (stdout, "Creating the socket file failed\n");
    goto out;
  }

  printf("Going to do_repeated_task()\n");
  do_repeated_task(sockfd);

out:
  Unlink(sockfile);
  return 0;
}
Exemple #3
0
int main(int argc, char *argv[]){

	if (argc < 2)
		error("ERROR, no port provided\n");

	int listen_socket;

	if ((listen_socket = create_and_bind_socket(argv[1])) == -1)
		error("Failed in create_and_bind_socket");

	if (make_socket_non_blocking(listen_socket) == -1)
		error("Error in setting non_block");

     	if (listen(listen_socket,SOMAXCONN) == -1)
		error("Listen failed");


//THREADING INITIALIZATION

      struct sockaddr_in cli_addr;
      socklen_t addrlen;

      struct arg_struct {
      	int conn_socket;
      	pthread_key_t thr_id_key;
      	int * thread_id;
      };

	struct arg_struct args;

	pthread_t thread;
	int * thread_id;
	pthread_key_t thr_id_key;
	thread_id = (int * ) malloc(sizeof(int));
	pthread_key_create(&thr_id_key, NULL);

	pthread_attr_t thread_attr;
	pthread_attr_init(&thread_attr);
	pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);

	int epollfd, nfds, i, conn_socket;
	struct epoll_event event;
	struct epoll_event events[MAX_EVENTS];
	
	if( (epollfd = epoll_create1 (0)) == -1)
		error ("epoll_create");
	
    //typedef union epoll_data {
    //	void    *ptr;
    //	int      fd;
    //	uint32_t u32;
    //	uint64_t u64;
    //} epoll_data_t;
    //
    //struct epoll_event {
    //	uint32_t     events;    /* Epoll events */
    //	epoll_data_t data;      /* User data variable */
    //	};

	event.events = EPOLLIN|EPOLLET;
	event.data.fd = listen_socket;

	if (epoll_ctl (epollfd, EPOLL_CTL_ADD, listen_socket, &event) == -1)
		error ("epoll_ctl fail");
	
	int count = 0;
	
	while(1){
		
		//printf("Waiting for connections[%d]....\n", count);
		
		if((nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1)) == -1)    //Returns number of events waiting
			error("epoll_pwait");

		for (i = 0; i < nfds; i++){
			count++;
			printf("Process connection [%d]\n", count);

			if (events[i].data.fd == listen_socket) {

				if((conn_socket = accept(listen_socket, (struct sockaddr *) &cli_addr, &addrlen)) == -1)
					error("accept failed");

				if (make_socket_non_blocking(conn_socket) == -1)
					error("Error in setting non_block");
			
				event.events = EPOLLIN|EPOLLET;
				event.data.fd = conn_socket;

				if ((epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_socket, &event)) == -1)
					error("epoll_ctl: conn_socket failed");
			}

			else{
				args.conn_socket = events[i].data.fd;
				args.thread_id = thread_id;

				if(pthread_create (&thread, &thread_attr, talk_to_client, &args) != 0)
					error("Error creating a thread.");
			}
		}
				
	}//end of while

	printf("Exited while(1) ?!?!? \n");
	pthread_exit(NULL);
	return 0; 					//should never get here
}
Exemple #4
0
int
main (int argc, char *argv[])
{
    int opt, hidfd, sfd = -1;
    int daemonize = 1;
    char *port = "9876";
    char *pid_path = NULL;
    char *hid_path = NULL;
    int pidfd = -1;

    while ((opt = getopt(argc, argv, "fp:P:d:")) != -1) {
	switch (opt) {
	    case 'f':
		daemonize = 0;
		break;
	    case 'p':
		port = optarg;
		break;
	    case 'P':
		pid_path = optarg;
		break;
	    default:
		usage(argv[0]);
		exit(EXIT_FAILURE);
	}
    }

    if (optind >= argc) {
	usage(argv[0]);
	exit(EXIT_FAILURE);
    }
    hid_path = argv[optind];

    if (pid_path) {
	/* open pidfile */
	pidfd = open(pid_path, O_RDWR | O_CREAT, 0644);
	if (pidfd < 0) {
	    perror("open pidfile");
	} else {
	    /* lock it */
	    if (flock(pidfd, LOCK_EX | LOCK_NB) != 0) {
		if (errno == EWOULDBLOCK) {
		    exit(EXIT_SUCCESS);
		} else {
		    perror("lock pidfile");
		}
	    }
	}
    }

    if (daemonize) {
	if (daemon(0, 0) != 0) {
	    perror("daemonize");
	}
    }

    if (pidfd >= 0) {
	/* truncate pidfile at 0 length */
	ftruncate(pidfd, 0);
	dprintf(pidfd, "%u\n", getpid());
	/* unlock the pidfile */
	flock(pidfd, LOCK_UN);
	close(pidfd);
    }

    signal(SIGINT, handle_sigterm);
    signal(SIGTERM, handle_sigterm);

    while (1) {
	int ok = 0;

	hidfd = open(hid_path, O_RDWR);
	if (hidfd < 0 || make_fd_non_blocking(hidfd) < 0) {
	    perror("open hid");
	} else {
	    sfd = create_and_bind_socket(port);
	    if (sfd < 0 || make_fd_non_blocking(sfd) < 0) {
		perror("open socket");
	    } else if (listen(sfd, SOMAXCONN) < 0) {
		perror("listen");
	    } else {
		ok = 1;
	    }
	}

	if (ok) {
	    event_loop(sfd, hidfd);
	}

	if (hidfd >= 0) {
	    close(hidfd);
	}
	if (sfd >= 0) {
	    close(sfd);
	}
	if (g_running) {
	    sleep(10);
	}
	if (!g_running) {
	    break;
	}
    }

    if (pidfd >= 0) {
	unlink(pid_path);
    }

    return 0;
}