Commit Graph

29 Commits

Author SHA1 Message Date
Iain Lane
36c79adb2f
gnetworkmonitornetlink: Pass a GError into read_netlink_messages()
Currently this function calls `g_warning()` explicitly. It would be
nicer to properly propagate these failure up to the caller that tried to
initialise us.
2018-09-13 11:44:16 +01:00
Iain Lane
6629423e7a
gnetworkmonitornetlink: Close the socket after disconnecting its GSources
`read_netlink_messages()` is the callback attached to the netlink socket
(G_IO_IN). It calls `g_socket_receive_message()`. There is a race
condition that if the socket is closed while there is a pending call, we
will try to receive on a closed socket, which fails.

To avoid this, we switch the order of the operations around: first
destroy the source and then close the socket.
2018-09-13 11:14:34 +01:00
Iain Lane
7fe8aa68a2
gnetworkmonitornetlink: Don't check if a passed-in GError ** is NULL
This is not a correct way to check if `g_socket_new_from_fd()` failed.
Instead just see if it returned `NULL` itself.

This was preventing the netlink monitor from being initialised.

Closes #1518
2018-09-13 11:14:34 +01:00
Milan Crha
ca0add4b8a gnetworkmonitor: Fix use-after-free when using from another thread
When using g_network_monitor_get_default() from another thread, it’s
possible for network-changed events to be processed after an instance of
GNetworkMonitor has been disposed, causing use-after-free problems.

Fix that by moving some of the initialisation into the GInitable.init()
chain, rather than in a main context idle callback.

This includes a unit test which probabilistically reproduces the bug
(but can’t do so deterministically due to it being a race condition).

Commit amended by Philip Withnall <withnall@endlessm.com> before
pushing.

https://bugzilla.gnome.org/show_bug.cgi?id=793727
2018-04-11 15:03:43 +01:00
Philip Withnall
61a899815b gnetworkmonitornetlink: Fix a memory leak in unusual circumstances
If the GNetworkMonitorNetlink is finalised part-way through a dump
(after request_dump() is called, but before finish_dump() is called),
dump_networks was leaked. Fix that.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793880
2018-02-28 17:39:04 +00:00
Philip Withnall
183c846b30 gnetworkmonitornetlink: Fix memory leaks on error paths
Use a common error handler to avoid leaks on the error paths.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793880
2018-02-28 17:39:04 +00:00
Philip Withnall
88bf4939f6 gnetworkmonitornetlink: Refactor some code to reduce duplication
This should introduce no functional changes. Factor out some common
code, flip some arguments around to use the more conventional (data,
length) order, and move some memory management calls out of
if-blocks.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793880
2018-02-28 17:39:04 +00:00
Philip Withnall
23fad11430 gnetworkmonitornetlink: Use a coarser-grained timer for dumps
By using g_timeout_source_new_seconds(), we can let timer wakeups be
coalesced by the scheduler, and reduce power consumption a bit. This
shouldn’t really affect the accuracy of the network monitoring.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793880
2018-02-28 17:39:04 +00:00
Philip Withnall
85f3bc133f gnetworkmonitornetlink: Fix potential GMainContext issue
Previously, the GSource would be attached to whatever GMainContext was
the thread default at the time; but that might no longer be the same as
the default at the time of constructing the GNetworkMonitor.

Save the default from construction time, so that source callbacks are
always invoked in the same GMainContext.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793880
2018-02-28 17:39:04 +00:00
Philip Withnall
5cddde1fb2 Consistently save errno immediately after the operation setting it
Prevent the situation where errno is set by function A, then function B
is called (which is typically _(), but could be anything else) and it
overwrites errno, then errno is checked by the caller.

errno is a horrific API, and we need to be careful to save its value as
soon as a function call (which might set it) returns. i.e. Follow the
pattern:
  int errsv, ret;
  ret = some_call_which_might_set_errno ();
  errsv = errno;

  if (ret < 0)
    puts (strerror (errsv));

