Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include <netinet/in.h>
4 : : #include <sys/stat.h>
5 : : #include <sys/types.h>
6 : :
7 : : #include "sd-daemon.h"
8 : : #include "sd-event.h"
9 : :
10 : : #include "capability-util.h"
11 : : #include "daemon-util.h"
12 : : #include "main-func.h"
13 : : #include "mkdir.h"
14 : : #include "networkd-conf.h"
15 : : #include "networkd-manager.h"
16 : : #include "signal-util.h"
17 : : #include "user-util.h"
18 : :
19 : 0 : static int run(int argc, char *argv[]) {
20 : 0 : _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
21 : 0 : _cleanup_(manager_freep) Manager *m = NULL;
22 : 0 : const char *user = "systemd-network";
23 : : uid_t uid;
24 : : gid_t gid;
25 : : int r;
26 : :
27 : 0 : log_setup_service();
28 : :
29 : 0 : umask(0022);
30 : :
31 [ # # ]: 0 : if (argc != 1)
32 [ # # ]: 0 : return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
33 : :
34 : 0 : r = get_user_creds(&user, &uid, &gid, NULL, NULL, 0);
35 [ # # ]: 0 : if (r < 0)
36 [ # # ]: 0 : return log_error_errno(r, "Cannot resolve user name %s: %m", user);
37 : :
38 : : /* Create runtime directory. This is not necessary when networkd is
39 : : * started with "RuntimeDirectory=systemd/netif", or after
40 : : * systemd-tmpfiles-setup.service. */
41 : 0 : r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid, MKDIR_WARN_MODE);
42 [ # # ]: 0 : if (r < 0)
43 [ # # ]: 0 : log_warning_errno(r, "Could not create runtime directory: %m");
44 : :
45 : : /* Drop privileges, but only if we have been started as root. If we are not running as root we assume all
46 : : * privileges are already dropped. */
47 [ # # ]: 0 : if (geteuid() == 0) {
48 : 0 : r = drop_privileges(uid, gid,
49 : : (1ULL << CAP_NET_ADMIN) |
50 : : (1ULL << CAP_NET_BIND_SERVICE) |
51 : : (1ULL << CAP_NET_BROADCAST) |
52 : : (1ULL << CAP_NET_RAW));
53 [ # # ]: 0 : if (r < 0)
54 [ # # ]: 0 : return log_error_errno(r, "Failed to drop privileges: %m");
55 : : }
56 : :
57 : : /* Always create the directories people can create inotify watches in.
58 : : * It is necessary to create the following subdirectories after drop_privileges()
59 : : * to support old kernels not supporting AmbientCapabilities=. */
60 : 0 : r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid, MKDIR_WARN_MODE);
61 [ # # ]: 0 : if (r < 0)
62 [ # # ]: 0 : log_warning_errno(r, "Could not create runtime directory 'links': %m");
63 : :
64 : 0 : r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid, MKDIR_WARN_MODE);
65 [ # # ]: 0 : if (r < 0)
66 [ # # ]: 0 : log_warning_errno(r, "Could not create runtime directory 'leases': %m");
67 : :
68 : 0 : r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid, MKDIR_WARN_MODE);
69 [ # # ]: 0 : if (r < 0)
70 [ # # ]: 0 : log_warning_errno(r, "Could not create runtime directory 'lldp': %m");
71 : :
72 [ # # ]: 0 : assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
73 : :
74 : 0 : r = manager_new(&m);
75 [ # # ]: 0 : if (r < 0)
76 [ # # ]: 0 : return log_error_errno(r, "Could not create manager: %m");
77 : :
78 : 0 : r = manager_connect_bus(m);
79 [ # # ]: 0 : if (r < 0)
80 [ # # ]: 0 : return log_error_errno(r, "Could not connect to bus: %m");
81 : :
82 : 0 : r = manager_parse_config_file(m);
83 [ # # ]: 0 : if (r < 0)
84 [ # # ]: 0 : log_warning_errno(r, "Failed to parse configuration file: %m");
85 : :
86 : 0 : r = manager_load_config(m);
87 [ # # ]: 0 : if (r < 0)
88 [ # # ]: 0 : return log_error_errno(r, "Could not load configuration files: %m");
89 : :
90 : 0 : r = manager_rtnl_enumerate_links(m);
91 [ # # ]: 0 : if (r < 0)
92 [ # # ]: 0 : return log_error_errno(r, "Could not enumerate links: %m");
93 : :
94 : 0 : r = manager_rtnl_enumerate_addresses(m);
95 [ # # ]: 0 : if (r < 0)
96 [ # # ]: 0 : return log_error_errno(r, "Could not enumerate addresses: %m");
97 : :
98 : 0 : r = manager_rtnl_enumerate_neighbors(m);
99 [ # # ]: 0 : if (r < 0)
100 [ # # ]: 0 : return log_error_errno(r, "Could not enumerate neighbors: %m");
101 : :
102 : 0 : r = manager_rtnl_enumerate_routes(m);
103 [ # # ]: 0 : if (r < 0)
104 [ # # ]: 0 : return log_error_errno(r, "Could not enumerate routes: %m");
105 : :
106 : 0 : r = manager_rtnl_enumerate_rules(m);
107 [ # # ]: 0 : if (r < 0)
108 [ # # ]: 0 : return log_error_errno(r, "Could not enumerate rules: %m");
109 : :
110 : 0 : r = manager_start(m);
111 [ # # ]: 0 : if (r < 0)
112 [ # # ]: 0 : return log_error_errno(r, "Could not start manager: %m");
113 : :
114 [ # # ]: 0 : log_info("Enumeration completed");
115 : :
116 : 0 : notify_message = notify_start(NOTIFY_READY, NOTIFY_STOPPING);
117 : :
118 : 0 : r = sd_event_loop(m->event);
119 [ # # ]: 0 : if (r < 0)
120 [ # # ]: 0 : return log_error_errno(r, "Event loop failed: %m");
121 : :
122 : 0 : return 0;
123 : : }
124 : :
125 : 0 : DEFINE_MAIN_FUNCTION(run);
|