Commit Graph

27331 Commits

Author SHA1 Message Date
Thomas Haller
cecbb25eeb gmain: fix race with waitpid() and child watcher sources
GChildWatchSource uses waitpid(), among pidfd and GetExitCodeProcess().
It thus only works for child processes which the user must ensure to
exist and not being reaped yet. Also, the user must not kill() the PID
after the child process is reaped and must not race kill() against
waitpid(). Also, the user must not call waitpid()/kill() after the child
process is reaped.

Previously, GChildWatchSource would call waitpid() already when adding
the source (g_child_watch_source_new()) and from the worker thread
(dispatch_unix_signals_unlocked()). That is racy:

- if a child watcher is attached and did not yet fire, you cannot call
  kill() on the PID without racing against the PID being reaped on the
  worker thread. That would then lead to ESRCH or even worse, killing
  the wrong process.

- if you g_source_destroy() the source that didn't fire yet, the user
  doesn't know whether the PID was reaped in the background. Any
  subsequent kill()/waitpid() may fail with ESRCH/ECHILD or even address
  the wrong process.

The race is most visible on Unix without pidfd support, because then the
process gets reaped on the worker thread or during g_child_watch_source_new().
But it's also with Windows and pidfd, because we would have waited for
the process in g_child_watch_check(), where other callbacks could fire
between reaping the process status and emitting the source's callback.

Fix all that by calling waitpid() right before dispatching the callback.
2023-05-18 11:26:33 +02:00
Thomas Haller
cdda194844 gmain: remove unnecessary initialization of *timeout in prepare() callbacks
Note that the prepare callback only has one caller, which pre-initializes
the timeout argument to -1. That may be an implementation detail and not
publicly promised, but it wouldn't make sense to do it any other way in
the caller.

Also, note that g_unix_signal_watch_prepare() and the UNIX branch of
g_child_watch_prepare() already relied on that.
2023-05-18 11:26:33 +02:00
Thomas Haller
30b5418608 gmain: remove unnecessary initialization of source_timeout in g_main_context_prepare_unlocked()
Note that the variable source_timeout is already initialized upon
definition, at the beginning of the block.

It's easy to see, that no code changes the variable between the variable
definition, and the place where it was initialized. It was thus
unnecessary.

It's not about dropping the unnecessary code (the compiler could do that
just fine too). It's that there is the other branch of the "if/else", where
the variable is also not initialized. But the other branch also requires
that the variable is in fact initialized to -1, because prepare()
callbacks are free not to explicitly set the output value. So both
branches require the variable to be initialized to -1, but only one of
them did. This poses unnecessary questions about whether anything is
wrong. Avoid that by dropping the redundant code.
2023-05-18 11:26:33 +02:00
Thomas Haller
a71b0c0461 gmain: simplify handling child watchers in dispatch_unix_signals_unlocked()
- if a child watch source has "using_pidfd", it is never linked in the
  unix_child_watches list. Drop that check.
- replace the deep nested if, with an early "continue" in the loop,
  if we detect there is nothing to do. It makes the code easier to
  read.
2023-05-18 11:26:33 +02:00
Thomas Haller
9315a211fa gmain: unify win/unix implementations for child watcher
Let's move the difference between the win/unix implementations closer to
where the difference is. Thereby, we easier see the two implementations
side by side. Splitting it at a higher layer makes the code harder to
read.

This is just a preparation for what comes next.
2023-05-18 11:26:33 +02:00
Thomas Haller
d1e558f492 gmain/tests: add unit test for g_child_watch_add() reaping processes without notifying
The test currently tests broken behavior. It will be fixed next.
2023-05-18 11:26:33 +02:00
Philip Withnall
54d1ebfc37 Merge branch 'ebassi/task-pretty-print-ignore-gir' into 'main'
Ignore g_task_print_alive_task() when introspecting GIO

See merge request GNOME/glib!3435
2023-05-16 14:50:21 +00:00
Emmanuele Bassi
20142de034 Ignore g_task_print_alive_task() when introspecting GIO
The `__GTK_DOC_IGNORE__` check is not enough for g-ir-scanner: we need
to check for `__GI_SCANNER__` as well.
2023-05-16 13:33:19 +01:00
Philip Withnall
ec8856d7d6 Merge branch 'wip/p3732/g-string-new-steal' into 'main'
gstring: add g_string_new_take

