*
* Returns the socket's fd if successful, otherwise a negative errno value. */
int
-make_unix_socket(int style, bool nonblock, bool passcred UNUSED,
+make_unix_socket(int style, bool nonblock, bool passcred OVS_UNUSED,
const char *bind_path, const char *connect_path)
{
int error;
* For TCP, the socket will have SO_REUSEADDR turned on.
*
* On success, returns a non-negative file descriptor. On failure, returns a
- * negative errno value. */
+ * negative errno value.
+ *
+ * If 'sinp' is non-null, then on success the bound address is stored into
+ * '*sinp'. */
int
-inet_open_passive(int style, const char *target_, int default_port)
+inet_open_passive(int style, const char *target_, int default_port,
+ struct sockaddr_in *sinp)
{
char *target = xstrdup(target_);
char *string_ptr = target;
VLOG_ERR("%s: listen: %s", target_, strerror(error));
goto exit_close;
}
+
+ if (sinp) {
+ 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));
+ goto exit_close;
+ }
+ if (sin.sin_family != AF_INET || sin_len != sizeof sin) {
+ VLOG_ERR("%s: getsockname: invalid socket name", target_);
+ goto exit_close;
+ }
+ *sinp = sin;
+ }
+
error = 0;
goto exit;
}
return 0;
}
+
+/* Given file name 'file_name', fsyncs the directory in which it is contained.
+ * Returns 0 if successful, otherwise a positive errno value. */
+int
+fsync_parent_dir(const char *file_name)
+{
+ int error = 0;
+ char *dir;
+ int fd;
+
+ dir = dir_name(file_name);
+ fd = open(dir, O_RDONLY);
+ if (fd >= 0) {
+ if (fsync(fd)) {
+ if (errno == EINVAL || errno == EROFS) {
+ /* This directory does not support synchronization. Not
+ * really an error. */
+ } else {
+ error = errno;
+ VLOG_ERR("%s: fsync failed (%s)", dir, strerror(error));
+ }
+ }
+ close(fd);
+ } else {
+ error = errno;
+ VLOG_ERR("%s: open failed (%s)", dir, strerror(error));
+ }
+ free(dir);
+
+ return error;
+}