3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author João Luis M. Assirati
8 * Full author contact details are available in file CREDITS.
13 #include "support/socktools.h"
14 #include "support/FileName.h"
16 #if !defined (HAVE_FCNTL)
17 // We provide stubs because we don't (yet?) support the native OS API.
23 int listen(FileName const &, int)
34 } // namespace socktools
35 } // namespace support
38 #else // defined (HAVE_FCNTL)
40 #include "support/debug.h"
42 #include <sys/socket.h>
56 // This MACRO eppears to be defined only on Linux.
59 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
67 // Returns a local socket already in the "listen" state (or -1 in case
68 // of error). The first argument is the socket address, the second
69 // is the length of the queue for connections. If successful, a socket
70 // special file 'name' will be created in the filesystem.
71 int listen(FileName const & name, int queue)
73 int fd; // File descriptor for the socket
74 sockaddr_un addr; // Structure that hold the socket address
76 // We use 'localname' to fill 'addr'
77 string const localname = name.toFilesystemEncoding();
78 string::size_type len = localname.size();
79 // the field sun_path in sockaddr_un is a char[108]
81 LYXERR0("lyx: Socket address '" << name.absFilename() << "' too long.");
84 // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
85 addr.sun_family = AF_UNIX;
86 localname.copy(addr.sun_path, 107);
87 addr.sun_path[len] = '\0';
89 // This creates a file descriptor for the socket
90 // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
91 // For local sockets, the protocol is always 0
92 // socket() returns -1 in case of error
93 if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
94 LYXERR0("lyx: Could not create socket descriptor: "
99 // Set NONBLOCK mode for the file descriptor
100 if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
101 LYXERR0("lyx: Could not set NONBLOCK mode for socket descriptor: "
107 // bind() gives the local address 'name' for 'fd', also creating
108 // the socket special file in the filesystem. bind() returns -1
110 if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
111 LYXERR0("lyx: Could not bind address '" << name.absFilename()
112 << "' to socket descriptor: " << strerror(errno));
118 // Puts the socket in listen state, that is, ready to accept
119 // connections. The second parameter of listen() defines the
120 // maximum length the queue of pending connections may grow to.
121 // It is not a restriction on the number of connections the socket
122 // can accept. Returns -1 in case of error
123 if (::listen (fd, queue) == -1) {
124 LYXERR0("lyx: Could not put socket in 'listen' state: "
134 // Returns a file descriptor for a new connection from the socket
135 // descriptor 'sd' (or -1 in case of error)
140 // Returns the new file descriptor or -1 in case of error
141 // Using null pointers for the second and third arguments
142 // dismiss all information about the connecting client
143 if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
144 LYXERR0("lyx: Could not accept connection: "
149 // Sets NONBLOCK mode for the file descriptor
150 if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
151 LYXERR0("lyx: Could not set NONBLOCK mode for connection: "
159 } // namespace socktools
160 } // namespace support
163 #endif // defined (HAVE_FCNTL)