[BACK]Return to res_state.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / resolv

Annotation of src/lib/libc/resolv/res_state.c, Revision 1.7.8.2

1.7.8.2 ! christos    1: /*     $NetBSD: res_state.c,v 1.7.8.1 2009/01/04 17:02:20 christos Exp $       */
        !             2:
        !             3: /*-
        !             4:  * Copyright (c) 2004 The NetBSD Foundation, Inc.
        !             5:  * All rights reserved.
        !             6:  *
        !             7:  * This code is derived from software contributed to The NetBSD Foundation
        !             8:  * by Christos Zoulas.
        !             9:  *
        !            10:  * Redistribution and use in source and binary forms, with or without
        !            11:  * modification, are permitted provided that the following conditions
        !            12:  * are met:
        !            13:  * 1. Redistributions of source code must retain the above copyright
        !            14:  *    notice, this list of conditions and the following disclaimer.
        !            15:  * 2. Redistributions in binary form must reproduce the above copyright
        !            16:  *    notice, this list of conditions and the following disclaimer in the
        !            17:  *    documentation and/or other materials provided with the distribution.
        !            18:  *
        !            19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
        !            20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
        !            21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
        !            22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
        !            23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        !            24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        !            25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        !            26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        !            27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        !            28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        !            29:  * POSSIBILITY OF SUCH DAMAGE.
        !            30:  */
        !            31:
        !            32: #include <sys/cdefs.h>
        !            33: #if defined(LIBC_SCCS) && !defined(lint)
        !            34: __RCSID("$NetBSD: res_state.c,v 1.7.8.1 2009/01/04 17:02:20 christos Exp $");
        !            35: #endif
        !            36:
        !            37: #include "namespace.h"
        !            38: #include "reentrant.h"
        !            39:
        !            40: #include <sys/types.h>
        !            41: #include <sys/stat.h>
        !            42: #include <sys/time.h>
        !            43: #include <arpa/inet.h>
        !            44: #include <arpa/nameser.h>
        !            45: #include <string.h>
        !            46: #include <stdlib.h>
        !            47: #include <netdb.h>
        !            48: #include <resolv.h>
        !            49:
        !            50: struct __res_state _nres
        !            51: # if defined(__BIND_RES_TEXT)
        !            52:        = { .retrans = RES_TIMEOUT, }   /*%< Motorola, et al. */
        !            53: # endif
        !            54:        ;
        !            55:
        !            56: res_state __res_get_state_nothread(void);
        !            57: void __res_put_state_nothread(res_state);
        !            58:
        !            59: #ifdef __weak_alias
        !            60: __weak_alias(__res_get_state, __res_get_state_nothread)
        !            61: __weak_alias(__res_put_state, __res_put_state_nothread)
        !            62: /* Source compatibility; only for single threaded programs */
        !            63: __weak_alias(__res_state, __res_get_state_nothread)
        !            64: __weak_alias(res_watch, _res_watch)
        !            65: #endif
        !            66:
        !            67: static int check;
        !            68: static struct timespec mtime;
        !            69: static char seq;
        !            70: static mutex_t check_mutex = MUTEX_INITIALIZER;
        !            71:
        !            72: static int
        !            73: checktime(void)
        !            74: {
        !            75:        struct stat st;
        !            76:
        !            77:        mutex_lock(&check_mutex);
        !            78:        if (stat(_PATH_RESCONF, &st) == -1)
        !            79:                return 0;
        !            80:
        !            81:        if (memcmp(&mtime, &st.st_mtimespec, sizeof(mtime)) != 0) {
        !            82:                mtime = st.st_mtimespec;
        !            83:                seq++;
        !            84:                return 1;
        !            85:        }
        !            86:        mutex_unlock(&check_mutex);
        !            87:
        !            88:        return 0;
        !            89: }
        !            90:
        !            91: res_state
        !            92: __res_check(res_state r)
        !            93: {
        !            94:        int uninit = (r->options & RES_INIT) == 0;
        !            95:
        !            96:        if (check == 0)
        !            97:                check = getenv("RES_CHECK") != NULL;
        !            98:
        !            99:        if (check && mtime.tv_sec == 0)
        !           100:                checktime();
        !           101:
        !           102:        if (uninit || (check && (r->seq != seq || checktime()))) {
        !           103:                if (!uninit)
        !           104:                        res_ndestroy(r);
        !           105:                if (res_ninit(r) == -1) {
        !           106:                        h_errno = NETDB_INTERNAL;
        !           107:                        return NULL;
        !           108:                }
        !           109:                r->seq = seq;
        !           110:        }
        !           111:        return r;
        !           112: }
        !           113:
        !           114: void
        !           115: res_watch(int onoff)
        !           116: {
        !           117:        check = onoff;
        !           118: }
        !           119:
        !           120: res_state
        !           121: __res_get_state_nothread(void)
        !           122: {
        !           123:        return __res_check(&_nres);
        !           124: }
        !           125:
        !           126: void
        !           127: /*ARGSUSED*/
        !           128: __res_put_state_nothread(res_state res)
        !           129: {
        !           130: }

CVSweb <webmaster@jp.NetBSD.org>