Ejemplo n.º 1
0
int 
main( int argc, char **argv )
{
	signal( SIGCHLD, SigUserProc );
	signal( SIGPIPE, SIG_IGN );
	signal_intr( SIGINT, SigInt );
	signal_intr( SIGTERM, SigInt );

	#if defined(PROXY)
	if( argc < 7 )
	#elif defined(HTTP_PROXY) || defined(SERVER) || defined(MULTI_PTHREAD)
	if( argc < 3 )
	#endif
	{
		#if defined(PROXY)
		SERVER_DEBUG( "[PROXY]usage:./server -l 8087 -s 127.0.0.1 -p 12307\n" );
		#elif defined(HTTP_PROXY)||defined(SERVER) || defined(MULTI_PTHREAD)
		SERVER_DEBUG( "[SERVER]usage:./server -l 8087 \n" );
		#endif
		return -1;
	}

	int port = 0;
	char *host_ip = NULL;
	int host_port = 0;
	char c = 0;

	while( (c = getopt( argc, argv, "l:s:p:" )) > 0 )
	{
		switch( c )
		{
			case ('l'):
				port = atoi( optarg );
				break;
			case ('s'):
				host_ip = optarg;
				break;
			case ('p'):
				host_port = atoi( optarg );
				break;
			default:
				#if defined(PROXY)
				SERVER_DEBUG( "[PROXY]usage:./server -l 8087 -s 127.0.0.1 -p 12307\n" );
				#elif defined(HTTP_PROXY) || defined(SERVER) || defined(MULTI_PTHREAD)
				SERVER_DEBUG( "[SERVER]usage:./server -l 8087 \n" );
				#endif
				return -1;
		}
	}

	#if defined(PROXY)
	SERVER_DEBUG( "[PROXY] listen_port:%d host_ip:%s host_port:%d\n", port, host_ip, host_port );
	#elif defined(HTTP_PROXY) || defined(SERVER) || defined(MULTI_PTHREAD)
	SERVER_DEBUG( "[SERVER] listen_port:%d\n", port );
	#endif

	(void)server( port, host_ip, host_port );

	return 0;
}
Ejemplo n.º 2
0
void		/* Establish the signal handler and set the alarm. */
set_alrm(unsigned int nsec)
{
	alrm_flag = 0;
	if (signal_intr(SIGALRM, sig_alrm) == SIG_ERR)
		log_sys("set_alrm: signal_intr error");
	alarm(nsec);
}
Ejemplo n.º 3
0
Archivo: loop.c Proyecto: abligh/pty
void
loop (int ptym, int ignoreeof)
{
  pid_t child;
  int nread;
  char buf[BUFFSIZE];

  if ((child = fork ()) < 0)
    {
      err_sys ("fork error");
    }
  else if (child == 0)
    {				/* child copies stdin to ptym */
      for (;;)
	{
	  if ((nread = read (STDIN_FILENO, buf, BUFFSIZE)) < 0)
	    err_sys ("read error from stdin");
	  else if (nread == 0)
	    break;		/* EOF on stdin means we're done */
	  if (writen (ptym, buf, nread) != nread)
	    err_sys ("writen error to master pty");
	}

      /*
       * We always terminate when we encounter an EOF on stdin,
       * but we notify the parent only if ignoreeof is 0.
       */
      if (ignoreeof == 0)
	kill (getppid (), SIGTERM);	/* notify parent */
      exit (0);			/* and terminate; child can't return */
    }

  /*
   * Parent copies ptym to stdout.
   */
  if (signal_intr (SIGTERM, sig_term) == SIG_ERR)
    err_sys ("signal_intr error for SIGTERM");

  for (;;)
    {
      if ((nread = read (ptym, buf, BUFFSIZE)) <= 0)
	break;			/* signal caught, error, or EOF */
      if (writen (STDOUT_FILENO, buf, nread) != nread)
	err_sys ("writen error to stdout");
    }

  /*
   * There are three ways to get here: sig_term() below caught the
   * SIGTERM from the child, we read an EOF on the pty master (which
   * means we have to signal the child to stop), or an error.
   */
  if (sigcaught == 0)		/* tell child if it didn't send us the signal */
    kill (child, SIGTERM);

  /*
   * Parent returns to caller.
   */
}
Ejemplo n.º 4
0
Sigfunc *
Signal_intr(int signo, Sigfunc *func)
{
	Sigfunc	*sigfunc;

	if ( (sigfunc = signal_intr(signo, func)) == SIG_ERR)
		err_sys("signal_intr error");
	return(sigfunc);
}
Ejemplo n.º 5
0
void
loop(int ptym, int ignoreeof)
{
	pid_t child;
	int nread;
	char buf[BUFFSIZE];

	if ((child = fork()) < 0) {
		err_sys("fork error");
	} else if (child == 0) { /* child copies stdin to stdout */
		for (;;) {
			if ((nread = read(STDIN_FILENO, buf, BUFFSIZE)) < 0)
				err_sys("read error from stdin");
			else if (nread == 0)
				break;
			if (writen(ptym, buf, nread) != nread)
				err_sys("writen error to master pty");
		}

		/*
		 * We always terminate when we encounter an EOF.
		 * But we notify the parent only if ignoreeof is set.		
		 */
		if (ignoreeof == 0)
			kill(getppid(), SIGTERM);
		exit(0);
	}

	/*
     * Parent copies ptym to stdout.
     */
	 if (signal_intr(SIGTERM, sig_term) == SIG_ERR)
		err_sys("signal_intr error for SIGTERM");
	
	for (;;) {
		if ((nread = read(ptym, buf, BUFFSIZE)) <= 0)
			break;
		if (writen(STDOUT_FILENO, buf, nread) != nread)
			err_sys("writen error to stdout");
	}

	if (sigcaught == 0)
		kill(child, SIGTERM);

	/*
     * Parent returns to caller.
     */
}
Ejemplo n.º 6
0
void main()
{
    pid_t pid1,pid2,pid3;
    int fd;

    setbuf(stdout,NULL);
    signal_intr(SIGINT,sigint);

    if( (fd = open("lockfile",O_RDWR|O_CREAT,0666)) < 0)
	err_sys("can't open/create lockfile");

    if( (pid1 = fork()) < 0)
	err_sys("fork failed");
    else if(pid1 == 0){
	if(lock_reg(fd,F_SETLK,F_RDLCK,0,SEEK_SET,0)<0)
	    err_sys("child 1: can't read-lock file");
	printf("child 1: obtained read lock on file\n");
	pause();
	printf("child 1: exit after pause\n");
	exit(0);
    }
    else 
	sleep(2);

    if( (pid2 = fork()) < 0)
	err_sys("fork failed");
    else if(pid2 == 0){
	if(lock_reg(fd,F_SETLK,F_RDLCK,0,SEEK_SET,0)<0)
	    err_sys("child 2: can't read-lock file");
	printf("child 2: obtained read lock on file\n");
	pause();
	printf("child 2: exit after pause\n");
	exit(0);
    }
    else 
	sleep(2);

    if( (pid3 = fork()) < 0)
	err_sys("fork failed");
    else if(pid3 == 0){
	if(lock_reg(fd,F_SETLK,F_WRLCK,0,SEEK_SET,0)<0)
	    printf("child 3: can't set write lock: %s\n",strerror(errno));
	printf("child 3 about to block in write-lock...\n");
	if(lock_reg(fd,F_SETLKW,F_WRLCK,0,SEEK_SET,0) < 0)
	    err_sys("child 3:can't write-lock file");

	printf("child 3 returned and got write lock??\n");
//	pause();
	printf("child 3: exit after pause\n");
	exit(0);
    }
    else 
	sleep(2);

    if(lock_reg(fd,F_SETLK,F_RDLCK,0,SEEK_SET,0)<0)
	printf("parent: can't set read lock: %s\n",strerror(errno));
    else
	printf("parent:obtained addtional read lock while"
		 " write lock is pending\n");

    printf("killing child 1...\n");
    kill(pid1,SIGINT);
    printf("killing child 2...\n");
    kill(pid2,SIGINT);
    printf("killing child 3...\n");
    kill(pid3,SIGINT);
    exit(0);
}
Ejemplo n.º 7
0
int main (void)
{
	pid_t pid1, pid2, pid3;
	int fd;

	setbuf (stdout, NULL);
	signal_intr (SIGINT, sigint);

	/*
	 * Create a file.
	 */
	if ((fd = open ("lockfile", O_RDWR | O_CREAT, 0666)) < 0) {
		err_sys ("can't open/create lockfile");
	}

	/*
	 * Read-lock the file.
	 */
	if ((pid1 = fork ()) < 0) {
		err_sys ("fork failed");
	} else if (pid1 == 0) {	/* child */
		if (lock_reg (fd, F_SETLK, F_RDLCK, 0, SEEK_SET, 0) < 0) {
			err_sys ("child 1: can't read-lock file");
		}
		printf ("child 1: obtained read lock on file\n");
		pause ();
		printf ("child 1: exit after pause\n");
		exit (0);
	} else {	/* parent */
		sleep (2);
	}

	/*
	 * Parent continues .. read-lock file again.
	 */
	if ((pid2 = fork ()) < 0) {
		err_sys ("fork failed");
	} else if (pid2 == 0) { /* child */
		if (lock_reg (fd, F_SETLK, F_RDLCK, 0, SEEK_SET, 0) < 0) {
			err_sys ("child 2: can't read-lock file");
		}
		printf ("child 2: obtained read lock on file\n");
		pause ();
		printf ("child 2: exit after pause\n");
		exit (0);
	} else {	/* parent */
		sleep (2);
	}

	/*
	 * Parent continues ... block while trying to write-lock
	 * the file
	 */
	if ((pid3 = fork ()) < 0) {
		err_sys ("fork failed");
	} else if (pid3 == 0) {	/* child */
		if (lock_reg (fd, F_SETLK, F_WRLCK, 0, SEEK_SET, 0) < 0) {
			printf ("child 3: can't set write lock: %s\n",
					strerror (errno));
		}
		printf ("child3 about to block in write-lock...\n");
		if (lock_reg (fd, F_SETLKW, F_WRLCK, 0, SEEK_SET, 0) < 0) {
			err_sys ("child3: can't write-lock file");
		}
		printf ("child 3 returned and got write lock???\n");
		pause ();
		printf ("child 3: exit after pause\n");
		exit (0);
	} else {	/* parent */
		sleep (2);
	}

	/*
	 * See if a pending write lock will block the next
	 * read-lock attempt
	 */
	if (lock_reg (fd, F_SETLK, F_RDLCK, 0, SEEK_SET, 0) < 0) {
		printf ("parent: can't set read lock: %s\n",
				strerror (errno));
	} else {
		printf ("parent: obtained additional read lock while"
				" write lock is pending\n");
	}
	printf ("killing child 1...\n");
	kill (pid1, SIGINT);
	printf ("killing child 2...\n");
	kill (pid2, SIGINT);
	printf ("killing child 3...\n");
	kill (pid3, SIGINT);
	exit (0);
}
Ejemplo n.º 8
0
int main(void){
   if(signal_intr(SIGUSR1,sig_usr1) == SIG_ERR)
       printf("SIGUSR1 error \n");
   kill(getpid(),SIGUSR1);
   sleep(5);
}