]> git.lyx.org Git - lyx.git/blob - src/support/socktools.cpp
compilation fix on openbsd (bug 5650)
[lyx.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 #include <cstring>
55
56 using namespace std;
57
58
59 // This MACRO eppears to be defined only on Linux.
60 #if !defined(SUN_LEN)
61 #define SUN_LEN(su) \
62         (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
63 #endif
64
65
66 namespace lyx {
67 namespace support {
68 namespace socktools {
69
70 // Returns a local socket already in the "listen" state (or -1 in case
71 // of error). The first argument is the socket address, the second
72 // is the length of the queue for connections. If successful, a socket
73 // special file 'name' will be created in the filesystem.
74 int listen(FileName const & name, int queue)
75 {
76         int fd; // File descriptor for the socket
77         sockaddr_un addr; // Structure that hold the socket address
78
79         // We use 'localname' to fill 'addr'
80         string const localname = name.toFilesystemEncoding();
81         string::size_type len = localname.size();
82         // the field sun_path in sockaddr_un is a char[108]
83         if (len > 107) {
84                 LYXERR0("lyx: Socket address '" << name.absFilename() << "' too long.");
85                 return -1;
86         }
87         // Synonims for AF_UNIX are AF_LOCAL and AF_FILE
88         addr.sun_family = AF_UNIX;
89         localname.copy(addr.sun_path, 107);
90         addr.sun_path[len] = '\0';
91
92         // This creates a file descriptor for the socket
93         // Synonims for PF_UNIX are PF_LOCAL and PF_FILE
94         // For local sockets, the protocol is always 0
95         // socket() returns -1 in case of error
96         if ((fd = ::socket(PF_UNIX, SOCK_STREAM, 0))== -1) {
97                 LYXERR0("lyx: Could not create socket descriptor: "
98                        << strerror(errno));
99                 return -1;
100         }
101
102         // Set NONBLOCK mode for the file descriptor
103         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
104                 LYXERR0("lyx: Could not set NONBLOCK mode for socket descriptor: "
105                      << strerror(errno));
106                 ::close(fd);
107                 return -1;
108         }
109
110         // bind() gives the local address 'name' for 'fd', also creating
111         // the socket special file in the filesystem. bind() returns -1
112         // in case of error
113         if ((::bind (fd, reinterpret_cast<sockaddr *>(&addr), SUN_LEN(&addr))) == -1) {
114                 LYXERR0("lyx: Could not bind address '" << name.absFilename()
115                        << "' to socket descriptor: " << strerror(errno));
116                 ::close(fd);
117                 name.removeFile();
118                 return -1;
119         }
120
121         // Puts the socket in listen state, that is, ready to accept
122         // connections. The second parameter of listen() defines the
123         // maximum length the queue of pending connections may grow to.
124         // It is not a restriction on the number of connections the socket
125         // can accept. Returns -1 in case of error
126         if (::listen (fd, queue) == -1) {
127                 LYXERR0("lyx: Could not put socket in 'listen' state: "
128                        << strerror(errno));
129                 ::close(fd);
130                 name.removeFile();
131                 return -1;
132         }
133
134         return fd;
135 }
136
137 // Returns a file descriptor for a new connection from the socket
138 // descriptor 'sd' (or -1 in case of error)
139 int accept(int sd)
140 {
141         int fd;
142
143         // Returns the new file descriptor or -1 in case of error
144         // Using null pointers for the second and third arguments
145         // dismiss all information about the connecting client
146         if ((fd = accept(sd, reinterpret_cast<sockaddr *>(0), reinterpret_cast<socklen_t *>(0))) == -1) {
147                 LYXERR0("lyx: Could not accept connection: "
148                        << strerror(errno));
149                 return -1;
150         }
151
152         // Sets NONBLOCK mode for the file descriptor
153         if (::fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
154                 LYXERR0("lyx: Could not set NONBLOCK mode for connection: "
155                        << strerror(errno));
156                 ::close(fd);
157                 return -1;
158         }
159         return fd;
160 }
161
162 } // namespace socktools
163 } // namespace support
164 } // namespace lyx
165
166 #endif // defined (HAVE_FCNTL)