Commit graph

540 commits

Author SHA1 Message Date
Alyssa Ross
3e24ee1e27
build: remove unused macros
These were/are used as autoconf/CMake variable names, but were never
used by C code, so there's no need to export them as macros.

Signed-off-by: Alyssa Ross <hi@alyssa.is>
Fixes: dca6591f ("Keep cmake defines GLIB_VERSION_... in sync with autotools.")
Fixes: cd2e3826 ("Add Meson build system")
2024-07-21 19:52:16 +02:00
Simon McVittie
88b5749984 sysdeps: Use C11 stdatomic.h where possible
On Unix, dbus has historically used gcc-specific lock-free atomic
intrinsics where available, falling back to a pthreads mutex where
possible. Meanwhile, on Windows, it has historically used
InterlockedIncrement() and similar library functions (in practice
wrappers around lock-free intrinsics on real Windows, but IPC calls into
wineserver on Wine).

ISO C11 provides a new header, stdatomic.h, with standardized support
for atomic operations. Exactly how these are implemented is a compiler
quality-of-implementation decision, but any reasonable compiler
implementation on a modern CPU should be using intrinsics. Let's use
this wherever possible, falling back to our old implementation only if
the C11 implementation is unsupported.

One concrete benefit that we get from this is that when compiling with
mingw-w64 gcc and running via Wine, this makes atomic reference counting
operations into a simple local operation, rather than IPC to wineserver
which can be very slow. This should make our CI tests considerably more
reliable.

In all vaguely modern gcc versions (gcc 5.5 or later) and in contemporary
versions of clang, the default compiler mode is C11 or later with GNU
extensions. We intentionally do not ask for any specific C standard, so
we can use C11 features like this one, as long as we do so conditionally.

The Microsoft Visual C compiler does not currently support this without
special options, so we still use the Interlocked family of functions
when compiling for Windows with MSVC.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-08-15 12:54:41 +00:00
Simon McVittie
3072109b11 On 32-bit glibc, define _TIME_BITS to 64 if not already defined
On older 32-bit architectures such as i386, this redefines time_t to be
64-bit, and correspondingly increases the size of all system data
structures that contain a time_t, such as struct timeval and struct stat.
This is necessary to allow timestamps beyond January 2038 to be
represented; as well as things that obviously deal with timestamps,
this affects functions like stat() (and therefore our wrapper
_dbus_stat()), which will fail with EOVERFLOW if asked to inspect a
file whose correct timestamp does not fit in time_t.

In particular, if the modification or access timestamp on
/etc/machine-id has somehow been set to a post-2038 time, libdbus will
consider the inability to stat() that file to be an installation error,
and when using the deprecated dbus_get_local_machine_id(), that can
cause third-party i386 software such as the Steam client to crash.
Using 64-bit timestamps avoids that failure mode.

Using 64-bit timestamps in glibc is an opt-in and not the default,
because if done carelessly it can change libraries' ABIs. However,
libdbus is careful not to include system headers and system data
types in its own headers, with the only exceptions being extremely
basic ISO C headers like <stddef.h> and <stdarg.h>; so we can safely
do this without it breaking our ABI. This is similar to the reasoning
for why commit 96ffc2a0 "configure.ac: support large-file for stat64"
was a safe change.

This change only affects glibc. Some non-GNU operating system libraries
(such as musl) are less concerned with binary backwards compatibility
than glibc, and therefore have incompatibly changed their ABI on 32-bit
platforms to switch to 64-bit timestamps throughout; no action is needed
on those platforms. If other non-GNU OS libraries have taken a route
similar to GNU's, then maintainers of those operating systems are
welcome to send tested merge requests similar to this one.

An extra subtlety here is that _TIME_BITS=64 requires
_FILE_OFFSET_BITS=64. In the Meson build, Meson unconditionally enables
_FILE_OFFSET_BITS=64 where appropriate, and in the Autotools build,
we already had that via AC_SYS_LARGEFILE, but in the CMake build we
did not necessarily have this; so we also define _FILE_OFFSET_BITS=64
there if necessary, as a continuation of commit 96ffc2a0
"configure.ac: support large-file for stat64".

