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
27 #include <stdio_ext.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
37 /* Wrapper functions with error checking for standard functions. */
38 extern char *xstrdup (const char *s
);
41 /* Names of the databases. */
42 const char *dbnames
[lastdb
] =
51 find_db (const char *name
)
53 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
54 if (strcmp (name
, dbnames
[cnt
]) == 0)
57 error (0, 0, _("database %s is not supported\n"), name
);
62 nscd_parse_file (const char *fname
, struct database_dyn dbs
[lastdb
])
65 char *line
, *cp
, *entry
, *arg1
, *arg2
;
68 const unsigned int initial_error_message_count
= error_message_count
;
70 /* Open the configuration file. */
71 fp
= fopen (fname
, "r");
75 /* The stream is not used by more than one thread. */
76 (void) __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
83 ssize_t n
= getline (&line
, &len
, fp
);
86 if (line
[n
- 1] == '\n')
89 /* Because the file format does not know any form of quoting we
90 can search forward for the next '#' character and if found
91 make it terminating the line. */
92 *strchrnul (line
, '#') = '\0';
94 /* If the line is blank it is ignored. */
99 while (isspace (*entry
) && *entry
!= '\0')
102 while (!isspace (*cp
) && *cp
!= '\0')
107 if (strlen (entry
) == 0)
108 error (0, 0, _("Parse error: %s"), line
);
109 while (isspace (*arg1
) && *arg1
!= '\0')
112 while (!isspace (*cp
) && *cp
!= '\0')
117 if (strlen (arg2
) > 0)
119 while (isspace (*arg2
) && *arg2
!= '\0')
122 while (!isspace (*cp
) && *cp
!= '\0')
127 if (strcmp (entry
, "positive-time-to-live") == 0)
129 int idx
= find_db (arg1
);
131 dbs
[idx
].postimeout
= atol (arg2
);
133 else if (strcmp (entry
, "negative-time-to-live") == 0)
135 int idx
= find_db (arg1
);
137 dbs
[idx
].negtimeout
= atol (arg2
);
139 else if (strcmp (entry
, "suggested-size") == 0)
141 int idx
= find_db (arg1
);
143 dbs
[idx
].suggested_module
= atol (arg2
);
145 else if (strcmp (entry
, "enable-cache") == 0)
147 int idx
= find_db (arg1
);
150 if (strcmp (arg2
, "no") == 0)
151 dbs
[idx
].enabled
= 0;
152 else if (strcmp (arg2
, "yes") == 0)
153 dbs
[idx
].enabled
= 1;
156 else if (strcmp (entry
, "check-files") == 0)
158 int idx
= find_db (arg1
);
161 if (strcmp (arg2
, "no") == 0)
162 dbs
[idx
].check_file
= 0;
163 else if (strcmp (arg2
, "yes") == 0)
164 dbs
[idx
].check_file
= 1;
167 else if (strcmp (entry
, "max-db-size") == 0)
169 int idx
= find_db (arg1
);
171 dbs
[idx
].max_db_size
= atol (arg2
);
173 else if (strcmp (entry
, "logfile") == 0)
175 else if (strcmp (entry
, "debug-level") == 0)
177 int level
= atoi (arg1
);
181 else if (strcmp (entry
, "threads") == 0)
184 nthreads
= MAX (atol (arg1
), lastdb
);
186 else if (strcmp (entry
, "max-threads") == 0)
188 max_nthreads
= MAX (atol (arg1
), lastdb
);
190 else if (strcmp (entry
, "server-user") == 0)
193 error (0, 0, _("Must specify user name for server-user option"));
195 server_user
= xstrdup (arg1
);
197 else if (strcmp (entry
, "stat-user") == 0)
200 error (0, 0, _("Must specify user name for stat-user option"));
203 stat_user
= xstrdup (arg1
);
205 struct passwd
*pw
= getpwnam (stat_user
);
207 stat_uid
= pw
->pw_uid
;
210 else if (strcmp (entry
, "persistent") == 0)
212 int idx
= find_db (arg1
);
215 if (strcmp (arg2
, "no") == 0)
216 dbs
[idx
].persistent
= 0;
217 else if (strcmp (arg2
, "yes") == 0)
218 dbs
[idx
].persistent
= 1;
221 else if (strcmp (entry
, "shared") == 0)
223 int idx
= find_db (arg1
);
226 if (strcmp (arg2
, "no") == 0)
228 else if (strcmp (arg2
, "yes") == 0)
232 else if (strcmp (entry
, "reload-count") == 0)
234 if (strcasecmp (arg1
, "unlimited") == 0)
235 reload_count
= UINT_MAX
;
238 unsigned int count
= strtoul (arg1
, NULL
, 0);
239 if (count
> UINT8_MAX
- 1)
240 reload_count
= UINT_MAX
;
242 reload_count
= count
;
244 error (0, 0, _("invalid value for 'reload-count': %u"), count
);
247 else if (strcmp (entry
, "paranoia") == 0)
249 if (strcmp (arg1
, "no") == 0)
251 else if (strcmp (arg1
, "yes") == 0)
254 else if (strcmp (entry
, "restart-interval") == 0)
257 restart_interval
= atol (arg1
);
259 error (0, 0, _("Must specify value for restart-interval option"));
262 error (0, 0, _("Unknown option: %s %s %s"), entry
, arg1
, arg2
);
264 while (!feof_unlocked (fp
));
268 restart_time
= time (NULL
) + restart_interval
;
270 /* Save the old current workding directory if we are in paranoia
271 mode. We have to change back to it. */
272 oldcwd
= get_current_dir_name ();
276 cannot get current working directory: %s; disabling paranoia mode"),
282 /* Enforce sanity. */
283 if (max_nthreads
< nthreads
)
284 max_nthreads
= nthreads
;
286 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
288 size_t datasize
= (sizeof (struct database_pers_head
)
289 + roundup (dbs
[cnt
].suggested_module
290 * sizeof (ref_t
), ALIGN
)
291 + (dbs
[cnt
].suggested_module
292 * DEFAULT_DATASIZE_PER_BUCKET
));
293 if (datasize
> dbs
[cnt
].max_db_size
)
295 error (0, 0, _("maximum file size for %s database too small"),
297 dbs
[cnt
].max_db_size
= datasize
;
302 /* Free the buffer. */
304 /* Close configuration file. */
307 return error_message_count
!= initial_error_message_count
;