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
thread(void * p)19 static void* thread(void *p) {
20 sd_device **d = p;
21
22 *d = sd_device_unref(*d);
23
24 return NULL;
25 }
26
main(int argc,char * argv[])27 int main(int argc, char *argv[]) {
28 sd_device *loopback;
29 pthread_t t;
30 const char *key, *value;
31 int r;
32
33 r = sd_device_new_from_syspath(&loopback, "/sys/class/net/lo");
34 if (r < 0)
35 return handle_error_errno(r, "Failed to create loopback device object");
36
37 FOREACH_DEVICE_PROPERTY(loopback, key, value)
38 printf("%s=%s\n", key, value);
39
40 r = pthread_create(&t, NULL, thread, &loopback);
41 if (r != 0)
42 return handle_error_errno(r, "Failed to create thread");
43
44 r = pthread_join(t, NULL);
45 if (r != 0)
46 return handle_error_errno(r, "Failed to wait thread finished");
47
48 if (loopback)
49 return handle_error_errno(r, "loopback device is not unref()ed");
50
51 return 0;
52 }
53