On newer 32-bit architectures like x32, time_t is always 64-bit and so
this has no practical effect.

On 64-bit, setting these would have no practical effect, but to minimize
risk I'm only doing this for 32-bit architectures.

Resolves: https://gitlab.freedesktop.org/dbus/dbus/-/issues/465
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-08-14 14:18:25 +00:00
Luca Boccassi
80d523dcc8 DBusCredentials: add support for PID FDs via SO_PEERPIDFD
The new socket option SO_PEERPIDFD allows to pin the process on the
other side of the socket by file descriptor, which closes a race
condition where a PID can be reused before we can pin it manually.
Available since Linux v6.5.

When storing credentials, pin the process by FD from the PID.
When querying the PID, if the PID FD is available, resolve
it from there first if possible.

Ensure the DBusCredentials object only returns the PID FD if it was
obtained by this call, so that we know for sure we can rely on it
being safe against PID reuse attacks.

Signed-off-by: Luca Boccassi <bluca@debian.org>
2023-08-08 12:24:20 +01:00
Ralf Habacker
e665f96c76 Let cmake retrieve version and variable information from meson build system
Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
2023-08-07 16:10:21 +02:00
Ralf Habacker
dbb4e0ea90 cmake: reorder include header and function checks to match meson build system 2023-08-07 16:10:21 +02:00
Simon McVittie
80392649da cmake: Define _GNU_SOURCE before checking for any symbols
Some of the symbols we check for, such as close_range(), are only
declared in their corresponding header files if _GNU_SOURCE was
defined.

Resolves: dbus/dbus#453
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-06-06 17:30:36 +00:00
Luca Boccassi
4364b9ebb3 Support /proc in _dbus_file_get_content
procfs has special semantics: most files are 0 size,
only one read can be done on a file, and they are
not larger than 4MB. Enhance _dbus_file_get_content()
so that we can read files from /proc with it.

Signed-off-by: Luca Boccassi <bluca@debian.org>
2023-05-15 19:06:54 +00:00
Luca Boccassi
62c870aa5e cmake: add missing HAVE_SYS_SYSCALL_H define
Signed-off-by: Luca Boccassi <bluca@debian.org>
2023-03-20 22:10:41 +00:00
Simon McVittie
0827d76553 Fix SPDX license reference in FindGLIB2
The `.txt` here was unnecessary.

Fixes: d6abc1da "cmake: Add BSD-style licenses"
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-29 20:44:50 +00:00
Simon McVittie
d6abc1da02 cmake: Add BSD-style licenses
I've erred on the side of caution and treated the COPYING-CMAKE-SCRIPTS
license (a BSD-3-Clause variation) as its own distinct license.

Co-authored-by: Ralf Habacker <ralf.habacker@freenet.de>
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-29 13:33:53 +00:00
Simon McVittie
37e0125943 test: Export G_TEST_BUILDDIR, G_TEST_SRCDIR
These environment variables are used by GLib's g_test_build_filename()
and related convenience functions, which make it easier for unit tests
to find data files in a way that works for both build-time tests and
"as-installed" tests. During "as-installed" testing, both variables
will normally be unset, and GLib uses the directory containing the
executable. In most cases that results in the right thing happening, and
this will also be true for dbus, since we install the test executables
in ${libexecdir}/installed-tests, helper executables in the same place,
and test data in ${libexecdir}/installed-tests/data.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-10-05 10:24:24 +01:00
Alex Richardson
6933a9263e dbus-message.c: skip 1.10 layout compatibility test on new architectures
These static assertions fail on CHERI-enabled architectures such as Arm
Morello, where pointers are 128 bits. Architectures with 128-bit pointers
were not supported in DBus 1.10, so we can skip the checks for DBus 1.10
structure layout compatibility for architectures with pointer size > 64 bit.
2022-09-21 11:35:05 +00:00
Alex Richardson
3b605a5a6d cmake: define DBUS_TEST_SOCKET_DIR in the config header
This matches autotools.
2022-09-15 18:22:49 +00:00
Alex Richardson
a214ed824d dbus-sysdeps-util-unix.c: Avoid including sys/syslimits.h
This header is GCC specific header that on my system just contains
`#include_next <limits.h>`. FreeBSD also provides this header but it
contains a `#warning` that it should not be used. Replace the one use
with `#include <limit.h>` and drop the configure checks.
2022-08-10 11:18:20 +00:00
Jordan Williams
fac146f44d
Use project relative paths in CMake files
This makes it possible for projects to incorporate D-Bus as a CMake sub-project in a larger CMake project.
Before this PR, doing so would result in many errors.
This is because CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR would point to directories above the D-Bus project.
Using paths relative to the project directory, PROJECT_SOURCE_DIR and PROJECT_BINARY_DIR, corrects for this.
2022-07-25 14:14:57 -05:00
Simon McVittie
98eff5513e build: Assume stdint.h is provided
We now require a (mostly-)C99 compiler, meaning we can rely on having
Standard C stdint.h.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-19 21:11:09 +01:00
Simon McVittie
36038fb126 Remove fallback implementation of strtoll(), strtoull()
We now require a (mostly-)C99 compiler, which guarantees the presence
of these functions.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-19 21:11:09 +01:00
Simon McVittie
624efc0781 cmake: Stop redefining snprintf() to _snprintf() on MSVC
We now require a mostly-C99 compiler like MSVC 2015, which provides
Standard C snprintf().

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-19 20:50:33 +01:00
Simon McVittie
a54ed9ffad Remove emulation of va_copy() in non-C99 compilers
dbus now requires a (mostly-)C99 compiler, which guarantees the presence
of Standard C va_copy().

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-19 20:50:33 +01:00
Simon McVittie
bf30fe6d60 build: Remove support for emulating inline with __inline or __inline__
We now require a (mostly-)C99 compiler, and C99 guarantees the presence
of the inline keyword.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-19 20:45:31 +01:00
Marc-André Lureau
2137f511db build-sys: check for afunix.h
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-07-15 16:26:18 +01:00
Marc-André Lureau
9016b48cf9 cmake: drop DBUS_VERBOSE_C_S
Apparently, not used anywhere.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-07-13 20:36:13 +01:00
Marc-André Lureau
a5c8dda5cf cmake: drop unused HAVE_VASPRINTF/HAVE_VSNPRINTF checks
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-07-13 20:36:13 +01:00
Marc-André Lureau
95df73b463 cmake/autotools: remove HAVE_DIRFD/HAVE_DDFD
dirfd() is assumed to be present on Linux, dd_fd is never used.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-07-13 20:36:13 +01:00
Marc-André Lureau
f0a4d92490 cmake: drop needless headers checks
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-07-13 20:36:13 +01:00
Simon McVittie
7445af51a4 build: Add machine-readable licensing markers to the Meson build system
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-13 20:36:13 +01:00
Simon McVittie
c02d34555a cmake: Generate the same DBus1Config with Meson as with Autotools
DBus1Config.cmake.in relies on DBus1Targets.cmake, which is generated
by CMake. DBus1Config.pkgconfig.in uses pkg-config and is independent
of CMake.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-07-13 20:36:13 +01:00
Félix Piédallu
cd2e382610 Add Meson build system
Signed-off-by: Félix Piédallu <felix@piedallu.me>
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-07-13 20:36:13 +01:00
Marc-André Lureau
e4d6b74b25 build-sys: drop DBUS_CMAKE
TEST_LISTEN is defined by the cmake/autotools (or meson) build systems.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-06-17 15:19:38 +04:00
Alex Richardson
dafb5ddc09 cmake: Check for setresuid and getresuid
This was not being checked, so the codepaths using the define were
never included.
2022-05-25 22:01:51 +00:00
Marc-André Lureau
5705d14cf0 cmake: drop -DDBUS_CONSOLE_AUTH_DIR=
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2022-05-19 22:19:25 +02:00
Alex Richardson
b932c343c4 cmake: Fix definition of DBUS_USE_SYNC
dbus-sysdeps-unix.c checks for DBUS_USE_SYNC using 0/1 checks not defined
checks, so we should be using #cmakedefine01. This fixes lots of -Wundef
warnings when compiling for FreeBSD and ensures that we actually use
atomics instead of the pthread fallback there.
2022-05-16 18:39:26 +00:00
Ralf Habacker
f075650634 cmake: in macro generate_compiler_warning_flags prevent duplicates in variable containing unsupported warnings
Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
2022-04-21 13:51:55 +00:00
Ralf Habacker
fb5449e08f cmake: Fix detecting -Wformat-* warnings for gcc
gcc expects -Wformat to be set along with these type of warnings.

Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
2022-04-21 13:51:55 +00:00
Ralf Habacker
bbd3f90a8d cmake: Fix not detecting unsupported g++ related warning checks
Unsupported warnings are detected by cmake through errors during
compilation, which causes g++ not to detect them, since they are
only output as warnings. Setting -Werror ensures this.

Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
2022-04-21 13:51:55 +00:00
Simon McVittie
ccea70515e sysdeps: On Linux, wrap close_range syscall directly if necessary
This was added to the Linux kernel in version 5.9, but the wrapper
wasn't added to glibc until 2.34. Adding our own wrapper for the
system call means we can use close_range() on Debian 11 and
contemporary distributions.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 11:02:44 +01:00
Simon McVittie
d307616177 sysdeps: Include <linux/close_range.h> if available
This lets us use CLOSE_RANGE_CLOEXEC whenever the kernel headers
support it, even if glibc doesn't include this header via unistd.h yet.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 11:02:44 +01:00
Simon McVittie
ee694ade61 sysdeps: Use close_range() if available
The version with no flags set, which is a slight generalization of
closefrom(), is available on recent Linux and FreeBSD.

The version with CLOSE_RANGE_CLOEXEC is Linux-specific.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-21 11:02:14 +01:00
rim
54c62040b2 Use closefrom() to not loop over all possible file descriptors
Resolves: https://gitlab.freedesktop.org/dbus/dbus/-/issues/278
2022-04-21 11:02:14 +01:00
Simon McVittie
6130ac4267 build: Define DBUS_INT64_MODIFIER, analogous to G_GINT64_MODIFIER
Using PRId64, etc. to print dbus_int64_t or dbus_uint64_t is not 100%
portable. On platforms where both long and long long are 64-bit (such as
Linux and macOS), we will prefer to define dbus_int64_t as long.
If the operating system has chosen to define int64_t as long long,
which is apparently the case on macOS, then the compiler can warn that
we are passing a long argument to PRId64, which is "lld" and therefore
expects a long long argument (even though that ends up with the same
bit-pattern being used).

We can't necessarily just use int64_t and uint64_t directly, even if all
our supported platforms have them available now, because swapping
dbus_int64_t between long and long long might change C++ name mangling,
causing ABI breaks in third-party libraries if they define C++ functions
that take a dbus_int64_t argument.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-19 18:27:47 +00:00
Simon McVittie
104df89947 cmake: Look for dbus-arch-deps.h next to DBus1Config.cmake
<dbus/dbus-arch-deps.h> is architecture-dependent, and compilers have
not traditionally supported an installation path for architecture-specific
headers (Debian-based systems have /usr/include/${multiarch_tuple}, but
that isn't portable beyond Debian). When dbus was built using Autotools,
dependent projects that use CMake need to look for this header in the
right place.

Unfortunately, it seems that at least recent versions of CMake will
ignore the HINTS we get from pkg-config if they are told to search in
a non-standard prefix via ${DBus1_ROOT}.

Look for dbus-arch-deps.h in a directory derived from the filename of
the CMake config file, before trying the normal search algorithm. The
CMake config file is in ${libdir}, and so is the architecture-specific
header, so this should work reasonably reliably.

According to the CMake documentation, if we search for the same thing
multiple times, the first successful result will be used; and searching
with NO_DEFAULT_PATH is the official way to prepend things to the
search order.

Resolves: https://gitlab.freedesktop.org/dbus/dbus/-/issues/314
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-03-30 07:36:38 +00:00
Alex Richardson
d35554c111 cmake: Only add warning flags if the compiler supports them
I am compiling for FreeBSD where the compiler is Clang and doesn't accept
all the GCC warning flags. This breaks the -Werror build:
```
error: unknown warning option '-Wduplicated-branches' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wduplicated-cond' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wjump-misses-init' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wlogical-op'; did you mean '-Wlong-long'? [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wrestrict' [-Werror,-Wunknown-warning-option]
error: unknown warning option '-Wunused-but-set-variable'; did you mean '-Wunused-const-variable'? [-Werror,-Wunknown-warning-option]
```

With this change we use check_{c,cxx}_compiler_flag to check if the flag
is supported before adding it. In the future this will allow adding
clang-specific warning flags to the list of warnings as well since they
will be ignored for GCC.
2022-03-29 12:21:46 +00:00
Alex Richardson
3f5bdf0c2d cmake: Detect backtrace() support on platforms such as FreeBSD
On FreeBSD use of backtrace requires linking libexecinfo. The current
check_symbol_exists() will fail due to that missing library. Fortunately,
CMake ships with a FindBacktrace module (at least since 3.0) that can
be used to correctly handle platforms such as FreeBSD (and OpenBSD
according to the FindBacktrace source).
2022-03-24 13:41:19 +00:00
Ralf Habacker
d74fee2727 cmake: setup variable WORDS_BIGENDIAN correctly
Since version 3.0 cmake provides the macro test_big_endian for
that purpose.

fixes #375

Signed-off-by: Ralf Habacker <ralf.habacker@freenet.de>
2022-03-02 14:35:54 +01:00
Simon McVittie
daf362ee61 build: Opt out from using mingw-w64's replacement printf(), etc.
The Windows code in dbus is careful to use Windows-specific equivalents
of the Standard C features that are not implemented by msvcrt.dll, so
we don't need to substitute a Standard C printf implementation.

This avoids compiler warnings/errors when gcc expects us to be using
Microsoft printf syntax (`ms_printf` attribute), but newer versions of
mingw-w64 expect us to be using GNU or Standard C printf syntax
(`gnu_printf` attribute) as a result of `__USE_MINGW_ANSI_STDIO` being
enabled by default if not otherwise specified.

Resolves: https://gitlab.freedesktop.org/dbus/dbus/-/issues/380
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-27 17:05:56 +00:00
Simon McVittie
9d60676ae0 Disable the Containers interface
We've had a request for a 1.14.x stable-branch, but the Containers
interface is only partially implemented, not yet described in the
D-Bus Specification, and not ready to be part of our API guarantees.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-12-10 16:48:51 +00:00
Ralf Habacker
667b153dce cmake: Make gcc builds use similar compiler warnings to autotools
Fixes #356
2021-12-10 13:36:28 +00:00
Ralf Habacker
a2ccc6773b cmake: In generated cmake support files get value for DBus1_INCLUDE_DIRS variable from related cmake target
This allows cmake to construct the resulting (relocatable) runtime paths.

Fixes dbus/dbus#346
2021-11-18 13:51:11 +00:00
Julien Schueller
1b8d560649 CMake: Set IMPORTED_IMPLIB property
Setting this property allows to fix linking to the imported target with MinGW.
This only happens when dbus is built using autotools, when cmake is used the DBus1Config.variant.in
is configured and the automatically exported target by cmake is fine.
2021-11-18 13:20:08 +00:00