See merge request GNOME/glib!3423
2023-05-16 12:04:37 +00:00
Peter Eisenmann
ac4d1e2686 docs: unify "dynamic memory needed" explanations
All `_take` method constructors require dynamic memory, this unifies
theses explanation texts.
2023-05-16 11:27:45 +01:00
Peter Eisenmann
afdab4f493 gstring: add g_string_new_take
Adds a GString constructor that takes over ownership of an existing,
dynamically allocated, string.
2023-05-16 11:27:45 +01:00
Philip Withnall
90c3438373 Merge branch 'fix_gdb_python_helper' into 'main'
gobject_gdb.py: fix regression caused by bfbe7127d5

See merge request GNOME/glib!3430
2023-05-16 10:27:41 +00:00
Nelson Benítez León
1a19095a36 gobject_gdb.py: fix regression caused by bfbe7127d5
commit bfbe7127d5 which did a code refactor in
gobject_gdb.py introduced a bug by failing to
return the signal name when a signal had no
'detail', this was preventing pretty printing
name for signals with no 'detail'.
2023-05-15 18:04:37 +01:00
Emmanuele Bassi
03aa8513f8 Merge branch 'wip/pwithnall/bsd-copy-file-range' into 'main'
gfile: Ensure loff_t is defined on FreeBSD

See merge request GNOME/glib!3431
2023-05-15 16:48:32 +00:00
Philip Withnall
e02fa2ec90 gfile: Ensure loff_t is defined on FreeBSD
When `copy_file_range()` support was added, I used the definition of
`copy_file_range()` from Linux, which uses `loff_t` to abstract the
different `off*_t` types.

`loff_t` doesn’t exist on FreeBSD, so this doesn’t compile, and was
caught in subsequent asynchronous CI.

Define `loff_t` with a fallback value if it’s not defined, which should
fix this and other uses of `loff_t` in `gfile.c` (for example, if
FreeBSD ever starts declaring `splice()`).

Fixes this CI failure: https://gitlab.gnome.org/GNOME/glib/-/jobs/2812302

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-15 16:22:35 +01:00
Philip Withnall
fde068300e Merge branch 'main' into 'main'
glib/tests/fileutils:fix asserts when running as root

See merge request GNOME/glib!3421
2023-05-11 11:12:10 +00:00
hanhuihui
e2973c9a41 tests: Fix assertion failures when running fileutils test as root 2023-05-11 11:46:08 +01:00
hanhuihui
d5d5fb9b5f tests: Move a function to allow looking at CAP_DAC_OVERRIDE
This introduces no functional changes but allows `CAP_DAC_OVERRIDE` to
be used in a subsequent commit.
2023-05-11 11:45:36 +01:00
Philip Withnall
a14803d06d Merge branch 'fix-group-comment' into 'main'
gkeyfile: Fix group comment management

Closes #104 and #2927

See merge request GNOME/glib!3380
2023-05-10 09:24:41 +00:00
Philip Withnall
eee3885077 Merge branch 'gtk-plus' into 'main'
Rename GTK+ to GTK (mostly comments and documentation)

See merge request GNOME/glib!3429
2023-05-10 08:39:58 +00:00
Philip Withnall
329213f742 Merge branch 'main' into 'main'
gtestutils: Improve g_assert_cmpuint

Closes #2997

See merge request GNOME/glib!3424
2023-05-10 08:35:27 +00:00
Philip Withnall
8a68b795c1 tests: Fix GIO file size tests as .desktop files have changed size
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-10 09:26:47 +01:00
Arnaud Rebillout
d6e57943ea Fix style-check for {gconvert,gutils,gobject}.c
Also take this chance to change a 'Glib' to 'GLib'.
2023-05-10 10:56:44 +07:00
Arnaud Rebillout
f722f11e57 Rename GTK+ to GTK (mostly comments and documentation)
GTK lost it's '+' suffix back in 2019, according to
<https://mail.gnome.org/archives/gtk-devel-list/2019-February/msg00000.html>

This commit can be re-generated with:

    git grep -l GTK+ \
    | grep -v -e ^NEWS -e ^glib/tests/collate.c \
    | xargs sed -i 's/GTK+/GTK/g'

