xref: /freebsd/contrib/libfido2/src/hid_unix.c (revision 2ccfa855)
10afa8e06SEd Maste /*
20afa8e06SEd Maste  * Copyright (c) 2020 Yubico AB. All rights reserved.
30afa8e06SEd Maste  * Use of this source code is governed by a BSD-style
40afa8e06SEd Maste  * license that can be found in the LICENSE file.
5*2ccfa855SEd Maste  * SPDX-License-Identifier: BSD-2-Clause
60afa8e06SEd Maste  */
70afa8e06SEd Maste 
80afa8e06SEd Maste #include <sys/stat.h>
90afa8e06SEd Maste 
100afa8e06SEd Maste #include <errno.h>
110afa8e06SEd Maste #include <fcntl.h>
120afa8e06SEd Maste #include <poll.h>
130afa8e06SEd Maste #include <unistd.h>
140afa8e06SEd Maste 
150afa8e06SEd Maste #include "fido.h"
160afa8e06SEd Maste 
170afa8e06SEd Maste #ifdef __NetBSD__
180afa8e06SEd Maste #define	ppoll	pollts
190afa8e06SEd Maste #endif
200afa8e06SEd Maste 
210afa8e06SEd Maste int
fido_hid_unix_open(const char * path)220afa8e06SEd Maste fido_hid_unix_open(const char *path)
230afa8e06SEd Maste {
240afa8e06SEd Maste 	int fd;
250afa8e06SEd Maste 	struct stat st;
260afa8e06SEd Maste 
270afa8e06SEd Maste 	if ((fd = open(path, O_RDWR)) == -1) {
280afa8e06SEd Maste 		if (errno != ENOENT && errno != ENXIO)
290afa8e06SEd Maste 			fido_log_error(errno, "%s: open %s", __func__, path);
300afa8e06SEd Maste 		return (-1);
310afa8e06SEd Maste 	}
320afa8e06SEd Maste 
330afa8e06SEd Maste 	if (fstat(fd, &st) == -1) {
340afa8e06SEd Maste 		fido_log_error(errno, "%s: fstat %s", __func__, path);
350afa8e06SEd Maste 		if (close(fd) == -1)
360afa8e06SEd Maste 			fido_log_error(errno, "%s: close", __func__);
370afa8e06SEd Maste 		return (-1);
380afa8e06SEd Maste 	}
390afa8e06SEd Maste 
400afa8e06SEd Maste 	if (S_ISCHR(st.st_mode) == 0) {
410afa8e06SEd Maste 		fido_log_debug("%s: S_ISCHR %s", __func__, path);
420afa8e06SEd Maste 		if (close(fd) == -1)
430afa8e06SEd Maste 			fido_log_error(errno, "%s: close", __func__);
440afa8e06SEd Maste 		return (-1);
450afa8e06SEd Maste 	}
460afa8e06SEd Maste 
470afa8e06SEd Maste 	return (fd);
480afa8e06SEd Maste }
490afa8e06SEd Maste 
500afa8e06SEd Maste int
fido_hid_unix_wait(int fd,int ms,const fido_sigset_t * sigmask)510afa8e06SEd Maste fido_hid_unix_wait(int fd, int ms, const fido_sigset_t *sigmask)
520afa8e06SEd Maste {
530afa8e06SEd Maste 	struct timespec ts;
540afa8e06SEd Maste 	struct pollfd pfd;
550afa8e06SEd Maste 	int r;
560afa8e06SEd Maste 
570afa8e06SEd Maste 	memset(&pfd, 0, sizeof(pfd));
580afa8e06SEd Maste 	pfd.events = POLLIN;
590afa8e06SEd Maste 	pfd.fd = fd;
600afa8e06SEd Maste 
610afa8e06SEd Maste #ifdef FIDO_FUZZ
620afa8e06SEd Maste 	return (0);
630afa8e06SEd Maste #endif
640afa8e06SEd Maste 	if (ms > -1) {
650afa8e06SEd Maste 		ts.tv_sec = ms / 1000;
660afa8e06SEd Maste 		ts.tv_nsec = (ms % 1000) * 1000000;
670afa8e06SEd Maste 	}
680afa8e06SEd Maste 
690afa8e06SEd Maste 	if ((r = ppoll(&pfd, 1, ms > -1 ? &ts : NULL, sigmask)) < 1) {
700afa8e06SEd Maste 		if (r == -1)
710afa8e06SEd Maste 			fido_log_error(errno, "%s: ppoll", __func__);
720afa8e06SEd Maste 		return (-1);
730afa8e06SEd Maste 	}
740afa8e06SEd Maste 
750afa8e06SEd Maste 	return (0);
760afa8e06SEd Maste }
77