1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Tom Gundersen <teg@jklm.no>
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <sys/socket.h>
#include <poll.h>
#include "macro.h"
#include "util.h"
#include "sd-rtnl.h"
#include "rtnl-internal.h"
static int sd_rtnl_new(sd_rtnl **ret) {
sd_rtnl *rtnl;
assert_return(ret, -EINVAL);
rtnl = new0(sd_rtnl, 1);
if (!rtnl)
return -ENOMEM;
rtnl->n_ref = REFCNT_INIT;
rtnl->fd = -1;
rtnl->sockaddr.nl.nl_family = AF_NETLINK;
rtnl->original_pid = getpid();
*ret = rtnl;
return 0;
}
static bool rtnl_pid_changed(sd_rtnl *rtnl) {
assert(rtnl);
/* We don't support people creating an rtnl connection and
* keeping it around over a fork(). Let's complain. */
return rtnl->original_pid != getpid();
}
int sd_rtnl_open(uint32_t groups, sd_rtnl **ret) {
_cleanup_sd_rtnl_unref_ sd_rtnl *rtnl = NULL;
socklen_t addrlen;
int r;
r = sd_rtnl_new(&rtnl);
if (r < 0)
return r;
rtnl->fd = socket(PF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_ROUTE);
if (rtnl->fd < 0)
return -errno;
rtnl->sockaddr.nl.nl_groups = groups;
addrlen = sizeof(rtnl->sockaddr);
r = bind(rtnl->fd, &rtnl->sockaddr.sa, addrlen);
if (r < 0)
return -errno;
r = getsockname(rtnl->fd, &rtnl->sockaddr.sa, &addrlen);
if (r < 0)
return r;
*ret = rtnl;
rtnl = NULL;
return 0;
}
sd_rtnl *sd_rtnl_ref(sd_rtnl *rtnl) {
if (rtnl)
assert_se(REFCNT_INC(rtnl->n_ref) >= 2);
return rtnl;
}
sd_rtnl *sd_rtnl_unref(sd_rtnl *rtnl) {
if (rtnl && REFCNT_DEC(rtnl->n_ref) <= 0) {
if (rtnl->fd >= 0)
close_nointr_nofail(rtnl->fd);
free(rtnl);
}
return NULL;
}
int sd_rtnl_send_with_reply_and_block(sd_rtnl *nl,
sd_rtnl_message *message,
uint64_t usec,
sd_rtnl_message **ret) {
struct pollfd p[1] = {};
struct timespec left;
usec_t timeout;
int r, serial;
assert_return(nl, -EINVAL);
assert_return(!rtnl_pid_changed(nl), -ECHILD);
assert_return(message, -EINVAL);
r = message_seal(nl, message);
if (r < 0)
return r;
serial = message_get_serial(message);
p[0].fd = nl->fd;
p[0].events = POLLOUT;
if (usec == (uint64_t) -1)
timeout = 0;
else if (usec == 0)
timeout = now(CLOCK_MONOTONIC) + RTNL_DEFAULT_TIMEOUT;
else
timeout = now(CLOCK_MONOTONIC) + usec;
for (;;) {
if (timeout) {
usec_t n;
n = now(CLOCK_MONOTONIC);
if (n >= timeout)
return -ETIMEDOUT;
timespec_store(&left, timeout - n);
}
r = ppoll(p, 1, timeout ? &left : NULL, NULL);
if (r < 0)
return 0;
r = socket_write_message(nl, message);
if (r < 0)
return r;
if (r > 0) {
break;
}
}
p[0].events = POLLIN;
for (;;) {
_cleanup_sd_rtnl_message_unref_ sd_rtnl_message *reply = NULL;
if (timeout) {
usec_t n;
n = now(CLOCK_MONOTONIC);
if (n >= timeout)
return -ETIMEDOUT;
timespec_store(&left, timeout - n);
}
r = ppoll(p, 1, timeout ? &left : NULL, NULL);
if (r < 0)
return r;
r = socket_read_message(nl, &reply);
if (r < 0)
return r;
if (r > 0) {
int received_serial = message_get_serial(reply);
if (received_serial == serial) {
r = message_get_errno(reply);
if (r < 0)
return r;
if (ret) {
*ret = reply;
reply = NULL;
}
break;;
}
}
}
return 0;
}
|