1 #include <sys/types.h> 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <sys/wait.h> 5 6 #include <err.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <unistd.h> 11 12 #define UNCONN_DGRAM_PATH "/tmp/unconn_dgram.sock" 13 14 static void 15 test_send_unconn_dgram(void) 16 { 17 struct sockaddr_un un; 18 int s, n; 19 20 s = socket(AF_LOCAL, SOCK_DGRAM, 0); 21 if (s < 0) 22 err(1, "send socket failed"); 23 24 memset(&un, 0, sizeof(un)); 25 un.sun_family = AF_LOCAL; 26 strlcpy(un.sun_path, UNCONN_DGRAM_PATH, sizeof(un.sun_path)); 27 28 n = sendto(s, UNCONN_DGRAM_PATH, sizeof(UNCONN_DGRAM_PATH), 0, 29 (const struct sockaddr *)&un, sizeof(un)); 30 if (n < 0) 31 err(1, "sendto failed"); 32 else if (n != sizeof(UNCONN_DGRAM_PATH)) 33 err(1, "sendto size mismatch"); 34 } 35 36 int 37 main(void) 38 { 39 struct sockaddr_un un; 40 char buf[64]; 41 pid_t pid; 42 int s, n, status; 43 44 s = socket(AF_LOCAL, SOCK_DGRAM, 0); 45 if (s < 0) 46 err(1, "socket failed"); 47 48 memset(&un, 0, sizeof(un)); 49 un.sun_family = AF_LOCAL; 50 strlcpy(un.sun_path, UNCONN_DGRAM_PATH, sizeof(un.sun_path)); 51 unlink(un.sun_path); 52 53 if (bind(s, (const struct sockaddr *)&un, sizeof(un)) < 0) 54 err(1, "bind failed"); 55 56 pid = fork(); 57 if (pid < 0) { 58 err(1, "fork failed"); 59 } else if (pid == 0) { 60 close(s); 61 test_send_unconn_dgram(); 62 exit(0); 63 } 64 waitpid(pid, &status, 0); 65 if (!WIFEXITED(status)) 66 err(1, "child did not exit"); 67 if (WEXITSTATUS(status) != 0) 68 err(WEXITSTATUS(status), "child failed"); 69 70 n = read(s, buf, sizeof(buf)); 71 if (n < 0) { 72 err(1, "read failed"); 73 } else if (n != sizeof(UNCONN_DGRAM_PATH)) { 74 warnx("dgram size mismatch"); 75 abort(); 76 } 77 fprintf(stderr, "%s\n", buf); 78 79 exit(0); 80 } 81