Path: blob/main/usr.sbin/bsdinstall/runconsoles/common.h
106715 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2022 Jessica Clarke <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#define KILL_TIMEOUT 102829/*30* NB: Most of these do not need to be volatile, but a handful are used in31* signal handler contexts, so for simplicity we make them all volatile rather32* than duplicate the implementation.33*/34struct pipe_barrier {35volatile int fds[2];36};3738static __inline int39pipe_barrier_init(struct pipe_barrier *p)40{41int error, fds[2], i;4243error = pipe(fds);44if (error != 0)45return (error);4647for (i = 0; i < 2; ++i)48p->fds[i] = fds[i];4950return (0);51}5253static __inline void54pipe_barrier_wait(struct pipe_barrier *p)55{56ssize_t ret;57char temp;58int fd;5960fd = p->fds[0];61p->fds[0] = -1;62do {63ret = read(fd, &temp, 1);64} while (ret == -1 && errno == EINTR);65close(fd);66}6768static __inline void69pipe_barrier_ready(struct pipe_barrier *p)70{71int fd;7273fd = p->fds[1];74p->fds[1] = -1;75close(fd);76}7778static __inline void79pipe_barrier_destroy_impl(struct pipe_barrier *p, int i)80{81int fd;8283fd = p->fds[i];84if (fd != -1) {85p->fds[i] = -1;86close(fd);87}88}8990static __inline void91pipe_barrier_destroy_wait(struct pipe_barrier *p)92{93pipe_barrier_destroy_impl(p, 0);94}9596static __inline void97pipe_barrier_destroy_ready(struct pipe_barrier *p)98{99pipe_barrier_destroy_impl(p, 1);100}101102static __inline void103pipe_barrier_destroy(struct pipe_barrier *p)104{105pipe_barrier_destroy_wait(p);106pipe_barrier_destroy_ready(p);107}108109void reproduce_signal_death(int sig);110111112