Most of the changes are in comments and documentation.
2023-05-10 10:56:44 +07:00
Eric Blake
171bcd524a gtestutils: Style touchup
Use more modern styling to the code added in the previous patch:
- split 'label: stmt; stmt;' into multiple lines
- add default: label with g_assert_not_reached() [yes, it's a bit
  weird adding an assertion inside code that handles assertions, but
  we should be okay since g_assertion_message_* are not public
  functions and should only be used by our macros]
- use <inttypes.h> for shorter format strings

Note, however, that using uint64_t in gtestutils.h is not feasible,
since it would require adding an '#include <stdint.h>' with potential
unintended namespace pollution to older clients.

Signed-off-by: Eric Blake <eblake@redhat.com>
2023-05-09 09:29:16 -05:00
Philip Withnall
20964ad4ab Merge branch '2863-copy-file-range' into 'main'
gfile: Support copy_file_range() for file copies

Closes #2863

See merge request GNOME/glib!3328
2023-05-09 14:08:18 +00:00
Eric Blake
2ab2ce57e6 gtestutils: Improve g_assert_cmpuint
While x86_64 has enough precision in long double to do a round trip
from guint64 to long double and back, this is platform-specific, and
is a disservice to users trying to debug failing unit tests on other
architectures where it loses precision for g_assert_cmp{int,uint,hex}.
See also https://bugzilla.gnome.org/show_bug.cgi?id=788385 which
mentions having to add casts to specifically silence the compiler on
platforms where the precision loss occurs.

Meanwhile, g_assert_cmpuint() does an unsigned comparison, but outputs
signed values if the comparison fails, which is confusing.

Fix both issues by introducing a new g_assertion_message_cmpint()
function with a new 'u' numtype.  For backwards compatibility, the
macros still call into the older g_assertion_message_cmpnum() when not
targetting 2.78, and that function still works when passed 'i' and 'x'
types even though code compiled for 2.78 and later will never invoke
it with numtype anything other than 'f'.  Note that g_assert_cmpmem
can also take advantage of the new code, even though in practice,
comparison between two size_t values representing array lengths that
can actually be compiled is unlikely to have ever hit the precision
loss.  The macros in signals.c test code does not have to worry about
versioning, since it is not part of the glib library proper.

Closes #2997
Signed-off-by: Eric Blake <eblake@redhat.com>
2023-05-09 08:28:09 -05:00
Philip Withnall
60f38b5f75 Merge branch 'main' into 'main'
Check for linux/netlink.h buildability

See merge request GNOME/glib!3422
2023-05-09 13:15:32 +00:00
Philip Withnall
0e5d9fd249 gfile: Support copy_file_range() for file copies
While it can’t be used in all situations, it is a little bit faster than
`splice()` in some situations, basically if the file system supports
copy on write. In other situations it’s no slower than `splice()`.

See `man copy_file_range` for the situations where it doesn’t work. In
all of these situations, it will return an error, and the GLib code will
fall through and try the existing `splice()` copy code instead.

From my testing of `time gio copy A B` with a 9GB file, the `splice()`
code path takes 22s, and the `copy_file_range()` code path takes 20s.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2863
2023-05-09 14:01:50 +01:00
Philip Withnall
aafa19fc0d tests: Add a test for progress callbacks from g_file_copy()
This wasn’t previously tested.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2863
2023-05-09 14:01:48 +01:00
Philip Withnall
0da948a5d1 gfile: Eliminate a stat() call from the file copy code
The start of the `g_file_copy()` implementation stats the source file to
find all the attributes to copy onto the destination file, so it makes
sense to get it to store the source file size at the same time.

This saves a subsequent `stat()` call on the source FD in the btrfs
reflink or splice code. Every little helps.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-09 13:53:44 +01:00
Marco Trevisan
08aee6b5f4 Merge branch 'unixfdlist-again' into 'main'
docs: Don’t ignore gunixfdlist.h when building docs on Windows

See merge request GNOME/glib!3409
2023-05-09 12:53:19 +00:00
Philip Withnall
63e1e83c33 Merge branch 'wip/p3732/timeout-seconds-once' into 'main'
add g_timeout_add_seconds_once

See merge request GNOME/glib!3383
2023-05-09 12:50:39 +00:00
Marco Trevisan
061881afba Merge branch 'aborting-tests-rlimit' into 'main'
tests: Zero the rlimit in standalone tests which are expected to abort

Closes #2939

