C: unix socket -> broken pipe -
i'm trying socket working in c code instead of calling command line find.
the child 3'sort' function piped child2 function 'cut' work fine, , program gets stuck in parent process waitpid() when 3 child functions included when executed.
i've tried isolate childs participate on socket , when ran executable on gdb message "find: 'standard output': broken pipe" , "find: write error"
here's example of 2 child functions interacting socket: child 1:
void child1() { int sock; struct sockaddr_un remote; sock = socket(af_unix, sock_stream, 0) memset(&remote, 0, sizeof(struct sockaddr_un)); remote.sun_family = af_unix; strncpy(remote.sun_path, "socket", sizeof(remote.sun_path) - 1); while((connect(sock, (struct sockaddr *)&remote, (socklen_t)sizeof(remote))) == -1) { if(errno != enoent && errno != econnrefused) erro("child 2 failed connect socket"); } dup2(sock, 1); close(sock); execlp("find", "find", ".", "-type" , "f", "-ls", null); }
and child2:
void child2(int *pipe_fd) { int sock; struct sockaddr_un local, remote; socklen_t sock_size = (socklen_t)sizeof(remote); sock= socket(af_unix, sock_stream, 0); memset(&local, 0, sizeof(struct sockaddr_un)); memset(&remote, 0, sizeof(struct sockaddr_un)); local.sun_family = af_unix; strncpy(local.sun_path, "socket", sizeof(local.sun_path) - 1); unlink("socket"); bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_un)); listen(sock, 1); sock = accept(sock,(struct sockaddr *)&remote, &sock_size)); dup2(sock, stdout_fileno); close(sock); close(pipe_fd[0]); dup2(pipe_fd[1],1); close(pipe_fd[1]); execlp("cut", "cut", "-d", " ", "-f", "3-", null); }
there's no need solve problem in specific, i'm trying understand i'm doing wrong in creation process don't again in future. apreciate in advance.
if change dup2(sock, stdout_fileno);
dup2(sock, stdin_fileno);
in child2
(the input there sock, , output pipe leads child3
), example works:
(error checking needed)
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <string.h> #include <sys/un.h> #include <sys/wait.h> void child1() { int sock; struct sockaddr_un remote; sock = socket(af_unix, sock_stream, 0); memset(&remote, 0, sizeof(struct sockaddr_un)); remote.sun_family = af_unix; strncpy(remote.sun_path, "socket", sizeof(remote.sun_path) - 1); while((connect(sock, (struct sockaddr *)&remote, (socklen_t)sizeof(remote))) == -1) { perror("connect"); if(errno != enoent && errno != econnrefused) perror("child 2 failed connect socket"); } dup2(sock, 1); close(sock); execlp("find", "find", ".", "-type" , "f", "-ls", (char*)0); } void child2(int *pipe_fd) { int sock; struct sockaddr_un local, remote; socklen_t sock_size = (socklen_t)sizeof(remote); sock= socket(af_unix, sock_stream, 0); memset(&local, 0, sizeof(struct sockaddr_un)); memset(&remote, 0, sizeof(struct sockaddr_un)); local.sun_family = af_unix; strncpy(local.sun_path, "socket", sizeof(local.sun_path) - 1); unlink("socket"); bind(sock, (struct sockaddr *)&local, sizeof(struct sockaddr_un)); listen(sock, 1); puts("listened"); sock = accept(sock,(struct sockaddr *)&remote, &sock_size); dup2(sock, stdin_fileno); close(sock); close(pipe_fd[0]); dup2(pipe_fd[1],1); close(pipe_fd[1]); execlp("cut", "cut", "-d", " ", "-f", "3-", (char*)0); } void child3(int *pipe_fd) { dup2(pipe_fd[0],0); close(pipe_fd[0]); execlp("sort", "sort", (char*)0); } int main() { int pi[2]; pid_t pid0, pid1, pid2; pid0 = fork(); if (0==pid0){ child1(); _exit(1); } pipe(pi); pid1 = fork(); if(0==pid1){ child2(pi); _exit(1); } close(pi[1]); pid2 = fork(); if(0==pid2){ child3(pi); _exit(1); } close(pi[0]); wait(0); wait(0); wait(0); }
this triple pipe:
find | cut | sort
where first |
not regular pipe rather unix socket connection through "socket"
.
Comments
Post a Comment