]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/network/networkd.c
util-lib: split out user/group/uid/gid calls into user-util.[ch]
[thirdparty/systemd.git] / src / network / networkd.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Tom Gundersen <teg@jklm.no>
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "sd-daemon.h"
23
24 #include "capability.h"
25 #include "networkd.h"
26 #include "signal-util.h"
27 #include "user-util.h"
28
29 int main(int argc, char *argv[]) {
30 _cleanup_manager_free_ Manager *m = NULL;
31 const char *user = "systemd-network";
32 uid_t uid;
33 gid_t gid;
34 int r;
35
36 log_set_target(LOG_TARGET_AUTO);
37 log_parse_environment();
38 log_open();
39
40 umask(0022);
41
42 if (argc != 1) {
43 log_error("This program takes no arguments.");
44 r = -EINVAL;
45 goto out;
46 }
47
48 r = get_user_creds(&user, &uid, &gid, NULL, NULL);
49 if (r < 0) {
50 log_error_errno(r, "Cannot resolve user name %s: %m", user);
51 goto out;
52 }
53
54 /* Always create the directories people can create inotify
55 * watches in. */
56 r = mkdir_safe_label("/run/systemd/netif", 0755, uid, gid);
57 if (r < 0)
58 log_warning_errno(r, "Could not create runtime directory: %m");
59
60 r = mkdir_safe_label("/run/systemd/netif/links", 0755, uid, gid);
61 if (r < 0)
62 log_warning_errno(r, "Could not create runtime directory 'links': %m");
63
64 r = mkdir_safe_label("/run/systemd/netif/leases", 0755, uid, gid);
65 if (r < 0)
66 log_warning_errno(r, "Could not create runtime directory 'leases': %m");
67
68 r = mkdir_safe_label("/run/systemd/netif/lldp", 0755, uid, gid);
69 if (r < 0)
70 log_warning_errno(r, "Could not create runtime directory 'lldp': %m");
71
72 r = drop_privileges(uid, gid,
73 (1ULL << CAP_NET_ADMIN) |
74 (1ULL << CAP_NET_BIND_SERVICE) |
75 (1ULL << CAP_NET_BROADCAST) |
76 (1ULL << CAP_NET_RAW));
77 if (r < 0)
78 goto out;
79
80 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
81
82 r = manager_new(&m);
83 if (r < 0) {
84 log_error_errno(r, "Could not create manager: %m");
85 goto out;
86 }
87
88 r = manager_connect_bus(m);
89 if (r < 0) {
90 log_error_errno(r, "Could not connect to bus: %m");
91 goto out;
92 }
93
94 r = manager_load_config(m);
95 if (r < 0) {
96 log_error_errno(r, "Could not load configuration files: %m");
97 goto out;
98 }
99
100 r = manager_rtnl_enumerate_links(m);
101 if (r < 0) {
102 log_error_errno(r, "Could not enumerate links: %m");
103 goto out;
104 }
105
106 r = manager_rtnl_enumerate_addresses(m);
107 if (r < 0) {
108 log_error_errno(r, "Could not enumerate addresses: %m");
109 goto out;
110 }
111
112 log_info("Enumeration completed");
113
114 sd_notify(false,
115 "READY=1\n"
116 "STATUS=Processing requests...");
117
118 r = manager_run(m);
119 if (r < 0) {
120 log_error_errno(r, "Event loop failed: %m");
121 goto out;
122 }
123
124 out:
125 sd_notify(false,
126 "STOPPING=1\n"
127 "STATUS=Shutting down...");
128
129 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
130 }