]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-device/test-sd-device-thread.c
device-util: Declare iterator variables inline
[thirdparty/systemd.git] / src / libsystemd / sd-device / test-sd-device-thread.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <pthread.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include "sd-device.h"
9
10 #include "device-util.h"
11
12 #define handle_error_errno(error, msg) \
13 ({ \
14 errno = abs(error); \
15 perror(msg); \
16 EXIT_FAILURE; \
17 })
18
19 static void* thread(void *p) {
20 sd_device **d = p;
21
22 *d = sd_device_unref(*d);
23
24 return NULL;
25 }
26
27 int main(int argc, char *argv[]) {
28 sd_device *loopback;
29 pthread_t t;
30 int r;
31
32 r = sd_device_new_from_syspath(&loopback, "/sys/class/net/lo");
33 if (r < 0)
34 return handle_error_errno(r, "Failed to create loopback device object");
35
36 FOREACH_DEVICE_PROPERTY(loopback, key, value)
37 printf("%s=%s\n", key, value);
38
39 r = pthread_create(&t, NULL, thread, &loopback);
40 if (r != 0)
41 return handle_error_errno(r, "Failed to create thread");
42
43 r = pthread_join(t, NULL);
44 if (r != 0)
45 return handle_error_errno(r, "Failed to wait thread finished");
46
47 if (loopback)
48 return handle_error_errno(r, "loopback device is not unref()ed");
49
50 return 0;
51 }