networkd.c revision da927ba997d68401563b927f92e6e40e021a8e5c
248N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
248N/A
248N/A/***
248N/A This file is part of systemd.
248N/A
248N/A Copyright 2013 Tom Gundersen <teg@jklm.no>
248N/A
248N/A systemd is free software; you can redistribute it and/or modify it
248N/A under the terms of the GNU Lesser General Public License as published by
248N/A the Free Software Foundation; either version 2.1 of the License, or
248N/A (at your option) any later version.
248N/A
248N/A systemd is distributed in the hope that it will be useful, but
248N/A WITHOUT ANY WARRANTY; without even the implied warranty of
248N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
248N/A Lesser General Public License for more details.
248N/A
248N/A You should have received a copy of the GNU Lesser General Public License
248N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
248N/A***/
248N/A
248N/A#include "capability.h"
248N/A#include "sd-event.h"
248N/A#include "sd-daemon.h"
248N/A
248N/A#include "networkd.h"
248N/A
248N/Aint main(int argc, char *argv[]) {
248N/A _cleanup_manager_free_ Manager *m = NULL;
248N/A const char *user = "systemd-network";
248N/A uid_t uid;
248N/A gid_t gid;
248N/A int r;
248N/A
248N/A log_set_target(LOG_TARGET_AUTO);
248N/A log_parse_environment();
248N/A log_open();
248N/A
248N/A umask(0022);
248N/A
248N/A if (argc != 1) {
248N/A log_error("This program takes no arguments.");
248N/A r = -EINVAL;
248N/A goto out;
248N/A }
248N/A
248N/A r = get_user_creds(&user, &uid, &gid, NULL, NULL);
248N/A if (r < 0) {
248N/A log_error_errno(r, "Cannot resolve user name %s: %m", user);
248N/A goto out;
248N/A }
248N/A
248N/A /* Always create the directories people can create inotify
248N/A * watches in. */
248N/A r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid);
248N/A if (r < 0)
248N/A log_error("Could not create runtime directory: %s",
248N/A strerror(-r));
248N/A
248N/A r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid);
248N/A if (r < 0)
248N/A log_error("Could not create runtime directory 'links': %s",
248N/A strerror(-r));
248N/A
248N/A r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid);
248N/A if (r < 0)
248N/A log_error("Could not create runtime directory 'leases': %s",
248N/A strerror(-r));
248N/A
248N/A r = drop_privileges(uid, gid,
248N/A (1ULL << CAP_NET_ADMIN) |
248N/A (1ULL << CAP_NET_BIND_SERVICE) |
248N/A (1ULL << CAP_NET_BROADCAST) |
248N/A (1ULL << CAP_NET_RAW));
248N/A if (r < 0)
248N/A goto out;
248N/A
assert_se(sigprocmask_many(SIG_BLOCK, SIGTERM, SIGINT, -1) == 0);
r = manager_new(&m);
if (r < 0) {
log_error_errno(r, "Could not create manager: %m");
goto out;
}
r = manager_udev_listen(m);
if (r < 0) {
log_error_errno(r, "Could not connect to udev: %m");
goto out;
}
r = manager_rtnl_listen(m);
if (r < 0) {
log_error_errno(r, "Could not connect to rtnl: %m");
goto out;
}
r = manager_bus_listen(m);
if (r < 0) {
log_error_errno(r, "Could not connect to system bus: %m");
goto out;
}
r = manager_load_config(m);
if (r < 0) {
log_error_errno(r, "Could not load configuration files: %m");
goto out;
}
r = manager_rtnl_enumerate_links(m);
if (r < 0) {
log_error_errno(r, "Could not enumerate links: %m");
goto out;
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
r = sd_event_loop(m->event);
if (r < 0) {
log_error_errno(r, "Event loop failed: %m");
goto out;
}
out:
sd_notify(false,
"STOPPING=1\n"
"STATUS=Shutting down...");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}