X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fsocket-util.c;h=961e00b4a19b23a4a77e09ca335db11deaccebfc;hb=4b6b46ce8a846269cf2ac95cea964ced7482030e;hp=350f5f065324148a78de2c49930820fb9101d35b;hpb=52f8a75e1b9cd27d35f45daabe9ca5495b4faee3;p=openvswitch diff --git a/lib/socket-util.c b/lib/socket-util.c index 350f5f06..961e00b4 100644 --- a/lib/socket-util.c +++ b/lib/socket-util.c @@ -26,13 +26,15 @@ #include #include #include +#include +#include #include #include #include "fatal-signal.h" #include "util.h" - #include "vlog.h" -#define THIS_MODULE VLM_socket_util + +VLOG_DEFINE_THIS_MODULE(socket_util) /* Sets 'fd' to non-blocking mode. Returns 0 if successful, otherwise a * positive errno value. */ @@ -53,6 +55,28 @@ set_nonblocking(int fd) } } +static bool +rlim_is_finite(rlim_t limit) +{ + if (limit == RLIM_INFINITY) { + return false; + } + +#ifdef RLIM_SAVED_CUR /* FreeBSD 8.0 lacks RLIM_SAVED_CUR. */ + if (limit == RLIM_SAVED_CUR) { + return false; + } +#endif + +#ifdef RLIM_SAVED_MAX /* FreeBSD 8.0 lacks RLIM_SAVED_MAX. */ + if (limit == RLIM_SAVED_MAX) { + return false; + } +#endif + + return true; +} + /* Returns the maximum valid FD value, plus 1. */ int get_max_fds(void) @@ -60,10 +84,7 @@ get_max_fds(void) static int max_fds = -1; if (max_fds < 0) { struct rlimit r; - if (!getrlimit(RLIMIT_NOFILE, &r) - && r.rlim_cur != RLIM_INFINITY - && r.rlim_cur != RLIM_SAVED_MAX - && r.rlim_cur != RLIM_SAVED_CUR) { + if (!getrlimit(RLIMIT_NOFILE, &r) && rlim_is_finite(r.rlim_cur)) { max_fds = r.rlim_cur; } else { VLOG_WARN("failed to obtain fd limit, defaulting to 1024"); @@ -77,7 +98,7 @@ get_max_fds(void) * address, into a numeric IP address in '*addr'. Returns 0 if successful, * otherwise a positive errno value. */ int -lookup_ip(const char *host_name, struct in_addr *addr) +lookup_ip(const char *host_name, struct in_addr *addr) { if (!inet_aton(host_name, addr)) { struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); @@ -90,7 +111,7 @@ lookup_ip(const char *host_name, struct in_addr *addr) /* Returns the error condition associated with socket 'fd' and resets the * socket's error status. */ int -get_socket_error(int fd) +get_socket_error(int fd) { int error; socklen_t len = sizeof(error); @@ -103,7 +124,7 @@ get_socket_error(int fd) } int -check_connection_completion(int fd) +check_connection_completion(int fd) { struct pollfd pfd; int retval; @@ -249,7 +270,6 @@ make_unix_socket(int style, bool nonblock, bool passcred OVS_UNUSED, make_sockaddr_un(connect_path, &un, &un_len); if (connect(fd, (struct sockaddr*) &un, un_len) && errno != EINPROGRESS) { - printf("connect failed with %s\n", strerror(errno)); goto error; } } @@ -436,7 +456,7 @@ inet_open_passive(int style, const char *target_, int default_port, struct sockaddr_in sin; const char *host_name; const char *port_string; - int fd, error, port; + int fd = 0, error, port; unsigned int yes = 1; /* Address defaults. */ @@ -611,3 +631,31 @@ fsync_parent_dir(const char *file_name) return error; } + +/* Obtains the modification time of the file named 'file_name' to the greatest + * supported precision. If successful, stores the mtime in '*mtime' and + * returns 0. On error, returns a positive errno value and stores zeros in + * '*mtime'. */ +int +get_mtime(const char *file_name, struct timespec *mtime) +{ + struct stat s; + + if (!stat(file_name, &s)) { + mtime->tv_sec = s.st_mtime; + +#if HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC + mtime->tv_nsec = s.st_mtim.tv_nsec; +#elif HAVE_STRUCT_STAT_ST_MTIMENSEC + mtime->tv_nsec = s.st_mtimensec; +#else + mtime->tv_nsec = 0; +#endif + + return 0; + } else { + mtime->tv_sec = mtime->tv_nsec = 0; + return errno; + } +} +