This patch implements that pattern throughout GLib. There might be a few
places in the test code which still use errno directly. They should be
ported as necessary. It doesn’t modify all the call sites like this:
  if (some_call_which_might_set_errno () && errno == ESOMETHING)
since the refactoring involved is probably more harmful than beneficial
there. It does, however, refactor other call sites regardless of whether
they were originally buggy.

https://bugzilla.gnome.org/show_bug.cgi?id=785577
2017-08-03 10:21:13 +01:00
Sébastien Wilmet
3bf4a720c3 gio/: LGPLv2+ -> LGPLv2.1+
Sub-directories inside gio/ already processed in a previous commit:
- fam/
- gdbus-2.0/ (which contains only codegen/)
- gvdb/
- inotify/
- tests/
- win32/
- xdgmime/

Other sub-directories inside gio/:
- completion/: no license headers
- kqueue/: not LGPL, BSD-style license

https://bugzilla.gnome.org/show_bug.cgi?id=776504
2017-05-29 19:53:34 +02:00
Milan Crha
4215c0ce91 Fix memory leaks in GNetworkMonitorNetlink
As claimed by valgrind in a downstream bug report:
https://bugzilla.redhat.com/show_bug.cgi?id=1317369

https://bugzilla.gnome.org/show_bug.cgi?id=766933
2016-07-16 21:15:30 -04:00
Alexander Larsson
7cba800a84 GNetworkMonitorNetlink: Fix check for non-kernel messages
This code used to look at the SCM_CREDENTIALS and ignore every message
not from uid 0. However, when user namespaces are in use this does not
work, as if uid 0 is not mapped you get overflowuid instead. Right now
this means we ignore all messages in such user namespaces and glib
apps hang on startup.

We can't look at pids either, as pid 0 is returned for processes
outside your pid namespace.

Instead the correct approach is to look at the sending sockaddr and
if the port id (nl_pid) is zero, then its from the kernel.

Source:
http://lists.linuxfoundation.org/pipermail/containers/2015-May/036032.html

https://bugzilla.gnome.org/show_bug.cgi?id=750203
2015-06-03 08:52:54 +02:00
Olivier Crête
638993f5ac networkmonitornetlink: IN6_IS_ADDR_MC_LINKLOCAL is only guaranteed to work on struct in6_addr*
https://bugzilla.gnome.org/show_bug.cgi?id=727119
2014-03-31 12:48:04 -04:00
Dan Winship
243b264fca gnetworkmonitornetlink: filter out IPv6 LL MC changes
For some reason, IPv6 link-local multicast routing changes all the
time. GNetworkMonitorBase was already ignoring them for purposes of
emitting network-changed, but GNetworkMonitorNetlink would still
trigger a re-dump after getting one, so network-changed would end up
getting emitted anyway.
2014-02-01 13:16:28 +01:00
Daniel Mustieles
078dbda148 Updated FSF's address 2014-01-31 14:31:55 +01:00
Michael Forney
75bb906e5e Add missing string.h include
Required for memset()
2013-10-01 09:00:10 -04:00
Dan Winship
c08ef6c165 gnetworkmonitornetlink: handle default route via device
If the default route is via a device rather than a particular IP
address, then neither RTA_DST nor RTA_GATEWAY will be present in the
RTM_NEWROUTE message, and so GNetworkMonitorNetlink would ignore it,
and then think there was no default route. (This could happen with
certain kinds of VPNs, if they were set to route all traffic through
the VPN.)

Fix this by recognizing routes that specify RTA_OIF ("output
interface") instead of RTA_GATEWAY.

https://bugzilla.gnome.org/show_bug.cgi?id=701609
2013-07-12 11:59:50 -04:00
Emmanuele Bassi
54cc43630d Rename the generated private data getter function
As it turns out, we have examples of internal functions called
type_name_get_private() in the wild (especially among older libraries),
so we need to use a name for the per-instance private data getter
function that hopefully won't conflict with anything.
2013-06-24 15:43:04 +01:00
Emmanuele Bassi
32747def4b gio: Use the new private instance data declaration
Use the newly added macros, and remove the explicit calls to
g_type_class_add_private().

https://bugzilla.gnome.org/show_bug.cgi?id=700035
2013-06-24 14:18:01 +01:00
Colin Walters
f398bec5bc Add g_close(), use it
There are two benefits to this:

1) We can centralize any operating system specific knowledge of
   close-vs-EINTR handling.  For example, while on Linux we should never
   retry, if someone cared enough later about HP-UX, they could come by
   and change this one spot.
