Global replace of Nicira Networks.
[sliver-openvswitch.git] / python / ovs / socket_util.py
1 # Copyright (c) 2010, 2012 Nicira, Inc.
2 #
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:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import errno
16 import os
17 import select
18 import socket
19 import sys
20
21 import ovs.fatal_signal
22 import ovs.vlog
23
24 vlog = ovs.vlog.Vlog("socket_util")
25
26
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.
32
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
35     None."""
36
37     try:
38         sock = socket.socket(socket.AF_UNIX, style)
39     except socket.error, e:
40         return get_exception_errno(e), None
41
42     try:
43         if nonblock:
44             set_nonblocking(sock)
45         if bind_path is not None:
46             # Delete bind_path but ignore ENOENT.
47             try:
48                 os.unlink(bind_path)
49             except OSError, e:
50                 if e.errno != errno.ENOENT:
51                     return e.errno, None
52
53             ovs.fatal_signal.add_file_to_unlink(bind_path)
54             sock.bind(bind_path)
55
56             try:
57                 if sys.hexversion >= 0x02060000:
58                     os.fchmod(sock.fileno(), 0700)
59                 else:
60                     os.chmod("/dev/fd/%d" % sock.fileno(), 0700)
61             except OSError, e:
62                 pass
63         if connect_path is not None:
64             try:
65                 sock.connect(connect_path)
66             except socket.error, e:
67                 if get_exception_errno(e) != errno.EINPROGRESS:
68                     raise
69         return 0, sock
70     except socket.error, e:
71         sock.close()
72         if bind_path is not None:
73             ovs.fatal_signal.unlink_file_now(bind_path)
74         return get_exception_errno(e), None
75
76
77 def check_connection_completion(sock):
78     p = select.poll()
79     p.register(sock, select.POLLOUT)
80     if len(p.poll(0)) == 1:
81         return get_socket_error(sock)
82     else:
83         return errno.EAGAIN
84
85
86 def get_socket_error(sock):
87     """Returns the errno value associated with 'socket' (0 if no error) and
88     resets the socket's error status."""
89     return sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
90
91
92 def get_exception_errno(e):
93     """A lot of methods on Python socket objects raise socket.error, but that
94     exception is documented as having two completely different forms of
95     arguments: either a string or a (errno, string) tuple.  We only want the
96     errno."""
97     if type(e.args) == tuple:
98         return e.args[0]
99     else:
100         return errno.EPROTO
101
102
103 null_fd = -1
104
105
106 def get_null_fd():
107     """Returns a readable and writable fd for /dev/null, if successful,
108     otherwise a negative errno value.  The caller must not close the returned
109     fd (because the same fd will be handed out to subsequent callers)."""
110     global null_fd
111     if null_fd < 0:
112         try:
113             null_fd = os.open("/dev/null", os.O_RDWR)
114         except OSError, e:
115             vlog.err("could not open /dev/null: %s" % os.strerror(e.errno))
116             return -e.errno
117     return null_fd
118
119
120 def write_fully(fd, buf):
121     """Returns an (error, bytes_written) tuple where 'error' is 0 on success,
122     otherwise a positive errno value, and 'bytes_written' is the number of
123     bytes that were written before the error occurred.  'error' is 0 if and
124     only if 'bytes_written' is len(buf)."""
125     bytes_written = 0
126     if len(buf) == 0:
127         return 0, 0
128     while True:
129         try:
130             retval = os.write(fd, buf)
131             assert retval >= 0
132             if retval == len(buf):
133                 return 0, bytes_written + len(buf)
134             elif retval == 0:
135                 vlog.warn("write returned 0")
136                 return errno.EPROTO, bytes_written
137             else:
138                 bytes_written += retval
139                 buf = buf[:retval]
140         except OSError, e:
141             return e.errno, bytes_written
142
143
144 def set_nonblocking(sock):
145     try:
146         sock.setblocking(0)
147     except socket.error, e:
148         vlog.err("could not set nonblocking mode on socket: %s"
149                  % os.strerror(get_socket_error(e)))