#include #include #include #include #include #include #include #include int create_socket(const char* path) { struct sockaddr_un domain_socket_address; // Check that the provided path will fit into the socket address. // [Add your code here] // Check whether the socket already exists, and remove it if so. struct stat file_stat; if (!stat(path, &file_stat)) { if ((file_stat.st_mode & S_IFMT) == S_IFSOCK) { unlink(path); } } // Now set up the socket address (domain_socket_address). // [Add your code here] // Next, create a SOCK_SEQPACKET socket using socket(2). // [Add your code here] // Bind the socket to the address using bind(2). // [Add your code here] // Mark the socket as a listening socket using listen(2). // [Add your code here] return listener_socket; } void accept_loop(int listen_socket) { // Repeatedly accept connections using accept(2). // [Add your code here] // Repeatedly recv(2) from the connection and print the result until no more data is recv(2)ed. // [Add your code here] // close(2) the connection. // [Add your code here] } void cleanup_socket(int socket, const char* path) { // close(2) the socket // [Add your code here] // unlink(2) the socket from the filesystem // [Add your code here] } int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "USAGE: domain-listener \n"); return 1; } const char* path = argv[1]; int sock = create_socket(path); if (sock < 0) { fprintf(stderr, "Failed to create listener socket: %s\n", strerror(errno)); return 1; } accept_loop(sock); cleanup_socket(sock, path); return 0; }