See merge request GNOME/glib!3417
2023-05-09 12:49:54 +00:00
Samuel Thibault
81d729dc53 Check for linux/netlink.h buildability
When cross-building with a non-Linux target, linux/netlink.h will not
actually be buildable, even if distributions typically put it in
/usr/include and thus exposed to the cross-compiler.
2023-05-09 13:31:28 +01:00
Philip Withnall
1461c29301 Merge branch 'win32-network-monitor-error' into 'main'
gwin32networkmonitor: Fix returning address of local variable

See merge request GNOME/glib!3425
2023-05-09 12:27:05 +00:00
Philip Withnall
e59e8ebf31 gwin32networkmonitor: Fix returning address of local variable
Something has changed recently which causes this error to now be emitted
when building on Windows msys2-mingw32:
```
../gio/gwin32networkmonitor.c: In function 'win_network_monitor_get_ip_info':
../gio/gwin32networkmonitor.c:92:15: error: storing the address of local variable 'prefix' in '*dest' [-Werror=dangling-pointer=]
   92 |         *dest = (guint8 *) &prefix.Prefix.Ipv4.sin_addr;
      |         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```

If `IP_ADDRESS_PREFIX` is defined as a scalar rather than a pointer,
that could explain the problem.

Change the function to always operate on a pointer to avoid any
potential such issues.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-09 12:52:42 +01:00
Patrick Griffis
de2ff26454 Merge branch '116-utf8-docs' into 'main'
docs: Document high-level UTF-8 requirements for GLib

Closes #116

See merge request GNOME/glib!3414
2023-05-02 19:20:30 +00:00
Patrick Griffis
8725d6ef4c Merge branch 'wiki-links' into 'main'
docs: Update various broken/redirected links and fix list formatting

See merge request GNOME/glib!3419
2023-05-02 19:18:46 +00:00
Philip Withnall
ff315e8407 docs: Fix list formatting in supported-platforms.md
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-02 14:34:21 +01:00
Philip Withnall
377e8a2d9e docs: Update various broken/redirected links
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-02 14:26:52 +01:00
Luca Bacci
cad4f85be9 Merge branch 'codeowners-lb90' into 'main'
docs: Add lb90 as a Windows maintainer

See merge request GNOME/glib!3418
2023-05-02 12:13:35 +00:00
Philip Withnall
85af9b73c9 tests: Zero the rlimit in standalone tests which are expected to abort
This should hopefully stop the kernel spending a lot of memory and disk
bandwidth creating coredumps for them unnecessarily, which slows down
the rest of the tests and generally wastes resources.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #2939
2023-05-02 12:53:13 +01:00
Peter Eisenmann
e25a4f995f use g_timeout_add_seconds_once()
Use the newly added g_timeout_add_seconds_once() where appropriate.
2023-05-02 13:42:54 +02:00
Peter Eisenmann
467b917719 gtimeout: use helper for seconds_full variant
Use timeout_add_full() helper to deduplicate code
2023-05-02 13:42:54 +02:00
Peter Eisenmann
3abf23b2a7 add g_timeout_add_seconds_once
Add a new call combing behaviors of g_timeout_add_seconds and
g_timeout_add_once.
2023-05-02 13:42:54 +02:00
Philip Withnall
5803a24826 docs: Add lb90 as a Windows maintainer
They’ve been actively doing good work.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-02 11:19:31 +01:00
Philip Withnall
e8c1bdbf4f tests: Add copyright/licensing header to assert-msg-test
This is put together through git archaeology:
```
git log -- glib/tests/assert-msg-test.c tests/assert-msg-test.c
```

The following commits were too trivial to have meaningful copyright:
 - 8e59d8602c
 - 44c004c84e
 - 207b8cb8a5
 - a1bee97d4f

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #1415
2023-05-02 11:03:49 +01:00
Philip Withnall
a1bee97d4f tests: Reformat assert-msg-test.c to follow coding style
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-05-02 11:03:00 +01:00
Philip Withnall
0e941418d1 docs: Document high-level UTF-8 requirements for GLib
I’ve finally found the right place in the docs to put this stuff.

This doesn’t auto-link this section from every string in the GLib
documentation, but I think that at this point (with gtk-doc in
maintenance mode, and gi-docgen not fully applied to GLib) I don’t think
we can do any better. The perfect is the enemy of the good, and having
this stuff documented somewhere means that someone can link to it from
multiple places in future *somehow*.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: #116
2023-05-02 10:41:50 +01:00