1 /* Inner loops of cache daemon.
2 Copyright (C) 1998-2007, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
34 #include <arpa/inet.h>
36 # include <sys/epoll.h>
39 # include <sys/inotify.h>
42 #include <sys/param.h>
45 # include <sys/sendfile.h>
47 #include <sys/socket.h>
54 #include <resolv/resolv.h>
56 # include <kernel-features.h>
60 /* Wrapper functions with error checking for standard functions. */
61 extern void *xmalloc (size_t n
);
62 extern void *xcalloc (size_t n
, size_t s
);
63 extern void *xrealloc (void *o
, size_t n
);
65 /* Support to run nscd as an unprivileged user */
66 const char *server_user
;
67 static uid_t server_uid
;
68 static gid_t server_gid
;
69 const char *stat_user
;
71 static gid_t
*server_groups
;
75 static int server_ngroups
;
77 static pthread_attr_t attr
;
79 static void begin_drop_privileges (void);
80 static void finish_drop_privileges (void);
82 /* Map request type to a string. */
83 const char *const serv2str
[LASTREQ
] =
85 [GETPWBYNAME
] = "GETPWBYNAME",
86 [GETPWBYUID
] = "GETPWBYUID",
87 [GETGRBYNAME
] = "GETGRBYNAME",
88 [GETGRBYGID
] = "GETGRBYGID",
89 [GETHOSTBYNAME
] = "GETHOSTBYNAME",
90 [GETHOSTBYNAMEv6
] = "GETHOSTBYNAMEv6",
91 [GETHOSTBYADDR
] = "GETHOSTBYADDR",
92 [GETHOSTBYADDRv6
] = "GETHOSTBYADDRv6",
93 [SHUTDOWN
] = "SHUTDOWN",
94 [GETSTAT
] = "GETSTAT",
95 [INVALIDATE
] = "INVALIDATE",
96 [GETFDPW
] = "GETFDPW",
97 [GETFDGR
] = "GETFDGR",
98 [GETFDHST
] = "GETFDHST",
100 [INITGROUPS
] = "INITGROUPS",
101 [GETSERVBYNAME
] = "GETSERVBYNAME",
102 [GETSERVBYPORT
] = "GETSERVBYPORT",
103 [GETFDSERV
] = "GETFDSERV"
106 /* The control data structures for the services. */
107 struct database_dyn dbs
[lastdb
] =
110 .lock
= PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
,
111 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
117 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
118 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
120 .filename
= "/etc/passwd",
121 .db_filename
= _PATH_NSCD_PASSWD_DB
,
122 .disabled_iov
= &pwd_iov_disabled
,
130 .lock
= PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
,
131 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
137 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
138 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
140 .filename
= "/etc/group",
141 .db_filename
= _PATH_NSCD_GROUP_DB
,
142 .disabled_iov
= &grp_iov_disabled
,
150 .lock
= PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
,
151 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
155 .propagate
= 0, /* Not used. */
157 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
158 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
160 .filename
= "/etc/hosts",
161 .db_filename
= _PATH_NSCD_HOSTS_DB
,
162 .disabled_iov
= &hst_iov_disabled
,
170 .lock
= PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
,
171 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
175 .propagate
= 0, /* Not used. */
177 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
178 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
180 .filename
= "/etc/services",
181 .db_filename
= _PATH_NSCD_SERVICES_DB
,
182 .disabled_iov
= &serv_iov_disabled
,
192 /* Mapping of request type to database. */
196 struct database_dyn
*db
;
197 } const reqinfo
[LASTREQ
] =
199 [GETPWBYNAME
] = { true, &dbs
[pwddb
] },
200 [GETPWBYUID
] = { true, &dbs
[pwddb
] },
201 [GETGRBYNAME
] = { true, &dbs
[grpdb
] },
202 [GETGRBYGID
] = { true, &dbs
[grpdb
] },
203 [GETHOSTBYNAME
] = { true, &dbs
[hstdb
] },
204 [GETHOSTBYNAMEv6
] = { true, &dbs
[hstdb
] },
205 [GETHOSTBYADDR
] = { true, &dbs
[hstdb
] },
206 [GETHOSTBYADDRv6
] = { true, &dbs
[hstdb
] },
207 [SHUTDOWN
] = { false, NULL
},
208 [GETSTAT
] = { false, NULL
},
209 [SHUTDOWN
] = { false, NULL
},
210 [GETFDPW
] = { false, &dbs
[pwddb
] },
211 [GETFDGR
] = { false, &dbs
[grpdb
] },
212 [GETFDHST
] = { false, &dbs
[hstdb
] },
213 [GETAI
] = { true, &dbs
[hstdb
] },
214 [INITGROUPS
] = { true, &dbs
[grpdb
] },
215 [GETSERVBYNAME
] = { true, &dbs
[servdb
] },
216 [GETSERVBYPORT
] = { true, &dbs
[servdb
] },
217 [GETFDSERV
] = { false, &dbs
[servdb
] }
221 /* Initial number of threads to use. */
223 /* Maximum number of threads to use. */
224 int max_nthreads
= 32;
226 /* Socket for incoming connections. */
230 /* Inotify descriptor. */
231 static int inotify_fd
= -1;
233 /* Watch descriptor for resolver configuration file. */
234 static int resolv_conf_descr
= -1;
237 /* Number of times clients had to wait. */
238 unsigned long int client_queued
;
240 /* Data structure for recording in-flight memory allocation. */
241 __thread
struct mem_in_flight mem_in_flight attribute_tls_model_ie
;
242 /* Global list of the mem_in_flight variables of all the threads. */
243 struct mem_in_flight
*mem_in_flight_list
;
247 writeall (int fd
, const void *buf
, size_t len
)
253 ret
= TEMP_FAILURE_RETRY (send (fd
, buf
, n
, MSG_NOSIGNAL
));
256 buf
= (const char *) buf
+ ret
;
260 return ret
< 0 ? ret
: len
- n
;
266 sendfileall (int tofd
, int fromfd
, off_t off
, size_t len
)
273 ret
= TEMP_FAILURE_RETRY (sendfile (tofd
, fromfd
, &off
, n
));
279 return ret
< 0 ? ret
: len
- n
;
287 /* The following three are not really used, they are symbolic constants. */
293 use_he_begin
= use_he
| use_begin
,
294 use_he_end
= use_he
| use_end
,
297 use_key_begin
= use_key
| use_begin
,
298 use_key_end
= use_key
| use_end
,
299 use_key_first
= use_key_begin
| use_first
,
302 use_data_begin
= use_data
| use_begin
,
303 use_data_end
= use_data
| use_end
,
304 use_data_first
= use_data_begin
| use_first
309 check_use (const char *data
, nscd_ssize_t first_free
, uint8_t *usemap
,
310 enum usekey use
, ref_t start
, size_t len
)
314 if (start
> first_free
|| start
+ len
> first_free
315 || (start
& BLOCK_ALIGN_M1
))
318 if (usemap
[start
] == use_not
)
320 /* Add the start marker. */
321 usemap
[start
] = use
| use_begin
;
325 if (usemap
[++start
] != use_not
)
330 /* Add the end marker. */
331 usemap
[start
] = use
| use_end
;
333 else if ((usemap
[start
] & ~use_first
) == ((use
| use_begin
) & ~use_first
))
335 /* Hash entries can't be shared. */
339 usemap
[start
] |= (use
& use_first
);
343 if (usemap
[++start
] != use
)
346 if (usemap
[++start
] != (use
| use_end
))
350 /* Points to a wrong object or somewhere in the middle. */
357 /* Verify data in persistent database. */
359 verify_persistent_db (void *mem
, struct database_pers_head
*readhead
, int dbnr
)
361 assert (dbnr
== pwddb
|| dbnr
== grpdb
|| dbnr
== hstdb
|| dbnr
== servdb
);
363 time_t now
= time (NULL
);
365 struct database_pers_head
*head
= mem
;
366 struct database_pers_head head_copy
= *head
;
368 /* Check that the header that was read matches the head in the database. */
369 if (memcmp (head
, readhead
, sizeof (*head
)) != 0)
372 /* First some easy tests: make sure the database header is sane. */
373 if (head
->version
!= DB_VERSION
374 || head
->header_size
!= sizeof (*head
)
375 /* We allow a timestamp to be one hour ahead of the current time.
376 This should cover daylight saving time changes. */
377 || head
->timestamp
> now
+ 60 * 60 + 60
378 || (head
->gc_cycle
& 1)
380 || (size_t) head
->module
> INT32_MAX
/ sizeof (ref_t
)
381 || (size_t) head
->data_size
> INT32_MAX
- head
->module
* sizeof (ref_t
)
382 || head
->first_free
< 0
383 || head
->first_free
> head
->data_size
384 || (head
->first_free
& BLOCK_ALIGN_M1
) != 0
385 || head
->maxnentries
< 0
386 || head
->maxnsearched
< 0)
389 uint8_t *usemap
= calloc (head
->first_free
, 1);
393 const char *data
= (char *) &head
->array
[roundup (head
->module
,
394 ALIGN
/ sizeof (ref_t
))];
396 nscd_ssize_t he_cnt
= 0;
397 for (nscd_ssize_t cnt
= 0; cnt
< head
->module
; ++cnt
)
399 ref_t trail
= head
->array
[cnt
];
403 while (work
!= ENDREF
)
405 if (! check_use (data
, head
->first_free
, usemap
, use_he
, work
,
406 sizeof (struct hashentry
)))
409 /* Now we know we can dereference the record. */
410 struct hashentry
*here
= (struct hashentry
*) (data
+ work
);
414 /* Make sure the record is for this type of service. */
415 if (here
->type
>= LASTREQ
416 || reqinfo
[here
->type
].db
!= &dbs
[dbnr
])
419 /* Validate boolean field value. */
420 if (here
->first
!= false && here
->first
!= true)
428 || here
->packet
> head
->first_free
429 || here
->packet
+ sizeof (struct datahead
) > head
->first_free
)
432 struct datahead
*dh
= (struct datahead
*) (data
+ here
->packet
);
434 if (! check_use (data
, head
->first_free
, usemap
,
435 use_data
| (here
->first
? use_first
: 0),
436 here
->packet
, dh
->allocsize
))
439 if (dh
->allocsize
< sizeof (struct datahead
)
440 || dh
->recsize
> dh
->allocsize
441 || (dh
->notfound
!= false && dh
->notfound
!= true)
442 || (dh
->usable
!= false && dh
->usable
!= true))
445 if (here
->key
< here
->packet
+ sizeof (struct datahead
)
446 || here
->key
> here
->packet
+ dh
->allocsize
447 || here
->key
+ here
->len
> here
->packet
+ dh
->allocsize
)
450 /* If keys can appear outside of data, this should be done
451 instead. But gc doesn't mark the data in that case. */
452 if (! check_use (data
, head
->first_free
, usemap
,
453 use_key
| (here
->first
? use_first
: 0),
454 here
->key
, here
->len
))
462 /* A circular list, this must not happen. */
465 trail
= ((struct hashentry
*) (data
+ trail
))->next
;
470 if (he_cnt
!= head
->nentries
)
473 /* See if all data and keys had at least one reference from
474 he->first == true hashentry. */
475 for (ref_t idx
= 0; idx
< head
->first_free
; ++idx
)
478 if (usemap
[idx
] == use_key_begin
)
481 if (usemap
[idx
] == use_data_begin
)
485 /* Finally, make sure the database hasn't changed since the first test. */
486 if (memcmp (mem
, &head_copy
, sizeof (*head
)) != 0)
499 # define EXTRA_O_FLAGS O_CLOEXEC
501 # define EXTRA_O_FLAGS 0
505 /* Initialize database information structures. */
509 /* Look up unprivileged uid/gid/groups before we start listening on the
511 if (server_user
!= NULL
)
512 begin_drop_privileges ();
515 /* No configuration for this value, assume a default. */
519 /* Use inotify to recognize changed files. */
520 inotify_fd
= inotify_init ();
521 if (inotify_fd
!= -1)
522 fcntl (inotify_fd
, F_SETFL
, O_NONBLOCK
);
525 for (size_t cnt
= 0; cnt
< lastdb
; ++cnt
)
526 if (dbs
[cnt
].enabled
)
528 pthread_rwlock_init (&dbs
[cnt
].lock
, NULL
);
529 pthread_mutex_init (&dbs
[cnt
].memlock
, NULL
);
531 if (dbs
[cnt
].persistent
)
533 /* Try to open the appropriate file on disk. */
534 int fd
= open (dbs
[cnt
].db_filename
, O_RDWR
| EXTRA_O_FLAGS
);
541 struct database_pers_head head
;
542 ssize_t n
= TEMP_FAILURE_RETRY (read (fd
, &head
,
544 if (n
!= sizeof (head
) || fstat64 (fd
, &st
) != 0)
547 /* The code is single-threaded at this point so
548 using strerror is just fine. */
549 msg
= strerror (errno
);
551 dbg_log (_("invalid persistent database file \"%s\": %s"),
552 dbs
[cnt
].db_filename
, msg
);
553 unlink (dbs
[cnt
].db_filename
);
555 else if (head
.module
== 0 && head
.data_size
== 0)
557 /* The file has been created, but the head has not
558 been initialized yet. */
559 msg
= _("uninitialized header");
562 else if (head
.header_size
!= (int) sizeof (head
))
564 msg
= _("header size does not match");
567 else if ((total
= (sizeof (head
)
568 + roundup (head
.module
* sizeof (ref_t
),
572 || total
< sizeof (head
))
574 msg
= _("file size does not match");
577 /* Note we map with the maximum size allowed for the
578 database. This is likely much larger than the
579 actual file size. This is OK on most OSes since
580 extensions of the underlying file will
581 automatically translate more pages available for
583 else if ((mem
= mmap (NULL
, dbs
[cnt
].max_db_size
,
584 PROT_READ
| PROT_WRITE
,
588 else if (!verify_persistent_db (mem
, &head
, cnt
))
591 msg
= _("verification failed");
596 /* Success. We have the database. */
598 dbs
[cnt
].memsize
= total
;
599 dbs
[cnt
].data
= (char *)
600 &dbs
[cnt
].head
->array
[roundup (dbs
[cnt
].head
->module
,
601 ALIGN
/ sizeof (ref_t
))];
602 dbs
[cnt
].mmap_used
= true;
604 if (dbs
[cnt
].suggested_module
> head
.module
)
605 dbg_log (_("suggested size of table for database %s larger than the persistent database's table"),
610 /* We also need a read-only descriptor. */
613 dbs
[cnt
].ro_fd
= open (dbs
[cnt
].db_filename
,
614 O_RDONLY
| EXTRA_O_FLAGS
);
615 if (dbs
[cnt
].ro_fd
== -1)
617 cannot create read-only descriptor for \"%s\"; no mmap"),
618 dbs
[cnt
].db_filename
);
621 // XXX Shall we test whether the descriptors actually
622 // XXX point to the same file?
625 /* Close the file descriptors in case something went
626 wrong in which case the variable have not been
633 if (dbs
[cnt
].head
== NULL
)
635 /* No database loaded. Allocate the data structure,
637 struct database_pers_head head
;
638 size_t total
= (sizeof (head
)
639 + roundup (dbs
[cnt
].suggested_module
640 * sizeof (ref_t
), ALIGN
)
641 + (dbs
[cnt
].suggested_module
642 * DEFAULT_DATASIZE_PER_BUCKET
));
644 /* Try to create the database. If we do not need a
645 persistent database create a temporary file. */
648 if (dbs
[cnt
].persistent
)
650 fd
= open (dbs
[cnt
].db_filename
,
651 O_RDWR
| O_CREAT
| O_EXCL
| O_TRUNC
| EXTRA_O_FLAGS
,
653 if (fd
!= -1 && dbs
[cnt
].shared
)
654 ro_fd
= open (dbs
[cnt
].db_filename
,
655 O_RDONLY
| EXTRA_O_FLAGS
);
659 char fname
[] = _PATH_NSCD_XYZ_DB_TMP
;
660 fd
= mkostemp (fname
, EXTRA_O_FLAGS
);
662 /* We do not need the file name anymore after we
663 opened another file descriptor in read-only mode. */
667 ro_fd
= open (fname
, O_RDONLY
| EXTRA_O_FLAGS
);
677 dbg_log (_("database for %s corrupted or simultaneously used; remove %s manually if necessary and restart"),
678 dbnames
[cnt
], dbs
[cnt
].db_filename
);
679 // XXX Correct way to terminate?
683 if (dbs
[cnt
].persistent
)
684 dbg_log (_("cannot create %s; no persistent database used"),
685 dbs
[cnt
].db_filename
);
687 dbg_log (_("cannot create %s; no sharing possible"),
688 dbs
[cnt
].db_filename
);
690 dbs
[cnt
].persistent
= 0;
691 // XXX remember: no mmap
695 /* Tell the user if we could not create the read-only
697 if (ro_fd
== -1 && dbs
[cnt
].shared
)
699 cannot create read-only descriptor for \"%s\"; no mmap"),
700 dbs
[cnt
].db_filename
);
702 /* Before we create the header, initialiye the hash
703 table. So that if we get interrupted if writing
704 the header we can recognize a partially initialized
706 size_t ps
= sysconf (_SC_PAGESIZE
);
708 assert (~ENDREF
== 0);
709 memset (tmpbuf
, '\xff', ps
);
711 size_t remaining
= dbs
[cnt
].suggested_module
* sizeof (ref_t
);
712 off_t offset
= sizeof (head
);
715 if (offset
% ps
!= 0)
717 towrite
= MIN (remaining
, ps
- (offset
% ps
));
718 if (pwrite (fd
, tmpbuf
, towrite
, offset
) != towrite
)
721 remaining
-= towrite
;
724 while (remaining
> ps
)
726 if (pwrite (fd
, tmpbuf
, ps
, offset
) == -1)
733 && pwrite (fd
, tmpbuf
, remaining
, offset
) != remaining
)
736 /* Create the header of the file. */
737 struct database_pers_head head
=
739 .version
= DB_VERSION
,
740 .header_size
= sizeof (head
),
741 .module
= dbs
[cnt
].suggested_module
,
742 .data_size
= (dbs
[cnt
].suggested_module
743 * DEFAULT_DATASIZE_PER_BUCKET
),
748 if ((TEMP_FAILURE_RETRY (write (fd
, &head
, sizeof (head
)))
750 || (TEMP_FAILURE_RETRY_VAL (posix_fallocate (fd
, 0, total
))
752 || (mem
= mmap (NULL
, dbs
[cnt
].max_db_size
,
753 PROT_READ
| PROT_WRITE
,
754 MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
757 unlink (dbs
[cnt
].db_filename
);
758 dbg_log (_("cannot write to database file %s: %s"),
759 dbs
[cnt
].db_filename
, strerror (errno
));
760 dbs
[cnt
].persistent
= 0;
766 dbs
[cnt
].data
= (char *)
767 &dbs
[cnt
].head
->array
[roundup (dbs
[cnt
].head
->module
,
768 ALIGN
/ sizeof (ref_t
))];
769 dbs
[cnt
].memsize
= total
;
770 dbs
[cnt
].mmap_used
= true;
772 /* Remember the descriptors. */
774 dbs
[cnt
].ro_fd
= ro_fd
;
786 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
787 /* We do not check here whether the O_CLOEXEC provided to the
788 open call was successful or not. The two fcntl calls are
789 only performed once each per process start-up and therefore
790 is not noticeable at all. */
792 && ((dbs
[cnt
].wr_fd
!= -1
793 && fcntl (dbs
[cnt
].wr_fd
, F_SETFD
, FD_CLOEXEC
) == -1)
794 || (dbs
[cnt
].ro_fd
!= -1
795 && fcntl (dbs
[cnt
].ro_fd
, F_SETFD
, FD_CLOEXEC
) == -1)))
798 cannot set socket to close on exec: %s; disabling paranoia mode"),
804 if (dbs
[cnt
].head
== NULL
)
806 /* We do not use the persistent database. Just
807 create an in-memory data structure. */
808 assert (! dbs
[cnt
].persistent
);
810 dbs
[cnt
].head
= xmalloc (sizeof (struct database_pers_head
)
811 + (dbs
[cnt
].suggested_module
813 memset (dbs
[cnt
].head
, '\0', sizeof (struct database_pers_head
));
814 assert (~ENDREF
== 0);
815 memset (dbs
[cnt
].head
->array
, '\xff',
816 dbs
[cnt
].suggested_module
* sizeof (ref_t
));
817 dbs
[cnt
].head
->module
= dbs
[cnt
].suggested_module
;
818 dbs
[cnt
].head
->data_size
= (DEFAULT_DATASIZE_PER_BUCKET
819 * dbs
[cnt
].head
->module
);
820 dbs
[cnt
].data
= xmalloc (dbs
[cnt
].head
->data_size
);
821 dbs
[cnt
].head
->first_free
= 0;
824 assert (dbs
[cnt
].ro_fd
== -1);
827 dbs
[cnt
].inotify_descr
= -1;
828 if (dbs
[cnt
].check_file
)
832 || (dbs
[cnt
].inotify_descr
833 = inotify_add_watch (inotify_fd
, dbs
[cnt
].filename
,
834 IN_DELETE_SELF
| IN_MODIFY
)) < 0)
835 /* We cannot notice changes in the main thread. */
838 /* We need the modification date of the file. */
841 if (stat64 (dbs
[cnt
].filename
, &st
) < 0)
843 /* We cannot stat() the file, disable file checking. */
844 dbg_log (_("cannot stat() file `%s': %s"),
845 dbs
[cnt
].filename
, strerror (errno
));
846 dbs
[cnt
].check_file
= 0;
849 dbs
[cnt
].file_mtime
= st
.st_mtime
;
854 if (cnt
== hstdb
&& inotify_fd
>= -1)
855 /* We also monitor the resolver configuration file. */
856 resolv_conf_descr
= inotify_add_watch (inotify_fd
,
858 IN_DELETE_SELF
| IN_MODIFY
);
862 /* Create the socket. */
863 sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
866 dbg_log (_("cannot open socket: %s"), strerror (errno
));
867 exit (errno
== EACCES
? 4 : 1);
869 /* Bind a name to the socket. */
870 struct sockaddr_un sock_addr
;
871 sock_addr
.sun_family
= AF_UNIX
;
872 strcpy (sock_addr
.sun_path
, _PATH_NSCDSOCKET
);
873 if (bind (sock
, (struct sockaddr
*) &sock_addr
, sizeof (sock_addr
)) < 0)
875 dbg_log ("%s: %s", _PATH_NSCDSOCKET
, strerror (errno
));
876 exit (errno
== EACCES
? 4 : 1);
879 /* We don't want to get stuck on accept. */
880 int fl
= fcntl (sock
, F_GETFL
);
881 if (fl
== -1 || fcntl (sock
, F_SETFL
, fl
| O_NONBLOCK
) == -1)
883 dbg_log (_("cannot change socket to nonblocking mode: %s"),
888 /* The descriptor needs to be closed on exec. */
889 if (paranoia
&& fcntl (sock
, F_SETFD
, FD_CLOEXEC
) == -1)
891 dbg_log (_("cannot set socket to close on exec: %s"),
896 /* Set permissions for the socket. */
897 chmod (_PATH_NSCDSOCKET
, DEFFILEMODE
);
899 /* Set the socket up to accept connections. */
900 if (listen (sock
, SOMAXCONN
) < 0)
902 dbg_log (_("cannot enable socket to accept connections: %s"),
907 /* Change to unprivileged uid/gid/groups if specifed in config file */
908 if (server_user
!= NULL
)
909 finish_drop_privileges ();
913 /* Close the connections. */
922 invalidate_cache (char *key
, int fd
)
927 for (number
= pwddb
; number
< lastdb
; ++number
)
928 if (strcmp (key
, dbnames
[number
]) == 0)
930 if (dbs
[number
].reset_res
)
936 if (number
== lastdb
)
939 writeall (fd
, &resp
, sizeof (resp
));
943 if (dbs
[number
].enabled
)
945 pthread_mutex_lock (&dbs
[number
].prune_lock
);
946 prune_cache (&dbs
[number
], LONG_MAX
, fd
);
947 pthread_mutex_unlock (&dbs
[number
].prune_lock
);
952 writeall (fd
, &resp
, sizeof (resp
));
959 send_ro_fd (struct database_dyn
*db
, char *key
, int fd
)
961 /* If we do not have an read-only file descriptor do nothing. */
965 /* We need to send some data along with the descriptor. */
966 uint64_t mapsize
= (db
->head
->data_size
967 + roundup (db
->head
->module
* sizeof (ref_t
), ALIGN
)
968 + sizeof (struct database_pers_head
));
970 iov
[0].iov_base
= key
;
971 iov
[0].iov_len
= strlen (key
) + 1;
972 iov
[1].iov_base
= &mapsize
;
973 iov
[1].iov_len
= sizeof (mapsize
);
975 /* Prepare the control message to transfer the descriptor. */
979 char bytes
[CMSG_SPACE (sizeof (int))];
981 struct msghdr msg
= { .msg_iov
= iov
, .msg_iovlen
= 2,
982 .msg_control
= buf
.bytes
,
983 .msg_controllen
= sizeof (buf
) };
984 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR (&msg
);
986 cmsg
->cmsg_level
= SOL_SOCKET
;
987 cmsg
->cmsg_type
= SCM_RIGHTS
;
988 cmsg
->cmsg_len
= CMSG_LEN (sizeof (int));
990 *(int *) CMSG_DATA (cmsg
) = db
->ro_fd
;
992 msg
.msg_controllen
= cmsg
->cmsg_len
;
994 /* Send the control message. We repeat when we are interrupted but
995 everything else is ignored. */
997 # define MSG_NOSIGNAL 0
999 (void) TEMP_FAILURE_RETRY (sendmsg (fd
, &msg
, MSG_NOSIGNAL
));
1001 if (__builtin_expect (debug_level
> 0, 0))
1002 dbg_log (_("provide access to FD %d, for %s"), db
->ro_fd
, key
);
1004 #endif /* SCM_RIGHTS */
1007 /* Handle new request. */
1009 handle_request (int fd
, request_header
*req
, void *key
, uid_t uid
, pid_t pid
)
1011 if (__builtin_expect (req
->version
, NSCD_VERSION
) != NSCD_VERSION
)
1013 if (debug_level
> 0)
1015 cannot handle old request version %d; current version is %d"),
1016 req
->version
, NSCD_VERSION
);
1020 /* Perform the SELinux check before we go on to the standard checks. */
1021 if (selinux_enabled
&& nscd_request_avc_has_perm (fd
, req
->type
) != 0)
1023 if (debug_level
> 0)
1032 snprintf (buf
, sizeof (buf
), "/proc/%ld/exe", (long int) pid
);
1033 ssize_t n
= readlink (buf
, buf
, sizeof (buf
) - 1);
1037 request from %ld not handled due to missing permission"), (long int) pid
);
1042 request from '%s' [%ld] not handled due to missing permission"),
1043 buf
, (long int) pid
);
1046 dbg_log (_("request not handled due to missing permission"));
1052 struct database_dyn
*db
= reqinfo
[req
->type
].db
;
1054 /* See whether we can service the request from the cache. */
1055 if (__builtin_expect (reqinfo
[req
->type
].data_request
, true))
1057 if (__builtin_expect (debug_level
, 0) > 0)
1059 if (req
->type
== GETHOSTBYADDR
|| req
->type
== GETHOSTBYADDRv6
)
1061 char buf
[INET6_ADDRSTRLEN
];
1063 dbg_log ("\t%s (%s)", serv2str
[req
->type
],
1064 inet_ntop (req
->type
== GETHOSTBYADDR
1065 ? AF_INET
: AF_INET6
,
1066 key
, buf
, sizeof (buf
)));
1069 dbg_log ("\t%s (%s)", serv2str
[req
->type
], (char *) key
);
1072 /* Is this service enabled? */
1073 if (__builtin_expect (!db
->enabled
, 0))
1075 /* No, sent the prepared record. */
1076 if (TEMP_FAILURE_RETRY (send (fd
, db
->disabled_iov
->iov_base
,
1077 db
->disabled_iov
->iov_len
,
1079 != (ssize_t
) db
->disabled_iov
->iov_len
1080 && __builtin_expect (debug_level
, 0) > 0)
1082 /* We have problems sending the result. */
1084 dbg_log (_("cannot write result: %s"),
1085 strerror_r (errno
, buf
, sizeof (buf
)));
1091 /* Be sure we can read the data. */
1092 if (__builtin_expect (pthread_rwlock_tryrdlock (&db
->lock
) != 0, 0))
1094 ++db
->head
->rdlockdelayed
;
1095 pthread_rwlock_rdlock (&db
->lock
);
1098 /* See whether we can handle it from the cache. */
1099 struct datahead
*cached
;
1100 cached
= (struct datahead
*) cache_search (req
->type
, key
, req
->key_len
,
1104 /* Hurray it's in the cache. */
1107 #ifdef HAVE_SENDFILE
1108 if (__builtin_expect (db
->mmap_used
, 1))
1110 assert (db
->wr_fd
!= -1);
1111 assert ((char *) cached
->data
> (char *) db
->data
);
1112 assert ((char *) cached
->data
- (char *) db
->head
1114 <= (sizeof (struct database_pers_head
)
1115 + db
->head
->module
* sizeof (ref_t
)
1116 + db
->head
->data_size
));
1117 nwritten
= sendfileall (fd
, db
->wr_fd
,
1118 (char *) cached
->data
1119 - (char *) db
->head
, cached
->recsize
);
1120 # ifndef __ASSUME_SENDFILE
1121 if (nwritten
== -1 && errno
== ENOSYS
)
1126 # ifndef __ASSUME_SENDFILE
1130 nwritten
= writeall (fd
, cached
->data
, cached
->recsize
);
1132 if (nwritten
!= cached
->recsize
1133 && __builtin_expect (debug_level
, 0) > 0)
1135 /* We have problems sending the result. */
1137 dbg_log (_("cannot write result: %s"),
1138 strerror_r (errno
, buf
, sizeof (buf
)));
1141 pthread_rwlock_unlock (&db
->lock
);
1146 pthread_rwlock_unlock (&db
->lock
);
1148 else if (__builtin_expect (debug_level
, 0) > 0)
1150 if (req
->type
== INVALIDATE
)
1151 dbg_log ("\t%s (%s)", serv2str
[req
->type
], (char *) key
);
1153 dbg_log ("\t%s", serv2str
[req
->type
]);
1156 /* Handle the request. */
1160 addpwbyname (db
, fd
, req
, key
, uid
);
1164 addpwbyuid (db
, fd
, req
, key
, uid
);
1168 addgrbyname (db
, fd
, req
, key
, uid
);
1172 addgrbygid (db
, fd
, req
, key
, uid
);
1176 addhstbyname (db
, fd
, req
, key
, uid
);
1179 case GETHOSTBYNAMEv6
:
1180 addhstbynamev6 (db
, fd
, req
, key
, uid
);
1184 addhstbyaddr (db
, fd
, req
, key
, uid
);
1187 case GETHOSTBYADDRv6
:
1188 addhstbyaddrv6 (db
, fd
, req
, key
, uid
);
1192 addhstai (db
, fd
, req
, key
, uid
);
1196 addinitgroups (db
, fd
, req
, key
, uid
);
1200 addservbyname (db
, fd
, req
, key
, uid
);
1204 addservbyport (db
, fd
, req
, key
, uid
);
1211 /* Get the callers credentials. */
1213 struct ucred caller
;
1214 socklen_t optlen
= sizeof (caller
);
1216 if (getsockopt (fd
, SOL_SOCKET
, SO_PEERCRED
, &caller
, &optlen
) < 0)
1220 dbg_log (_("error getting caller's id: %s"),
1221 strerror_r (errno
, buf
, sizeof (buf
)));
1227 /* Some systems have no SO_PEERCRED implementation. They don't
1228 care about security so we don't as well. */
1233 /* Accept shutdown, getstat and invalidate only from root. For
1234 the stat call also allow the user specified in the config file. */
1235 if (req
->type
== GETSTAT
)
1237 if (uid
== 0 || uid
== stat_uid
)
1238 send_stats (fd
, dbs
);
1242 if (req
->type
== INVALIDATE
)
1243 invalidate_cache (key
, fd
);
1245 termination_handler (0);
1254 send_ro_fd (reqinfo
[req
->type
].db
, key
, fd
);
1259 /* Ignore the command, it's nothing we know. */
1265 /* Restart the process. */
1269 /* First determine the parameters. We do not use the parameters
1270 passed to main() since in case nscd is started by running the
1271 dynamic linker this will not work. Yes, this is not the usual
1272 case but nscd is part of glibc and we occasionally do this. */
1273 size_t buflen
= 1024;
1274 char *buf
= alloca (buflen
);
1276 int fd
= open ("/proc/self/cmdline", O_RDONLY
);
1280 cannot open /proc/self/cmdline: %s; disabling paranoia mode"),
1289 ssize_t n
= TEMP_FAILURE_RETRY (read (fd
, buf
+ readlen
,
1294 cannot read /proc/self/cmdline: %s; disabling paranoia mode"),
1304 if (readlen
< buflen
)
1307 /* We might have to extend the buffer. */
1308 size_t old_buflen
= buflen
;
1309 char *newp
= extend_alloca (buf
, buflen
, 2 * buflen
);
1310 buf
= memmove (newp
, buf
, old_buflen
);
1315 /* Parse the command line. Worst case scenario: every two
1316 characters form one parameter (one character plus NUL). */
1317 char **argv
= alloca ((readlen
/ 2 + 1) * sizeof (argv
[0]));
1321 while (cp
< buf
+ readlen
)
1324 cp
= (char *) rawmemchr (cp
, '\0') + 1;
1328 /* Second, change back to the old user if we changed it. */
1329 if (server_user
!= NULL
)
1331 if (setresuid (old_uid
, old_uid
, old_uid
) != 0)
1334 cannot change to old UID: %s; disabling paranoia mode"),
1341 if (setresgid (old_gid
, old_gid
, old_gid
) != 0)
1344 cannot change to old GID: %s; disabling paranoia mode"),
1347 setuid (server_uid
);
1353 /* Next change back to the old working directory. */
1354 if (chdir (oldcwd
) == -1)
1357 cannot change to old working directory: %s; disabling paranoia mode"),
1360 if (server_user
!= NULL
)
1362 setuid (server_uid
);
1363 setgid (server_gid
);
1369 /* Synchronize memory. */
1370 int32_t certainly
[lastdb
];
1371 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
1372 if (dbs
[cnt
].enabled
)
1374 /* Make sure nobody keeps using the database. */
1375 dbs
[cnt
].head
->timestamp
= 0;
1376 certainly
[cnt
] = dbs
[cnt
].head
->nscd_certainly_running
;
1377 dbs
[cnt
].head
->nscd_certainly_running
= 0;
1379 if (dbs
[cnt
].persistent
)
1381 msync (dbs
[cnt
].head
, dbs
[cnt
].memsize
, MS_ASYNC
);
1384 /* The preparations are done. */
1385 execv ("/proc/self/exe", argv
);
1387 /* If we come here, we will never be able to re-exec. */
1388 dbg_log (_("re-exec failed: %s; disabling paranoia mode"),
1391 if (server_user
!= NULL
)
1393 setuid (server_uid
);
1394 setgid (server_gid
);
1396 if (chdir ("/") != 0)
1397 dbg_log (_("cannot change current working directory to \"/\": %s"),
1401 /* Reenable the databases. */
1402 time_t now
= time (NULL
);
1403 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
1404 if (dbs
[cnt
].enabled
)
1406 dbs
[cnt
].head
->timestamp
= now
;
1407 dbs
[cnt
].head
->nscd_certainly_running
= certainly
[cnt
];
1412 /* List of file descriptors. */
1416 struct fdlist
*next
;
1418 /* Memory allocated for the list. */
1419 static struct fdlist
*fdlist
;
1420 /* List of currently ready-to-read file descriptors. */
1421 static struct fdlist
*readylist
;
1423 /* Conditional variable and mutex to signal availability of entries in
1424 READYLIST. The condvar is initialized dynamically since we might
1425 use a different clock depending on availability. */
1426 static pthread_cond_t readylist_cond
= PTHREAD_COND_INITIALIZER
;
1427 static pthread_mutex_t readylist_lock
= PTHREAD_MUTEX_INITIALIZER
;
1429 /* The clock to use with the condvar. */
1430 static clockid_t timeout_clock
= CLOCK_REALTIME
;
1432 /* Number of threads ready to handle the READYLIST. */
1433 static unsigned long int nready
;
1436 /* Function for the clean-up threads. */
1438 __attribute__ ((__noreturn__
))
1439 nscd_run_prune (void *p
)
1441 const long int my_number
= (long int) p
;
1442 assert (dbs
[my_number
].enabled
);
1444 int dont_need_update
= setup_thread (&dbs
[my_number
]);
1446 time_t now
= time (NULL
);
1448 /* We are running. */
1449 dbs
[my_number
].head
->timestamp
= now
;
1451 struct timespec prune_ts
;
1452 if (__builtin_expect (clock_gettime (timeout_clock
, &prune_ts
) == -1, 0))
1453 /* Should never happen. */
1456 /* Compute the initial timeout time. Prevent all the timers to go
1457 off at the same time by adding a db-based value. */
1458 prune_ts
.tv_sec
+= CACHE_PRUNE_INTERVAL
+ my_number
;
1459 dbs
[my_number
].wakeup_time
= now
+ CACHE_PRUNE_INTERVAL
+ my_number
;
1461 pthread_mutex_t
*prune_lock
= &dbs
[my_number
].prune_lock
;
1462 pthread_cond_t
*prune_cond
= &dbs
[my_number
].prune_cond
;
1464 pthread_mutex_lock (prune_lock
);
1467 /* Wait, but not forever. */
1469 if (! dbs
[my_number
].clear_cache
)
1470 e
= pthread_cond_timedwait (prune_cond
, prune_lock
, &prune_ts
);
1471 assert (__builtin_expect (e
== 0 || e
== ETIMEDOUT
, 1));
1475 if (e
== ETIMEDOUT
|| now
>= dbs
[my_number
].wakeup_time
1476 || dbs
[my_number
].clear_cache
)
1478 /* We will determine the new timout values based on the
1479 cache content. Should there be concurrent additions to
1480 the cache which are not accounted for in the cache
1481 pruning we want to know about it. Therefore set the
1482 timeout to the maximum. It will be descreased when adding
1483 new entries to the cache, if necessary. */
1484 if (sizeof (time_t) == sizeof (long int))
1485 dbs
[my_number
].wakeup_time
= LONG_MAX
;
1487 dbs
[my_number
].wakeup_time
= INT_MAX
;
1489 /* Unconditionally reset the flag. */
1490 time_t prune_now
= dbs
[my_number
].clear_cache
? LONG_MAX
: now
;
1491 dbs
[my_number
].clear_cache
= 0;
1493 pthread_mutex_unlock (prune_lock
);
1495 next_wait
= prune_cache (&dbs
[my_number
], prune_now
, -1);
1497 next_wait
= MAX (next_wait
, CACHE_PRUNE_INTERVAL
);
1498 /* If clients cannot determine for sure whether nscd is running
1499 we need to wake up occasionally to update the timestamp.
1500 Wait 90% of the update period. */
1501 #define UPDATE_MAPPING_TIMEOUT (MAPPING_TIMEOUT * 9 / 10)
1502 if (__builtin_expect (! dont_need_update
, 0))
1504 next_wait
= MIN (UPDATE_MAPPING_TIMEOUT
, next_wait
);
1505 dbs
[my_number
].head
->timestamp
= now
;
1508 pthread_mutex_lock (prune_lock
);
1510 /* Make it known when we will wake up again. */
1511 if (now
+ next_wait
< dbs
[my_number
].wakeup_time
)
1512 dbs
[my_number
].wakeup_time
= now
+ next_wait
;
1514 next_wait
= dbs
[my_number
].wakeup_time
- now
;
1517 /* The cache was just pruned. Do not do it again now. Just
1518 use the new timeout value. */
1519 next_wait
= dbs
[my_number
].wakeup_time
- now
;
1521 if (clock_gettime (timeout_clock
, &prune_ts
) == -1)
1522 /* Should never happen. */
1525 /* Compute next timeout time. */
1526 prune_ts
.tv_sec
+= next_wait
;
1531 /* This is the main loop. It is replicated in different threads but
1532 the the use of the ready list makes sure only one thread handles an
1533 incoming connection. */
1535 __attribute__ ((__noreturn__
))
1536 nscd_run_worker (void *p
)
1540 /* Initialize the memory-in-flight list. */
1541 for (enum in_flight idx
= 0; idx
< IDX_last
; ++idx
)
1542 mem_in_flight
.block
[idx
].dbidx
= -1;
1543 /* And queue this threads structure. */
1545 mem_in_flight
.next
= mem_in_flight_list
;
1546 while (atomic_compare_and_exchange_bool_acq (&mem_in_flight_list
,
1548 mem_in_flight
.next
) != 0);
1550 /* Initial locking. */
1551 pthread_mutex_lock (&readylist_lock
);
1553 /* One more thread available. */
1558 while (readylist
== NULL
)
1559 pthread_cond_wait (&readylist_cond
, &readylist_lock
);
1561 struct fdlist
*it
= readylist
->next
;
1562 if (readylist
->next
== readylist
)
1563 /* Just one entry on the list. */
1566 readylist
->next
= it
->next
;
1568 /* Extract the information and mark the record ready to be used
1573 /* One more thread available. */
1576 /* We are done with the list. */
1577 pthread_mutex_unlock (&readylist_lock
);
1579 /* We do not want to block on a short read or so. */
1580 int fl
= fcntl (fd
, F_GETFL
);
1581 if (fl
== -1 || fcntl (fd
, F_SETFL
, fl
| O_NONBLOCK
) == -1)
1584 /* Now read the request. */
1586 if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd
, &req
, sizeof (req
)))
1587 != sizeof (req
), 0))
1589 /* We failed to read data. Note that this also might mean we
1590 failed because we would have blocked. */
1591 if (debug_level
> 0)
1592 dbg_log (_("short read while reading request: %s"),
1593 strerror_r (errno
, buf
, sizeof (buf
)));
1597 /* Check whether this is a valid request type. */
1598 if (req
.type
< GETPWBYNAME
|| req
.type
>= LASTREQ
)
1601 /* Some systems have no SO_PEERCRED implementation. They don't
1602 care about security so we don't as well. */
1607 if (__builtin_expect (debug_level
> 0, 0))
1609 struct ucred caller
;
1610 socklen_t optlen
= sizeof (caller
);
1612 if (getsockopt (fd
, SOL_SOCKET
, SO_PEERCRED
, &caller
, &optlen
) == 0)
1616 const pid_t pid
= 0;
1619 /* It should not be possible to crash the nscd with a silly
1620 request (i.e., a terribly large key). We limit the size to 1kb. */
1621 if (__builtin_expect (req
.key_len
, 1) < 0
1622 || __builtin_expect (req
.key_len
, 1) > MAXKEYLEN
)
1624 if (debug_level
> 0)
1625 dbg_log (_("key length in request too long: %d"), req
.key_len
);
1630 char keybuf
[MAXKEYLEN
];
1632 if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd
, keybuf
,
1636 /* Again, this can also mean we would have blocked. */
1637 if (debug_level
> 0)
1638 dbg_log (_("short read while reading request key: %s"),
1639 strerror_r (errno
, buf
, sizeof (buf
)));
1643 if (__builtin_expect (debug_level
, 0) > 0)
1648 handle_request: request received (Version = %d) from PID %ld"),
1649 req
.version
, (long int) pid
);
1653 handle_request: request received (Version = %d)"), req
.version
);
1656 /* Phew, we got all the data, now process it. */
1657 handle_request (fd
, &req
, keybuf
, uid
, pid
);
1665 pthread_mutex_lock (&readylist_lock
);
1667 /* One more thread available. */
1673 static unsigned int nconns
;
1678 pthread_mutex_lock (&readylist_lock
);
1680 /* Find an empty entry in FDLIST. */
1682 for (inner
= 0; inner
< nconns
; ++inner
)
1683 if (fdlist
[inner
].next
== NULL
)
1685 assert (inner
< nconns
);
1687 fdlist
[inner
].fd
= fd
;
1689 if (readylist
== NULL
)
1690 readylist
= fdlist
[inner
].next
= &fdlist
[inner
];
1693 fdlist
[inner
].next
= readylist
->next
;
1694 readylist
= readylist
->next
= &fdlist
[inner
];
1697 bool do_signal
= true;
1698 if (__builtin_expect (nready
== 0, 0))
1703 /* Try to start another thread to help out. */
1705 if (nthreads
< max_nthreads
1706 && pthread_create (&th
, &attr
, nscd_run_worker
,
1707 (void *) (long int) nthreads
) == 0)
1709 /* We got another thread. */
1711 /* The new thread might need a kick. */
1717 pthread_mutex_unlock (&readylist_lock
);
1719 /* Tell one of the worker threads there is work to do. */
1721 pthread_cond_signal (&readylist_cond
);
1725 /* Check whether restarting should happen. */
1727 restart_p (time_t now
)
1729 return (paranoia
&& readylist
== NULL
&& nready
== nthreads
1730 && now
>= restart_time
);
1734 /* Array for times a connection was accepted. */
1735 static time_t *starttime
;
1739 __attribute__ ((__noreturn__
))
1740 main_loop_poll (void)
1742 struct pollfd
*conns
= (struct pollfd
*) xmalloc (nconns
1743 * sizeof (conns
[0]));
1746 conns
[0].events
= POLLRDNORM
;
1748 size_t firstfree
= 1;
1751 if (inotify_fd
!= -1)
1753 conns
[1].fd
= inotify_fd
;
1754 conns
[1].events
= POLLRDNORM
;
1762 /* Wait for any event. We wait at most a couple of seconds so
1763 that we can check whether we should close any of the accepted
1764 connections since we have not received a request. */
1765 #define MAX_ACCEPT_TIMEOUT 30
1766 #define MIN_ACCEPT_TIMEOUT 5
1767 #define MAIN_THREAD_TIMEOUT \
1768 (MAX_ACCEPT_TIMEOUT * 1000 \
1769 - ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * 1000 * nused) / (2 * nconns))
1771 int n
= poll (conns
, nused
, MAIN_THREAD_TIMEOUT
);
1773 time_t now
= time (NULL
);
1775 /* If there is a descriptor ready for reading or there is a new
1776 connection, process this now. */
1779 if (conns
[0].revents
!= 0)
1781 /* We have a new incoming connection. Accept the connection. */
1782 int fd
= TEMP_FAILURE_RETRY (accept (sock
, NULL
, NULL
));
1784 /* Use the descriptor if we have not reached the limit. */
1787 if (firstfree
< nconns
)
1789 conns
[firstfree
].fd
= fd
;
1790 conns
[firstfree
].events
= POLLRDNORM
;
1791 starttime
[firstfree
] = now
;
1792 if (firstfree
>= nused
)
1793 nused
= firstfree
+ 1;
1797 while (firstfree
< nused
&& conns
[firstfree
].fd
!= -1);
1800 /* We cannot use the connection so close it. */
1809 if (inotify_fd
!= -1 && conns
[1].fd
== inotify_fd
)
1811 if (conns
[1].revents
!= 0)
1813 bool to_clear
[lastdb
] = { false, };
1816 struct inotify_event i
;
1822 ssize_t nb
= TEMP_FAILURE_RETRY (read (inotify_fd
, &inev
,
1824 if (nb
< (ssize_t
) sizeof (struct inotify_event
))
1828 /* Something went wrong when reading the inotify
1829 data. Better disable inotify. */
1836 dbg_log (_("disabled inotify after read error"));
1841 /* Check which of the files changed. */
1842 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
1843 if (inev
.i
.wd
== dbs
[dbcnt
].inotify_descr
)
1845 to_clear
[dbcnt
] = true;
1849 if (inev
.i
.wd
== resolv_conf_descr
)
1852 to_clear
[hstdb
] = true;
1857 /* Actually perform the cache clearing. */
1858 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
1859 if (to_clear
[dbcnt
])
1861 pthread_mutex_lock (&dbs
[dbcnt
].prune_lock
);
1862 dbs
[dbcnt
].clear_cache
= 1;
1863 pthread_mutex_unlock (&dbs
[dbcnt
].prune_lock
);
1864 pthread_cond_signal (&dbs
[dbcnt
].prune_cond
);
1874 for (size_t cnt
= first
; cnt
< nused
&& n
> 0; ++cnt
)
1875 if (conns
[cnt
].revents
!= 0)
1877 fd_ready (conns
[cnt
].fd
);
1879 /* Clean up the CONNS array. */
1881 if (cnt
< firstfree
)
1883 if (cnt
== nused
- 1)
1886 while (conns
[nused
- 1].fd
== -1);
1892 /* Now find entries which have timed out. */
1895 /* We make the timeout length depend on the number of file
1896 descriptors currently used. */
1897 #define ACCEPT_TIMEOUT \
1898 (MAX_ACCEPT_TIMEOUT \
1899 - ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * nused) / nconns)
1900 time_t laststart
= now
- ACCEPT_TIMEOUT
;
1902 for (size_t cnt
= nused
- 1; cnt
> 0; --cnt
)
1904 if (conns
[cnt
].fd
!= -1 && starttime
[cnt
] < laststart
)
1906 /* Remove the entry, it timed out. */
1907 (void) close (conns
[cnt
].fd
);
1910 if (cnt
< firstfree
)
1912 if (cnt
== nused
- 1)
1915 while (conns
[nused
- 1].fd
== -1);
1919 if (restart_p (now
))
1927 main_loop_epoll (int efd
)
1929 struct epoll_event ev
= { 0, };
1933 /* Add the socket. */
1934 ev
.events
= EPOLLRDNORM
;
1936 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, sock
, &ev
) == -1)
1937 /* We cannot use epoll. */
1941 if (inotify_fd
!= -1)
1943 ev
.events
= EPOLLRDNORM
;
1944 ev
.data
.fd
= inotify_fd
;
1945 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, inotify_fd
, &ev
) == -1)
1946 /* We cannot use epoll. */
1954 struct epoll_event revs
[100];
1955 # define nrevs (sizeof (revs) / sizeof (revs[0]))
1957 int n
= epoll_wait (efd
, revs
, nrevs
, MAIN_THREAD_TIMEOUT
);
1959 time_t now
= time (NULL
);
1961 for (int cnt
= 0; cnt
< n
; ++cnt
)
1962 if (revs
[cnt
].data
.fd
== sock
)
1964 /* A new connection. */
1965 int fd
= TEMP_FAILURE_RETRY (accept (sock
, NULL
, NULL
));
1969 /* Try to add the new descriptor. */
1972 || epoll_ctl (efd
, EPOLL_CTL_ADD
, fd
, &ev
) == -1)
1973 /* The descriptor is too large or something went
1974 wrong. Close the descriptor. */
1978 /* Remember when we accepted the connection. */
1979 starttime
[fd
] = now
;
1989 else if (revs
[cnt
].data
.fd
== inotify_fd
)
1991 bool to_clear
[lastdb
] = { false, };
1994 struct inotify_event i
;
2000 ssize_t nb
= TEMP_FAILURE_RETRY (read (inotify_fd
, &inev
,
2002 if (nb
< (ssize_t
) sizeof (struct inotify_event
))
2006 /* Something went wrong when reading the inotify
2007 data. Better disable inotify. */
2008 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, inotify_fd
,
2012 dbg_log (_("disabled inotify after read error"));
2017 /* Check which of the files changed. */
2018 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
2019 if (inev
.i
.wd
== dbs
[dbcnt
].inotify_descr
)
2021 to_clear
[dbcnt
] = true;
2025 if (inev
.i
.wd
== resolv_conf_descr
)
2028 to_clear
[hstdb
] = true;
2033 /* Actually perform the cache clearing. */
2034 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
2035 if (to_clear
[dbcnt
])
2037 pthread_mutex_lock (&dbs
[dbcnt
].prune_lock
);
2038 dbs
[dbcnt
].clear_cache
= 1;
2039 pthread_mutex_unlock (&dbs
[dbcnt
].prune_lock
);
2040 pthread_cond_signal (&dbs
[dbcnt
].prune_cond
);
2046 /* Remove the descriptor from the epoll descriptor. */
2047 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, revs
[cnt
].data
.fd
, NULL
);
2049 /* Get a worker to handle the request. */
2050 fd_ready (revs
[cnt
].data
.fd
);
2052 /* Reset the time. */
2053 starttime
[revs
[cnt
].data
.fd
] = 0;
2054 if (revs
[cnt
].data
.fd
== highest
)
2057 while (highest
> 0 && starttime
[highest
] == 0);
2062 /* Now look for descriptors for accepted connections which have
2063 no reply in too long of a time. */
2064 time_t laststart
= now
- ACCEPT_TIMEOUT
;
2065 assert (starttime
[sock
] == 0);
2066 assert (inotify_fd
== -1 || starttime
[inotify_fd
] == 0);
2067 for (int cnt
= highest
; cnt
> STDERR_FILENO
; --cnt
)
2068 if (starttime
[cnt
] != 0 && starttime
[cnt
] < laststart
)
2070 /* We are waiting for this one for too long. Close it. */
2071 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, cnt
, NULL
);
2079 else if (cnt
!= sock
&& starttime
[cnt
] == 0 && cnt
== highest
)
2082 if (restart_p (now
))
2089 /* Start all the threads we want. The initial process is thread no. 1. */
2091 start_threads (void)
2093 /* Initialize the conditional variable we will use. The only
2094 non-standard attribute we might use is the clock selection. */
2095 pthread_condattr_t condattr
;
2096 pthread_condattr_init (&condattr
);
2098 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0 \
2099 && defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
2100 /* Determine whether the monotonous clock is available. */
2101 struct timespec dummy
;
2102 # if _POSIX_MONOTONIC_CLOCK == 0
2103 if (sysconf (_SC_MONOTONIC_CLOCK
) > 0)
2105 # if _POSIX_CLOCK_SELECTION == 0
2106 if (sysconf (_SC_CLOCK_SELECTION
) > 0)
2108 if (clock_getres (CLOCK_MONOTONIC
, &dummy
) == 0
2109 && pthread_condattr_setclock (&condattr
, CLOCK_MONOTONIC
) == 0)
2110 timeout_clock
= CLOCK_MONOTONIC
;
2113 /* Create the attribute for the threads. They are all created
2115 pthread_attr_init (&attr
);
2116 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
2117 /* Use 1MB stacks, twice as much for 64-bit architectures. */
2118 pthread_attr_setstacksize (&attr
, NSCD_THREAD_STACKSIZE
);
2120 /* We allow less than LASTDB threads only for debugging. */
2121 if (debug_level
== 0)
2122 nthreads
= MAX (nthreads
, lastdb
);
2124 /* Create the threads which prune the databases. */
2125 // XXX Ideally this work would be done by some of the worker threads.
2126 // XXX But this is problematic since we would need to be able to wake
2127 // XXX them up explicitly as well as part of the group handling the
2128 // XXX ready-list. This requires an operation where we can wait on
2129 // XXX two conditional variables at the same time. This operation
2130 // XXX does not exist (yet).
2131 for (long int i
= 0; i
< lastdb
; ++i
)
2133 /* Initialize the conditional variable. */
2134 if (pthread_cond_init (&dbs
[i
].prune_cond
, &condattr
) != 0)
2136 dbg_log (_("could not initialize conditional variable"));
2142 && pthread_create (&th
, &attr
, nscd_run_prune
, (void *) i
) != 0)
2144 dbg_log (_("could not start clean-up thread; terminating"));
2149 pthread_condattr_destroy (&condattr
);
2151 for (long int i
= 0; i
< nthreads
; ++i
)
2154 if (pthread_create (&th
, &attr
, nscd_run_worker
, NULL
) != 0)
2158 dbg_log (_("could not start any worker thread; terminating"));
2166 /* Determine how much room for descriptors we should initially
2167 allocate. This might need to change later if we cap the number
2169 const long int nfds
= sysconf (_SC_OPEN_MAX
);
2171 #define MAXCONN 16384
2172 if (nfds
== -1 || nfds
> MAXCONN
)
2174 else if (nfds
< MINCONN
)
2179 /* We need memory to pass descriptors on to the worker threads. */
2180 fdlist
= (struct fdlist
*) xcalloc (nconns
, sizeof (fdlist
[0]));
2181 /* Array to keep track when connection was accepted. */
2182 starttime
= (time_t *) xcalloc (nconns
, sizeof (starttime
[0]));
2184 /* In the main thread we execute the loop which handles incoming
2187 int efd
= epoll_create (100);
2190 main_loop_epoll (efd
);
2199 /* Look up the uid, gid, and supplementary groups to run nscd as. When
2200 this function is called, we are not listening on the nscd socket yet so
2201 we can just use the ordinary lookup functions without causing a lockup */
2203 begin_drop_privileges (void)
2205 struct passwd
*pwd
= getpwnam (server_user
);
2209 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2210 error (EXIT_FAILURE
, 0, _("Failed to run nscd as user '%s'"),
2214 server_uid
= pwd
->pw_uid
;
2215 server_gid
= pwd
->pw_gid
;
2217 /* Save the old UID/GID if we have to change back. */
2220 old_uid
= getuid ();
2221 old_gid
= getgid ();
2224 if (getgrouplist (server_user
, server_gid
, NULL
, &server_ngroups
) == 0)
2226 /* This really must never happen. */
2227 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2228 error (EXIT_FAILURE
, errno
, _("initial getgrouplist failed"));
2231 server_groups
= (gid_t
*) xmalloc (server_ngroups
* sizeof (gid_t
));
2233 if (getgrouplist (server_user
, server_gid
, server_groups
, &server_ngroups
)
2236 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2237 error (EXIT_FAILURE
, errno
, _("getgrouplist failed"));
2242 /* Call setgroups(), setgid(), and setuid() to drop root privileges and
2243 run nscd as the user specified in the configuration file. */
2245 finish_drop_privileges (void)
2247 #if defined HAVE_LIBAUDIT && defined HAVE_LIBCAP
2248 /* We need to preserve the capabilities to connect to the audit daemon. */
2249 cap_t new_caps
= preserve_capabilities ();
2252 if (setgroups (server_ngroups
, server_groups
) == -1)
2254 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2255 error (EXIT_FAILURE
, errno
, _("setgroups failed"));
2260 res
= setresgid (server_gid
, server_gid
, old_gid
);
2262 res
= setgid (server_gid
);
2265 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2271 res
= setresuid (server_uid
, server_uid
, old_uid
);
2273 res
= setuid (server_uid
);
2276 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2281 #if defined HAVE_LIBAUDIT && defined HAVE_LIBCAP
2282 /* Remove the temporary capabilities. */
2283 install_real_capabilities (new_caps
);