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