]>
git.proxmox.com Git - systemd.git/blob - src/network/networkd.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "capability-util.h"
10 #include "daemon-util.h"
11 #include "main-func.h"
13 #include "networkd-conf.h"
14 #include "networkd-manager.h"
15 #include "signal-util.h"
16 #include "user-util.h"
18 static int run(int argc
, char *argv
[]) {
19 _cleanup_(notify_on_cleanup
) const char *notify_message
= NULL
;
20 _cleanup_(manager_freep
) Manager
*m
= NULL
;
21 const char *user
= "systemd-network";
31 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes no arguments.");
33 r
= get_user_creds(&user
, &uid
, &gid
, NULL
, NULL
, 0);
35 return log_error_errno(r
, "Cannot resolve user name %s: %m", user
);
37 /* Create runtime directory. This is not necessary when networkd is
38 * started with "RuntimeDirectory=systemd/netif", or after
39 * systemd-tmpfiles-setup.service. */
40 r
= mkdir_safe_label("/run/systemd/netif", 0755, uid
, gid
, MKDIR_WARN_MODE
);
42 log_warning_errno(r
, "Could not create runtime directory: %m");
44 /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
45 * privileges are already dropped. */
47 r
= drop_privileges(uid
, gid
,
48 (1ULL << CAP_NET_ADMIN
) |
49 (1ULL << CAP_NET_BIND_SERVICE
) |
50 (1ULL << CAP_NET_BROADCAST
) |
51 (1ULL << CAP_NET_RAW
));
53 return log_error_errno(r
, "Failed to drop privileges: %m");
56 /* Always create the directories people can create inotify watches in.
57 * It is necessary to create the following subdirectories after drop_privileges()
58 * to support old kernels not supporting AmbientCapabilities=. */
59 r
= mkdir_safe_label("/run/systemd/netif/links", 0755, uid
, gid
, MKDIR_WARN_MODE
);
61 log_warning_errno(r
, "Could not create runtime directory 'links': %m");
63 r
= mkdir_safe_label("/run/systemd/netif/leases", 0755, uid
, gid
, MKDIR_WARN_MODE
);
65 log_warning_errno(r
, "Could not create runtime directory 'leases': %m");
67 r
= mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid
, gid
, MKDIR_WARN_MODE
);
69 log_warning_errno(r
, "Could not create runtime directory 'lldp': %m");
71 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGTERM
, SIGINT
, -1) >= 0);
75 return log_error_errno(r
, "Could not create manager: %m");
77 r
= manager_connect_bus(m
);
79 return log_error_errno(r
, "Could not connect to bus: %m");
81 r
= manager_parse_config_file(m
);
83 log_warning_errno(r
, "Failed to parse configuration file: %m");
85 r
= manager_load_config(m
);
87 return log_error_errno(r
, "Could not load configuration files: %m");
89 r
= manager_rtnl_enumerate_links(m
);
91 return log_error_errno(r
, "Could not enumerate links: %m");
93 r
= manager_rtnl_enumerate_addresses(m
);
95 return log_error_errno(r
, "Could not enumerate addresses: %m");
97 r
= manager_rtnl_enumerate_routes(m
);
99 return log_error_errno(r
, "Could not enumerate routes: %m");
101 r
= manager_rtnl_enumerate_rules(m
);
103 return log_error_errno(r
, "Could not enumerate rules: %m");
105 r
= manager_start(m
);
107 return log_error_errno(r
, "Could not start manager: %m");
109 log_info("Enumeration completed");
111 notify_message
= notify_start(NOTIFY_READY
, NOTIFY_STOPPING
);
113 r
= sd_event_loop(m
->event
);
115 return log_error_errno(r
, "Event loop failed: %m");
120 DEFINE_MAIN_FUNCTION(run
);