1 /* Copyright (c) 1998, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <stdio_ext.h>
30 #include <sys/param.h>
31 #include <sys/types.h>
36 /* Wrapper functions with error checking for standard functions. */
37 extern char *xstrdup (const char *s
);
40 /* Names of the databases. */
41 const char *dbnames
[lastdb
] =
49 nscd_parse_file (const char *fname
, struct database_dyn dbs
[lastdb
])
52 char *line
, *cp
, *entry
, *arg1
, *arg2
;
56 /* Open the configuration file. */
57 fp
= fopen (fname
, "r");
61 /* The stream is not used by more than one thread. */
62 (void) __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
69 ssize_t n
= getline (&line
, &len
, fp
);
72 if (line
[n
- 1] == '\n')
75 /* Because the file format does not know any form of quoting we
76 can search forward for the next '#' character and if found
77 make it terminating the line. */
78 *strchrnul (line
, '#') = '\0';
80 /* If the line is blank it is ignored. */
85 while (isspace (*entry
) && *entry
!= '\0')
88 while (!isspace (*cp
) && *cp
!= '\0')
93 if (strlen (entry
) == 0)
94 dbg_log (_("Parse error: %s"), line
);
95 while (isspace (*arg1
) && *arg1
!= '\0')
98 while (!isspace (*cp
) && *cp
!= '\0')
103 if (strlen (arg2
) > 0)
105 while (isspace (*arg2
) && *arg2
!= '\0')
108 while (!isspace (*cp
) && *cp
!= '\0')
113 if (strcmp (entry
, "positive-time-to-live") == 0)
115 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
116 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
118 dbs
[cnt
].postimeout
= atol (arg2
);
122 dbg_log ("database %s is not supported\n", arg1
);
124 else if (strcmp (entry
, "negative-time-to-live") == 0)
126 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
127 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
129 dbs
[cnt
].negtimeout
= atol (arg2
);
133 dbg_log ("database %s is not supported\n", arg1
);
135 else if (strcmp (entry
, "suggested-size") == 0)
137 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
138 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
140 dbs
[cnt
].suggested_module
= atol (arg2
);
144 dbg_log ("database %s is not supported\n", arg1
);
146 else if (strcmp (entry
, "enable-cache") == 0)
148 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
149 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
151 if (strcmp (arg2
, "no") == 0)
152 dbs
[cnt
].enabled
= 0;
153 else if (strcmp (arg2
, "yes") == 0)
154 dbs
[cnt
].enabled
= 1;
158 dbg_log ("database %s is not supported\n", arg1
);
160 else if (strcmp (entry
, "check-files") == 0)
162 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
163 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
165 if (strcmp (arg2
, "no") == 0)
166 dbs
[cnt
].check_file
= 0;
167 else if (strcmp (arg2
, "yes") == 0)
168 dbs
[cnt
].check_file
= 1;
172 dbg_log ("database %s is not supported\n", arg1
);
174 else if (strcmp (entry
, "max-db-size") == 0)
176 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
177 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
179 dbs
[cnt
].max_db_size
= atol (arg2
);
183 dbg_log ("database %s is not supported\n", arg1
);
185 else if (strcmp (entry
, "logfile") == 0)
187 else if (strcmp (entry
, "debug-level") == 0)
189 int level
= atoi (arg1
);
193 else if (strcmp (entry
, "threads") == 0)
196 nthreads
= MAX (atol (arg1
), lastdb
);
198 else if (strcmp (entry
, "max-threads") == 0)
200 max_nthreads
= MAX (atol (arg1
), lastdb
);
202 else if (strcmp (entry
, "server-user") == 0)
205 dbg_log (_("Must specify user name for server-user option"));
207 server_user
= xstrdup (arg1
);
209 else if (strcmp (entry
, "stat-user") == 0)
212 dbg_log (_("Must specify user name for stat-user option"));
215 stat_user
= xstrdup (arg1
);
217 struct passwd
*pw
= getpwnam (stat_user
);
219 stat_uid
= pw
->pw_uid
;
222 else if (strcmp (entry
, "persistent") == 0)
224 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
225 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
227 if (strcmp (arg2
, "no") == 0)
228 dbs
[cnt
].persistent
= 0;
229 else if (strcmp (arg2
, "yes") == 0)
230 dbs
[cnt
].persistent
= 1;
234 dbg_log ("database %s is not supported\n", arg1
);
236 else if (strcmp (entry
, "shared") == 0)
238 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
239 if (strcmp (arg1
, dbnames
[cnt
]) == 0)
241 if (strcmp (arg2
, "no") == 0)
243 else if (strcmp (arg2
, "yes") == 0)
248 dbg_log ("database %s is not supported\n", arg1
);
250 else if (strcmp (entry
, "reload-count") == 0)
252 if (strcasecmp (arg1
, "unlimited") == 0)
253 reload_count
= UINT_MAX
;
256 unsigned int count
= strtoul (arg1
, NULL
, 0);
257 if (count
> UINT8_MAX
- 1)
258 reload_count
= UINT_MAX
;
260 reload_count
= count
;
262 dbg_log (_("invalid value for 'reload-count': %u"), count
);
265 else if (strcmp (entry
, "paranoia") == 0)
267 if (strcmp (arg1
, "no") == 0)
269 else if (strcmp (arg1
, "yes") == 0)
272 else if (strcmp (entry
, "restart-interval") == 0)
275 restart_interval
= atol (arg1
);
277 dbg_log (_("Must specify value for restart-interval option"));
280 dbg_log (_("Unknown option: %s %s %s"), entry
, arg1
, arg2
);
282 while (!feof_unlocked (fp
));
286 restart_time
= time (NULL
) + restart_interval
;
288 /* Save the old current workding directory if we are in paranoia
289 mode. We have to change back to it. */
290 oldcwd
= get_current_dir_name ();
294 cannot get current working directory: %s; disabling paranoia mode"),
300 /* Enforce sanity. */
301 if (max_nthreads
< nthreads
)
302 max_nthreads
= nthreads
;
304 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
306 size_t datasize
= (sizeof (struct database_pers_head
)
307 + roundup (dbs
[cnt
].suggested_module
308 * sizeof (ref_t
), ALIGN
)
309 + (dbs
[cnt
].suggested_module
310 * DEFAULT_DATASIZE_PER_BUCKET
));
311 if (datasize
> dbs
[cnt
].max_db_size
)
313 dbg_log (_("maximum file size for %s database too small"),
315 dbs
[cnt
].max_db_size
= datasize
;
320 /* Free the buffer. */
322 /* Close configuration file. */