#include #include #include #include #include #include #include #include #include static const char s_socketpath[] = "/tmp/test.sn.sock"; void die(int x, const char *s) { perror(s); unlink(s_socketpath); exit(x); } int main() { int ret; int s; struct sockaddr_un sock; char namebuf[256]; socklen_t bufsize = sizeof(namebuf); namebuf[0] = 'a'; namebuf[1] = 0; memset(&sock, 0, sizeof(sock)); sock.sun_family = AF_UNIX; strcpy(sock.sun_path, s_socketpath); s = socket(AF_UNIX, SOCK_STREAM, PF_UNSPEC); if (s < 0) die(1, "socket"); ret = bind(s, (struct sockaddr *)&sock, sizeof(sock)); if (ret < 0) die(2, "bind"); ret = getsockname(s, (struct sockaddr *)namebuf, &bufsize); { struct sockaddr_un *ss = (struct sockaddr_un *)namebuf; printf("> getsockname: %d, %d ('%s') vs %u/%u\n", ret, bufsize, namebuf, offsetof(struct sockaddr, sa_data) + strlen(ss->sun_path) + 1,sizeof(sock)); printf("> ... %d vs %d, %s (%u)\n", ss->sun_family, AF_UNIX, ss->sun_path, strlen(ss->sun_path)); } if (ret < 0) die(3, "getsockname"); shutdown(s, SHUT_RDWR); close(s); unlink(s_socketpath); return 0; }