X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=python%2Fovs%2Fsocket_util.py;h=d00d3989b699c07f791417c61f9e7245942f6133;hb=688af176440823dbf644b00992eeae2f82397781;hp=4f4f603624b094bfd465131613c87c5098c3f706;hpb=991559357f6a03c3a5b70c053c8c2554aa8d5ee4;p=openvswitch diff --git a/python/ovs/socket_util.py b/python/ovs/socket_util.py index 4f4f6036..d00d3989 100644 --- a/python/ovs/socket_util.py +++ b/python/ovs/socket_util.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010 Nicira Networks +# Copyright (c) 2010, 2012 Nicira Networks # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,13 +13,16 @@ # limitations under the License. import errno -import logging import os import select import socket import sys import ovs.fatal_signal +import ovs.vlog + +vlog = ovs.vlog.Vlog("socket_util") + def make_unix_socket(style, nonblock, bind_path, connect_path): """Creates a Unix domain socket in the given 'style' (either @@ -66,14 +69,11 @@ def make_unix_socket(style, nonblock, bind_path, connect_path): return 0, sock except socket.error, e: sock.close() - try: - os.unlink(bind_path) - except OSError, e: - pass if bind_path is not None: - ovs.fatal_signal.add_file_to_unlink(bind_path) + ovs.fatal_signal.unlink_file_now(bind_path) return get_exception_errno(e), None + def check_connection_completion(sock): p = select.poll() p.register(sock, select.POLLOUT) @@ -82,11 +82,13 @@ def check_connection_completion(sock): else: return errno.EAGAIN + def get_socket_error(sock): """Returns the errno value associated with 'socket' (0 if no error) and resets the socket's error status.""" return sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) + def get_exception_errno(e): """A lot of methods on Python socket objects raise socket.error, but that exception is documented as having two completely different forms of @@ -97,7 +99,10 @@ def get_exception_errno(e): else: return errno.EPROTO + null_fd = -1 + + def get_null_fd(): """Returns a readable and writable fd for /dev/null, if successful, otherwise a negative errno value. The caller must not close the returned @@ -107,11 +112,11 @@ def get_null_fd(): try: null_fd = os.open("/dev/null", os.O_RDWR) except OSError, e: - logging.error("could not open /dev/null: %s" - % os.strerror(e.errno)) + vlog.err("could not open /dev/null: %s" % os.strerror(e.errno)) return -e.errno return null_fd + def write_fully(fd, buf): """Returns an (error, bytes_written) tuple where 'error' is 0 on success, otherwise a positive errno value, and 'bytes_written' is the number of @@ -127,7 +132,7 @@ def write_fully(fd, buf): if retval == len(buf): return 0, bytes_written + len(buf) elif retval == 0: - logging.warning("write returned 0") + vlog.warn("write returned 0") return errno.EPROTO, bytes_written else: bytes_written += retval @@ -135,9 +140,10 @@ def write_fully(fd, buf): except OSError, e: return e.errno, bytes_written + def set_nonblocking(sock): try: sock.setblocking(0) except socket.error, e: - logging.error("could not set nonblocking mode on socket: %s" - % os.strerror(get_socket_error(e))) + vlog.err("could not set nonblocking mode on socket: %s" + % os.strerror(get_socket_error(e)))