2) For places that do care about the return value and want to provide
   the caller with a GError, this function makes it convenient to do so.

Note that gspawn.c had an incorrect EINTR loop-retry around close().

https://bugzilla.gnome.org/show_bug.cgi?id=682819
2013-01-29 09:46:04 -05:00
Dan Winship
5932e16acd GNetworkMonitorNetlink: make the netlink socket cloexec
Use the same code GSocket does, to try SOCK_CLOEXEC first, and then
fall back to FD_CLOEXEC if it fails. (And fix that code to not call
fcntl if SOCK_CLOEXEC worked.)

https://bugzilla.gnome.org/show_bug.cgi?id=692332
2013-01-23 08:48:32 -05:00
Dan Winship
211ed1775d gsocket: add getsockopt/setsockopt wrappers
Add g_socket_get_option() and g_socket_set_option(), wrapping
getsockopt/setsockopt for the case of integer-valued options. Update
code to use these instead of the underlying calls.

https://bugzilla.gnome.org/show_bug.cgi?id=623187
2012-12-12 15:20:22 +01:00
Dan Winship
17e95c59c9 gnetworkmonitornetlink: don't leak the list of networks
https://bugzilla.gnome.org/show_bug.cgi?id=676265
2012-05-17 13:48:56 -04:00
Dan Winship
8f558d8010 gnetworkmonitornetlink.c: Fix a gsize->gssize
https://bugzilla.gnome.org/show_bug.cgi?id=666422
2011-12-19 15:20:08 -05:00
Christophe Fergeau
f3dde2d405 gnetworkmonitornetlink.c: Fix compilation on RHEL 6.2
When trying to compile glib master on a RHEL 6.2 system, it fails with:

make[4]: Entering directory `/home/teuf/gnome/src/glib/gio'
  CC     libgio_2_0_la-gnetworkmonitornetlink.lo
In file included from gnetworkmonitornetlink.c:25:
/usr/include/linux/netlink.h:35: error: expected specifier-qualifier-list before 'sa_family_t'
gnetworkmonitornetlink.c: In function 'g_network_monitor_netlink_initable_init':
gnetworkmonitornetlink.c:99: error: 'struct sockaddr_nl' has no member named 'nl_family'
gnetworkmonitornetlink.c💯 error: 'struct sockaddr_nl' has no member named 'nl_pid'
gnetworkmonitornetlink.c💯 error: 'struct sockaddr_nl' has no member named 'nl_pad'
gnetworkmonitornetlink.c:101: error: 'struct sockaddr_nl' has no member named 'nl_groups'
make[4]: *** [libgio_2_0_la-gnetworkmonitornetlink.lo] Error 1

sa_family_t is defined in sys/socket.h, this commit makes sure this header is included before netlink.h

This fixes bgo bug #666001
2011-12-13 16:40:34 +01:00
Matthias Clasen
2d6d127c96 Whitespace fixes
And add some missing since tags.
2011-12-10 21:46:39 -05:00
Dan Winship
1f33446e1f gnetworkmonitornetlink.c: Make the error messages less geeky
This makes several different errors have identical messages, but
they're pretty much "can't happen" situations anyway.

https://bugzilla.gnome.org/show_bug.cgi?id=664406
2011-11-29 21:45:37 +01:00
Dan Winship
fe5ba0f291 add GNetworkMonitor, for... monitoring the network
Add GNetworkMonitor and its associated extension point, provide a base
implementation that always claims the network is available, and a
netlink-based implementation built on top of that that actually tracks
the network state.

https://bugzilla.gnome.org/show_bug.cgi?id=620932
2011-11-14 13:42:30 -05:00