1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright (c) 2015, Joyent, Inc. All rights reserved. 14 * Copyright 2021 OmniOS Community Edition (OmniOSce) Association. 15 */ 16 17 #include <sys/eventfd.h> 18 #include <sys/stat.h> 19 #include <unistd.h> 20 #include <errno.h> 21 #include <fcntl.h> 22 23 int 24 eventfd(unsigned int initval, int flags) 25 { 26 int oflags = O_RDWR; 27 uint64_t val = initval; 28 int fd; 29 30 if (flags & ~(EFD_NONBLOCK | EFD_CLOEXEC | EFD_SEMAPHORE)) { 31 errno = EINVAL; 32 return (-1); 33 } 34 35 if (flags & EFD_NONBLOCK) 36 oflags |= O_NONBLOCK; 37 38 if (flags & EFD_CLOEXEC) 39 oflags |= O_CLOEXEC; 40 41 if ((fd = open("/dev/eventfd", oflags)) < 0) 42 return (-1); 43 44 if ((flags & EFD_SEMAPHORE) && 45 ioctl(fd, EVENTFDIOC_SEMAPHORE, 0) != 0) { 46 (void) close(fd); 47 return (-1); 48 } 49 50 if (write(fd, &val, sizeof (val)) < sizeof (val)) { 51 (void) close(fd); 52 return (-1); 53 } 54 55 return (fd); 56 } 57 58 int 59 eventfd_read(int fd, eventfd_t *valp) 60 { 61 ssize_t ret = read(fd, valp, sizeof (*valp)); 62 if (ret == -1 || (size_t)ret < sizeof (*valp)) 63 return (-1); 64 return (0); 65 } 66 67 int 68 eventfd_write(int fd, eventfd_t val) 69 { 70 ssize_t ret = write(fd, &val, sizeof (val)); 71 if (ret == -1 || (size_t)ret < sizeof (val)) 72 return (-1); 73 return (0); 74 } 75