summaryrefslogtreecommitdiffstats
path: root/src/libsystemd/sd-device/test-sd-device-thread.c
blob: 539dabd7a6b75d4b2d09bdfcc2def80334d3b72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* 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"
#include "tests.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;
        int r;

        r = sd_device_new_from_syspath(&loopback, "/sys/class/net/lo");
        if (r == -ENODEV)
                return log_tests_skipped("Loopback device not found");
        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;
}