1 /* Copyright (c) 1998, 2000, 2003-2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include <stdio_ext.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
35 /* Wrapper functions with error checking for standard functions. */
36 extern char *xstrdup (const char *s
);
39 /* Names of the databases. */
40 const char *const dbnames
[lastdb
] =
50 find_db (const char *name
)
52 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
53 if (strcmp (name
, dbnames
[cnt
]) == 0)
56 error (0, 0, _("database %s is not supported"), name
);
61 nscd_parse_file (const char *fname
, struct database_dyn dbs
[lastdb
])
64 char *line
, *cp
, *entry
, *arg1
, *arg2
;
67 const unsigned int initial_error_message_count
= error_message_count
;
69 /* Open the configuration file. */
70 fp
= fopen (fname
, "r");
74 /* The stream is not used by more than one thread. */
75 (void) __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
82 ssize_t n
= getline (&line
, &len
, fp
);
85 if (line
[n
- 1] == '\n')
88 /* Because the file format does not know any form of quoting we
89 can search forward for the next '#' character and if found
90 make it terminating the line. */
91 *strchrnul (line
, '#') = '\0';
93 /* If the line is blank it is ignored. */
98 while (isspace (*entry
) && *entry
!= '\0')
101 while (!isspace (*cp
) && *cp
!= '\0')
106 if (strlen (entry
) == 0)
107 error (0, 0, _("Parse error: %s"), line
);
108 while (isspace (*arg1
) && *arg1
!= '\0')
111 while (!isspace (*cp
) && *cp
!= '\0')
116 if (strlen (arg2
) > 0)
118 while (isspace (*arg2
) && *arg2
!= '\0')
121 while (!isspace (*cp
) && *cp
!= '\0')
126 if (strcmp (entry
, "positive-time-to-live") == 0)
128 int idx
= find_db (arg1
);
130 dbs
[idx
].postimeout
= atol (arg2
);
132 else if (strcmp (entry
, "negative-time-to-live") == 0)
134 int idx
= find_db (arg1
);
136 dbs
[idx
].negtimeout
= atol (arg2
);
138 else if (strcmp (entry
, "suggested-size") == 0)
140 int idx
= find_db (arg1
);
142 dbs
[idx
].suggested_module
= atol (arg2
);
144 else if (strcmp (entry
, "enable-cache") == 0)
146 int idx
= find_db (arg1
);
149 if (strcmp (arg2
, "no") == 0)
150 dbs
[idx
].enabled
= 0;
151 else if (strcmp (arg2
, "yes") == 0)
152 dbs
[idx
].enabled
= 1;
155 else if (strcmp (entry
, "check-files") == 0)
157 int idx
= find_db (arg1
);
160 if (strcmp (arg2
, "no") == 0)
161 dbs
[idx
].check_file
= 0;
162 else if (strcmp (arg2
, "yes") == 0)
163 dbs
[idx
].check_file
= 1;
166 else if (strcmp (entry
, "max-db-size") == 0)
168 int idx
= find_db (arg1
);
170 dbs
[idx
].max_db_size
= atol (arg2
);
172 else if (strcmp (entry
, "logfile") == 0)
174 else if (strcmp (entry
, "debug-level") == 0)
176 int level
= atoi (arg1
);
180 else if (strcmp (entry
, "threads") == 0)
183 nthreads
= MAX (atol (arg1
), lastdb
);
185 else if (strcmp (entry
, "max-threads") == 0)
187 max_nthreads
= MAX (atol (arg1
), lastdb
);
189 else if (strcmp (entry
, "server-user") == 0)
192 error (0, 0, _("Must specify user name for server-user option"));
194 server_user
= xstrdup (arg1
);
196 else if (strcmp (entry
, "stat-user") == 0)
199 error (0, 0, _("Must specify user name for stat-user option"));
202 stat_user
= xstrdup (arg1
);
204 struct passwd
*pw
= getpwnam (stat_user
);
206 stat_uid
= pw
->pw_uid
;
209 else if (strcmp (entry
, "persistent") == 0)
211 int idx
= find_db (arg1
);
214 if (strcmp (arg2
, "no") == 0)
215 dbs
[idx
].persistent
= 0;
216 else if (strcmp (arg2
, "yes") == 0)
217 dbs
[idx
].persistent
= 1;
220 else if (strcmp (entry
, "shared") == 0)
222 int idx
= find_db (arg1
);
225 if (strcmp (arg2
, "no") == 0)
227 else if (strcmp (arg2
, "yes") == 0)
231 else if (strcmp (entry
, "reload-count") == 0)
233 if (strcasecmp (arg1
, "unlimited") == 0)
234 reload_count
= UINT_MAX
;
237 unsigned int count
= strtoul (arg1
, NULL
, 0);
238 if (count
> UINT8_MAX
- 1)
239 reload_count
= UINT_MAX
;
241 reload_count
= count
;
243 error (0, 0, _("invalid value for 'reload-count': %u"), count
);
246 else if (strcmp (entry
, "paranoia") == 0)
248 if (strcmp (arg1
, "no") == 0)
250 else if (strcmp (arg1
, "yes") == 0)
253 else if (strcmp (entry
, "restart-interval") == 0)
256 restart_interval
= atol (arg1
);
258 error (0, 0, _("Must specify value for restart-interval option"));
260 else if (strcmp (entry
, "auto-propagate") == 0)
262 int idx
= find_db (arg1
);
265 if (strcmp (arg2
, "no") == 0)
266 dbs
[idx
].propagate
= 0;
267 else if (strcmp (arg2
, "yes") == 0)
268 dbs
[idx
].propagate
= 1;
272 error (0, 0, _("Unknown option: %s %s %s"), entry
, arg1
, arg2
);
274 while (!feof_unlocked (fp
));
278 restart_time
= time (NULL
) + restart_interval
;
280 /* Save the old current workding directory if we are in paranoia
281 mode. We have to change back to it. */
282 oldcwd
= get_current_dir_name ();
286 cannot get current working directory: %s; disabling paranoia mode"),
292 /* Enforce sanity. */
293 if (max_nthreads
< nthreads
)
294 max_nthreads
= nthreads
;
296 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
298 size_t datasize
= (sizeof (struct database_pers_head
)
299 + roundup (dbs
[cnt
].suggested_module
300 * sizeof (ref_t
), ALIGN
)
301 + (dbs
[cnt
].suggested_module
302 * DEFAULT_DATASIZE_PER_BUCKET
));
303 if (datasize
> dbs
[cnt
].max_db_size
)
305 error (0, 0, _("maximum file size for %s database too small"),
307 dbs
[cnt
].max_db_size
= datasize
;
312 /* Free the buffer. */
314 /* Close configuration file. */
317 return error_message_count
!= initial_error_message_count
;