diff --git a/src/backends/NetworkManagerGeneric.c b/src/backends/NetworkManagerGeneric.c index 2ef8583c0..7491f52a5 100644 --- a/src/backends/NetworkManagerGeneric.c +++ b/src/backends/NetworkManagerGeneric.c @@ -94,7 +94,8 @@ void nm_generic_enable_loopback (void) if ((err = rtnl_addr_add (nlh, addr, 0)) < 0) { if (err != -EEXIST) { - nm_log_warn (LOGD_CORE, "error %d returned from rtnl_addr_add():\n%s", err, nl_geterror(err)); + nm_log_warn (LOGD_CORE, "error setting loopback address: (%d) %s", + err, nl_geterror (err)); } } out: diff --git a/src/ip6-manager/nm-ip6-manager.c b/src/ip6-manager/nm-ip6-manager.c index 60045234c..fb9e77ba3 100644 --- a/src/ip6-manager/nm-ip6-manager.c +++ b/src/ip6-manager/nm-ip6-manager.c @@ -1346,8 +1346,10 @@ nm_ip6_manager_init (NMIP6Manager *manager) G_CALLBACK (netlink_notification), manager); priv->nlh = nm_netlink_get_default_handle (); - rtnl_addr_alloc_cache(priv->nlh, &priv->addr_cache); + rtnl_addr_alloc_cache (priv->nlh, &priv->addr_cache); + g_warn_if_fail (priv->addr_cache != NULL); rtnl_route_alloc_cache (priv->nlh, NETLINK_ROUTE, NL_AUTO_PROVIDE, &priv->route_cache); + g_warn_if_fail (priv->route_cache != NULL); } static void diff --git a/src/nm-netlink-compat.c b/src/nm-netlink-compat.c index 38bbf88dd..ca2fff337 100644 --- a/src/nm-netlink-compat.c +++ b/src/nm-netlink-compat.c @@ -24,33 +24,40 @@ #include "nm-netlink-compat.h" #ifndef HAVE_LIBNL1 -struct rtnl_nexthop * nm_netlink_get_nh(struct rtnl_route * route) { +struct rtnl_nexthop * +nm_netlink_get_nh (struct rtnl_route * route) +{ int hops; hops = rtnl_route_get_nnexthops (route); g_return_val_if_fail(hops > 0, NULL); - return rtnl_route_nexthop_n (route, 0); } -int rtnl_route_get_oif(struct rtnl_route * route) { +int +rtnl_route_get_oif (struct rtnl_route * route) +{ struct rtnl_nexthop * nh; + nh = nm_netlink_get_nh(route); g_return_val_if_fail(nh, -NLE_OBJ_NOTFOUND); return rtnl_route_nh_get_ifindex (nh); } -int rtnl_route_set_oif(struct rtnl_route * route, int ifindex) { +int +rtnl_route_set_oif (struct rtnl_route * route, int ifindex) +{ struct rtnl_nexthop * nh; nh = rtnl_route_nh_alloc(); rtnl_route_nh_set_ifindex(nh, ifindex); rtnl_route_add_nexthop(route, nh); - return 0; } -struct nl_addr * rtnl_route_get_gateway(struct rtnl_route * route) { +struct nl_addr * +rtnl_route_get_gateway (struct rtnl_route * route) +{ struct rtnl_nexthop * nh; nh = nm_netlink_get_nh(route); @@ -58,7 +65,9 @@ struct nl_addr * rtnl_route_get_gateway(struct rtnl_route * route) { return rtnl_route_nh_get_gateway(nh); } -int rtnl_route_set_gateway(struct rtnl_route * route, struct nl_addr * gw_addr) { +int +rtnl_route_set_gateway (struct rtnl_route * route, struct nl_addr * gw_addr) +{ struct rtnl_nexthop * nh; nh = nm_netlink_get_nh(route); @@ -68,7 +77,9 @@ int rtnl_route_set_gateway(struct rtnl_route * route, struct nl_addr * gw_addr) return 0; } -int rtnl_route_get_dst_len(struct rtnl_route * rtnlroute) { +int +rtnl_route_get_dst_len(struct rtnl_route * rtnlroute) +{ struct nl_addr * dst; dst = rtnl_route_get_dst(rtnlroute); @@ -77,14 +88,16 @@ int rtnl_route_get_dst_len(struct rtnl_route * rtnlroute) { #endif #ifdef HAVE_LIBNL1 -int nl_compat_error(int err) { - err = abs(err); +int +nl_compat_error (int err) +{ + err = abs (err); - if(err==EEXIST) + if (err == EEXIST) err = NLE_EXIST; - else if(err==ENOENT) + else if (err == ENOENT) err = NLE_OBJ_NOTFOUND; - else if(err==ERANGE) + else if (err == ERANGE) err = NLE_RANGE; return -err; diff --git a/src/nm-netlink-compat.h b/src/nm-netlink-compat.h index 867c9f4ee..f4e3d466c 100644 --- a/src/nm-netlink-compat.h +++ b/src/nm-netlink-compat.h @@ -17,6 +17,7 @@ * * Copyright (C) 2011 Caixa Magica Software. */ + #ifndef NM_NETLINK_COMPAT_H #define NM_NETLINK_COMPAT_H @@ -53,7 +54,9 @@ struct nl_addr * rtnl_route_get_gateway(struct rtnl_route *); /* libnl-2 API compatibility for libnl-3 */ #ifdef HAVE_LIBNL3 -static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { +static inline int +__rtnl_link_alloc_cache (struct nl_sock *h, struct nl_cache **cache) +{ return rtnl_link_alloc_cache (h, AF_UNSPEC, cache); } #define rtnl_link_alloc_cache __rtnl_link_alloc_cache @@ -73,64 +76,82 @@ static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **c #define rtnl_route_set_priority rtnl_route_set_prio /* auxiliary functions */ -int nl_compat_error(int); +int nl_compat_error (int); /* libnl-1.0 functions with modified prototypes in libnl-2/3*/ -static inline const char * __nl_geterror(int err) +static inline const char * +__nl_geterror (int err) { /* err is set, can be parsed */ - return nl_geterror(); + return nl_geterror (); } #define nl_geterror __nl_geterror -static inline int __rtnl_addr_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { - *cache = rtnl_addr_alloc_cache(h); - if(!*cache) - return -ENOMEM; - return 0; +static inline int +__rtnl_addr_alloc_cache (struct nl_sock *h, struct nl_cache **cache) +{ + g_return_val_if_fail (cache != NULL, -EINVAL); + + *cache = rtnl_addr_alloc_cache (h); + return *cache ? 0 : -ENOMEM; } #define rtnl_addr_alloc_cache __rtnl_addr_alloc_cache -static inline int __rtnl_route_alloc_cache(struct nl_sock *h, int family, int flags, struct nl_cache **cache) { - *cache = rtnl_route_alloc_cache(h); - if(!*cache) - return -ENOMEM; - return 0; +static inline int +__rtnl_route_alloc_cache (struct nl_sock *h, int family, int flags, struct nl_cache **cache) +{ + g_return_val_if_fail (cache != NULL, -EINVAL); + + *cache = rtnl_route_alloc_cache (h); + return *cache ? 0 : -ENOMEM; } #define rtnl_route_alloc_cache __rtnl_route_alloc_cache -static inline int __rtnl_link_alloc_cache(struct nl_sock *h, struct nl_cache **cache) { - *cache = rtnl_link_alloc_cache (h); - if(!*cache) - return -ENOMEM; - return 0; +static inline int +__rtnl_link_alloc_cache (struct nl_sock *h, struct nl_cache **cache) +{ + g_return_val_if_fail (cache != NULL, -EINVAL); + + *cache = rtnl_link_alloc_cache (h); + return *cache ? 0 : -ENOMEM; } #define rtnl_link_alloc_cache __rtnl_link_alloc_cache -static inline int __rtnl_route_get_metric(struct rtnl_route * route, int metric, unsigned int *value) { - *value = rtnl_route_get_metric(route, metric); +static inline int +__rtnl_route_get_metric (struct rtnl_route *route, int metric, unsigned int *value) +{ + g_return_val_if_fail (value != NULL, -EINVAL); + + *value = rtnl_route_get_metric (route, metric); return 0; } #define rtnl_route_get_metric __rtnl_route_get_metric -static inline int __rtnl_addr_add(struct nl_sock * h, struct rtnl_addr * addr, int flags) { - return nl_compat_error(rtnl_addr_add(h,addr,flags)); +static inline int +__rtnl_addr_add (struct nl_sock *h, struct rtnl_addr *addr, int flags) +{ + return nl_compat_error (rtnl_addr_add (h, addr, flags)); } #define rtnl_addr_add __rtnl_addr_add -static inline int rtnl_route_delete(struct nl_sock * h, struct rtnl_route * route, int flags) { - return nl_compat_error(rtnl_route_del(h, route, flags)); +static inline int +rtnl_route_delete (struct nl_sock *h, struct rtnl_route *route, int flags) +{ + return nl_compat_error (rtnl_route_del (h, route, flags)); } #define rtnl_route_del rtnl_route_delete -static inline int __rtnl_link_change(struct nl_sock * h, struct rtnl_link *old, struct rtnl_link * tmpl, int flags) { - return nl_compat_error(rtnl_link_change(h, old, tmpl,flags)); +static inline int +__rtnl_link_change (struct nl_sock *h, struct rtnl_link *old, struct rtnl_link *tmpl, int flags) +{ + return nl_compat_error (rtnl_link_change (h, old, tmpl,flags)); } #define rtnl_link_change __rtnl_link_change -static inline int __nl_cache_include(struct nl_cache * cache, struct nl_object * obj, change_func_t cb, void * data) +static inline int +__nl_cache_include (struct nl_cache *cache, struct nl_object *obj, change_func_t cb, void *data) { - return nl_cache_include(cache, obj, cb); + return nl_cache_include (cache, obj, cb); } #define nl_cache_include __nl_cache_include diff --git a/src/nm-netlink-monitor.c b/src/nm-netlink-monitor.c index 978e87d6a..040ef5af5 100644 --- a/src/nm-netlink-monitor.c +++ b/src/nm-netlink-monitor.c @@ -227,7 +227,8 @@ event_handler (GIOChannel *channel, g_return_val_if_fail (!(io_condition & ~EVENT_CONDITIONS), FALSE); /* Process the netlink messages */ - if ((err = nl_recvmsgs_default (priv->nlh_event)) < 0) { + err = nl_recvmsgs_default (priv->nlh_event); + if (err < 0) { error = g_error_new (NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_PROCESSING_MESSAGE, _("error processing netlink message: %s"), @@ -252,7 +253,8 @@ nlh_setup (struct nl_sock *nlh, if (valid_func) nl_socket_modify_cb (nlh, NL_CB_VALID, NL_CB_CUSTOM, valid_func, cb_data); - if ((err = nl_connect (nlh, NETLINK_ROUTE)) < 0) { + err = nl_connect (nlh, NETLINK_ROUTE); + if (err < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_CONNECT, _("unable to connect to netlink for monitoring link status: %s"), @@ -371,13 +373,13 @@ sync_connection_setup (NMNetlinkMonitor *self, GError **error) * themselves, busting caching. */ rtnl_addr_alloc_cache (priv->nlh_sync, &addr_cache); + g_warn_if_fail (addr_cache != NULL); nl_cache_get_ops (addr_cache)->co_obj_ops->oo_id_attrs &= ~0x80; nl_cache_free (addr_cache); #endif err = rtnl_link_alloc_cache (priv->nlh_sync, &priv->link_cache); - - if (err) { + if (err < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_ALLOC_LINK_CACHE, _("unable to allocate netlink link cache for monitoring link status: %s"), @@ -503,7 +505,8 @@ nm_netlink_monitor_subscribe (NMNetlinkMonitor *self, int group, GError **error) subs = get_subs (self, group) + 1; if (subs == 1) { - if ((err = nl_socket_add_membership (priv->nlh_event, group)) < 0) { + err = nl_socket_add_membership (priv->nlh_event, group); + if (err < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_NETLINK_JOIN_GROUP, _("unable to join netlink group: %s"), @@ -572,9 +575,10 @@ deferred_emit_carrier_state (gpointer user_data) /* Update the link cache with latest state, and if there are no errors * emit the link states for all the interfaces in the cache. */ - if ((err = nl_cache_refill (priv->nlh_sync, priv->link_cache)) != 0) { + err = nl_cache_refill (priv->nlh_sync, priv->link_cache); + if (err < 0) nm_log_err (LOGD_HW, "error updating link cache: %s", nl_geterror (err)); - } else + else nl_cache_foreach_filter (priv->link_cache, NULL, link_msg_handler, self); return FALSE; @@ -631,7 +635,8 @@ nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *self, priv = NM_NETLINK_MONITOR_GET_PRIVATE (self); /* Update the link cache with the latest information */ - if ((err = nl_cache_refill (priv->nlh_sync, priv->link_cache)) != 0) { + err = nl_cache_refill (priv->nlh_sync, priv->link_cache); + if (err < 0) { g_set_error (error, NM_NETLINK_MONITOR_ERROR, NM_NETLINK_MONITOR_ERROR_LINK_CACHE_UPDATE, diff --git a/src/nm-netlink-monitor.h b/src/nm-netlink-monitor.h index 38ccd350d..5c1fa3399 100644 --- a/src/nm-netlink-monitor.h +++ b/src/nm-netlink-monitor.h @@ -96,6 +96,6 @@ gboolean nm_netlink_monitor_get_flags_sync (NMNetlinkMonitor *monitor int nm_netlink_iface_to_index (const char *iface); char * nm_netlink_index_to_iface (int idx); struct rtnl_link *nm_netlink_index_to_rtnl_link (int idx); -struct nl_sock *nm_netlink_get_default_handle (void); +struct nl_sock * nm_netlink_get_default_handle (void); #endif /* NM_NETLINK_MONITOR_H */ diff --git a/src/nm-netlink-utils.c b/src/nm-netlink-utils.c index dfb726546..cbdbe6eb7 100644 --- a/src/nm-netlink-utils.c +++ b/src/nm-netlink-utils.c @@ -174,14 +174,14 @@ gboolean nm_netlink_route_delete (struct rtnl_route *route) { struct nl_sock *nlh; - int err=0; + int err = 0; g_return_val_if_fail (route != NULL, FALSE); nlh = nm_netlink_get_default_handle (); err = rtnl_route_delete (nlh, route, 0); - return ((err < 0) && (err != -NLE_RANGE)) ? FALSE: TRUE; + return (err && (err != -NLE_RANGE)) ? FALSE : TRUE; } diff --git a/src/nm-system.c b/src/nm-system.c index 4f6236db3..11e9ad090 100644 --- a/src/nm-system.c +++ b/src/nm-system.c @@ -194,9 +194,8 @@ sync_addresses (int ifindex, if (!nlh) return FALSE; - rtnl_addr_alloc_cache(nlh, &addr_cache); - - if (!addr_cache) + err = rtnl_addr_alloc_cache (nlh, &addr_cache); + if (err < 0) return FALSE; filter_addr = rtnl_addr_alloc ();