summaryrefslogtreecommitdiffstats
path: root/src/hid_unix.c
blob: e53882d79e86a6779be4e45264d448603d93e0c1 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * Copyright (c) 2020 Yubico AB. All rights reserved.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the LICENSE file.
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <sys/stat.h>

#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>

#include "fido.h"

#ifdef __NetBSD__
#define	ppoll	pollts
#endif

int
fido_hid_unix_open(const char *path)
{
	int fd;
	struct stat st;

	if ((fd = open(path, O_RDWR)) == -1) {
		if (errno != ENOENT && errno != ENXIO)
			fido_log_error(errno, "%s: open %s", __func__, path);
		return (-1);
	}

	if (fstat(fd, &st) == -1) {
		fido_log_error(errno, "%s: fstat %s", __func__, path);
		if (close(fd) == -1)
			fido_log_error(errno, "%s: close", __func__);
		return (-1);
	}

	if (S_ISCHR(st.st_mode) == 0) {
		fido_log_debug("%s: S_ISCHR %s", __func__, path);
		if (close(fd) == -1)
			fido_log_error(errno, "%s: close", __func__);
		return (-1);
	}

	return (fd);
}

int
fido_hid_unix_wait(int fd, int ms, const fido_sigset_t *sigmask)
{
	struct timespec ts;
	struct pollfd pfd;
	int r;

	memset(&pfd, 0, sizeof(pfd));
	pfd.events = POLLIN;
	pfd.fd = fd;

#ifdef FIDO_FUZZ
	return (0);
#endif
	if (ms > -1) {
		ts.tv_sec = ms / 1000;
		ts.tv_nsec = (ms % 1000) * 1000000;
	}

	if ((r = ppoll(&pfd, 1, ms > -1 ? &ts : NULL, sigmask)) < 1) {
		if (r == -1)
			fido_log_error(errno, "%s: ppoll", __func__);
		return (-1);
	}

	return (0);
}