return 0;
}
+/* Translates 'host_name', which must be a string representation of an IPv6
+ * address, into a numeric IPv6 address in '*addr'. Returns 0 if successful,
+ * otherwise a positive errno value. */
+int
+lookup_ipv6(const char *host_name, struct in6_addr *addr)
+{
+ if (inet_pton(AF_INET6, host_name, addr) != 1) {
+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
+ VLOG_ERR_RL(&rl, "\"%s\" is not a valid IPv6 address", host_name);
+ return ENOENT;
+ }
+ return 0;
+}
+
/* Returns the error condition associated with socket 'fd' and resets the
* socket's error status. */
int
make_sockaddr_un__(const char *name, struct sockaddr_un *un, socklen_t *un_len)
{
un->sun_family = AF_UNIX;
- strncpy(un->sun_path, name, sizeof un->sun_path);
- un->sun_path[sizeof un->sun_path - 1] = '\0';
+ ovs_strzcpy(un->sun_path, name, sizeof un->sun_path);
*un_len = (offsetof(struct sockaddr_un, sun_path)
+ strlen (un->sun_path) + 1);
}
dirfd = open(dir, O_DIRECTORY | O_RDONLY);
if (dirfd < 0) {
+ free(base);
+ free(dir);
return errno;
}
}
}
+/* Binds Unix domain socket 'fd' to a file with permissions 0700. */
+static int
+bind_unix_socket(int fd, struct sockaddr *sun, socklen_t sun_len)
+{
+#ifdef __linux__
+ /* On Linux, calling fchmod() *before* bind() sets permissions for the file
+ * about to be created. Calling fchmod() *after* bind has no effect on the
+ * file that was created.) */
+ return fchmod(fd, 0700) || bind(fd, sun, sun_len) ? errno : 0;
+#else
+ /* According to _Unix Network Programming_, umask should affect bind(). */
+ mode_t old_umask = umask(0077);
+ int error = bind(fd, sun, sun_len) ? errno : 0;
+ umask(old_umask);
+ return error;
+#endif
+}
+
/* Creates a Unix domain socket in the given 'style' (either SOCK_DGRAM or
* SOCK_STREAM) that is bound to '*bind_path' (if 'bind_path' is non-null) and
* connected to '*connect_path' (if 'connect_path' is non-null). If 'nonblock'
fatal_signal_add_file_to_unlink(bind_path);
error = make_sockaddr_un(bind_path, &un, &un_len, &dirfd);
- if (!error && (bind(fd, (struct sockaddr*) &un, un_len)
- || fchmod(fd, S_IRWXU))) {
- error = errno;
+ if (!error) {
+ error = bind_unix_socket(fd, (struct sockaddr *) &un, un_len);
}
if (dirfd >= 0) {
close(dirfd);
}
}
+void
+xpipe(int fds[2])
+{
+ if (pipe(fds)) {
+ VLOG_FATAL("failed to create pipe (%s)", strerror(errno));
+ }
+}