]> git.lyx.org Git - features.git/blob - src/support/socktools.cpp
Guard new code for builds with OS X 10.11 SDK and lesser (part 2).
[features.git] / src / support / socktools.cpp
1 /**
2  * \file socktools.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author João Luis M. Assirati
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/socktools.h"
14 #include "support/FileName.h"
15
16 #if !defined (HAVE_FCNTL)
17 // We provide stubs because we don't (yet?) support the native OS API.
18
19 namespace lyx {
20 namespace support {
21 namespace socktools {
22
23 int listen(FileName const &, int)
24 {
25         return -1;
26 }
27
28
29 int accept(int)
30 {
31         return -1;
32 }
33
34 } // namespace socktools
35 } // namespace support
36 } // namespace lyx
37
38 #else // defined (HAVE_FCNTL)
39
40 #include "support/debug.h"
41
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
44 #endif
45 #include <sys/socket.h>
46 #include <sys/un.h>
47 #include <fcntl.h>
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52 #include <cerrno>
53 #include <string>
54 //needed for Mac OSX 10.5.2 Leopard
55 #include <cstring>
56
57 using namespace std;
58
59
60 // This MACRO eppears to be defined only on Linux.
61 #if !defined(SUN_LEN)
62 #define SUN_LEN(su) \
63         (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
64 #endif
65
66
67 namespace lyx {
68 namespace support {
69 namespace socktools {
70
71 // Returns a local socket already in the "listen" state (or -1 in case
72 // of error). The first argument is the socket address, the second
73 // is the length of the queue for connections. If successful, a socket
74 // special file 'name' will be created in the filesystem.
75 int listen(FileName const & name, int queue)
76 {
77         int fd; // File descriptor for the socket
78         sockaddr_un addr; // Structure that hold the socket address
79
80         // We use 'localname' to fill 'addr'
81         string const localname = name.toFilesystemEncoding();
82         string::size_type len = localname.size();
83         // the field sun_path in sockaddr_un is a char[108]
84         if (len > 107) {
85                 LYXERR0("lyx: Socket address '" << name.absFileName() << "' too long.");
86                 return -1;
87         }
88         // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
89         addr.sun_family = AF_UNIX;
90         localname.copy(addr.sun_path, 107);
91         addr.sun_path[len] = '\0';
92
93         // This creates a file descriptor for the socket
94         // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
95         // For local sockets, the protocol is always 0
96         // socket() returns -1 in case of error
97         if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
98                 LYXERR0("lyx: Could not create socket descriptor: "
99                        << strerror(errno));
100                 return -1;
101         }
102
103         // Set NONBLOCK mode for the file descriptor
104         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
105                 LYXERR0("lyx: Could not set NONBLOCK mode for socket descriptor: "
106                      << strerror(errno));
107                 ::close(fd);
108                 return -1;
109         }
110
111         // bind() gives the local address 'name' for 'fd', also creating
112         // the socket special file in the filesystem. bind() returns -1
113         // in case of error
114         //
115         // Using Clang and fsanitize suggests there is an issue here but we do
116         // not understand the code enough to change it and we are not aware of
117         // how to trigger a crash or other issue while using LyX, so we leave
118         // it as is. For ML discussion, see here:
119         //   https://www.mail-archive.com/search?l=mid&q=20211227113249.53bf5a63%40admin1-desktop
120         //
121         if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
122                 LYXERR0("lyx: Could not bind address '" << name.absFileName()
123                        << "' to socket descriptor: " << strerror(errno));
124                 ::close(fd);
125                 name.removeFile();
126                 return -1;
127         }
128
129         // Puts the socket in listen state, that is, ready to accept
130         // connections. The second parameter of listen() defines the
131         // maximum length the queue of pending connections may grow to.
132         // It is not a restriction on the number of connections the socket
133         // can accept. Returns -1 in case of error
134         if (::listen (fd, queue) == -1) {
135                 LYXERR0("lyx: Could not put socket in 'listen' state: "
136                        << strerror(errno));
137                 ::close(fd);
138                 name.removeFile();
139                 return -1;
140         }
141
142         return fd;
143 }
144
145 // Returns a file descriptor for a new connection from the socket
146 // descriptor 'sd' (or -1 in case of error)
147 int accept(int sd)
148 {
149         int fd;
150
151         // Returns the new file descriptor or -1 in case of error
152         // Using null pointers for the second and third arguments
153         // dismiss all information about the connecting client
154         if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
155                 LYXERR0("lyx: Could not accept connection: "
156                        << strerror(errno));
157                 return -1;
158         }
159
160         // Sets NONBLOCK mode for the file descriptor
161         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
162                 LYXERR0("lyx: Could not set NONBLOCK mode for connection: "
163                        << strerror(errno));
164                 ::close(fd);
165                 return -1;
166         }
167         return fd;
168 }
169
170 } // namespace socktools
171 } // namespace support
172 } // namespace lyx
173
174 #endif // defined (HAVE_FCNTL)