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 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <sys/stat.h> |
9 | | |
10 | | #include <errno.h> |
11 | | #include <fcntl.h> |
12 | | #include <poll.h> |
13 | | #include <unistd.h> |
14 | | |
15 | | #include "fido.h" |
16 | | |
17 | | #ifdef __NetBSD__ |
18 | | #define ppoll pollts |
19 | | #endif |
20 | | |
21 | | int |
22 | | fido_hid_unix_open(const char *path) |
23 | 484k | { |
24 | 484k | int fd; |
25 | 484k | struct stat st; |
26 | | |
27 | 484k | if ((fd = open(path, O_RDWR)) == -1) { |
28 | 0 | if (errno != ENOENT && errno != ENXIO) |
29 | 0 | fido_log_error(errno, "%s: open %s", __func__, path); |
30 | 0 | return (-1); |
31 | 0 | } |
32 | | |
33 | 484k | if (fstat(fd, &st) == -1) { |
34 | 0 | fido_log_error(errno, "%s: fstat %s", __func__, path); |
35 | 0 | if (close(fd) == -1) |
36 | 0 | fido_log_error(errno, "%s: close", __func__); |
37 | 0 | return (-1); |
38 | 0 | } |
39 | | |
40 | 484k | if (S_ISCHR(st.st_mode) == 0) { |
41 | 0 | fido_log_debug("%s: S_ISCHR %s", __func__, path); |
42 | 0 | if (close(fd) == -1) |
43 | 0 | fido_log_error(errno, "%s: close", __func__); |
44 | 0 | return (-1); |
45 | 0 | } |
46 | | |
47 | 484k | return (fd); |
48 | 484k | } |
49 | | |
50 | | int |
51 | | fido_hid_unix_wait(int fd, int ms, const fido_sigset_t *sigmask) |
52 | 539k | { |
53 | 539k | struct timespec ts; |
54 | 539k | struct pollfd pfd; |
55 | 539k | int r; |
56 | | |
57 | 539k | memset(&pfd, 0, sizeof(pfd)); |
58 | 539k | pfd.events = POLLIN; |
59 | 539k | pfd.fd = fd; |
60 | | |
61 | 539k | #ifdef FIDO_FUZZ |
62 | 539k | 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 | |
|
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 | | |
75 | 0 | return (0); |
76 | 0 | } |