return 0;
}
-static int request_meta(void **connection_cls, int fd, char *hostname) {
+static int request_meta(void **connection_cls, int fd, char *_hostname) {
int r;
assert(connection_cls);
+ /* This takes ownership of the hostname in all cases, including on failure. */
+ _cleanup_free_ char *hostname = TAKE_PTR(_hostname);
+
if (*connection_cls)
return 0; /* already assigned. */
Writer *writer;
r = journal_remote_get_writer(journal_remote_server_global, hostname, &writer);
if (r < 0)
- return log_warning_errno(r, "Failed to get writer for source %s: %m",
- hostname);
+ return log_warning_errno(r, "Failed to get writer for source %s: %m", hostname);
- _cleanup_(source_freep) RemoteSource *source = source_new(fd, true, hostname, writer);
+ _cleanup_(source_freep) RemoteSource *source = source_new(fd, true, TAKE_PTR(hostname), writer);
if (!source)
return log_oom();
assert(hostname);
- r = request_meta(connection_cls, fd, hostname);
+ r = request_meta(connection_cls, fd, TAKE_PTR(hostname));
if (r == -ENOMEM)
return respond_oom(connection);
else if (r < 0)
return mhd_respondf(connection, r, MHD_HTTP_INTERNAL_SERVER_ERROR, "%m");
- hostname = NULL;
return MHD_YES;
}
/**
* Initialize zero-filled source with given values. On success, takes
- * ownership of fd, name, and writer, otherwise does not touch them.
+ * ownership of fd and writer, otherwise does not touch them. Always takes
+ * ownership of name, even on failure.
*/
RemoteSource* source_new(int fd, bool passive_fd, char *name, Writer *writer) {
RemoteSource *source;
assert(fd >= 0);
source = new0(RemoteSource, 1);
- if (!source)
+ if (!source) {
+ free(name);
return NULL;
+ }
source->importer = JOURNAL_IMPORTER_MAKE(fd);
source->importer.passive_fd = passive_fd;
void *userdata);
static int get_source_for_fd(RemoteServer *s,
- int fd, char *name, RemoteSource **source) {
+ int fd, char *_name, RemoteSource **source) {
+ _cleanup_free_ char *name = TAKE_PTR(_name);
Writer *writer;
int r;
- /* This takes ownership of name, but only on success. */
+ /* This takes ownership of the name in all cases, including on failure. */
assert(s);
assert(fd >= 0);
r = journal_remote_get_writer(s, name, &writer);
if (r < 0)
- return log_warning_errno(r, "Failed to get writer for source %s: %m",
- name);
+ return log_warning_errno(r, "Failed to get writer for source %s: %m", name);
if (!s->sources[fd]) {
- s->sources[fd] = source_new(fd, false, name, writer);
+ s->sources[fd] = source_new(fd, false, TAKE_PTR(name), writer);
if (!s->sources[fd]) {
writer_unref(writer);
return log_oom();
return 0;
}
-int journal_remote_add_source(RemoteServer *s, int fd, char *name, bool own_name) {
+int journal_remote_add_source(RemoteServer *s, int fd, char *_name, bool own_name) {
+ _cleanup_free_ char *name = NULL;
RemoteSource *source = NULL;
int r;
- /* This takes ownership of name, even on failure, if own_name is true. */
+ /* This takes ownership of _name, even on failure, if own_name is true. */
assert(s);
assert(fd >= 0);
- assert(name);
+ assert(_name);
- if (!own_name) {
- name = strdup(name);
+ if (own_name)
+ name = TAKE_PTR(_name);
+ else {
+ name = strdup(_name);
if (!name)
return log_oom();
}
- r = get_source_for_fd(s, fd, name, &source);
- if (r < 0) {
- log_error_errno(r, "Failed to create source for fd:%d (%s): %m",
- fd, name);
- free(name);
- return r;
- }
+ /* get_source_for_fd() takes ownership of the name in all cases, so it must not be touched below. */
+ r = get_source_for_fd(s, fd, TAKE_PTR(name), &source);
+ if (r < 0)
+ return log_error_errno(r, "Failed to create source for fd:%d: %m", fd);
r = sd_event_add_io(s->event, &source->event,
fd, EPOLLIN|EPOLLRDHUP|EPOLLPRI,
if (r == 0)
r = sd_event_source_set_enabled(source->buffer_event, SD_EVENT_OFF);
} else if (r == -EPERM) {
- log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd, name);
+ log_debug("Falling back to sd_event_add_defer for fd:%d (%s)", fd, source->importer.name);
r = sd_event_add_defer(s->event, &source->event,
dispatch_blocking_source_event, source);
if (r == 0)
goto error;
}
- r = sd_event_source_set_description(source->event, name);
+ r = sd_event_source_set_description(source->event, source->importer.name);
if (r < 0) {
log_error_errno(r, "Failed to set source name for fd:%d: %m", fd);
goto error;