[systemd-commits] 4 commits - src/libsystemd src/libsystemd-network src/udev

Tom Gundersen tomegun at kemper.freedesktop.org
Wed Aug 13 16:36:52 PDT 2014


 src/libsystemd-network/sd-dhcp-server.c   |   73 +++++++++++++++++++-----------
 src/libsystemd-network/test-dhcp-server.c |    2 
 src/libsystemd/sd-event/sd-event.c        |   15 ++++++
 src/udev/net/link-config.c                |    3 -
 4 files changed, 65 insertions(+), 28 deletions(-)

New commits:
commit eb7040ec50fbfe5aad9eaf305bd442a4a235abaa
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Aug 14 01:35:16 2014 +0200

    udev: link-config - fix crash due to missing hwaddr
    
    Reported by: master.nosferatu at gmail.com

diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
index e2280f1..c2881d6 100644
--- a/src/udev/net/link-config.c
+++ b/src/udev/net/link-config.c
@@ -251,11 +251,12 @@ int link_config_get(link_config_ctx *ctx, struct udev_device *device,
         link_config *link;
 
         LIST_FOREACH(links, link, ctx->links) {
+                const char* attr_value = udev_device_get_sysattr_value(device, "address");
 
                 if (net_match_config(link->match_mac, link->match_path, link->match_driver,
                                      link->match_type, NULL, link->match_host,
                                      link->match_virt, link->match_kernel, link->match_arch,
-                                     ether_aton(udev_device_get_sysattr_value(device, "address")),
+                                     attr_value ? ether_aton(attr_value) : NULL,
                                      udev_device_get_property_value(device, "ID_PATH"),
                                      udev_device_get_driver(udev_device_get_parent(device)),
                                      udev_device_get_property_value(device, "ID_NET_DRIVER"),

commit 2f8557eb3649840d76d9eb7d688a3c5c2922d31c
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Aug 14 01:28:35 2014 +0200

    test: dhcp-server - fix test
    
    Reported by Corey Hammerton on G+

diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c
index a94b62f..3e68f31 100644
--- a/src/libsystemd-network/test-dhcp-server.c
+++ b/src/libsystemd-network/test-dhcp-server.c
@@ -57,7 +57,7 @@ static int test_basic(sd_event *event) {
 
         assert_se(sd_dhcp_server_start(server) == -EUNATCH);
         assert_se(sd_dhcp_server_set_address(server, &address_any, 28) == -EINVAL);
-        assert_se(sd_dhcp_server_set_address(server, &address_any, 38) == -ERANGE);
+        assert_se(sd_dhcp_server_set_address(server, &address_lo, 38) == -ERANGE);
         assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) >= 0);
         assert_se(sd_dhcp_server_set_address(server, &address_lo, 8) == -EBUSY);
 

commit a6f1e036de8f212f33ead7f5387c297afd8be26e
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Aug 14 01:26:55 2014 +0200

    sd-dhcp-server: linebreaks
    
    Don't overflow unnecessarily.

diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c
index c224aa1..ab68322 100644
--- a/src/libsystemd-network/sd-dhcp-server.c
+++ b/src/libsystemd-network/sd-dhcp-server.c
@@ -31,7 +31,8 @@
 
 #define DHCP_DEFAULT_LEASE_TIME         3600 /* one hour */
 
-int sd_dhcp_server_set_lease_pool(sd_dhcp_server *server, struct in_addr *address,
+int sd_dhcp_server_set_lease_pool(sd_dhcp_server *server,
+                                  struct in_addr *address,
                                   size_t size) {
         assert_return(server, -EINVAL);
         assert_return(address, -EINVAL);
@@ -51,7 +52,8 @@ int sd_dhcp_server_set_lease_pool(sd_dhcp_server *server, struct in_addr *addres
         return 0;
 }
 
-int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address, unsigned char prefixlen) {
+int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address,
+                               unsigned char prefixlen) {
         assert_return(server, -EINVAL);
         assert_return(address, -EINVAL);
         assert_return(address->s_addr, -EINVAL);
@@ -78,7 +80,8 @@ sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server) {
         return server;
 }
 
-unsigned long client_id_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
+unsigned long client_id_hash_func(const void *p,
+                                  const uint8_t hash_key[HASH_KEY_SIZE]) {
         uint64_t u;
         const DHCPClientId *id = p;
 
@@ -155,7 +158,8 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
         server->address = htobe32(INADDR_ANY);
         server->netmask = htobe32(INADDR_ANY);
         server->index = ifindex;
-        server->leases_by_client_id = hashmap_new(client_id_hash_func, client_id_compare_func);
+        server->leases_by_client_id = hashmap_new(client_id_hash_func,
+                                                  client_id_compare_func);
 
         *ret = server;
         server = NULL;
@@ -163,7 +167,8 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
         return 0;
 }
 
-int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event, int priority) {
+int sd_dhcp_server_attach_event(sd_dhcp_server *server, sd_event *event,
+                                int priority) {
         int r;
 
         assert_return(server, -EINVAL);
@@ -210,8 +215,8 @@ int sd_dhcp_server_stop(sd_dhcp_server *server) {
         return 0;
 }
 
-static int dhcp_server_send_unicast_raw(sd_dhcp_server *server, DHCPPacket *packet,
-                                        size_t len) {
+static int dhcp_server_send_unicast_raw(sd_dhcp_server *server,
+                                        DHCPPacket *packet, size_t len) {
         union sockaddr_union link = {
                 .ll.sll_family = AF_PACKET,
                 .ll.sll_protocol = htons(ETH_P_IP),
@@ -229,7 +234,8 @@ static int dhcp_server_send_unicast_raw(sd_dhcp_server *server, DHCPPacket *pack
         memcpy(&link.ll.sll_addr, &packet->dhcp.chaddr, ETH_ALEN);
 
         dhcp_packet_append_ip_headers(packet, server->address, DHCP_PORT_SERVER,
-                                      packet->dhcp.yiaddr, DHCP_PORT_CLIENT, len);
+                                      packet->dhcp.yiaddr,
+                                      DHCP_PORT_CLIENT, len);
 
         r = dhcp_network_send_raw_socket(server->fd_raw, &link, packet, len);
         if (r < 0)
@@ -354,17 +360,20 @@ int dhcp_server_send_packet(sd_dhcp_server *server,
                 return dhcp_server_send_udp(server, destination, &packet->dhcp,
                                             sizeof(DHCPMessage) + optoffset);
         else if (requested_broadcast(req) || type == DHCP_NAK)
-                return dhcp_server_send_udp(server, INADDR_BROADCAST, &packet->dhcp,
+                return dhcp_server_send_udp(server, INADDR_BROADCAST,
+                                            &packet->dhcp,
                                             sizeof(DHCPMessage) + optoffset);
         else
-                /* we cannot send UDP packet to specific MAC address when the address is
-                   not yet configured, so must fall back to raw packets */
+                /* we cannot send UDP packet to specific MAC address when the
+                   address is not yet configured, so must fall back to raw
+                   packets */
                 return dhcp_server_send_unicast_raw(server, packet,
                                                     sizeof(DHCPPacket) + optoffset);
 }
 
 static int server_message_init(sd_dhcp_server *server, DHCPPacket **ret,
-                               uint8_t type, size_t *_optoffset, DHCPRequest *req) {
+                               uint8_t type, size_t *_optoffset,
+                               DHCPRequest *req) {
         _cleanup_free_ DHCPPacket *packet = NULL;
         size_t optoffset = 0;
         int r;
@@ -378,8 +387,9 @@ static int server_message_init(sd_dhcp_server *server, DHCPPacket **ret,
         if (!packet)
                 return -ENOMEM;
 
-        r = dhcp_message_init(&packet->dhcp, BOOTREPLY, be32toh(req->message->xid),
-                              type, req->max_optlen, &optoffset);
+        r = dhcp_message_init(&packet->dhcp, BOOTREPLY,
+                              be32toh(req->message->xid), type, req->max_optlen,
+                              &optoffset);
         if (r < 0)
                 return r;
 
@@ -394,7 +404,8 @@ static int server_message_init(sd_dhcp_server *server, DHCPPacket **ret,
         return 0;
 }
 
-static int server_send_offer(sd_dhcp_server *server, DHCPRequest *req, be32_t address) {
+static int server_send_offer(sd_dhcp_server *server, DHCPRequest *req,
+                             be32_t address) {
         _cleanup_free_ DHCPPacket *packet = NULL;
         size_t offset;
         be32_t lease_time;
@@ -408,7 +419,8 @@ static int server_send_offer(sd_dhcp_server *server, DHCPRequest *req, be32_t ad
 
         lease_time = htobe32(req->lifetime);
         r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
-                               DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4, &lease_time);
+                               DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4,
+                               &lease_time);
         if (r < 0)
                 return r;
 
@@ -429,7 +441,8 @@ static int server_send_offer(sd_dhcp_server *server, DHCPRequest *req, be32_t ad
         return 0;
 }
 
-static int server_send_ack(sd_dhcp_server *server, DHCPRequest *req, be32_t address) {
+static int server_send_ack(sd_dhcp_server *server, DHCPRequest *req,
+                           be32_t address) {
         _cleanup_free_ DHCPPacket *packet = NULL;
         size_t offset;
         be32_t lease_time;
@@ -443,7 +456,8 @@ static int server_send_ack(sd_dhcp_server *server, DHCPRequest *req, be32_t addr
 
         lease_time = htobe32(req->lifetime);
         r = dhcp_option_append(&packet->dhcp, req->max_optlen, &offset, 0,
-                               DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4, &lease_time);
+                               DHCP_OPTION_IP_ADDRESS_LEASE_TIME, 4,
+                               &lease_time);
         if (r < 0)
                 return r;
 
@@ -480,8 +494,8 @@ static int server_send_nak(sd_dhcp_server *server, DHCPRequest *req) {
         return 0;
 }
 
-static int server_send_forcerenew(sd_dhcp_server *server, be32_t address, be32_t gateway,
-                                  uint8_t chaddr[]) {
+static int server_send_forcerenew(sd_dhcp_server *server, be32_t address,
+                                  be32_t gateway, uint8_t chaddr[]) {
         _cleanup_free_ DHCPPacket *packet = NULL;
         size_t optoffset = 0;
         int r;
@@ -579,7 +593,8 @@ static int ensure_sane_request(DHCPRequest *req, DHCPMessage *message) {
 
         req->message = message;
 
-        /* set client id based on mac address if client did not send an explicit one */
+        /* set client id based on mac address if client did not send an explicit
+           one */
         if (!req->client_id.data) {
                 uint8_t *data;
 
@@ -643,7 +658,8 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
                 /* this only fails on critical errors */
                 return r;
 
-        existing_lease = hashmap_get(server->leases_by_client_id, &req->client_id);
+        existing_lease = hashmap_get(server->leases_by_client_id,
+                                     &req->client_id);
 
         switch(type) {
         case DHCP_DISCOVER:
@@ -768,12 +784,15 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
                                         return -ENOMEM;
                                 }
                                 lease->client_id.length = req->client_id.length;
-                                memcpy(&lease->chaddr, &req->message->chaddr, ETH_ALEN);
+                                memcpy(&lease->chaddr, &req->message->chaddr,
+                                       ETH_ALEN);
                                 lease->gateway = req->message->giaddr;
                         } else
                                 lease = existing_lease;
 
-                        r = sd_event_now(server->event, clock_boottime_or_monotonic(), &time_now);
+                        r = sd_event_now(server->event,
+                                         clock_boottime_or_monotonic(),
+                                         &time_now);
                         if (r < 0)
                                 time_now = now(clock_boottime_or_monotonic());
                         lease->expiration = req->lifetime * USEC_PER_SEC + time_now;
@@ -793,7 +812,8 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message,
                                                 be32toh(req->message->xid));
 
                                 server->bound_leases[pool_offset] = lease;
-                                hashmap_put(server->leases_by_client_id, &lease->client_id, lease);
+                                hashmap_put(server->leases_by_client_id,
+                                            &lease->client_id, lease);
 
                                 return DHCP_ACK;
                         }
@@ -885,7 +905,8 @@ static int server_receive_message(sd_event_source *s, int fd,
                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct in_pktinfo))) {
                         struct in_pktinfo *info = (struct in_pktinfo*)CMSG_DATA(cmsg);
 
-                        /* TODO figure out if this can be done as a filter on the socket, like for IPv6 */
+                        /* TODO figure out if this can be done as a filter on
+                         * the socket, like for IPv6 */
                         if (server->index != info->ipi_ifindex)
                                 return 0;
 

commit 212bbb1798e5746a669dfba4a1732a4eabe4f2d8
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Aug 14 00:22:27 2014 +0200

    sd-event: do not arm timers unnecessarily
    
    Rather than recalculating the next timeout on every loop, we only do it when something changed.

diff --git a/src/libsystemd/sd-event/sd-event.c b/src/libsystemd/sd-event/sd-event.c
index bf6f01c..d654914 100644
--- a/src/libsystemd/sd-event/sd-event.c
+++ b/src/libsystemd/sd-event/sd-event.c
@@ -131,6 +131,8 @@ struct clock_data {
         Prioq *earliest;
         Prioq *latest;
         usec_t next;
+
+        bool needs_rearm:1;
 };
 
 struct sd_event {
@@ -616,6 +618,7 @@ static void source_disconnect(sd_event_source *s) {
 
                 prioq_remove(d->earliest, s, &s->time.earliest_index);
                 prioq_remove(d->latest, s, &s->time.latest_index);
+                d->needs_rearm = true;
                 break;
         }
 
@@ -715,6 +718,7 @@ static int source_set_pending(sd_event_source *s, bool b) {
 
                 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
                 prioq_reshuffle(d->latest, s, &s->time.latest_index);
+                d->needs_rearm = true;
         }
 
         return 0;
@@ -898,6 +902,8 @@ _public_ int sd_event_add_time(
         if (r < 0)
                 goto fail;
 
+        d->needs_rearm = true;
+
         if (ret)
                 *ret = s;
 
@@ -1408,6 +1414,7 @@ _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
 
                         prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+                        d->needs_rearm = true;
                         break;
                 }
 
@@ -1471,6 +1478,7 @@ _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
 
                         prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+                        d->needs_rearm = true;
                         break;
                 }
 
@@ -1548,6 +1556,7 @@ _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
 
         prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+        d->needs_rearm = true;
 
         return 0;
 }
@@ -1582,6 +1591,7 @@ _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec
         assert(d);
 
         prioq_reshuffle(d->latest, s, &s->time.latest_index);
+        d->needs_rearm = true;
 
         return 0;
 }
@@ -1745,6 +1755,11 @@ static int event_arm_timer(
         assert(e);
         assert(d);
 
+        if (_likely_(!d->needs_rearm))
+                return 0;
+        else
+                d->needs_rearm = false;
+
         a = prioq_peek(d->earliest);
         if (!a || a->enabled == SD_EVENT_OFF) {
 



More information about the systemd-commits mailing list