Exemplo n.º 1
0
int main (void)
{
	int		n;
	int		fd[2];
	pid_t	pid;
	char	line[MAXLINE];

	if (signal (SIGPIPE, sig_pipe) == SIG_ERR) {
		err_sys ("signal error");
	}

	if (s_pipe (fd) < 0) {	/* need only a single stream pipe */
		err_sys ("pipe error");
	}
	if ((pid = fork ()) < 0) {
		err_sys ("fork error");
	} else if (pid > 0) {	/* parent */
		close (fd[1]);
		while (fgets (line, MAXLINE, stdin) != NULL) {
			n = strlen (line);
			if (write (fd[0], line, n) != n) {
				err_sys ("write error to pipe");
			}
			if ((n = read (fd[0], line, MAXLINE)) < 0) {
				err_sys ("read error from pipe");
			}
			if (n == 0) {
				err_msg ("child closed pipe");
				break;
			}
			line[n] = 0;	/* null terminate */
			if (fputs (line, stdout) == EOF) {
				err_sys ("fputs error");
			}
		}
		if (ferror (stdin)) {
			err_sys ("fgets error on stdin");
		}
		exit (0);
	} else {		/* child */
		close (fd[0]);
		if (fd[1] != STDIN_FILENO &&
				dup2 (fd[1], STDIN_FILENO) != STDIN_FILENO) {
			err_sys ("dup2 error to stdin");
		}
		if (fd[1] != STDOUT_FILENO &&
				dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
			err_sys ("dup2 error to stdout");
		}
		if (execl ("./fig15.17", "fig15.17", (char *)0) < 0) {
			err_sys ("execl error");
		}
	}
	exit (0);
}
Exemplo n.º 2
0
int csopen(char *name, int oflag)
{
	pid_t pid;
	int len;
	char buf[10];
	struct iovec iov[3];
	static int fd[2] = {-1, -1};

	if (fd[0] < 0) {		/* fork/exec our open server first time */
		if (s_pipe(fd) < 0) {
			perror("s_pipe error\n");
			exit(1);
		}
		if ((pid = fork()) < 0) {
			perror("fork error\n");
			exit(1);
		} else if (pid == 0) {		/* child */
			close(fd[0]);
			if (fd[1] != STDIN_FILENO) {
				if (dup2(fd[1], STDIN_FILENO) != STDIN_FILENO) {
					perror("dup2 error to stdin\n");
					exit(1);
				}
			}
			if (fd[1] != STDOUT_FILENO) {
				if (dup2(fd[2], STDOUT_FILENO) != STDOUT_FILENO) {
					perror("dup2 error to stdout\n");
					exit(1);
				}
			}
			if (execl("./opend", "opend", NULL) < 0) {
				perror("execl error\n");
				exit(1);
			}
		}
		close(fd[1]);			/* parenet */
	}

	sprintf(buf, " %d", oflag);		/* oflag to ascii */
	iov[0].iov_base = CL_OPEN " ";
	iov[0].iov_len = strlen(CL_OPEN) + 1;
	iov[1].iov_base = name;
	iov[1].iov_len = strlen(name);
	iov[2].iov_base = buf;
	iov[2].iov_len = strlen(buf) + 1;	/* +1 for null at end of buf */
	len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
	if (writev(fd[0], &iov[0], 3) != len) {
		perror("writev error\n");
		exit(1);
	}

					/* read descriptor, returned errors handled by write() */
	return(recv_fd(fd[0], write));
}
Exemplo n.º 3
0
Arquivo: 19-7.c Projeto: klion26/APUE
void do_driver(char* driver)
{
	pid_t 	child;
	int 	pipe[2];

	/*
	 * Create a stream pipe to communicate with the driver.
	 */
	if(s_pipe(pipe) < 0)
		err_sys("can't create stream pipe");

	if((child = fork()) < 0)
		err_sys("fork error");
	else if(child == 0)
	{
		close(pipe[1]);

		/* stdin for driver */
		if(dup2(pipe[0], STDIN_FILENO) != STDIN_FILENO)
			err_sys("dup2 error to stdin");

		/* stdout for driver */
		if(dup2(pipe[0], STDOUT_FILENO) != STDOUT_FILENO)
			err_sys("dup2 error to stdout");
		if(pipe[0] != STDIN_FILENO && pipe[0] != STDOUT_FILENO)
			close(pipe[0]);

		/* leave stderr for driver alone */
		execlp(driver, driver, (char*) 0);
		err_sys("execlp error for: %s", driver);
	}
	close(pipe[0]);
	if(dup2(pipe[1], STDIN_FILENO) != STDIN_FILENO)
		err_sys("dup2 error to stdin");

		/* stdout for driver */
	if(dup2(pipe[1], STDOUT_FILENO) != STDOUT_FILENO)
		err_sys("dup2 error to stdout");
	if(pipe[1] != STDIN_FILENO && pipe[1] != STDOUT_FILENO)
		close(pipe[1]);

	/*
	 * Parent returns, but with stdin and stdout connected
	 * to the driver.
	 */
}
Exemplo n.º 4
0
NOEXPORT int signal_pipe_init(void) {
#ifdef USE_WIN32
    if(make_sockets(signal_pipe))
        return 1;
#elif defined(__INNOTEK_LIBC__)
    /* Innotek port of GCC can not use select on a pipe:
     * use local socket instead */
    struct sockaddr_un un;
    fd_set set_pipe;
    int pipe_in;

    FD_ZERO(&set_pipe);
    signal_pipe[0]=s_socket(PF_OS2, SOCK_STREAM, 0, 0, "socket#1");
    signal_pipe[1]=s_socket(PF_OS2, SOCK_STREAM, 0, 0, "socket#2");

    /* connect the two endpoints */
    memset(&un, 0, sizeof un);
    un.sun_len=sizeof un;
    un.sun_family=AF_OS2;
    sprintf(un.sun_path, "\\socket\\stunnel-%u", getpid());
    /* make the first endpoint listen */
    bind(signal_pipe[0], (struct sockaddr *)&un, sizeof un);
    listen(signal_pipe[0], 1);
    connect(signal_pipe[1], (struct sockaddr *)&un, sizeof un);
    FD_SET(signal_pipe[0], &set_pipe);
    if(select(signal_pipe[0]+1, &set_pipe, NULL, NULL, NULL)>0) {
        pipe_in=signal_pipe[0];
        signal_pipe[0]=s_accept(signal_pipe[0], NULL, 0, 0, "accept");
        closesocket(pipe_in);
    } else {
        sockerror("select");
        return 1;
    }
#else /* Unix */
    if(s_pipe(signal_pipe, 1, "signal_pipe"))
        return 1;
#endif /* USE_WIN32 */
    return 0;
}
Exemplo n.º 5
0
int
csopen(char *name, int oflag)
{
    pid_t          pid;
    int            len;
    char           buf[10];
    struct iovec   iov[3];
    static int     fd[2] = { -1, -1};
    
    if (fd[0] < 0) {
        if (s_pipe(fd) < 0)
            err_sys("s_pipe error");
        if ((pid = fork()) < 0) {
            err_sys("fork error");
        } else if (pid == 0) {
            close(fd[0]);
            if (fd[1] != STDIN_FILENO &&
              dup2(fd[1], STDIN_FILENO) != STDIN_FILENO)
                err_sys("dup2 error to stdout");
            if (execl("./opend", "opend", (char *)0) < 0)
                err_sys("execl error");
        }
        close(fd[1]);
    }
    sprintf(buf, " %d", oflag);
    iov[0].iov_base = CL_OPEN " ";
    iov[0].iov_len  = strlen(CL_OPEN) + 1;
    iov[1].iov_base = name;
    iov[1].iov_len  = strlen(name);
    iov[2].iov_base = buf;
    iov[2].iov_len  = strlen(buf) + 1;
    len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
    if (writev(fd[0], &iov[0], 3) != len)
        err_sys("writev error");

    return (recv_fd(fd[0], write));
}
Exemplo n.º 6
0
int socketpair(int a_family, int s_type, int s_protocol, int s_fd)
{
    return s_pipe(s_fd);
}