#include #include #include #include #include #include #define SV_SOCK_PATH "/tmp/waitall" #define BUF_SIZE 10 #define BACKLOG 5 int main () { struct sockaddr_un addr; int sfd, cfd; ssize_t nread; char buf[BUF_SIZE]; if ((sfd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) { perror ("socket"); exit (1); } if (remove (SV_SOCK_PATH) < 0 && errno != ENOENT) { perror ("remove"); exit (1); } memset (&addr, 0, sizeof (struct sockaddr_un)); addr.sun_family = AF_UNIX; strncpy (addr.sun_path, SV_SOCK_PATH, sizeof (addr.sun_path) - 1); if (bind (sfd, (struct sockaddr *) &addr, sizeof (struct sockaddr_un)) < 0) { perror ("bind"); exit (1); } if (listen (sfd, BACKLOG) < 0) { perror ("listen"); exit (1); } while (1) { cfd = accept (sfd, NULL, NULL); if (cfd < 0) { perror ("accept"); exit (1); } /* Transfer data from connected socket to stdout until EOF. */ while ((nread = recv (cfd, buf, BUF_SIZE, MSG_WAITALL)) > 0) if (write (STDOUT_FILENO, buf, nread) != nread) { perror ("partial/failed write"); exit (1); } if (nread < 0) { perror ("read"); exit (1); } if (close (cfd) < 0) { perror ("close"); exit (1); } } }