#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) != -1) {
return 0;
} else {
- VLOG_ERR("fcntl(F_SETFL) failed: %s", strerror(errno));
+ VLOG_ERR("fcntl(F_SETFL) failed: %s", ovs_strerror(errno));
return errno;
}
} else {
- VLOG_ERR("fcntl(F_GETFL) failed: %s", strerror(errno));
+ VLOG_ERR("fcntl(F_GETFL) failed: %s", ovs_strerror(errno));
return errno;
}
}
int
get_max_fds(void)
{
- static int max_fds = -1;
- if (max_fds < 0) {
+ static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+ static int max_fds;
+
+ if (ovsthread_once_start(&once)) {
struct rlimit r;
if (!getrlimit(RLIMIT_NOFILE, &r) && rlim_is_finite(r.rlim_cur)) {
max_fds = r.rlim_cur;
VLOG_WARN("failed to obtain fd limit, defaulting to 1024");
max_fds = 1024;
}
+ ovsthread_once_done(&once);
}
+
return max_fds;
}
* successful, otherwise a positive errno value.
*
* Most Open vSwitch code should not use this because it causes deadlocks:
- * gethostbyname() sends out a DNS request but that starts a new flow for which
+ * getaddrinfo() sends out a DNS request but that starts a new flow for which
* OVS must set up a flow, but it can't because it's waiting for a DNS reply.
* The synchronous lookup also delays other activity. (Of course we can solve
* this but it doesn't seem worthwhile quite yet.) */
int
lookup_hostname(const char *host_name, struct in_addr *addr)
{
- struct hostent *h;
+ struct addrinfo *result;
+ struct addrinfo hints;
if (inet_aton(host_name, addr)) {
return 0;
}
- h = gethostbyname(host_name);
- if (h) {
- *addr = *(struct in_addr *) h->h_addr;
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_INET;
+
+ switch (getaddrinfo(host_name, NULL, &hints, &result)) {
+ case 0:
+ *addr = ALIGNED_CAST(struct sockaddr_in *,
+ result->ai_addr)->sin_addr;
+ freeaddrinfo(result);
return 0;
- }
- return (h_errno == HOST_NOT_FOUND ? ENOENT
- : h_errno == TRY_AGAIN ? EAGAIN
- : h_errno == NO_RECOVERY ? EIO
- : h_errno == NO_ADDRESS ? ENXIO
- : EINVAL);
+#ifdef EAI_ADDRFAMILY
+ case EAI_ADDRFAMILY:
+#endif
+ case EAI_NONAME:
+ case EAI_SERVICE:
+ return ENOENT;
+
+ case EAI_AGAIN:
+ return EAGAIN;
+
+ case EAI_BADFLAGS:
+ case EAI_FAMILY:
+ case EAI_SOCKTYPE:
+ return EINVAL;
+
+ case EAI_FAIL:
+ return EIO;
+
+ case EAI_MEMORY:
+ return ENOMEM;
+
+#ifdef EAI_NODATA
+ case EAI_NODATA:
+ return ENXIO;
+#endif
+
+ case EAI_SYSTEM:
+ return errno;
+
+ default:
+ return EPROTO;
+ }
}
int
}
return 0;
} else if (retval < 0) {
- VLOG_ERR_RL(&rl, "poll: %s", strerror(errno));
+ VLOG_ERR_RL(&rl, "poll: %s", ovs_strerror(errno));
return errno;
} else {
return EAGAIN;
int dirfd;
if (unlink(bind_path) && errno != ENOENT) {
- VLOG_WARN("unlinking \"%s\": %s\n", bind_path, strerror(errno));
+ VLOG_WARN("unlinking \"%s\": %s\n",
+ bind_path, ovs_strerror(errno));
}
fatal_signal_add_file_to_unlink(bind_path);
/* Create non-blocking socket. */
fd = socket(AF_INET, style, 0);
if (fd < 0) {
- VLOG_ERR("%s: socket: %s", target, strerror(errno));
+ VLOG_ERR("%s: socket: %s", target, ovs_strerror(errno));
error = errno;
goto exit;
}
* connect(), the handshake SYN frames will be sent with a TOS of 0. */
error = set_dscp(fd, dscp);
if (error) {
- VLOG_ERR("%s: socket: %s", target, strerror(error));
+ VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
goto exit;
}
fd = socket(AF_INET, style, 0);
if (fd < 0) {
error = errno;
- VLOG_ERR("%s: socket: %s", target, strerror(error));
+ VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
return -error;
}
error = set_nonblocking(fd);
if (style == SOCK_STREAM
&& setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
error = errno;
- VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", target, strerror(error));
+ VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s",
+ target, ovs_strerror(error));
goto error;
}
/* Bind. */
if (bind(fd, (struct sockaddr *) &sin, sizeof sin) < 0) {
error = errno;
- VLOG_ERR("%s: bind: %s", target, strerror(error));
+ VLOG_ERR("%s: bind: %s", target, ovs_strerror(error));
goto error;
}
* connect(), the handshake SYN frames will be sent with a TOS of 0. */
error = set_dscp(fd, dscp);
if (error) {
- VLOG_ERR("%s: socket: %s", target, strerror(error));
+ VLOG_ERR("%s: socket: %s", target, ovs_strerror(error));
goto error;
}
/* Listen. */
if (style == SOCK_STREAM && listen(fd, 10) < 0) {
error = errno;
- VLOG_ERR("%s: listen: %s", target, strerror(error));
+ VLOG_ERR("%s: listen: %s", target, ovs_strerror(error));
goto error;
}
socklen_t sin_len = sizeof sin;
if (getsockname(fd, (struct sockaddr *) &sin, &sin_len) < 0) {
error = errno;
- VLOG_ERR("%s: getsockname: %s", target, strerror(error));
+ VLOG_ERR("%s: getsockname: %s", target, ovs_strerror(error));
goto error;
}
if (sin.sin_family != AF_INET || sin_len != sizeof sin) {
int
get_null_fd(void)
{
- static int null_fd = -1;
- if (null_fd < 0) {
+ static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+ static int null_fd;
+
+ if (ovsthread_once_start(&once)) {
null_fd = open("/dev/null", O_RDWR);
if (null_fd < 0) {
int error = errno;
- VLOG_ERR("could not open /dev/null: %s", strerror(error));
- return -error;
+ VLOG_ERR("could not open /dev/null: %s", ovs_strerror(error));
+ null_fd = -error;
}
+ ovsthread_once_done(&once);
}
+
return null_fd;
}
* really an error. */
} else {
error = errno;
- VLOG_ERR("%s: fsync failed (%s)", dir, strerror(error));
+ VLOG_ERR("%s: fsync failed (%s)", dir, ovs_strerror(error));
}
}
close(fd);
} else {
error = errno;
- VLOG_ERR("%s: open failed (%s)", dir, strerror(error));
+ VLOG_ERR("%s: open failed (%s)", dir, ovs_strerror(error));
}
free(dir);
xpipe(int fds[2])
{
if (pipe(fds)) {
- VLOG_FATAL("failed to create pipe (%s)", strerror(errno));
+ VLOG_FATAL("failed to create pipe (%s)", ovs_strerror(errno));
}
}
xsocketpair(int domain, int type, int protocol, int fds[2])
{
if (socketpair(domain, type, protocol, fds)) {
- VLOG_FATAL("failed to create socketpair (%s)", strerror(errno));
+ VLOG_FATAL("failed to create socketpair (%s)", ovs_strerror(errno));
}
}
len = sizeof value;
if (getsockopt(fd, level, option, &value, &len)) {
error = errno;
- VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, strerror(error));
+ VLOG_ERR_RL(&rl, "getsockopt(%s): %s", optname, ovs_strerror(error));
} else if (len != sizeof value) {
error = EINVAL;
VLOG_ERR_RL(&rl, "getsockopt(%s): value is %u bytes (expected %zu)",
ds_init(&string);
if (fstat(fd, &s)) {
- ds_put_format(&string, "fstat failed (%s)", strerror(errno));
+ ds_put_format(&string, "fstat failed (%s)", ovs_strerror(errno));
} else if (S_ISSOCK(s.st_mode)) {
describe_sockaddr(&string, fd, getsockname);
ds_put_cstr(&string, "<->");
goto error;
} else {
size_t n_fds = (p->cmsg_len - CMSG_LEN(0)) / sizeof *fds;
- const int *fds_data = (const int *) CMSG_DATA(p);
+ const int *fds_data = ALIGNED_CAST(const int *, CMSG_DATA(p));
ovs_assert(n_fds > 0);
if (n_fds > SOUTIL_MAX_FDS) {
*n_fdsp = 0;
return EPROTO;
}
+
+/* Calls ioctl() on an AF_INET sock, passing the specified 'command' and
+ * 'arg'. Returns 0 if successful, otherwise a positive errno value. */
+int
+af_inet_ioctl(unsigned long int command, const void *arg)
+{
+ static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+ static int sock;
+
+ if (ovsthread_once_start(&once)) {
+ sock = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sock < 0) {
+ sock = -errno;
+ VLOG_ERR("failed to create inet socket: %s", ovs_strerror(errno));
+ }
+ ovsthread_once_done(&once);
+ }
+
+ return (sock < 0 ? -sock
+ : ioctl(sock, command, arg) == -1 ? errno
+ : 0);
+}
+
+int
+af_inet_ifreq_ioctl(const char *name, struct ifreq *ifr, unsigned long int cmd,
+ const char *cmd_name)
+{
+ int error;
+
+ ovs_strzcpy(ifr->ifr_name, name, sizeof ifr->ifr_name);
+ error = af_inet_ioctl(cmd, ifr);
+ if (error) {
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
+ VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
+ ovs_strerror(error));
+ }
+ return error;
+}
+