tests/unixsock.c

説明を見る。
00001 /* -*- c-file-style: "linux" -*-
00002  *
00003  * Try creating a Unix-domain socket, opening it, and reading from it.
00004  * The POSIX name for these is AF_LOCAL/PF_LOCAL.
00005  *
00006  * This is used by the Samba autoconf scripts to detect systems which
00007  * don't have Unix-domain sockets, such as (probably) VMS, or systems
00008  * on which they are broken under some conditions, such as RedHat 7.0
00009  * (unpatched).  We can't build WinBind there at the moment.
00010  *
00011  * Coding standard says to always use exit() for this, not return, so
00012  * we do.
00013  *
00014  * Martin Pool <mbp@samba.org>, June 2000. */
00015 
00016 /* TODO: Look for AF_LOCAL (most standard), AF_UNIX, and AF_FILE. */
00017 
00018 #include <stdio.h>
00019 
00020 #ifdef HAVE_SYS_SOCKET_H
00021 #  include <sys/socket.h>
00022 #endif
00023 
00024 #ifdef HAVE_SYS_UN_H
00025 #  include <sys/un.h>
00026 #endif
00027 
00028 #ifdef HAVE_SYS_TYPES_H
00029 #  include <sys/types.h>
00030 #endif
00031 
00032 #if HAVE_SYS_WAIT_H
00033 # include <sys/wait.h>
00034 #endif
00035 
00036 #if HAVE_ERRNO_DECL
00037 # include <errno.h>
00038 #else
00039 extern int errno;
00040 #endif
00041 
00042 static int bind_socket(char const *filename)
00043 {
00044         int sock_fd;
00045         struct sockaddr_un name;
00046         size_t size;
00047         
00048         /* Create the socket. */
00049         if ((sock_fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
00050                 perror ("socket(PF_LOCAL, SOCK_STREAM)");
00051                 exit(1);
00052         }
00053      
00054         /* Bind a name to the socket. */
00055         name.sun_family = AF_LOCAL;
00056         strncpy(name.sun_path, filename, sizeof (name.sun_path));
00057      
00058        /* The size of the address is
00059           the offset of the start of the filename,
00060           plus its length,
00061           plus one for the terminating null byte.
00062           Alternatively you can just do:
00063           size = SUN_LEN (&name);
00064       */
00065         size = SUN_LEN(&name);
00066         /* XXX: This probably won't work on unfriendly libcs */
00067      
00068         if (bind(sock_fd, (struct sockaddr *) &name, size) < 0) {
00069                 perror ("bind");
00070                 exit(1);
00071         }
00072 
00073         return sock_fd;
00074 }
00075 
00076 
00077 int main(void)
00078 {
00079         int sock_fd;
00080         int kid;
00081         char const *filename = "conftest.unixsock.sock";
00082 
00083         /* abolish hanging */
00084         alarm(15);              /* secs */
00085 
00086         if ((sock_fd = bind_socket(filename)) < 0)
00087                 exit(1);
00088 
00089         /* the socket will be deleted when autoconf cleans up these
00090            files. */
00091 
00092         exit(0);
00093 }

Sambaに対してSat Aug 29 21:23:27 2009に生成されました。  doxygen 1.4.7