📄 mapc.c
字号:
/*- * Copyright (c) 1989 Jan-Simon Pendry * Copyright (c) 1989 Imperial College of Science, Technology & Medicine * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Jan-Simon Pendry at Imperial College, London. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: mapc.c,v 5.2.2.1 1992/02/09 15:08:38 jsp beta $ */#ifndef lintstatic char sccsid[] = "@(#)mapc.c 8.1 (Berkeley) 6/6/93";#endif /* not lint *//* * Mount map cache */#include "am.h"#ifdef HAS_REGEXP#include RE_HDR#endif/* * Hash table size */#define NKVHASH (1 << 2) /* Power of two *//* * Wildcard key */static char wildcard[] = "*";/* * Map cache types * default, none, incremental, all, regexp * MAPC_RE implies MAPC_ALL and must be numerically * greater. */#define MAPC_DFLT 0x000#define MAPC_NONE 0x001#define MAPC_INC 0x002#define MAPC_ROOT 0x004#define MAPC_ALL 0x010#ifdef HAS_REGEXP#define MAPC_RE 0x020#define MAPC_ISRE(m) ((m)->alloc == MAPC_RE)#else#define MAPC_ISRE(m) FALSE#endif#define MAPC_CACHE_MASK 0x0ff#define MAPC_SYNC 0x100static struct opt_tab mapc_opt[] = { { "all", MAPC_ALL }, { "default", MAPC_DFLT }, { "inc", MAPC_INC }, { "mapdefault", MAPC_DFLT }, { "none", MAPC_NONE },#ifdef HAS_REGEXP { "re", MAPC_RE }, { "regexp", MAPC_RE },#endif { "sync", MAPC_SYNC }, { 0, 0 }};/* * Lookup recursion */#define MREC_FULL 2#define MREC_PART 1#define MREC_NONE 0/* * Cache map operations */typedef void add_fn P((mnt_map*, char*, char*));typedef int init_fn P((char*, time_t*));typedef int search_fn P((mnt_map*, char*, char*, char**, time_t*));typedef int reload_fn P((mnt_map*, char*, add_fn*));typedef int mtime_fn P((char*, time_t*));static void mapc_sync P((mnt_map*));/* * Map type */typedef struct map_type map_type;struct map_type { char *name; /* Name of this map type */ init_fn *init; /* Initialisation */ reload_fn *reload; /* Reload or fill */ search_fn *search; /* Search for new entry */ mtime_fn *mtime; /* Find modify time */ int def_alloc; /* Default allocation mode */};/* * Key-value pair */typedef struct kv kv;struct kv { kv *next; char *key; char *val;};struct mnt_map { qelem hdr; int refc; /* Reference count */ short flags; /* Allocation flags */ short alloc; /* Allocation mode */ time_t modify; /* Modify time of map */ char *map_name; /* Name of this map */ char *wildcard; /* Wildcard value */ reload_fn *reload; /* Function to be used for reloads */ search_fn *search; /* Function to be used for searching */ mtime_fn *mtime; /* Modify time function */ kv *kvhash[NKVHASH]; /* Cached data */};/* * Map for root node */static mnt_map *root_map;/* * List of known maps */extern qelem map_list_head;qelem map_list_head = { &map_list_head, &map_list_head };/* * Configuration */ /* ROOT MAP */static int root_init P((char*, time_t*));/* FILE MAPS */#ifdef HAS_FILE_MAPSextern int file_init P((char*, time_t*));extern int file_reload P((mnt_map*, char*, add_fn*));extern int file_search P((mnt_map*, char*, char*, char**, time_t*));extern int file_mtime P((char*, time_t*));#endif /* HAS_FILE_MAPS *//* Network Information Service (NIS) MAPS */#ifdef HAS_NIS_MAPSextern int nis_init P((char*, time_t*));#ifdef HAS_NIS_RELOADextern int nis_reload P((mnt_map*, char*, add_fn*));#else#define nis_reload error_reload#endifextern int nis_search P((mnt_map*, char*, char*, char**, time_t*));#define nis_mtime nis_init#endif /* HAS_NIS_MAPS *//* NDBM MAPS */#ifdef HAS_NDBM_MAPS#ifdef OS_HAS_NDBMextern int ndbm_init P((char*, time_t*));extern int ndbm_search P((mnt_map*, char*, char*, char**, time_t*));#define ndbm_mtime ndbm_init#endif /* OS_HAS_NDBM */#endif /* HAS_NDBM_MAPS *//* PASSWD MAPS */#ifdef HAS_PASSWD_MAPSextern int passwd_init P((char*, time_t*));extern int passwd_search P((mnt_map*, char*, char*, char**, time_t*));#endif /* HAS_PASSWD_MAPS *//* HESIOD MAPS */#ifdef HAS_HESIOD_MAPSextern int hesiod_init P((char*, time_t*));#ifdef HAS_HESIOD_RELOADextern int hesiod_reload P((mnt_map*, char*, add_fn*));#else#define hesiod_reload error_reload#endifextern int hesiod_search P((mnt_map*, char*, char*, char**, time_t*));#endif /* HAS_HESIOD_MAPS *//* UNION MAPS */#ifdef HAS_UNION_MAPSextern int union_init P((char*, time_t*));extern int union_search P((mnt_map*, char*, char*, char**, time_t*));extern int union_reload P((mnt_map*, char*, add_fn*));#endif /* HAS_UNION_MAPS *//* ERROR MAP */static int error_init P((char*, time_t*));static int error_reload P((mnt_map*, char*, add_fn*));static int error_search P((mnt_map*, char*, char*, char**, time_t*));static int error_mtime P((char*, time_t*));static map_type maptypes[] = { { "root", root_init, error_reload, error_search, error_mtime, MAPC_ROOT },#ifdef HAS_PASSWD_MAPS { "passwd", passwd_init, error_reload, passwd_search, error_mtime, MAPC_INC },#endif#ifdef HAS_HESIOD_MAPS { "hesiod", hesiod_init, hesiod_reload, hesiod_search, error_mtime, MAPC_ALL },#endif#ifdef HAS_UNION_MAPS { "union", union_init, union_reload, union_search, error_mtime, MAPC_ALL },#endif#ifdef HAS_NIS_MAPS { "nis", nis_init, nis_reload, nis_search, nis_mtime, MAPC_INC },#endif#ifdef HAS_NDBM_MAPS { "ndbm", ndbm_init, error_reload, ndbm_search, ndbm_mtime, MAPC_INC },#endif#ifdef HAS_FILE_MAPS { "file", file_init, file_reload, file_search, file_mtime, MAPC_ALL },#endif { "error", error_init, error_reload, error_search, error_mtime, MAPC_NONE },};/* * Hash function */static unsigned int kvhash_of P((char *key));static unsigned int kvhash_of(key)char *key;{ unsigned int i, j; for (i = 0; j = *key++; i += j) ; return i % NKVHASH;}void mapc_showtypes P((FILE *fp));void mapc_showtypes(fp)FILE *fp;{ map_type *mt; char *sep = ""; for (mt = maptypes; mt < maptypes+sizeof(maptypes)/sizeof(maptypes[0]); mt++) { fprintf(fp, "%s%s", sep, mt->name); sep = ", "; }}static Const char *reg_error = "?";void regerror P((Const char *m));void regerror(m)Const char *m;{ reg_error = m;}/* * Add key and val to the map m. * key and val are assumed to be safe copies */void mapc_add_kv P((mnt_map *m, char *key, char *val));void mapc_add_kv(m, key, val)mnt_map *m;char *key;char *val;{ kv **h; kv *n; int hash = kvhash_of(key);#ifdef DEBUG dlog("add_kv: %s -> %s", key, val);#endif#ifdef HAS_REGEXP if (MAPC_ISRE(m)) { char keyb[MAXPATHLEN]; regexp *re; /* * Make sure the string is bound to the start and end */ sprintf(keyb, "^%s$", key); re = regcomp(keyb); if (re == 0) { plog(XLOG_USER, "error compiling RE \"%s\": %s", keyb, reg_error); return; } else { free(key); key = (char *) re; } }#endif h = &m->kvhash[hash]; n = ALLOC(kv); n->key = key; n->val = val; n->next = *h; *h = n;}void mapc_repl_kv P((mnt_map *m, char *key, char *val));void mapc_repl_kv(m, key, val)mnt_map *m;char *key;char *val;{ kv *k; /* * Compute the hash table offset */ k = m->kvhash[kvhash_of(key)]; /* * Scan the linked list for the key */ while (k && !FSTREQ(k->key, key)) k = k->next; if (k) { free(k->val); k->val = val; } else { mapc_add_kv(m, key, val); }}/* * Search a map for a key. * Calls map specific search routine. * While map is out of date, keep re-syncing. */static int search_map P((mnt_map *m, char *key, char **valp));static int search_map(m, key, valp)mnt_map *m;char *key;char **valp;{ int rc; do { rc = (*m->search)(m, m->map_name, key, valp, &m->modify); if (rc < 0) { plog(XLOG_MAP, "Re-synchronizing cache for map %s", m->map_name); mapc_sync(m); } } while (rc < 0); return rc;}/* * Do a wildcard lookup in the map and * save the result. */static void mapc_find_wildcard P((mnt_map *m));static void mapc_find_wildcard(m)mnt_map *m;{ /* * Attempt to find the wildcard entry */ int rc = search_map(m, wildcard, &m->wildcard); if (rc != 0) m->wildcard = 0;}/* * Make a duplicate reference to an existing map */#define mapc_dup(m) ((m)->refc++, (m))/* * Do a map reload */static int mapc_reload_map(m)mnt_map *m;{ int error;#ifdef DEBUG dlog("calling map reload on %s", m->map_name);#endif error = (*m->reload)(m, m->map_name, mapc_add_kv); if (error) return error; m->wildcard = 0;#ifdef DEBUG dlog("calling mapc_search for wildcard");#endif error = mapc_search(m, wildcard, &m->wildcard); if (error) m->wildcard = 0; return 0;}/* * Create a new map */static mnt_map *mapc_create P((char *map, char *opt));static mnt_map *mapc_create(map, opt)char *map;char *opt;{ mnt_map *m = ALLOC(mnt_map); map_type *mt; time_t modify; int alloc = 0; (void) cmdoption(opt, mapc_opt, &alloc); for (mt = maptypes; mt < maptypes+sizeof(maptypes)/sizeof(maptypes[0]); mt++) if ((*mt->init)(map, &modify) == 0) break; /* assert: mt in maptypes */ m->flags = alloc & ~MAPC_CACHE_MASK; alloc &= MAPC_CACHE_MASK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -