summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-device/test-sd-device-thread.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:35:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 15:35:18 +0000
commitb750101eb236130cf056c675997decbac904cc49 (patch)
treea5df1a06754bdd014cb975c051c83b01c9a97532 /src/libsystemd/sd-device/test-sd-device-thread.c
parentInitial commit. (diff)
downloadsystemd-b750101eb236130cf056c675997decbac904cc49.tar.xz
systemd-b750101eb236130cf056c675997decbac904cc49.zip
Adding upstream version 252.22.upstream/252.22
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/libsystemd/sd-device/test-sd-device-thread.c')
-rw-r--r--src/libsystemd/sd-device/test-sd-device-thread.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/libsystemd/sd-device/test-sd-device-thread.c b/src/libsystemd/sd-device/test-sd-device-thread.c
new file mode 100644
index 0000000..644f3c2
--- /dev/null
+++ b/src/libsystemd/sd-device/test-sd-device-thread.c
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "sd-device.h"
+
+#include "device-util.h"
+
+#define handle_error_errno(error, msg) \
+ ({ \
+ errno = abs(error); \
+ perror(msg); \
+ EXIT_FAILURE; \
+ })
+
+static void* thread(void *p) {
+ sd_device **d = p;
+
+ *d = sd_device_unref(*d);
+
+ return NULL;
+}
+
+int main(int argc, char *argv[]) {
+ sd_device *loopback;
+ pthread_t t;
+ const char *key, *value;
+ int r;
+
+ r = sd_device_new_from_syspath(&loopback, "/sys/class/net/lo");
+ if (r < 0)
+ return handle_error_errno(r, "Failed to create loopback device object");
+
+ FOREACH_DEVICE_PROPERTY(loopback, key, value)
+ printf("%s=%s\n", key, value);
+
+ r = pthread_create(&t, NULL, thread, &loopback);
+ if (r != 0)
+ return handle_error_errno(r, "Failed to create thread");
+
+ r = pthread_join(t, NULL);
+ if (r != 0)
+ return handle_error_errno(r, "Failed to wait thread finished");
+
+ if (loopback)
+ return handle_error_errno(r, "loopback device is not unref()ed");
+
+ return 0;
+}