source: rtems-libbsd/freebsd/lib/libc/resolv/res_state.c

6-freebsd-12
Last change on this file was bb80d9d, checked in by Sebastian Huber <sebastian.huber@…>, on 08/09/18 at 12:02:09

Update to FreeBSD head 2017-12-01

Git mirror commit e724f51f811a4b2bd29447f8b85ab5c2f9b88266.

Update #3472.

  • Property mode set to 100644
File size: 3.3 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*-
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 2006 The FreeBSD Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/time.h>
35#include <netinet/in.h>
36#include <arpa/nameser.h>
37#include <resolv.h>
38#include <stdlib.h>
39
40#include "namespace.h"
41#include "reentrant.h"
42#include "un-namespace.h"
43
44#include "res_private.h"
45
46#undef _res
47
48struct __res_state _res;
49
50static thread_key_t res_key;
51static once_t res_init_once = ONCE_INITIALIZER;
52static int res_thr_keycreated = 0;
53
54static void
55free_res(void *ptr)
56{
57        res_state statp = ptr;
58
59        if (statp->_u._ext.ext != NULL)
60                res_ndestroy(statp);
61        free(statp);
62}
63
64static void
65res_keycreate(void)
66{
67        res_thr_keycreated = thr_keycreate(&res_key, free_res) == 0;
68}
69
70static res_state
71res_check_reload(res_state statp)
72{
73        struct timespec now;
74        struct stat sb;
75        struct __res_state_ext *ext;
76
77        if ((statp->options & RES_INIT) == 0) {
78                return (statp);
79        }
80
81        ext = statp->_u._ext.ext;
82        if (ext == NULL || ext->reload_period == 0) {
83                return (statp);
84        }
85
86        if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) != 0 ||
87            (now.tv_sec - ext->conf_stat) < ext->reload_period) {
88                return (statp);
89        }
90
91        ext->conf_stat = now.tv_sec;
92        if (stat(_PATH_RESCONF, &sb) == 0 &&
93            (sb.st_mtim.tv_sec  != ext->conf_mtim.tv_sec ||
94             sb.st_mtim.tv_nsec != ext->conf_mtim.tv_nsec)) {
95                statp->options &= ~RES_INIT;
96        }
97
98        return (statp);
99}
100
101res_state
102__res_state(void)
103{
104        res_state statp;
105
106#ifndef __rtems__
107        if (thr_main() != 0)
108                return res_check_reload(&_res);
109#endif /* __rtems__ */
110
111        if (thr_once(&res_init_once, res_keycreate) != 0 ||
112            !res_thr_keycreated)
113                return (&_res);
114
115        statp = thr_getspecific(res_key);
116        if (statp != NULL)
117                return res_check_reload(statp);
118        statp = calloc(1, sizeof(*statp));
119        if (statp == NULL)
120                return (&_res);
121#ifdef __BIND_RES_TEXT
122        statp->options = RES_TIMEOUT;                   /* Motorola, et al. */
123#endif
124        if (thr_setspecific(res_key, statp) == 0)
125                return (statp);
126        free(statp);
127        return (&_res);
128}
Note: See TracBrowser for help on using the repository browser.