Coverage Report

Created: 2022-07-22 12:05

/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
439k
{
23
439k
        int fd;
24
439k
        struct stat st;
25
26
439k
        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
32
439k
        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
39
439k
        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
46
439k
        return (fd);
47
439k
}
48
49
int
50
fido_hid_unix_wait(int fd, int ms, const fido_sigset_t *sigmask)
51
444k
{
52
444k
        struct timespec ts;
53
444k
        struct pollfd pfd;
54
444k
        int r;
55
56
444k
        memset(&pfd, 0, sizeof(pfd));
57
444k
        pfd.events = POLLIN;
58
444k
        pfd.fd = fd;
59
60
444k
#ifdef FIDO_FUZZ
61
444k
        return (0);
62
0
#endif
63
0
        if (ms > -1) {
64
0
                ts.tv_sec = ms / 1000;
65
0
                ts.tv_nsec = (ms % 1000) * 1000000;
66
0
        }
67
68
0
        if ((r = ppoll(&pfd, 1, ms > -1 ? &ts : NULL, sigmask)) < 1) {
69
0
                if (r == -1)
70
0
                        fido_log_error(errno, "%s: ppoll", __func__);
71
0
                return (-1);
72
0
        }
73
74
0
        return (0);
75
0
}