1 # Copyright (c) 2010, 2012 Nicira Networks
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
24 vlog = ovs.vlog.Vlog("socket_util")
27 def make_unix_socket(style, nonblock, bind_path, connect_path):
28 """Creates a Unix domain socket in the given 'style' (either
29 socket.SOCK_DGRAM or socket.SOCK_STREAM) that is bound to 'bind_path' (if
30 'bind_path' is not None) and connected to 'connect_path' (if 'connect_path'
31 is not None). If 'nonblock' is true, the socket is made non-blocking.
33 Returns (error, socket): on success 'error' is 0 and 'socket' is a new
34 socket object, on failure 'error' is a positive errno value and 'socket' is
38 sock = socket.socket(socket.AF_UNIX, style)
39 except socket.error, e:
40 return get_exception_errno(e), None
45 if bind_path is not None:
46 # Delete bind_path but ignore ENOENT.
50 if e.errno != errno.ENOENT:
53 ovs.fatal_signal.add_file_to_unlink(bind_path)
57 if sys.hexversion >= 0x02060000:
58 os.fchmod(sock.fileno(), 0700)
60 os.chmod("/dev/fd/%d" % sock.fileno(), 0700)
63 if connect_path is not None:
65 sock.connect(connect_path)
66 except socket.error, e:
67 if get_exception_errno(e) != errno.EINPROGRESS:
70 except socket.error, e:
72 if bind_path is not None:
77 ovs.fatal_signal.add_file_to_unlink(bind_path)
78 return get_exception_errno(e), None
81 def check_connection_completion(sock):
83 p.register(sock, select.POLLOUT)
84 if len(p.poll(0)) == 1:
85 return get_socket_error(sock)
90 def get_socket_error(sock):
91 """Returns the errno value associated with 'socket' (0 if no error) and
92 resets the socket's error status."""
93 return sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
96 def get_exception_errno(e):
97 """A lot of methods on Python socket objects raise socket.error, but that
98 exception is documented as having two completely different forms of
99 arguments: either a string or a (errno, string) tuple. We only want the
101 if type(e.args) == tuple:
111 """Returns a readable and writable fd for /dev/null, if successful,
112 otherwise a negative errno value. The caller must not close the returned
113 fd (because the same fd will be handed out to subsequent callers)."""
117 null_fd = os.open("/dev/null", os.O_RDWR)
119 vlog.err("could not open /dev/null: %s" % os.strerror(e.errno))
124 def write_fully(fd, buf):
125 """Returns an (error, bytes_written) tuple where 'error' is 0 on success,
126 otherwise a positive errno value, and 'bytes_written' is the number of
127 bytes that were written before the error occurred. 'error' is 0 if and
128 only if 'bytes_written' is len(buf)."""
134 retval = os.write(fd, buf)
136 if retval == len(buf):
137 return 0, bytes_written + len(buf)
139 vlog.warn("write returned 0")
140 return errno.EPROTO, bytes_written
142 bytes_written += retval
145 return e.errno, bytes_written
148 def set_nonblocking(sock):
151 except socket.error, e:
152 vlog.err("could not set nonblocking mode on socket: %s"
153 % os.strerror(get_socket_error(e)))