Coverage Report

Created: 2021-03-26 11:35

/libfido2/src/hid_unix.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020 Yubico AB. All rights reserved.
3
 * Use of this source code is governed by a BSD-style
4
 * license that can be found in the LICENSE file.
5
 */
6
7
#include <sys/stat.h>
8
9
#include <errno.h>
10
#include <fcntl.h>
11
#include <poll.h>
12
#include <unistd.h>
13
14
#include "fido.h"
15
16
#ifdef __NetBSD__
17
#define ppoll   pollts
18
#endif
19
20
int
21
fido_hid_unix_open(const char *path)
22
0
{
23
0
        int fd;
24
0
        struct stat st;
25
0
26
0
        if ((fd = open(path, O_RDWR)) == -1) {
27
0
                if (errno != ENOENT && errno != ENXIO)
28
0
                        fido_log_error(errno, "%s: open %s", __func__, path);
29
0
                return (-1);
30
0
        }
31
0
32
0
        if (fstat(fd, &st) == -1) {
33
0
                fido_log_error(errno, "%s: fstat %s", __func__, path);
34
0
                if (close(fd) == -1)
35
0
                        fido_log_error(errno, "%s: close", __func__);
36
0
                return (-1);
37
0
        }
38
0
39
0
        if (S_ISCHR(st.st_mode) == 0) {
40
0
                fido_log_debug("%s: S_ISCHR %s", __func__, path);
41
0
                if (close(fd) == -1)
42
0
                        fido_log_error(errno, "%s: close", __func__);
43
0
                return (-1);
44
0
        }
45
0
46
0
        return (fd);
47
0
}
48
49
int
50
fido_hid_unix_wait(int fd, int ms, const fido_sigset_t *sigmask)
51
1.88k
{
52
1.88k
        struct timespec ts;
53
1.88k
        struct pollfd pfd;
54
1.88k
        int r;
55
1.88k
56
1.88k
        memset(&pfd, 0, sizeof(pfd));
57
1.88k
        pfd.events = POLLIN;
58
1.88k
        pfd.fd = fd;
59
1.88k
60
1.88k
#ifdef FIDO_FUZZ
61
1.88k
        if (ms < 0)
62
1.88k
                return (0);
63
0
#endif
64
0
        if (ms > -1) {
65
0
                ts.tv_sec = ms / 1000;
66
0
                ts.tv_nsec = (ms % 1000) * 1000000;
67
0
        }
68
0
69
0
        if ((r = ppoll(&pfd, 1, ms > -1 ? &ts : NULL, sigmask)) < 1) {
70
0
                if (r == -1)
71
0
                        fido_log_error(errno, "%s: ppoll", __func__);
72
0
                return (-1);
73
0
        }
74
0
75
0
        return (0);
76
0
}