2 * Copyright (c) 1998-2001, 2003 Sendmail, Inc. and its suppliers.
4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved.
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * By using this file, you agree to the terms and conditions set
9 * forth in the LICENSE file which can be found at the top level of
10 * the sendmail distribution.
16 SM_RCSID("@(#)$Id: stab.c,v 8.86.4.1 2003/03/31 17:44:24 ca Exp $")
19 ** STAB -- manage the symbol table
22 ** name -- the name to be looked up or inserted.
23 ** type -- the type of symbol.
25 ** ST_ENTER -- enter the name if not already present.
26 ** ST_FIND -- find it only.
29 ** pointer to a STAB entry for this name.
30 ** NULL if not found and not entered.
33 ** can update the symbol table.
37 #define SM_LOWER(c) ((isascii(c) && isupper(c)) ? tolower(c) : (c))
39 static STAB *SymTab[STABSIZE];
54 sm_dprintf("STAB: %s %d ", name, type);
57 ** Compute the hashing function
61 for (p = name; *p != '\0'; p++)
62 hfunc = ((hfunc << 1) ^ (SM_LOWER(*p) & 0377)) % STABSIZE;
65 sm_dprintf("(hfunc=%d) ", hfunc);
68 if (type == ST_MACRO || type == ST_RULESET)
70 while ((s = *ps) != NULL &&
71 (s->s_symtype != type || strcmp(name, s->s_name)))
76 while ((s = *ps) != NULL &&
77 (s->s_symtype != type || sm_strcasecmp(name, s->s_name)))
82 ** Dispose of the entry.
85 if (s != NULL || op == ST_FIND)
90 sm_dprintf("not found\n");
93 long *lp = (long *) s->s_class;
95 sm_dprintf("type %d val %lx %lx %lx %lx\n",
96 s->s_symtype, lp[0], lp[1], lp[2], lp[3]);
103 ** Make a new entry and link it in.
107 sm_dprintf("entered\n");
109 /* determine size of new entry */
113 len = sizeof s->s_class;
117 len = sizeof s->s_address;
121 len = sizeof s->s_mailer;
125 len = sizeof s->s_alias;
129 len = sizeof s->s_mapclass;
133 len = sizeof s->s_map;
137 len = sizeof s->s_hostsig;
141 len = sizeof s->s_namecanon;
145 len = sizeof s->s_macro;
149 len = sizeof s->s_ruleset;
153 len = sizeof s->s_header;
157 len = sizeof s->s_service;
162 len = sizeof s->s_lmap;
168 len = sizeof s->s_milter;
173 len = sizeof s->s_quegrp;
178 ** Each mailer has its own MCI stab entry:
180 ** s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
182 ** Therefore, anything ST_MCI or larger is an s_mci.
186 len = sizeof s->s_mci;
189 syserr("stab: unknown symbol type %d", type);
190 len = sizeof s->s_value;
194 len += sizeof *s - sizeof s->s_value;
197 sm_dprintf("size of stab entry: %d\n", len);
200 s = (STAB *) sm_pmalloc_x(len);
201 memset((char *) s, '\0', len);
202 s->s_name = sm_pstrdup_x(name);
208 /* set a default value for rulesets */
209 if (type == ST_RULESET)
215 ** STABAPPLY -- apply function to all stab entries
218 ** func -- the function to apply. It will be given two
219 ** parameters (the stab entry and the arg).
220 ** arg -- an arbitrary argument, passed to func.
228 void (*func)__P((STAB *, int));
231 register STAB **shead;
234 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
236 for (s = *shead; s != NULL; s = s->s_next)
239 sm_dprintf("stabapply: trying %d/%s\n",
240 s->s_symtype, s->s_name);
246 ** QUEUEUP_MACROS -- queueup the macros in a class
248 ** Write the macros listed in the specified class into the
249 ** file referenced by qfp.
252 ** class -- class ID.
253 ** qfp -- file pointer to the queue file.
254 ** e -- the envelope.
261 queueup_macros(class, qfp, e)
266 register STAB **shead;
272 class = bitidx(class);
273 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
275 for (s = *shead; s != NULL; s = s->s_next)
280 if (s->s_symtype == ST_CLASS &&
281 bitnset(bitidx(class), s->s_class) &&
282 (m = macid(s->s_name)) != 0 &&
283 (p = macvalue(m, e)) != NULL)
285 (void) sm_io_fprintf(qfp, SM_TIME_DEFAULT,
295 ** COPY_CLASS -- copy class members from one class to another
298 ** src -- source class.
299 ** dst -- destination class.
310 register STAB **shead;
315 for (shead = SymTab; shead < &SymTab[STABSIZE]; shead++)
317 for (s = *shead; s != NULL; s = s->s_next)
319 if (s->s_symtype == ST_CLASS &&
320 bitnset(src, s->s_class))
321 setbitn(dst, s->s_class);
327 ** RMEXPSTAB -- remove expired entries from SymTab.
329 ** These entries need to be removed in long-running processes,
330 ** e.g., persistent queue runners, to avoid consuming memory.
332 ** XXX It might be useful to restrict the maximum TTL to avoid
333 ** caching data very long.
342 ** can remove entries from the symbol table.
345 #define SM_STAB_FREE(x) \
362 for (i = 0; i < STABSIZE; i++)
368 switch (s->s_symtype)
371 if (s->s_hostsig.hs_exp >= now)
372 goto next; /* not expired */
373 SM_STAB_FREE(s->s_hostsig.hs_sig); /* XXX */
377 if (s->s_namecanon.nc_exp >= now)
378 goto next; /* not expired */
379 SM_STAB_FREE(s->s_namecanon.nc_cname); /* XXX */
383 if (s->s_symtype >= ST_MCI)
385 /* call mci_uncache? */
386 SM_STAB_FREE(s->s_mci.mci_status);
387 SM_STAB_FREE(s->s_mci.mci_rstatus);
388 SM_STAB_FREE(s->s_mci.mci_heloname);
390 /* not dynamically allocated */
391 SM_STAB_FREE(s->s_mci.mci_host);
392 SM_STAB_FREE(s->s_mci.mci_tolist);
395 /* should always by NULL */
396 SM_STAB_FREE(s->s_mci.mci_sasl_string);
398 if (s->s_mci.mci_rpool != NULL)
400 sm_rpool_free(s->s_mci.mci_rpool);
401 s->s_mci.mci_macro.mac_rpool = NULL;
402 s->s_mci.mci_rpool = NULL;
413 SM_STAB_FREE(s->s_name); /* XXX */
416 sm_free(f); /* XXX */
427 ** DUMPSTAB -- dump symbol table.
432 #define MAXSTTYPES (ST_MCI + 1)
437 int i, t, total, types[MAXSTTYPES];
439 static int prevt[MAXSTTYPES], prev = 0;
442 for (i = 0; i < MAXSTTYPES; i++)
444 for (i = 0; i < STABSIZE; i++)
451 if (t > MAXSTTYPES - 1)
457 sm_syslog(LOG_INFO, NOQID, "stab: total=%d (%d)", total, total - prev);
459 for (i = 0; i < MAXSTTYPES; i++)
463 sm_syslog(LOG_INFO, NOQID, "stab: type[%2d]=%2d (%d)",
464 i, types[i], types[i] - prevt[i]);
469 #endif /* SM_HEAP_CHECK */