aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-10-19 17:40:56 +0200
committerMax Kellermann <max@duempel.org>2013-10-19 17:40:56 +0200
commit3274bb54ad673cfd9d9ab6d6d86e5cebaadf89f2 (patch)
treeaa20f6db3c400e2bc6b185cd1be92c6cee15780d
parent7db124068f60f8f8208c9f5601407202381e2ecb (diff)
downloadmpd-3274bb54ad673cfd9d9ab6d6d86e5cebaadf89f2.tar.gz
mpd-3274bb54ad673cfd9d9ab6d6d86e5cebaadf89f2.tar.xz
mpd-3274bb54ad673cfd9d9ab6d6d86e5cebaadf89f2.zip
event/ServerSocket: pass AllocatedPath to AddPath()
-rw-r--r--src/Listen.cxx5
-rw-r--r--src/event/ServerSocket.cxx29
-rw-r--r--src/event/ServerSocket.hxx3
3 files changed, 21 insertions, 16 deletions
diff --git a/src/Listen.cxx b/src/Listen.cxx
index 1365cae3f..a953c80b4 100644
--- a/src/Listen.cxx
+++ b/src/Listen.cxx
@@ -67,8 +67,9 @@ listen_add_config_param(unsigned int port,
if (0 == strcmp(param->value.c_str(), "any")) {
return listen_socket->AddPort(port, error_r);
} else if (param->value[0] == '/' || param->value[0] == '~') {
- const auto path = config_parse_path(param, error_r);
- return !path.IsNull() && listen_socket->AddPath(path.c_str(), error_r);
+ auto path = config_parse_path(param, error_r);
+ return !path.IsNull() &&
+ listen_socket->AddPath(std::move(path), error_r);
} else {
return listen_socket->AddHost(param->value.c_str(), port,
error_r);
diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx
index c3dbbad3d..081362882 100644
--- a/src/event/ServerSocket.cxx
+++ b/src/event/ServerSocket.cxx
@@ -29,12 +29,16 @@
#include "event/SocketMonitor.hxx"
#include "system/Resolver.hxx"
#include "system/fd_util.h"
+#include "fs/AllocatedPath.hxx"
+#include "fs/FileSystem.hxx"
#include "util/Error.hxx"
#include "util/Domain.hxx"
#include "Log.hxx"
#include <glib.h>
+#include <string>
+
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -60,7 +64,7 @@ class OneServerSocket final : private SocketMonitor {
const unsigned serial;
- char *path;
+ AllocatedPath path;
size_t address_length;
struct sockaddr *address;
@@ -72,7 +76,7 @@ public:
size_t _address_length)
:SocketMonitor(_loop),
parent(_parent), serial(_serial),
- path(nullptr),
+ path(AllocatedPath::Null()),
address_length(_address_length),
address((sockaddr *)g_memdup(_address, _address_length))
{
@@ -84,7 +88,6 @@ public:
OneServerSocket &operator=(const OneServerSocket &other) = delete;
~OneServerSocket() {
- g_free(path);
g_free(address);
}
@@ -92,10 +95,10 @@ public:
return serial;
}
- void SetPath(const char *_path) {
- assert(path == nullptr);
+ void SetPath(AllocatedPath &&_path) {
+ assert(path.IsNull());
- path = g_strdup(_path);
+ path = std::move(_path);
}
bool Open(Error &error);
@@ -203,8 +206,8 @@ OneServerSocket::Open(Error &error)
/* allow everybody to connect */
- if (path != nullptr)
- chmod(path, 0666);
+ if (!path.IsNull())
+ chmod(path.c_str(), 0666);
/* register in the GLib main loop */
@@ -403,25 +406,25 @@ ServerSocket::AddHost(const char *hostname, unsigned port, Error &error)
}
bool
-ServerSocket::AddPath(const char *path, Error &error)
+ServerSocket::AddPath(AllocatedPath &&path, Error &error)
{
#ifdef HAVE_UN
struct sockaddr_un s_un;
- size_t path_length = strlen(path);
+ const size_t path_length = path.length();
if (path_length >= sizeof(s_un.sun_path)) {
error.Set(server_socket_domain,
"UNIX socket path is too long");
return false;
}
- unlink(path);
+ RemoveFile(path);
s_un.sun_family = AF_UNIX;
- memcpy(s_un.sun_path, path, path_length + 1);
+ memcpy(s_un.sun_path, path.c_str(), path_length + 1);
OneServerSocket &s = AddAddress((const sockaddr &)s_un, sizeof(s_un));
- s.SetPath(path);
+ s.SetPath(std::move(path));
return true;
#else /* !HAVE_UN */
diff --git a/src/event/ServerSocket.hxx b/src/event/ServerSocket.hxx
index 9f0745708..a4ca2d96d 100644
--- a/src/event/ServerSocket.hxx
+++ b/src/event/ServerSocket.hxx
@@ -27,6 +27,7 @@
struct sockaddr;
class EventLoop;
class Error;
+class AllocatedPath;
typedef void (*server_socket_callback_t)(int fd,
const struct sockaddr *address,
@@ -100,7 +101,7 @@ public:
* ignore errors
* @return true on success
*/
- bool AddPath(const char *path, Error &error);
+ bool AddPath(AllocatedPath &&path, Error &error);
/**
* Add a socket descriptor that is accepting connections. After this