/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "alloc-util.h"
#include "bus-internal.h"
#include "bus-kernel.h"
#include "bus-policy.h"
#include "bus-util.h"
#include "busname.h"
#include "dbus-busname.h"
#include "fd-util.h"
#include "formats-util.h"
#include "kdbus.h"
#include "parse-util.h"
#include "process-util.h"
#include "service.h"
#include "signal-util.h"
#include "special.h"
#include "string-table.h"
#include "string-util.h"
};
assert(u);
n->starter_fd = -1;
n->accept_fd = true;
n->activating = true;
}
assert(n);
if (n->control_pid <= 0)
return;
n->control_pid = 0;
}
BusNamePolicy *p;
assert(n);
while ((p = n->policy)) {
free(p);
}
}
assert(n);
}
assert(n);
busname_close_fd(n);
unit_ref_unset(&n->service);
}
int r;
assert(n);
if (n->timer_event_source) {
if (r < 0)
return r;
}
if (usec == USEC_INFINITY)
return 0;
r = sd_event_add_time(
&n->timer_event_source,
usec, 0,
if (r < 0)
return r;
return 0;
}
int r;
assert(n);
if (r < 0)
return r;
r = unit_add_two_dependencies_by_name(UNIT(n), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
if (r < 0)
return r;
}
return unit_add_two_dependencies_by_name(UNIT(n), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
}
int r;
assert(n);
if (!n->name) {
if (r < 0)
return r;
}
if (!u->description) {
r = unit_set_description(u, n->name);
if (r < 0)
return r;
}
if (n->activating) {
if (!UNIT_DEREF(n->service)) {
Unit *x;
r = unit_load_related_unit(u, ".service", &x);
if (r < 0)
return r;
unit_ref_set(&n->service, x);
}
if (r < 0)
return r;
}
if (u->default_dependencies) {
if (r < 0)
return r;
}
return 0;
}
char *e;
assert(n);
return 0;
if (!service_name_is_valid(n->name)) {
return -EINVAL;
}
if (!unit_has_name(UNIT(n), e)) {
return -EINVAL;
}
return 0;
}
int r;
assert(u);
r = unit_load_fragment_and_dropin(u);
if (r < 0)
return r;
if (u->load_state == UNIT_LOADED) {
/* This is a new unit? Then let's add in some extras */
r = busname_add_extras(n);
if (r < 0)
return r;
}
return busname_verify(n);
}
assert(n);
assert(f);
fprintf(f,
"%sBus Name State: %s\n"
"%sResult: %s\n"
"%sName: %s\n"
"%sActivating: %s\n"
"%sAccept FD: %s\n",
if (n->control_pid > 0)
fprintf(f,
prefix, n->control_pid);
}
int r;
assert(n);
if (!n->starter_event_source)
return;
if (r < 0)
}
int r;
assert(n);
if (n->starter_fd < 0)
return 0;
if (n->starter_event_source) {
if (r < 0)
goto fail;
} else {
r = sd_event_add_io(UNIT(n)->manager->event, &n->starter_event_source, n->starter_fd, EPOLLIN, busname_dispatch_io, n);
if (r < 0)
goto fail;
}
return 0;
fail:
return r;
}
const char *mode;
assert(n);
if (n->starter_fd >= 0)
return 0;
if (n->starter_fd < 0)
return 0;
}
assert(n);
}
if (state != BUSNAME_LISTENING)
busname_close_fd(n);
log_unit_debug(UNIT(n), "Changed %s -> %s", busname_state_to_string(old_state), busname_state_to_string(state));
}
int r;
assert(n);
if (n->deserialized_state == n->state)
return 0;
if (n->control_pid > 0 &&
pid_is_unwaited(n->control_pid) &&
if (r < 0)
return r;
if (r < 0)
return r;
}
if (IN_SET(n->deserialized_state, BUSNAME_MAKING, BUSNAME_LISTENING, BUSNAME_REGISTERED, BUSNAME_RUNNING)) {
r = busname_open_fd(n);
if (r < 0)
return r;
}
if (n->deserialized_state == BUSNAME_LISTENING) {
r = busname_watch_fd(n);
if (r < 0)
return r;
}
busname_set_state(n, n->deserialized_state);
return 0;
}
int r;
if (r < 0)
goto fail;
* hence let's fork here. It's messy, but well, what can we
* do? */
if (pid < 0)
return -errno;
if (pid == 0) {
int ret;
r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, n->policy, n->policy_world);
if (r < 0) {
goto fail_child;
}
_exit(0);
log_open();
log_error_errno(r, "Failed to create starter connection at step %s: %m", exit_status_to_string(ret, EXIT_STATUS_SYSTEMD));
}
if (r < 0)
goto fail;
return 0;
fail:
return r;
}
assert(n);
if (f != BUSNAME_SUCCESS)
n->result = f;
}
int r;
assert(n);
if (f != BUSNAME_SUCCESS)
n->result = f;
r = unit_kill_context(UNIT(n),
-1,
n->control_pid,
false);
if (r < 0) {
goto fail;
}
if (r > 0) {
if (r < 0) {
goto fail;
}
busname_set_state(n, state);
} else if (state == BUSNAME_SIGTERM)
else
return;
fail:
}
int r;
assert(n);
if (n->activating) {
r = busname_watch_fd(n);
if (r < 0) {
goto fail;
}
} else
return;
fail:
}
int r;
assert(n);
r = busname_open_fd(n);
if (r < 0)
goto fail;
if (n->policy) {
* names, which we can't do from PID1, hence let's
* fork. */
r = busname_make_starter(n, &n->control_pid);
if (r < 0) {
goto fail;
}
} else {
/* If there is no policy, we can do everything
* directly from PID 1, hence do so. */
r = bus_kernel_make_starter(n->starter_fd, n->name, n->activating, n->accept_fd, NULL, n->policy_world);
if (r < 0) {
goto fail;
}
}
return;
fail:
}
bool pending = false;
Iterator i;
int r;
assert(n);
if (!n->activating)
return;
/* We don't take connections anymore if we are supposed to
* shut down anyway */
if (unit_stop_pending(UNIT(n))) {
/* Flush all queued activation reqeuest by closing and reopening the connection */
return;
}
/* If there's already a start pending don't bother to do
* anything */
if (unit_active_or_pending(other)) {
pending = true;
break;
}
if (!pending) {
if (!UNIT_ISSET(n->service)) {
r = -ENOENT;
goto fail;
}
r = manager_add_job(UNIT(n)->manager, JOB_START, UNIT_DEREF(n->service), JOB_REPLACE, &error, NULL);
if (r < 0)
goto fail;
}
return;
fail:
}
assert(n);
/* We cannot fulfill this request right now, try again later
* please! */
return -EAGAIN;
/* Already on it! */
if (n->state == BUSNAME_MAKING)
return 0;
return -ENOENT;
}
}
n->result = BUSNAME_SUCCESS;
return 1;
}
assert(n);
/* Already on it */
return 0;
/* If there's already something running, we go directly into
* kill mode. */
if (n->state == BUSNAME_MAKING) {
return -EAGAIN;
}
return 1;
}
int r;
assert(n);
assert(f);
if (n->control_pid > 0)
if (r < 0)
return r;
return 0;
}
assert(n);
if (state < 0)
else
n->deserialized_state = state;
if (f < 0)
else if (f != BUSNAME_SUCCESS)
n->result = f;
else
n->control_pid = pid;
int fd;
else {
safe_close(n->starter_fd);
}
} else
return 0;
}
assert(u);
}
assert(u);
}
.flags = KDBUS_RECV_PEEK,
};
};
struct kdbus_item *d;
struct kdbus_msg *k;
void *p = NULL;
int r;
/* Generate a friendly debug log message about which process
* caused triggering of this bus name. This simply peeks the
* metadata of the first queued message and logs it. */
assert(n);
/* Let's shortcut things a bit, if debug logging is turned off
* anyway. */
if (log_get_max_level() < LOG_DEBUG)
return 0;
if (r < 0) {
return 0;
}
/* We map as late as possible, and unmap imemdiately after
* use. On 32bit address space is scarce and we want to be
* able to handle a lot of activator connections at the same
* time, and hence shouldn't keep the mmap()s around for
* longer than necessary. */
if (p == MAP_FAILED) {
goto finish;
}
switch (d->type) {
case KDBUS_ITEM_PIDS:
break;
case KDBUS_ITEM_PID_COMM:
break;
}
}
if (pid > 0)
r = 0;
if (p)
return r;
}
assert(n);
if (n->state != BUSNAME_LISTENING)
return 0;
goto fail;
}
return 0;
fail:
return 0;
}
assert(n);
if (pid != n->control_pid)
return;
n->control_pid = 0;
f = BUSNAME_SUCCESS;
else if (code == CLD_EXITED)
else if (code == CLD_KILLED)
else if (code == CLD_DUMPED)
else
assert_not_reached("Unknown sigchld code");
if (f != BUSNAME_SUCCESS)
n->result = f;
switch (n->state) {
case BUSNAME_MAKING:
if (f == BUSNAME_SUCCESS)
else
busname_enter_signal(n, BUSNAME_SIGTERM, f);
break;
case BUSNAME_SIGTERM:
case BUSNAME_SIGKILL:
busname_enter_dead(n, f);
break;
default:
assert_not_reached("Uh, control process died at wrong time.");
}
/* Notify clients about changed exit status */
}
assert(n);
switch (n->state) {
case BUSNAME_MAKING:
break;
case BUSNAME_SIGTERM:
break;
case BUSNAME_SIGKILL:
break;
default:
assert_not_reached("Timeout at wrong time.");
}
return 0;
}
assert(n);
if (n->state == BUSNAME_FAILED)
n->result = BUSNAME_SUCCESS;
}
assert(n);
return;
if (other->start_limit_hit) {
return;
}
return;
}
}
usec_t t;
int r;
if (!n->timer_event_source)
return 0;
r = sd_event_source_get_time(n->timer_event_source, &t);
if (r < 0)
return r;
if (t == USEC_INFINITY)
return 0;
*timeout = t;
return 1;
}
static bool busname_supported(void) {
if (supported < 0)
return supported;
}
[BUSNAME_SUCCESS] = "success",
[BUSNAME_FAILURE_RESOURCES] = "resources",
[BUSNAME_FAILURE_TIMEOUT] = "timeout",
[BUSNAME_FAILURE_EXIT_CODE] = "exit-code",
[BUSNAME_FAILURE_SIGNAL] = "signal",
[BUSNAME_FAILURE_CORE_DUMP] = "core-dump",
[BUSNAME_FAILURE_SERVICE_START_LIMIT_HIT] = "service-start-limit-hit",
};
.object_size = sizeof(BusName),
.sections =
"Unit\0"
"BusName\0"
"Install\0",
.private_section = "BusName",
.no_alias = true,
.no_instances = true,
.init = busname_init,
.done = busname_done,
.load = busname_load,
.dump = busname_dump,
.start = busname_start,
.stop = busname_stop,
.kill = busname_kill,
.finished_start_job = {
[JOB_DONE] = "Listening on %s.",
[JOB_FAILED] = "Failed to listen on %s.",
},
.finished_stop_job = {
[JOB_DONE] = "Closed %s.",
[JOB_FAILED] = "Failed stopping %s.",
},
},
};