event/ServerSocket: pass AllocatedPath to AddPath()

This commit is contained in:
Max Kellermann 2013-10-19 17:40:56 +02:00
parent 7db124068f
commit 3274bb54ad
3 changed files with 21 additions and 16 deletions

View File

@ -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);

View File

@ -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 */

View File

@ -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