1 /* Inner loops of cache daemon.
2 Copyright (C) 1998, 1999 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 <arpa/inet.h>
28 #include <sys/param.h>
30 #include <sys/socket.h>
38 /* Mapping of request type to database. */
39 static const dbtype serv2db
[LASTDBREQ
+ 1] =
41 [GETPWBYNAME
] = pwddb
,
43 [GETGRBYNAME
] = grpdb
,
45 [GETHOSTBYNAME
] = hstdb
,
46 [GETHOSTBYNAMEv6
] = hstdb
,
47 [GETHOSTBYADDR
] = hstdb
,
48 [GETHOSTBYADDRv6
] = hstdb
,
51 /* Map request type to a string. */
52 const char *serv2str
[LASTREQ
] =
54 [GETPWBYNAME
] = "GETPWBYNAME",
55 [GETPWBYUID
] = "GETPWBYUID",
56 [GETGRBYNAME
] = "GETGRBYNAME",
57 [GETGRBYGID
] = "GETGRBYGID",
58 [GETHOSTBYNAME
] = "GETHOSTBYNAME",
59 [GETHOSTBYNAMEv6
] = "GETHOSTBYNAMEv6",
60 [GETHOSTBYADDR
] = "GETHOSTBYADDR",
61 [GETHOSTBYADDRv6
] = "GETHOSTBYADDRv6",
62 [SHUTDOWN
] = "SHUTDOWN",
66 /* The control data structures for the services. */
67 static struct database dbs
[lastdb
] =
70 lock
: PTHREAD_RWLOCK_INITIALIZER
,
73 filename
: "/etc/passwd",
75 disabled_iov
: &pwd_iov_disabled
,
80 lock
: PTHREAD_RWLOCK_INITIALIZER
,
83 filename
: "/etc/group",
85 disabled_iov
: &grp_iov_disabled
,
90 lock
: PTHREAD_RWLOCK_INITIALIZER
,
93 filename
: "/etc/hosts",
95 disabled_iov
: &hst_iov_disabled
,
101 /* Number of seconds between two cache pruning runs. */
102 #define CACHE_PRUNE_INTERVAL 15
104 /* Number of threads to use. */
107 /* Socket for incoming connections. */
111 /* Initialize database information structures. */
113 nscd_init (const char *conffile
)
115 struct sockaddr_un sock_addr
;
118 /* Read the configuration file. */
119 if (nscd_parse_file (conffile
, dbs
) != 0)
121 /* We couldn't read the configuration file. Disable all services
122 by shutting down the srever. */
123 dbg_log (_("cannot read configuration file; this is fatal"));
127 /* No configuration for this value, assume a default. */
128 nthreads
= 2 * lastdb
;
130 for (cnt
= 0; cnt
< lastdb
; ++cnt
)
131 if (dbs
[cnt
].enabled
)
133 pthread_rwlock_init (&dbs
[cnt
].lock
, NULL
);
135 dbs
[cnt
].array
= (struct hashentry
**)
136 calloc (dbs
[cnt
].module
, sizeof (struct hashentry
*));
137 if (dbs
[cnt
].array
== NULL
)
138 error (EXIT_FAILURE
, errno
, "while allocating cache");
140 if (dbs
[cnt
].check_file
)
142 /* We need the modification date of the file. */
145 if (stat (dbs
[cnt
].filename
, &st
) < 0)
148 /* We cannot stat() the file, disable file checking. */
149 dbg_log (_("cannot stat() file `%s': %s"),
151 strerror_r (errno
, buf
, sizeof (buf
)));
152 dbs
[cnt
].check_file
= 0;
155 dbs
[cnt
].file_mtime
= st
.st_mtime
;
159 /* Create the socket. */
160 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
163 dbg_log (_("cannot open socket: %s"), strerror (errno
));
166 /* Bind a name to the socket. */
167 sock_addr
.sun_family
= AF_UNIX
;
168 strcpy (sock_addr
.sun_path
, _PATH_NSCDSOCKET
);
169 if (bind (sock
, (struct sockaddr
*) &sock_addr
, sizeof (sock_addr
)) < 0)
171 dbg_log ("%s: %s", _PATH_NSCDSOCKET
, strerror (errno
));
175 /* Set permissions for the socket. */
176 chmod (_PATH_NSCDSOCKET
, 0666);
178 /* Set the socket up to accept connections. */
179 if (listen (sock
, SOMAXCONN
) < 0)
181 dbg_log (_("cannot enable socket to accept connections: %s"),
188 /* Close the connections. */
196 /* Handle new request. */
198 handle_request (int fd
, request_header
*req
, void *key
)
201 dbg_log (_("handle_request: request received (Version = %d)"),
204 if (req
->version
!= NSCD_VERSION
)
207 cannot handle old request version %d; current version is %d"),
208 req
->version
, NSCD_VERSION
);
212 if (req
->type
>= GETPWBYNAME
&& req
->type
<= LASTDBREQ
)
214 struct hashentry
*cached
;
215 struct database
*db
= &dbs
[serv2db
[req
->type
]];
219 if (req
->type
== GETHOSTBYADDR
|| req
->type
== GETHOSTBYADDRv6
)
221 char buf
[INET6_ADDRSTRLEN
];
223 dbg_log ("\t%s (%s)", serv2str
[req
->type
],
224 inet_ntop (req
->type
== GETHOSTBYADDR
225 ? AF_INET
: AF_INET6
,
226 key
, buf
, sizeof (buf
)));
229 dbg_log ("\t%s (%s)", serv2str
[req
->type
], key
);
232 /* Is this service enabled? */
235 /* No, sent the prepared record. */
236 if (TEMP_FAILURE_RETRY (write (fd
, db
->disabled_iov
->iov_base
,
237 db
->disabled_iov
->iov_len
))
238 != db
->disabled_iov
->iov_len
)
240 /* We have problems sending the result. */
242 dbg_log (_("cannot write result: %s"),
243 strerror_r (errno
, buf
, sizeof (buf
)));
249 /* Be sure we can read the data. */
250 pthread_rwlock_rdlock (&db
->lock
);
252 /* See whether we can handle it from the cache. */
253 cached
= (struct hashentry
*) cache_search (req
->type
, key
, req
->key_len
,
257 /* Hurray it's in the cache. */
258 if (TEMP_FAILURE_RETRY (write (fd
, cached
->packet
, cached
->total
))
261 /* We have problems sending the result. */
263 dbg_log (_("cannot write result: %s"),
264 strerror_r (errno
, buf
, sizeof (buf
)));
267 pthread_rwlock_unlock (&db
->lock
);
272 pthread_rwlock_unlock (&db
->lock
);
276 dbg_log ("\t%s", serv2str
[req
->type
]);
278 /* Handle the request. */
282 addpwbyname (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
286 addpwbyuid (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
290 addgrbyname (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
294 addgrbygid (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
298 addhstbyname (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
301 case GETHOSTBYNAMEv6
:
302 addhstbynamev6 (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
306 addhstbyaddr (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
309 case GETHOSTBYADDRv6
:
310 addhstbyaddrv6 (&dbs
[serv2db
[req
->type
]], fd
, req
, key
);
314 send_stats (fd
, dbs
);
318 termination_handler (0);
327 /* This is the main loop. It is replicated in different threads but the
328 `poll' call makes sure only one thread handles an incoming connection. */
330 __attribute__ ((__noreturn__
))
333 int my_number
= (int) p
;
335 int run_prune
= my_number
< lastdb
&& dbs
[my_number
].enabled
;
336 time_t now
= time (NULL
);
337 time_t next_prune
= now
+ CACHE_PRUNE_INTERVAL
;
338 int timeout
= run_prune
? 1000 * (next_prune
- now
) : -1;
341 conn
.events
= POLLRDNORM
;
345 int nr
= poll (&conn
, 1, timeout
);
349 /* The `poll' call timed out. It's time to clean up the cache. */
350 assert (my_number
< lastdb
);
352 prune_cache (&dbs
[my_number
], now
);
353 next_prune
= now
+ CACHE_PRUNE_INTERVAL
;
354 timeout
= 1000 * (next_prune
- now
);
358 /* We have a new incoming connection. */
359 if (conn
.revents
& (POLLRDNORM
|POLLERR
|POLLHUP
|POLLNVAL
))
361 /* Accept the connection. */
362 int fd
= accept (conn
.fd
, NULL
, NULL
);
368 dbg_log (_("while accepting connection: %s"),
369 strerror_r (errno
, buf
, sizeof (buf
)));
373 /* Now read the request. */
374 if (TEMP_FAILURE_RETRY (read (fd
, &req
, sizeof (req
)))
377 dbg_log (_("short read while reading request: %s"),
378 strerror_r (errno
, buf
, sizeof (buf
)));
383 /* It should not be possible to crash the nscd with a silly
384 request (i.e., a terribly large key. We limit the size
386 if (req
.key_len
< 0 || req
.key_len
> 1024)
388 dbg_log (_("key length in request too long: %Zd"), req
.key_len
);
395 char keybuf
[req
.key_len
];
397 if (TEMP_FAILURE_RETRY (read (fd
, keybuf
, req
.key_len
))
400 dbg_log (_("short read while reading request key: %s"),
401 strerror_r (errno
, buf
, sizeof (buf
)));
406 /* Phew, we got all the data, now process it. */
407 handle_request (fd
, &req
, keybuf
);
417 timeout
= now
< next_prune
? 1000 * (next_prune
- now
) : 0;
423 /* Start all the threads we want. The initial process is thread no. 1. */
431 pthread_attr_init (&attr
);
432 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
434 /* We allow less than LASTDB threads only for debugging. */
435 if (debug_level
== 0)
436 nthreads
= MAX (nthreads
, lastdb
);
438 for (i
= 1; i
< nthreads
; ++i
)
439 pthread_create (&th
, &attr
, nscd_run
, (void *) i
);
441 pthread_attr_destroy (&attr
);
443 nscd_run ((void *) 0);