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")) { if (0 == strcmp(param->value.c_str(), "any")) {
return listen_socket->AddPort(port, error_r); return listen_socket->AddPort(port, error_r);
} else if (param->value[0] == '/' || param->value[0] == '~') { } else if (param->value[0] == '/' || param->value[0] == '~') {
const auto path = config_parse_path(param, error_r); auto path = config_parse_path(param, error_r);
return !path.IsNull() && listen_socket->AddPath(path.c_str(), error_r); return !path.IsNull() &&
listen_socket->AddPath(std::move(path), error_r);
} else { } else {
return listen_socket->AddHost(param->value.c_str(), port, return listen_socket->AddHost(param->value.c_str(), port,
error_r); error_r);

View File

@ -29,12 +29,16 @@
#include "event/SocketMonitor.hxx" #include "event/SocketMonitor.hxx"
#include "system/Resolver.hxx" #include "system/Resolver.hxx"
#include "system/fd_util.h" #include "system/fd_util.h"
#include "fs/AllocatedPath.hxx"
#include "fs/FileSystem.hxx"
#include "util/Error.hxx" #include "util/Error.hxx"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "Log.hxx" #include "Log.hxx"
#include <glib.h> #include <glib.h>
#include <string>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@ -60,7 +64,7 @@ class OneServerSocket final : private SocketMonitor {
const unsigned serial; const unsigned serial;
char *path; AllocatedPath path;
size_t address_length; size_t address_length;
struct sockaddr *address; struct sockaddr *address;
@ -72,7 +76,7 @@ public:
size_t _address_length) size_t _address_length)
:SocketMonitor(_loop), :SocketMonitor(_loop),
parent(_parent), serial(_serial), parent(_parent), serial(_serial),
path(nullptr), path(AllocatedPath::Null()),
address_length(_address_length), address_length(_address_length),
address((sockaddr *)g_memdup(_address, _address_length)) address((sockaddr *)g_memdup(_address, _address_length))
{ {
@ -84,7 +88,6 @@ public:
OneServerSocket &operator=(const OneServerSocket &other) = delete; OneServerSocket &operator=(const OneServerSocket &other) = delete;
~OneServerSocket() { ~OneServerSocket() {
g_free(path);
g_free(address); g_free(address);
} }
@ -92,10 +95,10 @@ public:
return serial; return serial;
} }
void SetPath(const char *_path) { void SetPath(AllocatedPath &&_path) {
assert(path == nullptr); assert(path.IsNull());
path = g_strdup(_path); path = std::move(_path);
} }
bool Open(Error &error); bool Open(Error &error);
@ -203,8 +206,8 @@ OneServerSocket::Open(Error &error)
/* allow everybody to connect */ /* allow everybody to connect */
if (path != nullptr) if (!path.IsNull())
chmod(path, 0666); chmod(path.c_str(), 0666);
/* register in the GLib main loop */ /* register in the GLib main loop */
@ -403,25 +406,25 @@ ServerSocket::AddHost(const char *hostname, unsigned port, Error &error)
} }
bool bool
ServerSocket::AddPath(const char *path, Error &error) ServerSocket::AddPath(AllocatedPath &&path, Error &error)
{ {
#ifdef HAVE_UN #ifdef HAVE_UN
struct sockaddr_un s_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)) { if (path_length >= sizeof(s_un.sun_path)) {
error.Set(server_socket_domain, error.Set(server_socket_domain,
"UNIX socket path is too long"); "UNIX socket path is too long");
return false; return false;
} }
unlink(path); RemoveFile(path);
s_un.sun_family = AF_UNIX; 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)); OneServerSocket &s = AddAddress((const sockaddr &)s_un, sizeof(s_un));
s.SetPath(path); s.SetPath(std::move(path));
return true; return true;
#else /* !HAVE_UN */ #else /* !HAVE_UN */

View File

@ -27,6 +27,7 @@
struct sockaddr; struct sockaddr;
class EventLoop; class EventLoop;
class Error; class Error;
class AllocatedPath;
typedef void (*server_socket_callback_t)(int fd, typedef void (*server_socket_callback_t)(int fd,
const struct sockaddr *address, const struct sockaddr *address,
@ -100,7 +101,7 @@ public:
* ignore errors * ignore errors
* @return true on success * @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 * Add a socket descriptor that is accepting connections. After this