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