1 /* Inner loops of cache daemon.
2 Copyright (C) 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include <sys/param.h>
29 #include <sys/socket.h>
37 /* Mapping of request type to database. */
38 static const dbtype serv2db
[LASTDBREQ
+ 1] =
40 [GETPWBYNAME
] = pwddb
,
42 [GETGRBYNAME
] = grpdb
,
44 [GETHOSTBYNAME
] = hstdb
,
45 [GETHOSTBYNAMEv6
] = hstdb
,
46 [GETHOSTBYADDR
] = hstdb
,
47 [GETHOSTBYADDRv6
] = hstdb
,
50 /* Map request type to a string. */
51 const char *serv2str
[LASTREQ
] =
53 [GETPWBYNAME
] = "GETPWBYNAME",
54 [GETPWBYUID
] = "GETPWBYUID",
55 [GETGRBYNAME
] = "GETGRBYNAME",
56 [GETGRBYGID
] = "GETGRBYGID",
57 [GETHOSTBYNAME
] = "GETHOSTBYNAME",
58 [GETHOSTBYNAMEv6
] = "GETHOSTBYNAMEv6",
59 [GETHOSTBYADDR
] = "GETHOSTBYADDR",
60 [GETHOSTBYADDRv6
] = "GETHOSTBYADDRv6",
61 [SHUTDOWN
] = "SHUTDOWN",
65 /* The control data structures for the services. */
66 static struct database dbs
[lastdb
] =
69 lock
: PTHREAD_RWLOCK_INITIALIZER
,
72 filename
: "/etc/passwd",
74 disabled_iov
: &pwd_iov_disabled
,
79 lock
: PTHREAD_RWLOCK_INITIALIZER
,
82 filename
: "/etc/group",
84 disabled_iov
: &grp_iov_disabled
,
89 lock
: PTHREAD_RWLOCK_INITIALIZER
,
92 filename
: "/etc/hosts",
94 disabled_iov
: &hst_iov_disabled
,
100 /* Number of seconds between two cache pruning runs. */
101 #define CACHE_PRUNE_INTERVAL 15
103 /* Number of threads to use. */
106 /* Socket for incoming connections. */
110 /* Initialize database information structures. */
112 nscd_init (const char *conffile
)
114 struct sockaddr_un sock_addr
;
117 /* Read the configuration file. */
118 if (nscd_parse_file (conffile
, dbs
) != 0)
120 /* We couldn't read the configuration file. Disable all services
121 by shutting down the srever. */
122 dbg_log (_("cannot read configuration file; this is fatal"));
126 /* No configuration for this value, assume a default. */
127 nthreads
= 2 * lastdb
;
129 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
130 if (dbs
[cnt
].enabled
)
132 pthread_rwlock_init (&dbs
[cnt
].lock
, NULL
);
134 dbs
[cnt
].array
= (struct hashentry
**)
135 calloc (dbs
[cnt
].module
, sizeof (struct hashentry
*));
136 if (dbs
[cnt
].array
== NULL
)
137 error (EXIT_FAILURE
, errno
, "while allocating cache");
139 if (dbs
[cnt
].check_file
)
141 /* We need the modification date of the file. */
144 if (stat (dbs
[cnt
].filename
, &st
) < 0)
147 /* We cannot stat() the file, disable file checking. */
148 dbg_log (_("cannot stat() file `%s': %s"),
150 strerror_r (errno
, buf
, sizeof (buf
)));
151 dbs
[cnt
].check_file
= 0;
154 dbs
[cnt
].file_mtime
= st
.st_mtime
;
158 /* Create the socket. */
159 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
162 dbg_log (_("cannot open socket: %s"), strerror (errno
));
165 /* Bind a name to the socket. */
166 sock_addr
.sun_family
= AF_UNIX
;
167 strcpy (sock_addr
.sun_path
, _PATH_NSCDSOCKET
);
168 if (bind (sock
, (struct sockaddr
*) &sock_addr
, sizeof (sock_addr
)) < 0)
170 dbg_log ("%s: %s", _PATH_NSCDSOCKET
, strerror (errno
));
174 /* Set permissions for the socket. */
175 chmod (_PATH_NSCDSOCKET
, 0666);
177 /* Set the socket up to accept connections. */
178 if (listen (sock
, SOMAXCONN
) < 0)
180 dbg_log (_("cannot enable socket to accept connections: %s"),
187 /* Close the connections. */
195 /* Handle new request. */
197 handle_request (int fd
, request_header
*req
, void *key
)
200 dbg_log (_("handle_request: request received (Version = %d)"),
203 if (req
->version
!= NSCD_VERSION
)
206 cannot handle old request version %d; current version is %d"),
207 req
->version
, NSCD_VERSION
);
211 if (req
->type
>= GETPWBYNAME
&& req
->type
<= LASTDBREQ
)
213 struct hashentry
*cached
;
214 struct database
*db
= &dbs
[serv2db
[req
->type
]];
217 dbg_log ("\t%s (%s)", serv2str
[req
->type
], key
);
219 /* Is this service enabled? */
222 /* No, sent the prepared record. */
223 if (TEMP_FAILURE_RETRY (write (fd
, db
->disabled_iov
->iov_base
,
224 db
->disabled_iov
->iov_len
))
225 != db
->disabled_iov
->iov_len
)
227 /* We have problems sending the result. */
229 dbg_log (_("cannot write result: %s"),
230 strerror_r (errno
, buf
, sizeof (buf
)));
236 /* Be sure we can read the data. */
237 pthread_rwlock_rdlock (&db
->lock
);
239 /* See whether we can handle it from the cache. */
240 cached
= (struct hashentry
*) cache_search (req
->type
, key
, req
->key_len
,
244 /* Hurray it's in the cache. */
245 if (TEMP_FAILURE_RETRY (write (fd
, cached
->packet
, cached
->total
))
248 /* We have problems sending the result. */
250 dbg_log (_("cannot write result: %s"),
251 strerror_r (errno
, buf
, sizeof (buf
)));
254 pthread_rwlock_unlock (&db
->lock
);
259 pthread_rwlock_unlock (&db
->lock
);
263 dbg_log ("\t%s", serv2str
[req
->type
]);
265 /* Handle the request. */
269 addpwbyname (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
273 addpwbyuid (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
277 addgrbyname (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
281 addgrbygid (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
285 addhstbyname (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
288 case GETHOSTBYNAMEv6
:
289 addhstbynamev6 (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
293 addhstbyaddr (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
296 case GETHOSTBYADDRv6
:
297 addhstbyaddrv6 (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
301 send_stats (fd
, dbs
);
305 termination_handler (0);
314 /* This is the main loop. It is replicated in different threads but the
315 `poll' call makes sure only one thread handles an incoming connection. */
317 __attribute__ ((__noreturn__
))
320 int my_number
= (int) p
;
322 int run_prune
= my_number
< lastdb
&& dbs
[my_number
].enabled
;
323 time_t now
= time (NULL
);
324 time_t next_prune
= now
+ CACHE_PRUNE_INTERVAL
;
325 int timeout
= run_prune
? 1000 * (next_prune
- now
) : -1;
328 conn
.events
= POLLRDNORM
;
332 int nr
= poll (&conn
, 1, timeout
);
336 /* The `poll' call timed out. It's time to clean up the cache. */
337 assert (my_number
< lastdb
);
339 prune_cache (&dbs
[my_number
], now
);
340 next_prune
= now
+ CACHE_PRUNE_INTERVAL
;
341 timeout
= 1000 * (next_prune
- now
);
345 /* We have a new incoming connection. */
346 if (conn
.revents
& (POLLRDNORM
|POLLERR
|POLLHUP
|POLLNVAL
))
348 /* Accept the connection. */
349 int fd
= accept (conn
.fd
, NULL
, NULL
);
355 dbg_log (_("while accepting connection: %s"),
356 strerror_r (errno
, buf
, sizeof (buf
)));
360 /* Now read the request. */
361 if (TEMP_FAILURE_RETRY (read (fd
, &req
, sizeof (req
)))
364 dbg_log (_("short read while reading request: %s"),
365 strerror_r (errno
, buf
, sizeof (buf
)));
370 /* It should not be possible to crash the nscd with a silly
371 request (i.e., a terribly large key. We limit the size
373 if (req
.key_len
< 0 || req
.key_len
> 1024)
375 dbg_log (_("key length in request too long: %Zd"), req
.key_len
);
382 char keybuf
[req
.key_len
];
384 if (TEMP_FAILURE_RETRY (read (fd
, keybuf
, req
.key_len
))
387 dbg_log (_("short read while reading request key: %s"),
388 strerror_r (errno
, buf
, sizeof (buf
)));
393 /* Phew, we got all the data, now process it. */
394 handle_request (fd
, &req
, keybuf
);
404 timeout
= now
< next_prune
? 1000 * (next_prune
- now
) : 0;
410 /* Start all the threads we want. The initial process is thread no. 1. */
418 pthread_attr_init (&attr
);
419 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
421 /* We allow less than LASTDB threads only for debugging. */
422 if (debug_level
== 0)
423 nthreads
= MAX (nthreads
, lastdb
);
425 for (i
= 1; i
< nthreads
; ++i
)
426 pthread_create (&th
, &attr
, nscd_run
, (void *) i
);
428 pthread_attr_destroy (&attr
);
430 nscd_run ((void *) 0);