19851b340SKonstantin Belousov /*- 29851b340SKonstantin Belousov * Copyright (c) 2017 Juniper Networks. All rights reserved. 39851b340SKonstantin Belousov * 49851b340SKonstantin Belousov * Redistribution and use in source and binary forms, with or without 59851b340SKonstantin Belousov * modification, are permitted provided that the following conditions 69851b340SKonstantin Belousov * are met: 79851b340SKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 89851b340SKonstantin Belousov * notice, this list of conditions and the following disclaimer. 99851b340SKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 109851b340SKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 119851b340SKonstantin Belousov * documentation and/or other materials provided with the distribution. 129851b340SKonstantin Belousov * 139851b340SKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 149851b340SKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 159851b340SKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 169851b340SKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 179851b340SKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 189851b340SKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 199851b340SKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 209851b340SKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 219851b340SKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 229851b340SKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 239851b340SKonstantin Belousov * SUCH DAMAGE. 249851b340SKonstantin Belousov */ 259851b340SKonstantin Belousov 269851b340SKonstantin Belousov #include <sys/cdefs.h> 279851b340SKonstantin Belousov __FBSDID("$FreeBSD$"); 289851b340SKonstantin Belousov 299851b340SKonstantin Belousov #include "namespace.h" 309851b340SKonstantin Belousov #include <sys/types.h> 319851b340SKonstantin Belousov #include <machine/atomic.h> 329851b340SKonstantin Belousov #include <errno.h> 339851b340SKonstantin Belousov #include <pthread.h> 349851b340SKonstantin Belousov #include <stddef.h> 359851b340SKonstantin Belousov #include <stdlib.h> 36*25b73e63SKonstantin Belousov #include <string.h> 37*25b73e63SKonstantin Belousov #include <unistd.h> 389851b340SKonstantin Belousov #include "un-namespace.h" 399851b340SKonstantin Belousov #include "libc_private.h" 409851b340SKonstantin Belousov 419851b340SKonstantin Belousov /* 429851b340SKonstantin Belousov * Rationale recommends allocating new memory each time. 439851b340SKonstantin Belousov */ 449851b340SKonstantin Belousov static constraint_handler_t *_ch = NULL; 459851b340SKonstantin Belousov static pthread_mutex_t ch_lock = PTHREAD_MUTEX_INITIALIZER; 469851b340SKonstantin Belousov 479851b340SKonstantin Belousov constraint_handler_t 489851b340SKonstantin Belousov set_constraint_handler_s(constraint_handler_t handler) 499851b340SKonstantin Belousov { 509851b340SKonstantin Belousov constraint_handler_t *new, *old, ret; 519851b340SKonstantin Belousov 529851b340SKonstantin Belousov new = malloc(sizeof(constraint_handler_t)); 539851b340SKonstantin Belousov if (new == NULL) 549851b340SKonstantin Belousov return (NULL); 559851b340SKonstantin Belousov *new = handler; 569851b340SKonstantin Belousov if (__isthreaded) 579851b340SKonstantin Belousov _pthread_mutex_lock(&ch_lock); 589851b340SKonstantin Belousov old = _ch; 599851b340SKonstantin Belousov _ch = new; 609851b340SKonstantin Belousov if (__isthreaded) 619851b340SKonstantin Belousov _pthread_mutex_unlock(&ch_lock); 629851b340SKonstantin Belousov if (old == NULL) { 639851b340SKonstantin Belousov ret = NULL; 649851b340SKonstantin Belousov } else { 659851b340SKonstantin Belousov ret = *old; 669851b340SKonstantin Belousov free(old); 679851b340SKonstantin Belousov } 689851b340SKonstantin Belousov return (ret); 699851b340SKonstantin Belousov } 709851b340SKonstantin Belousov 719851b340SKonstantin Belousov void 729851b340SKonstantin Belousov __throw_constraint_handler_s(const char * restrict msg, errno_t error) 739851b340SKonstantin Belousov { 749851b340SKonstantin Belousov constraint_handler_t ch; 759851b340SKonstantin Belousov 769851b340SKonstantin Belousov if (__isthreaded) 779851b340SKonstantin Belousov _pthread_mutex_lock(&ch_lock); 789851b340SKonstantin Belousov ch = _ch != NULL ? *_ch : NULL; 799851b340SKonstantin Belousov if (__isthreaded) 809851b340SKonstantin Belousov _pthread_mutex_unlock(&ch_lock); 819851b340SKonstantin Belousov if (ch != NULL) 829851b340SKonstantin Belousov ch(msg, NULL, error); 839851b340SKonstantin Belousov } 849851b340SKonstantin Belousov 859851b340SKonstantin Belousov void 86*25b73e63SKonstantin Belousov abort_handler_s(const char * restrict msg, void * restrict ptr __unused, 87*25b73e63SKonstantin Belousov errno_t error __unused) 889851b340SKonstantin Belousov { 89*25b73e63SKonstantin Belousov static const char ahs[] = "abort_handler_s : "; 909851b340SKonstantin Belousov 91*25b73e63SKonstantin Belousov (void) _write(STDERR_FILENO, ahs, sizeof(ahs) - 1); 92*25b73e63SKonstantin Belousov (void) _write(STDERR_FILENO, msg, strlen(msg)); 93*25b73e63SKonstantin Belousov (void) _write(STDERR_FILENO, "\n", 1); 949851b340SKonstantin Belousov abort(); 959851b340SKonstantin Belousov } 969851b340SKonstantin Belousov 979851b340SKonstantin Belousov void 989851b340SKonstantin Belousov ignore_handler_s(const char * restrict msg __unused, 999851b340SKonstantin Belousov void * restrict ptr __unused, errno_t error __unused) 1009851b340SKonstantin Belousov { 1019851b340SKonstantin Belousov } 102