Blame src/syscall-clamp.c

Packit fc043f
/* syscall-clamp.c - Syscall clamp related stuff
Packit fc043f
 * Copyright (C) 2016, 2017 g10 Code GmbH
Packit fc043f
 *
Packit fc043f
 * This file is part of Libgpg-error.
Packit fc043f
 *
Packit fc043f
 * Libgpg-error is free software; you can redistribute it and/or
Packit fc043f
 * modify it under the terms of the GNU Lesser General Public License
Packit fc043f
 * as published by the Free Software Foundation; either version 2.1 of
Packit fc043f
 * the License, or (at your option) any later version.
Packit fc043f
 *
Packit fc043f
 * Libgpg-error is distributed in the hope that it will be useful, but
Packit fc043f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit fc043f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit fc043f
 * Lesser General Public License for more details.
Packit fc043f
 *
Packit fc043f
 * You should have received a copy of the GNU Lesser General Public
Packit fc043f
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit fc043f
 * SPDX-License-Identifier: LGPL-2.1+
Packit fc043f
 */
Packit fc043f
Packit fc043f
#include <config.h>
Packit fc043f
Packit fc043f
#include "gpgrt-int.h"
Packit fc043f
Packit fc043f
/*
Packit fc043f
 * Functions called before and after blocking syscalls.
Packit fc043f
 * gpgrt_set_syscall_clamp is used to set them.
Packit fc043f
 */
Packit fc043f
static void (*pre_syscall_func)(void);
Packit fc043f
static void (*post_syscall_func)(void);
Packit fc043f
Packit fc043f
Packit fc043f
/*
Packit fc043f
 * Register the syscall clamp.  These two functions are called
Packit fc043f
 * immediately before and after a possible blocking system call.  This
Packit fc043f
 * should be used before any I/O happens.  The function is commonly
Packit fc043f
 * used with the nPth library:
Packit fc043f
 *
Packit fc043f
 *    gpgrt_set_syscall_clamp (npth_unprotect, npth_protect);
Packit fc043f
 *
Packit fc043f
 * These functions may not modify ERRNO.
Packit fc043f
 *
Packit fc043f
 * Setting the clamp is not thread-safe and should thus be done as
Packit fc043f
 * early as possible.
Packit fc043f
 */
Packit fc043f
void
Packit fc043f
_gpgrt_set_syscall_clamp (void (*pre)(void), void (*post)(void))
Packit fc043f
{
Packit fc043f
  pre_syscall_func = pre;
Packit fc043f
  post_syscall_func = post;
Packit fc043f
}
Packit fc043f
Packit fc043f
/*
Packit fc043f
 * Return the current sycall clamp functions.  This can be used by
Packit fc043f
 * other libraries which have blocking functions.
Packit fc043f
 */
Packit fc043f
void
Packit fc043f
_gpgrt_get_syscall_clamp (void (**r_pre)(void), void (**r_post)(void))
Packit fc043f
{
Packit fc043f
  *r_pre  = pre_syscall_func;
Packit fc043f
  *r_post = post_syscall_func;
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
/* Call this function before a blocking system or libc call.  */
Packit fc043f
void
Packit fc043f
_gpgrt_pre_syscall (void)
Packit fc043f
{
Packit fc043f
  if (pre_syscall_func)
Packit fc043f
    pre_syscall_func ();
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
/* Call this function after a blocking system or libc call.  */
Packit fc043f
void
Packit fc043f
_gpgrt_post_syscall (void)
Packit fc043f
{
Packit fc043f
  if (post_syscall_func)
Packit fc043f
    post_syscall_func ();
Packit fc043f
}