This commit was generated by cvs2svn to compensate for changes in r30,
[kai/samba-autobuild/.git] / source4 / build / tests / unixsock.c
1 /* -*- c-file-style: "linux" -*-
2  *
3  * Try creating a Unix-domain socket, opening it, and reading from it.
4  * The POSIX name for these is AF_LOCAL/PF_LOCAL.
5  *
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.
10  *
11  * Coding standard says to always use exit() for this, not return, so
12  * we do.
13  *
14  * Martin Pool <mbp@samba.org>, June 2000. */
15
16 /* TODO: Look for AF_LOCAL (most standard), AF_UNIX, and AF_FILE. */
17
18 #include <stdio.h>
19
20 #ifdef HAVE_SYS_SOCKET_H
21 #  include <sys/socket.h>
22 #endif
23
24 #ifdef HAVE_SYS_UN_H
25 #  include <sys/un.h>
26 #endif
27
28 #ifdef HAVE_SYS_TYPES_H
29 #  include <sys/types.h>
30 #endif
31
32 #if HAVE_SYS_WAIT_H
33 # include <sys/wait.h>
34 #endif
35
36 #if HAVE_ERRNO_DECL
37 # include <errno.h>
38 #else
39 extern int errno;
40 #endif
41
42 static int bind_socket(char const *filename)
43 {
44         int sock_fd;
45         struct sockaddr_un name;
46         size_t size;
47         
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);
52         }
53      
54         /* Bind a name to the socket. */
55         name.sun_family = AF_LOCAL;
56         strncpy(name.sun_path, filename, sizeof (name.sun_path));
57      
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);
64       */
65         size = SUN_LEN(&name);
66         /* XXX: This probably won't work on unfriendly libcs */
67      
68         if (bind(sock_fd, (struct sockaddr *) &name, size) < 0) {
69                 perror ("bind");
70                 exit(1);
71         }
72
73         return sock_fd;
74 }
75
76
77 int main(void)
78 {
79         int sock_fd;
80         int kid;
81         char const *filename = "conftest.unixsock.sock";
82
83         /* abolish hanging */
84         alarm(15);              /* secs */
85
86         if ((sock_fd = bind_socket(filename)) < 0)
87                 exit(1);
88
89         /* the socket will be deleted when autoconf cleans up these
90            files. */
91
92         exit(0);
93 }