r9038: Revert svn rev 414. Discussed with Jerry and Metze.
[Samba.git] / source / tests / unixsock.c
blobf2765d68f67decfd2709fa9db9711bb699534296
1 /* -*- c-file-style: "linux" -*-
3 * Try creating a Unix-domain socket, opening it, and reading from it.
4 * The POSIX name for these is AF_LOCAL/PF_LOCAL.
6 * This is used by the Samba autoconf scripts to detect systems which
7 * don't have Unix-domain sockets, such as (probably) VMS, or systems
8 * on which they are broken under some conditions, such as RedHat 7.0
9 * (unpatched). We can't build WinBind there at the moment.
11 * Coding standard says to always use exit() for this, not return, so
12 * we do.
14 * Martin Pool <mbp@samba.org>, June 2000. */
16 /* TODO: Look for AF_LOCAL (most standard), AF_UNIX, and AF_FILE. */
18 #include <stdio.h>
20 #ifdef HAVE_SYS_SOCKET_H
21 # include <sys/socket.h>
22 #endif
24 #ifdef HAVE_SYS_UN_H
25 # include <sys/un.h>
26 #endif
28 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
30 #endif
32 #if HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 #endif
36 #if HAVE_ERRNO_DECL
37 # include <errno.h>
38 #else
39 extern int errno;
40 #endif
42 static int bind_socket(char const *filename)
44 int sock_fd;
45 struct sockaddr_un name;
46 size_t size;
48 /* Create the socket. */
49 if ((sock_fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
50 perror ("socket(PF_LOCAL, SOCK_STREAM)");
51 exit(1);
54 /* Bind a name to the socket. */
55 name.sun_family = AF_LOCAL;
56 strncpy(name.sun_path, filename, sizeof (name.sun_path));
58 /* The size of the address is
59 the offset of the start of the filename,
60 plus its length,
61 plus one for the terminating null byte.
62 Alternatively you can just do:
63 size = SUN_LEN (&name);
65 size = SUN_LEN(&name);
66 /* XXX: This probably won't work on unfriendly libcs */
68 if (bind(sock_fd, (struct sockaddr *) &name, size) < 0) {
69 perror ("bind");
70 exit(1);
73 return sock_fd;
77 int main(void)
79 int sock_fd;
80 int kid;
81 char const *filename = "conftest.unixsock.sock";
83 /* abolish hanging */
84 alarm(15); /* secs */
86 if ((sock_fd = bind_socket(filename)) < 0)
87 exit(1);
89 /* the socket will be deleted when autoconf cleans up these
90 files. */
92 exit(0);