1 # Copyright (c) 2010, 2012 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 import ovs.fatal_signal
25 vlog = ovs.vlog.Vlog("socket_util")
28 def make_unix_socket(style, nonblock, bind_path, connect_path):
29 """Creates a Unix domain socket in the given 'style' (either
30 socket.SOCK_DGRAM or socket.SOCK_STREAM) that is bound to 'bind_path' (if
31 'bind_path' is not None) and connected to 'connect_path' (if 'connect_path'
32 is not None). If 'nonblock' is true, the socket is made non-blocking.
34 Returns (error, socket): on success 'error' is 0 and 'socket' is a new
35 socket object, on failure 'error' is a positive errno value and 'socket' is
39 sock = socket.socket(socket.AF_UNIX, style)
40 except socket.error, e:
41 return get_exception_errno(e), None
46 if bind_path is not None:
47 # Delete bind_path but ignore ENOENT.
51 if e.errno != errno.ENOENT:
54 ovs.fatal_signal.add_file_to_unlink(bind_path)
58 if sys.hexversion >= 0x02060000:
59 os.fchmod(sock.fileno(), 0700)
61 os.chmod("/dev/fd/%d" % sock.fileno(), 0700)
64 if connect_path is not None:
66 sock.connect(connect_path)
67 except socket.error, e:
68 if get_exception_errno(e) != errno.EINPROGRESS:
71 except socket.error, e:
73 if (bind_path is not None and
74 os.path.exists(bind_path)):
75 ovs.fatal_signal.unlink_file_now(bind_path)
76 eno = ovs.socket_util.get_exception_errno(e)
77 if (eno == "AF_UNIX path too long" and
78 os.uname()[0] == "Linux"):
79 short_connect_path = None
80 short_bind_path = None
83 # Try workaround using /proc/self/fd
84 if connect_path is not None:
85 dirname = os.path.dirname(connect_path)
86 basename = os.path.basename(connect_path)
88 connect_dirfd = os.open(dirname, os.O_DIRECTORY | os.O_RDONLY)
90 return get_exception_errno(err), None
91 short_connect_path = "/proc/self/fd/%d/%s" % (connect_dirfd, basename)
93 if bind_path is not None:
94 dirname = os.path.dirname(bind_path)
95 basename = os.path.basename(bind_path)
97 bind_dirfd = os.open(dirname, os.O_DIRECTORY | os.O_RDONLY)
99 return get_exception_errno(err), None
100 short_bind_path = "/proc/self/fd/%d/%s" % (bind_dirfd, basename)
103 return make_unix_socket(style, nonblock, short_bind_path, short_connect_path)
105 if connect_dirfd is not None:
106 os.close(connect_dirfd)
107 if bind_dirfd is not None:
110 return get_exception_errno(e), None
113 def check_connection_completion(sock):
114 p = ovs.poller.SelectPoll()
115 p.register(sock, ovs.poller.POLLOUT)
119 if revents & ovs.poller.POLLERR:
121 # The following should raise an exception.
122 socket.send("\0", socket.MSG_DONTWAIT)
124 # (Here's where we end up if it didn't.)
126 vlog.err("poll return POLLERR but send succeeded")
128 except socket.error, e:
129 return get_exception_errno(e)
136 def inet_parse_active(target, default_port):
137 address = target.split(":")
138 host_name = address[0]
140 raise ValueError("%s: bad peer name format" % target)
141 if len(address) >= 2:
142 port = int(address[1])
146 raise ValueError("%s: port number must be specified" % target)
147 return (host_name, port)
150 def inet_open_active(style, target, default_port, dscp):
151 address = inet_parse_active(target, default_port)
153 sock = socket.socket(socket.AF_INET, style, 0)
154 except socket.error, e:
155 return get_exception_errno(e), None
158 set_nonblocking(sock)
161 sock.connect(address)
162 except socket.error, e:
163 if get_exception_errno(e) != errno.EINPROGRESS:
166 except socket.error, e:
168 return get_exception_errno(e), None
171 def get_exception_errno(e):
172 """A lot of methods on Python socket objects raise socket.error, but that
173 exception is documented as having two completely different forms of
174 arguments: either a string or a (errno, string) tuple. We only want the
176 if type(e.args) == tuple:
186 """Returns a readable and writable fd for /dev/null, if successful,
187 otherwise a negative errno value. The caller must not close the returned
188 fd (because the same fd will be handed out to subsequent callers)."""
192 null_fd = os.open("/dev/null", os.O_RDWR)
194 vlog.err("could not open /dev/null: %s" % os.strerror(e.errno))
199 def write_fully(fd, buf):
200 """Returns an (error, bytes_written) tuple where 'error' is 0 on success,
201 otherwise a positive errno value, and 'bytes_written' is the number of
202 bytes that were written before the error occurred. 'error' is 0 if and
203 only if 'bytes_written' is len(buf)."""
209 retval = os.write(fd, buf)
211 if retval == len(buf):
212 return 0, bytes_written + len(buf)
214 vlog.warn("write returned 0")
215 return errno.EPROTO, bytes_written
217 bytes_written += retval
220 return e.errno, bytes_written
223 def set_nonblocking(sock):
226 except socket.error, e:
227 vlog.err("could not set nonblocking mode on socket: %s"
228 % os.strerror(get_exception_errno(e)))
231 def set_dscp(sock, dscp):
233 raise ValueError("Invalid dscp %d" % dscp)
235 sock.setsockopt(socket.IPPROTO_IP, socket.IP_TOS, val)