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)
+{
+ /* 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;
+}
+
/* 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);
: 0);
}
-uint32_t
-guess_netmask(uint32_t ip)
+ovs_be32
+guess_netmask(ovs_be32 ip_)
{
- ip = ntohl(ip);
+ uint32_t ip = ntohl(ip_);
return ((ip >> 31) == 0 ? htonl(0xff000000) /* Class A */
: (ip >> 30) == 2 ? htonl(0xffff0000) /* Class B */
: (ip >> 29) == 6 ? htonl(0xffffff00) /* Class C */
}
}
+void
+xpipe(int fds[2])
+{
+ if (pipe(fds)) {
+ VLOG_FATAL("failed to create pipe (%s)", strerror(errno));
+ }
+}