sd-rtnl-message: store reference to the bus in the message
This mimics the sd-bus api, as we may need it in the future.
This commit is contained in:
@@ -88,6 +88,8 @@ struct sd_rtnl {
|
||||
struct sd_rtnl_message {
|
||||
RefCount n_ref;
|
||||
|
||||
sd_rtnl *rtnl;
|
||||
|
||||
struct nlmsghdr *hdr;
|
||||
size_t container_offsets[RTNL_CONTAINER_DEPTH]; /* offset from hdr to each container's start */
|
||||
unsigned n_containers; /* number of containers */
|
||||
@@ -96,7 +98,7 @@ struct sd_rtnl_message {
|
||||
bool sealed:1;
|
||||
};
|
||||
|
||||
int message_new(sd_rtnl_message **ret, size_t initial_size);
|
||||
int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, size_t initial_size);
|
||||
|
||||
int socket_write_message(sd_rtnl *nl, sd_rtnl_message *m);
|
||||
int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret);
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
#define UPDATE_RTA(m, new) (m)->next_rta_offset = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
|
||||
#define PUSH_CONTAINER(m, new) (m)->container_offsets[(m)->n_containers ++] = (uint8_t*)(new) - (uint8_t*)(m)->hdr;
|
||||
|
||||
int message_new(sd_rtnl_message **ret, size_t initial_size) {
|
||||
int message_new(sd_rtnl *rtnl, sd_rtnl_message **ret, size_t initial_size) {
|
||||
sd_rtnl_message *m;
|
||||
|
||||
assert_return(ret, -EINVAL);
|
||||
@@ -58,6 +58,9 @@ int message_new(sd_rtnl_message **ret, size_t initial_size) {
|
||||
m->hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
|
||||
m->sealed = false;
|
||||
|
||||
if (rtnl)
|
||||
m->rtnl = sd_rtnl_ref(rtnl);
|
||||
|
||||
*ret = m;
|
||||
|
||||
return 0;
|
||||
@@ -81,7 +84,8 @@ int sd_rtnl_message_route_set_dst_prefixlen(sd_rtnl_message *m, unsigned char pr
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
|
||||
int sd_rtnl_message_new_route(sd_rtnl *rtnl, uint16_t nlmsg_type,
|
||||
unsigned char rtm_family,
|
||||
sd_rtnl_message **ret) {
|
||||
struct rtmsg *rtm;
|
||||
int r;
|
||||
@@ -90,7 +94,7 @@ int sd_rtnl_message_new_route(uint16_t nlmsg_type, unsigned char rtm_family,
|
||||
assert_return(rtm_family == AF_INET || rtm_family == AF_INET6, -EINVAL);
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
r = message_new(ret, NLMSG_SPACE(sizeof(struct rtmsg)));
|
||||
r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct rtmsg)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -142,7 +146,8 @@ int sd_rtnl_message_link_set_type(sd_rtnl_message *m, unsigned type) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_rtnl_message_new_link(uint16_t nlmsg_type, int index, sd_rtnl_message **ret) {
|
||||
int sd_rtnl_message_new_link(sd_rtnl *rtnl, uint16_t nlmsg_type, int index,
|
||||
sd_rtnl_message **ret) {
|
||||
struct ifinfomsg *ifi;
|
||||
int r;
|
||||
|
||||
@@ -151,7 +156,7 @@ int sd_rtnl_message_new_link(uint16_t nlmsg_type, int index, sd_rtnl_message **r
|
||||
nlmsg_type == RTM_SETLINK || index > 0, -EINVAL);
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
r = message_new(ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
|
||||
r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -216,7 +221,8 @@ int sd_rtnl_message_addr_set_scope(sd_rtnl_message *m, unsigned char scope) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sd_rtnl_message_new_addr(uint16_t nlmsg_type, int index, unsigned char family,
|
||||
int sd_rtnl_message_new_addr(sd_rtnl *rtnl, uint16_t nlmsg_type, int index,
|
||||
unsigned char family,
|
||||
sd_rtnl_message **ret) {
|
||||
struct ifaddrmsg *ifa;
|
||||
int r;
|
||||
@@ -226,7 +232,7 @@ int sd_rtnl_message_new_addr(uint16_t nlmsg_type, int index, unsigned char famil
|
||||
assert_return(family == AF_INET || family == AF_INET6, -EINVAL);
|
||||
assert_return(ret, -EINVAL);
|
||||
|
||||
r = message_new(ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
|
||||
r = message_new(rtnl, ret, NLMSG_SPACE(sizeof(struct ifaddrmsg)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -258,6 +264,7 @@ sd_rtnl_message *sd_rtnl_message_ref(sd_rtnl_message *m) {
|
||||
|
||||
sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m) {
|
||||
if (m && REFCNT_DEC(m->n_ref) <= 0) {
|
||||
sd_rtnl_unref(m->rtnl);
|
||||
free(m->hdr);
|
||||
free(m);
|
||||
}
|
||||
@@ -897,7 +904,7 @@ int socket_read_message(sd_rtnl *nl, sd_rtnl_message **ret) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = message_new(&m, need);
|
||||
r = message_new(nl, &m, need);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ int rtnl_set_link_name(sd_rtnl *rtnl, int ifindex, const char *name) {
|
||||
assert(ifindex > 0);
|
||||
assert(name);
|
||||
|
||||
r = sd_rtnl_message_new_link(RTM_SETLINK, ifindex, &message);
|
||||
r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, ifindex, &message);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -61,7 +61,7 @@ int rtnl_set_link_properties(sd_rtnl *rtnl, int ifindex, const char *alias,
|
||||
if (!alias && !mac && mtu == 0)
|
||||
return 0;
|
||||
|
||||
r = sd_rtnl_message_new_link(RTM_SETLINK, ifindex, &message);
|
||||
r = sd_rtnl_message_new_link(rtnl, RTM_SETLINK, ifindex, &message);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@@ -105,7 +105,7 @@ int rtnl_message_new_synthetic_error(int error, uint32_t serial, sd_rtnl_message
|
||||
|
||||
assert(error <= 0);
|
||||
|
||||
r = message_new(ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
|
||||
r = message_new(NULL, ret, NLMSG_SPACE(sizeof(struct nlmsgerr)));
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
|
||||
void *data;
|
||||
|
||||
/* we'd really like to test NEWLINK, but let's not mess with the running kernel */
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &message) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &message) >= 0);
|
||||
assert(sd_rtnl_message_append_string(message, IFLA_IFNAME, name) >= 0);
|
||||
assert(sd_rtnl_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
|
||||
assert(sd_rtnl_message_append_u32(message, IFLA_MTU, mtu) >= 0);
|
||||
@@ -65,7 +65,7 @@ static void test_link_get(sd_rtnl *rtnl, int ifindex) {
|
||||
void *data;
|
||||
uint16_t type;
|
||||
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m) >= 0);
|
||||
assert(m);
|
||||
|
||||
/* u8 test cases */
|
||||
@@ -136,7 +136,7 @@ static void test_route(void) {
|
||||
void *data;
|
||||
int r;
|
||||
|
||||
r = sd_rtnl_message_new_route(RTM_NEWROUTE, AF_INET, &req);
|
||||
r = sd_rtnl_message_new_route(NULL, RTM_NEWROUTE, AF_INET, &req);
|
||||
if (r < 0) {
|
||||
log_error("Could not create RTM_NEWROUTE message: %s", strerror(-r));
|
||||
return;
|
||||
@@ -215,7 +215,7 @@ static void test_event_loop(int ifindex) {
|
||||
assert(ifname);
|
||||
|
||||
assert(sd_rtnl_open(0, &rtnl) >= 0);
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m) >= 0);
|
||||
|
||||
assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, NULL) >= 0);
|
||||
|
||||
@@ -249,7 +249,7 @@ static void test_async(int ifindex) {
|
||||
|
||||
assert(sd_rtnl_open(0, &rtnl) >= 0);
|
||||
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m) >= 0);
|
||||
|
||||
assert(sd_rtnl_call_async(rtnl, m, &link_handler, ifname, 0, &serial) >= 0);
|
||||
|
||||
@@ -264,8 +264,8 @@ static void test_pipe(int ifindex) {
|
||||
|
||||
assert(sd_rtnl_open(0, &rtnl) >= 0);
|
||||
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m1) >= 0);
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, ifindex, &m2) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m1) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, ifindex, &m2) >= 0);
|
||||
|
||||
counter ++;
|
||||
assert(sd_rtnl_call_async(rtnl, m1, &pipe_handler, &counter, 0, NULL) >= 0);
|
||||
@@ -284,7 +284,7 @@ static void test_container(void) {
|
||||
uint16_t type;
|
||||
void *data;
|
||||
|
||||
assert(sd_rtnl_message_new_link(RTM_NEWLINK, 0, &m) >= 0);
|
||||
assert(sd_rtnl_message_new_link(NULL, RTM_NEWLINK, 0, &m) >= 0);
|
||||
|
||||
assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
|
||||
assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -ENOTSUP);
|
||||
@@ -364,7 +364,7 @@ int main(void) {
|
||||
|
||||
test_link_configure(rtnl, if_loopback);
|
||||
|
||||
assert(sd_rtnl_message_new_link(RTM_GETLINK, if_loopback, &m) >= 0);
|
||||
assert(sd_rtnl_message_new_link(rtnl, RTM_GETLINK, if_loopback, &m) >= 0);
|
||||
assert(m);
|
||||
|
||||
assert(sd_rtnl_message_get_type(m, &type) >= 0);
|
||||
|
||||
Reference in New Issue
Block a user