networkd.c revision b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen/***
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen This file is part of systemd.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Copyright 2013 Tom Gundersen <teg@jklm.no>
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is free software; you can redistribute it and/or modify it
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen under the terms of the GNU Lesser General Public License as published by
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen (at your option) any later version.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen systemd is distributed in the hope that it will be useful, but
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen Lesser General Public License for more details.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen You should have received a copy of the GNU Lesser General Public License
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen***/
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
a553fd32d14739b746a05ae502b8acc38e5f9a50Tom Gundersen#include "sd-daemon.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "capability.h"
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen#include "networkd.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "signal-util.h"
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering#include "user-util.h"
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersenint main(int argc, char *argv[]) {
e877666c1553a3dd5b05b4c48090f889ffc079e6Patrik Flykt _cleanup_manager_free_ Manager *m = NULL;
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering const char *user = "systemd-network";
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering uid_t uid;
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering gid_t gid;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen int r;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen log_set_target(LOG_TARGET_AUTO);
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen log_parse_environment();
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen log_open();
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen umask(0022);
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen if (argc != 1) {
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen log_error("This program takes no arguments.");
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen r = -EINVAL;
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen goto out;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen }
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering r = get_user_creds(&user, &uid, &gid, NULL, NULL);
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Cannot resolve user name %s: %m", user);
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering goto out;
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering }
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen /* Always create the directories people can create inotify
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen * watches in. */
85b5673b337048fa881a5afb1d00d1a7b95950fbTom Gundersen r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid);
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen if (r < 0)
8bde1d0c9d322f3b9a8dd24cfc8417b9231314d8Zbigniew Jędrzejewski-Szmek log_warning_errno(r, "Could not create runtime directory: %m");
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen
85b5673b337048fa881a5afb1d00d1a7b95950fbTom Gundersen r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid);
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen if (r < 0)
8bde1d0c9d322f3b9a8dd24cfc8417b9231314d8Zbigniew Jędrzejewski-Szmek log_warning_errno(r, "Could not create runtime directory 'links': %m");
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen
85b5673b337048fa881a5afb1d00d1a7b95950fbTom Gundersen r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid);
0ea51a1129b9984a3c6d96cef1b0e33c99b5e9cfTom Gundersen if (r < 0)
8bde1d0c9d322f3b9a8dd24cfc8417b9231314d8Zbigniew Jędrzejewski-Szmek log_warning_errno(r, "Could not create runtime directory 'leases': %m");
fe8db0c5ee3365a2fc80ee7ebffa238f9a0a2ae2Tom Gundersen
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid);
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani if (r < 0)
8bde1d0c9d322f3b9a8dd24cfc8417b9231314d8Zbigniew Jędrzejewski-Szmek log_warning_errno(r, "Could not create runtime directory 'lldp': %m");
49699bac94d24b444274f91f85c82e6fad04d029Susant Sahani
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering r = drop_privileges(uid, gid,
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering (1ULL << CAP_NET_ADMIN) |
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering (1ULL << CAP_NET_BIND_SERVICE) |
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering (1ULL << CAP_NET_BROADCAST) |
bddfc8afd329ac68a23f66a3512d4e249af25191Tom Gundersen (1ULL << CAP_NET_RAW));
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering if (r < 0)
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering goto out;
d3cf48f4bd3d69a276f17aa7c910e0b35215cabaLennart Poettering
72c0a2c255b172ebbb2a2b7dab7c9aec4c9582d9Lennart Poettering assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
186fe1db20902b6542cee58ba499ced914d687a8Lennart Poettering
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen r = manager_new(&m);
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Could not create manager: %m");
8f203c166ecb24f904a068f2d8f0c636c1511a8fTom Gundersen goto out;
8f203c166ecb24f904a068f2d8f0c636c1511a8fTom Gundersen }
8f203c166ecb24f904a068f2d8f0c636c1511a8fTom Gundersen
8f203c166ecb24f904a068f2d8f0c636c1511a8fTom Gundersen r = manager_connect_bus(m);
8f203c166ecb24f904a068f2d8f0c636c1511a8fTom Gundersen if (r < 0) {
8f203c166ecb24f904a068f2d8f0c636c1511a8fTom Gundersen log_error_errno(r, "Could not connect to bus: %m");
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen goto out;
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen }
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
f0c4cd7a2c2835a392b5b1807d8c506ab0af44deTom Gundersen r = manager_load_config(m);
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Could not load configuration files: %m");
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen goto out;
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen }
3bef724f7e7f7eaca69881548b06e221b77d7031Tom Gundersen
505f8da7325591defe5f751f328bd26915267602Tom Gundersen r = manager_rtnl_enumerate_links(m);
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Could not enumerate links: %m");
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen goto out;
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen }
1346b1f0388f4100bb3c2a2bb23bc881769c020cTom Gundersen
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen r = manager_rtnl_enumerate_addresses(m);
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen if (r < 0) {
2ee5363b7409b9eaace61e17d8abbc44d5b36055rinrinne log_error_errno(r, "Could not enumerate addresses: %m");
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen goto out;
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen }
45af44d47da6933b260c734ad9ff721f63f80a4dTom Gundersen
73432d67b590c8c8954cf2f8954d174a55d58c7bTom Gundersen log_info("Enumeration completed");
73432d67b590c8c8954cf2f8954d174a55d58c7bTom Gundersen
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen sd_notify(false,
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen "READY=1\n"
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen "STATUS=Processing requests...");
f882c247ad59776c3a7753bb963c1f8e2386cb79Tom Gundersen
a97dcc12e486ecff531809802930a26c4da827f2Tom Gundersen r = manager_run(m);
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Event loop failed: %m");
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen goto out;
1f6d9bc9ee894d4023b7eacea1754986f25bce57Tom Gundersen }
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersenout:
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen sd_notify(false,
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering "STOPPING=1\n"
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen "STATUS=Shutting down...");
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen
4b6141c42497dc199618f95fa3f359615eecfaa0Tom Gundersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
f579559b3a14c1f1ef96c372e7626c4733e6ef7dTom Gundersen}