90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * This file and its contents are supplied under the terms of the
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * Common Development and Distribution License ("CDDL"), version 1.0.
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * You may only use this file in accordance with the terms of version
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * 1.0 of the CDDL.
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * A full copy of the text of the CDDL should have accompanied this
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * source. A copy of the CDDL is also available via the Internet at
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * Copyright 2016 Joyent, Inc.
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * Acquire the specified kind of lock with the specified parameters. After
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * acquiring the lock, a byte will be written to stdout. The program will
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * then wait for a byte to be written to stdin before exiting.
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * Usage: <posix|ofd|flock> <shared|exclusive> <path>
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mellostatic void acq_fcntl(int, int, int);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mellostatic void acq_run(int, lock_style_t, boolean_t);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * Acquire the lock, and then try reacquiring it several times. Once we
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * have acquired the lock, trying to acquire it again should succeed,
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * and shouldn't upgrade, downgrade or free the lock.
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello for (i = 0; i < 3; i++) {
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello /* Let the parent know we have the lock and wait */
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello /* Now unlock */
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * Acquire the lock, and then try reacquiring it several times. Once we
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * have acquired the lock, trying to acquire it again should succeed,
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello * and shouldn't upgrade, downgrade or free the lock.
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello for (i = 0; i < 3; i++) {
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello /* Wait to be okayed to unlock */
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello /* Release lock */
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Melloacq_run(int fd, lock_style_t style, boolean_t is_exclusive)
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello acq_fcntl(fd, F_SETLKW, is_exclusive ? F_WRLCK : F_RDLCK);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello acq_fcntl(fd, F_OFD_SETLKW, is_exclusive ? F_WRLCK : F_RDLCK);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello acq_flock(fd, is_exclusive ? LOCK_EX : LOCK_SH);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello errx(EXIT_FAILURE, BAD_ARGS_MESSAGE, argc - 1);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello } else if (strcmp(modestr, "exclusive") == 0) {
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello boolean_t rdonly = style == LSTYLE_FLOCK || !is_exclusive;
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello fd = open(path, rdonly ? O_RDONLY : O_WRONLY);
90221f9148b67fdc90178b67f9600b7bd4e3bc7cCody Peter Mello err(EXIT_FAILURE, "Failed to open %s", path);