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 */ 15 16 #include <sys/eventfd.h> 17 #include <sys/stat.h> 18 #include <unistd.h> 19 #include <errno.h> 20 #include <fcntl.h> 21 22 int 23 eventfd(unsigned int initval, int flags) 24 { 25 int oflags = O_RDWR; 26 uint64_t val = initval; 27 int fd; 28 29 if (flags & ~(EFD_NONBLOCK | EFD_CLOEXEC | EFD_SEMAPHORE)) { 30 errno = EINVAL; 31 return (-1); 32 } 33 34 if (flags & EFD_NONBLOCK) 35 oflags |= O_NONBLOCK; 36 37 if (flags & EFD_CLOEXEC) 38 oflags |= O_CLOEXEC; 39 40 if ((fd = open("/dev/eventfd", oflags)) < 0) 41 return (-1); 42 43 if ((flags & EFD_SEMAPHORE) && 44 ioctl(fd, EVENTFDIOC_SEMAPHORE, 0) != 0) { 45 (void) close(fd); 46 return (-1); 47 } 48 49 if (write(fd, &val, sizeof (val)) < sizeof (val)) { 50 (void) close(fd); 51 return (-1); 52 } 53 54 return (fd); 55 } 56 57 int 58 eventfd_read(int fd, eventfd_t *valp) 59 { 60 return (read(fd, valp, sizeof (*valp)) < sizeof (*valp) ? -1 : 0); 61 } 62 63 int 64 eventfd_write(int fd, eventfd_t val) 65 { 66 return (write(fd, &val, sizeof (val)) < sizeof (val) ? -1 : 0); 67 } 68