1 /* Inner loops of cache daemon.
2 Copyright (C) 1998-2020 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, see <https://www.gnu.org/licenses/>. */
35 #include <arpa/inet.h>
37 # include <linux/netlink.h>
38 # include <linux/rtnetlink.h>
41 # include <sys/epoll.h>
44 # include <sys/inotify.h>
47 #include <sys/param.h>
49 #include <sys/socket.h>
56 #include <resolv/resolv.h>
58 #include <kernel-features.h>
59 #include <libc-diag.h>
62 /* Support to run nscd as an unprivileged user */
63 const char *server_user
;
64 static uid_t server_uid
;
65 static gid_t server_gid
;
66 const char *stat_user
;
68 static gid_t
*server_groups
;
72 static int server_ngroups
;
74 static pthread_attr_t attr
;
76 static void begin_drop_privileges (void);
77 static void finish_drop_privileges (void);
79 /* Map request type to a string. */
80 const char *const serv2str
[LASTREQ
] =
82 [GETPWBYNAME
] = "GETPWBYNAME",
83 [GETPWBYUID
] = "GETPWBYUID",
84 [GETGRBYNAME
] = "GETGRBYNAME",
85 [GETGRBYGID
] = "GETGRBYGID",
86 [GETHOSTBYNAME
] = "GETHOSTBYNAME",
87 [GETHOSTBYNAMEv6
] = "GETHOSTBYNAMEv6",
88 [GETHOSTBYADDR
] = "GETHOSTBYADDR",
89 [GETHOSTBYADDRv6
] = "GETHOSTBYADDRv6",
90 [SHUTDOWN
] = "SHUTDOWN",
91 [GETSTAT
] = "GETSTAT",
92 [INVALIDATE
] = "INVALIDATE",
93 [GETFDPW
] = "GETFDPW",
94 [GETFDGR
] = "GETFDGR",
95 [GETFDHST
] = "GETFDHST",
97 [INITGROUPS
] = "INITGROUPS",
98 [GETSERVBYNAME
] = "GETSERVBYNAME",
99 [GETSERVBYPORT
] = "GETSERVBYPORT",
100 [GETFDSERV
] = "GETFDSERV",
101 [GETNETGRENT
] = "GETNETGRENT",
102 [INNETGR
] = "INNETGR",
103 [GETFDNETGR
] = "GETFDNETGR"
106 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
107 # define RWLOCK_INITIALIZER PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
109 # define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
112 /* The control data structures for the services. */
113 struct database_dyn dbs
[lastdb
] =
116 .lock
= RWLOCK_INITIALIZER
,
117 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
118 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
124 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
125 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
126 .db_filename
= _PATH_NSCD_PASSWD_DB
,
127 .disabled_iov
= &pwd_iov_disabled
,
135 .lock
= RWLOCK_INITIALIZER
,
136 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
137 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
143 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
144 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
145 .db_filename
= _PATH_NSCD_GROUP_DB
,
146 .disabled_iov
= &grp_iov_disabled
,
154 .lock
= RWLOCK_INITIALIZER
,
155 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
156 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
160 .propagate
= 0, /* Not used. */
162 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
163 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
164 .db_filename
= _PATH_NSCD_HOSTS_DB
,
165 .disabled_iov
= &hst_iov_disabled
,
173 .lock
= RWLOCK_INITIALIZER
,
174 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
175 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
179 .propagate
= 0, /* Not used. */
181 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
182 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
183 .db_filename
= _PATH_NSCD_SERVICES_DB
,
184 .disabled_iov
= &serv_iov_disabled
,
192 .lock
= RWLOCK_INITIALIZER
,
193 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
194 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
198 .propagate
= 0, /* Not used. */
200 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
201 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
202 .db_filename
= _PATH_NSCD_NETGROUP_DB
,
203 .disabled_iov
= &netgroup_iov_disabled
,
213 /* Mapping of request type to database. */
217 struct database_dyn
*db
;
218 } const reqinfo
[LASTREQ
] =
220 [GETPWBYNAME
] = { true, &dbs
[pwddb
] },
221 [GETPWBYUID
] = { true, &dbs
[pwddb
] },
222 [GETGRBYNAME
] = { true, &dbs
[grpdb
] },
223 [GETGRBYGID
] = { true, &dbs
[grpdb
] },
224 [GETHOSTBYNAME
] = { true, &dbs
[hstdb
] },
225 [GETHOSTBYNAMEv6
] = { true, &dbs
[hstdb
] },
226 [GETHOSTBYADDR
] = { true, &dbs
[hstdb
] },
227 [GETHOSTBYADDRv6
] = { true, &dbs
[hstdb
] },
228 [SHUTDOWN
] = { false, NULL
},
229 [GETSTAT
] = { false, NULL
},
230 [GETFDPW
] = { false, &dbs
[pwddb
] },
231 [GETFDGR
] = { false, &dbs
[grpdb
] },
232 [GETFDHST
] = { false, &dbs
[hstdb
] },
233 [GETAI
] = { true, &dbs
[hstdb
] },
234 [INITGROUPS
] = { true, &dbs
[grpdb
] },
235 [GETSERVBYNAME
] = { true, &dbs
[servdb
] },
236 [GETSERVBYPORT
] = { true, &dbs
[servdb
] },
237 [GETFDSERV
] = { false, &dbs
[servdb
] },
238 [GETNETGRENT
] = { true, &dbs
[netgrdb
] },
239 [INNETGR
] = { true, &dbs
[netgrdb
] },
240 [GETFDNETGR
] = { false, &dbs
[netgrdb
] }
244 /* Initial number of threads to use. */
246 /* Maximum number of threads to use. */
247 int max_nthreads
= 32;
249 /* Socket for incoming connections. */
253 /* Inotify descriptor. */
258 /* Descriptor for netlink status updates. */
259 static int nl_status_fd
= -1;
262 /* Number of times clients had to wait. */
263 unsigned long int client_queued
;
267 writeall (int fd
, const void *buf
, size_t len
)
273 ret
= TEMP_FAILURE_RETRY (send (fd
, buf
, n
, MSG_NOSIGNAL
));
276 buf
= (const char *) buf
+ ret
;
280 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
,
296 use_data_begin
= use_data
| use_begin
,
297 use_data_end
= use_data
| use_end
,
298 use_data_first
= use_data_begin
| use_first
303 check_use (const char *data
, nscd_ssize_t first_free
, uint8_t *usemap
,
304 enum usekey use
, ref_t start
, size_t len
)
309 if (start
> first_free
|| start
+ len
> first_free
310 || (start
& BLOCK_ALIGN_M1
))
313 if (usemap
[start
] == use_not
)
315 /* Add the start marker. */
316 usemap
[start
] = use
| use_begin
;
320 if (usemap
[++start
] != use_not
)
325 /* Add the end marker. */
326 usemap
[start
] = use
| use_end
;
328 else if ((usemap
[start
] & ~use_first
) == ((use
| use_begin
) & ~use_first
))
330 /* Hash entries can't be shared. */
334 usemap
[start
] |= (use
& use_first
);
338 if (usemap
[++start
] != use
)
341 if (usemap
[++start
] != (use
| use_end
))
345 /* Points to a wrong object or somewhere in the middle. */
352 /* Verify data in persistent database. */
354 verify_persistent_db (void *mem
, struct database_pers_head
*readhead
, int dbnr
)
356 assert (dbnr
== pwddb
|| dbnr
== grpdb
|| dbnr
== hstdb
|| dbnr
== servdb
359 time_t now
= time (NULL
);
361 struct database_pers_head
*head
= mem
;
362 struct database_pers_head head_copy
= *head
;
364 /* Check that the header that was read matches the head in the database. */
365 if (memcmp (head
, readhead
, sizeof (*head
)) != 0)
368 /* First some easy tests: make sure the database header is sane. */
369 if (head
->version
!= DB_VERSION
370 || head
->header_size
!= sizeof (*head
)
371 /* We allow a timestamp to be one hour ahead of the current time.
372 This should cover daylight saving time changes. */
373 || head
->timestamp
> now
+ 60 * 60 + 60
374 || (head
->gc_cycle
& 1)
376 || (size_t) head
->module
> INT32_MAX
/ sizeof (ref_t
)
377 || (size_t) head
->data_size
> INT32_MAX
- head
->module
* sizeof (ref_t
)
378 || head
->first_free
< 0
379 || head
->first_free
> head
->data_size
380 || (head
->first_free
& BLOCK_ALIGN_M1
) != 0
381 || head
->maxnentries
< 0
382 || head
->maxnsearched
< 0)
385 uint8_t *usemap
= calloc (head
->first_free
, 1);
389 const char *data
= (char *) &head
->array
[roundup (head
->module
,
390 ALIGN
/ sizeof (ref_t
))];
392 nscd_ssize_t he_cnt
= 0;
393 for (nscd_ssize_t cnt
= 0; cnt
< head
->module
; ++cnt
)
395 ref_t trail
= head
->array
[cnt
];
399 while (work
!= ENDREF
)
401 if (! check_use (data
, head
->first_free
, usemap
, use_he
, work
,
402 sizeof (struct hashentry
)))
405 /* Now we know we can dereference the record. */
406 struct hashentry
*here
= (struct hashentry
*) (data
+ work
);
410 /* Make sure the record is for this type of service. */
411 if (here
->type
>= LASTREQ
412 || reqinfo
[here
->type
].db
!= &dbs
[dbnr
])
415 /* Validate boolean field value. */
416 if (here
->first
!= false && here
->first
!= true)
424 || here
->packet
> head
->first_free
425 || here
->packet
+ sizeof (struct datahead
) > head
->first_free
)
428 struct datahead
*dh
= (struct datahead
*) (data
+ here
->packet
);
430 if (! check_use (data
, head
->first_free
, usemap
,
431 use_data
| (here
->first
? use_first
: 0),
432 here
->packet
, dh
->allocsize
))
435 if (dh
->allocsize
< sizeof (struct datahead
)
436 || dh
->recsize
> dh
->allocsize
437 || (dh
->notfound
!= false && dh
->notfound
!= true)
438 || (dh
->usable
!= false && dh
->usable
!= true))
441 if (here
->key
< here
->packet
+ sizeof (struct datahead
)
442 || here
->key
> here
->packet
+ dh
->allocsize
443 || here
->key
+ here
->len
> here
->packet
+ dh
->allocsize
)
449 /* A circular list, this must not happen. */
452 trail
= ((struct hashentry
*) (data
+ trail
))->next
;
457 if (he_cnt
!= head
->nentries
)
460 /* See if all data and keys had at least one reference from
461 he->first == true hashentry. */
462 for (ref_t idx
= 0; idx
< head
->first_free
; ++idx
)
464 if (usemap
[idx
] == use_data_begin
)
468 /* Finally, make sure the database hasn't changed since the first test. */
469 if (memcmp (mem
, &head_copy
, sizeof (*head
)) != 0)
481 /* Initialize database information structures. */
485 /* Look up unprivileged uid/gid/groups before we start listening on the
487 if (server_user
!= NULL
)
488 begin_drop_privileges ();
491 /* No configuration for this value, assume a default. */
494 for (size_t cnt
= 0; cnt
< lastdb
; ++cnt
)
495 if (dbs
[cnt
].enabled
)
497 pthread_rwlock_init (&dbs
[cnt
].lock
, NULL
);
498 pthread_mutex_init (&dbs
[cnt
].memlock
, NULL
);
500 if (dbs
[cnt
].persistent
)
502 /* Try to open the appropriate file on disk. */
503 int fd
= open (dbs
[cnt
].db_filename
, O_RDWR
| O_CLOEXEC
);
510 struct database_pers_head head
;
511 ssize_t n
= TEMP_FAILURE_RETRY (read (fd
, &head
,
513 if (n
!= sizeof (head
) || fstat64 (fd
, &st
) != 0)
516 /* The code is single-threaded at this point so
517 using strerror is just fine. */
518 msg
= strerror (errno
);
520 dbg_log (_("invalid persistent database file \"%s\": %s"),
521 dbs
[cnt
].db_filename
, msg
);
522 unlink (dbs
[cnt
].db_filename
);
524 else if (head
.module
== 0 && head
.data_size
== 0)
526 /* The file has been created, but the head has not
527 been initialized yet. */
528 msg
= _("uninitialized header");
531 else if (head
.header_size
!= (int) sizeof (head
))
533 msg
= _("header size does not match");
536 else if ((total
= (sizeof (head
)
537 + roundup (head
.module
* sizeof (ref_t
),
541 || total
< sizeof (head
))
543 msg
= _("file size does not match");
546 /* Note we map with the maximum size allowed for the
547 database. This is likely much larger than the
548 actual file size. This is OK on most OSes since
549 extensions of the underlying file will
550 automatically translate more pages available for
552 else if ((mem
= mmap (NULL
, dbs
[cnt
].max_db_size
,
553 PROT_READ
| PROT_WRITE
,
557 else if (!verify_persistent_db (mem
, &head
, cnt
))
560 msg
= _("verification failed");
565 /* Success. We have the database. */
567 dbs
[cnt
].memsize
= total
;
568 dbs
[cnt
].data
= (char *)
569 &dbs
[cnt
].head
->array
[roundup (dbs
[cnt
].head
->module
,
570 ALIGN
/ sizeof (ref_t
))];
571 dbs
[cnt
].mmap_used
= true;
573 if (dbs
[cnt
].suggested_module
> head
.module
)
574 dbg_log (_("suggested size of table for database %s larger than the persistent database's table"),
579 /* We also need a read-only descriptor. */
582 dbs
[cnt
].ro_fd
= open (dbs
[cnt
].db_filename
,
583 O_RDONLY
| O_CLOEXEC
);
584 if (dbs
[cnt
].ro_fd
== -1)
586 cannot create read-only descriptor for \"%s\"; no mmap"),
587 dbs
[cnt
].db_filename
);
590 // XXX Shall we test whether the descriptors actually
591 // XXX point to the same file?
594 /* Close the file descriptors in case something went
595 wrong in which case the variable have not been
600 else if (errno
== EACCES
)
601 do_exit (EXIT_FAILURE
, 0, _("cannot access '%s'"),
602 dbs
[cnt
].db_filename
);
605 if (dbs
[cnt
].head
== NULL
)
607 /* No database loaded. Allocate the data structure,
609 struct database_pers_head head
;
610 size_t total
= (sizeof (head
)
611 + roundup (dbs
[cnt
].suggested_module
612 * sizeof (ref_t
), ALIGN
)
613 + (dbs
[cnt
].suggested_module
614 * DEFAULT_DATASIZE_PER_BUCKET
));
616 /* Try to create the database. If we do not need a
617 persistent database create a temporary file. */
620 if (dbs
[cnt
].persistent
)
622 fd
= open (dbs
[cnt
].db_filename
,
623 O_RDWR
| O_CREAT
| O_EXCL
| O_TRUNC
| O_CLOEXEC
,
625 if (fd
!= -1 && dbs
[cnt
].shared
)
626 ro_fd
= open (dbs
[cnt
].db_filename
,
627 O_RDONLY
| O_CLOEXEC
);
631 char fname
[] = _PATH_NSCD_XYZ_DB_TMP
;
632 fd
= mkostemp (fname
, O_CLOEXEC
);
634 /* We do not need the file name anymore after we
635 opened another file descriptor in read-only mode. */
639 ro_fd
= open (fname
, O_RDONLY
| O_CLOEXEC
);
649 dbg_log (_("database for %s corrupted or simultaneously used; remove %s manually if necessary and restart"),
650 dbnames
[cnt
], dbs
[cnt
].db_filename
);
651 do_exit (1, 0, NULL
);
654 if (dbs
[cnt
].persistent
)
655 dbg_log (_("cannot create %s; no persistent database used"),
656 dbs
[cnt
].db_filename
);
658 dbg_log (_("cannot create %s; no sharing possible"),
659 dbs
[cnt
].db_filename
);
661 dbs
[cnt
].persistent
= 0;
662 // XXX remember: no mmap
666 /* Tell the user if we could not create the read-only
668 if (ro_fd
== -1 && dbs
[cnt
].shared
)
670 cannot create read-only descriptor for \"%s\"; no mmap"),
671 dbs
[cnt
].db_filename
);
673 /* Before we create the header, initialize the hash
674 table. That way if we get interrupted while writing
675 the header we can recognize a partially initialized
677 size_t ps
= sysconf (_SC_PAGESIZE
);
679 assert (~ENDREF
== 0);
680 memset (tmpbuf
, '\xff', ps
);
682 size_t remaining
= dbs
[cnt
].suggested_module
* sizeof (ref_t
);
683 off_t offset
= sizeof (head
);
686 if (offset
% ps
!= 0)
688 towrite
= MIN (remaining
, ps
- (offset
% ps
));
689 if (pwrite (fd
, tmpbuf
, towrite
, offset
) != towrite
)
692 remaining
-= towrite
;
695 while (remaining
> ps
)
697 if (pwrite (fd
, tmpbuf
, ps
, offset
) == -1)
704 && pwrite (fd
, tmpbuf
, remaining
, offset
) != remaining
)
707 /* Create the header of the file. */
708 struct database_pers_head head
=
710 .version
= DB_VERSION
,
711 .header_size
= sizeof (head
),
712 .module
= dbs
[cnt
].suggested_module
,
713 .data_size
= (dbs
[cnt
].suggested_module
714 * DEFAULT_DATASIZE_PER_BUCKET
),
719 if ((TEMP_FAILURE_RETRY (write (fd
, &head
, sizeof (head
)))
721 || (TEMP_FAILURE_RETRY_VAL (posix_fallocate (fd
, 0, total
))
723 || (mem
= mmap (NULL
, dbs
[cnt
].max_db_size
,
724 PROT_READ
| PROT_WRITE
,
725 MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
728 unlink (dbs
[cnt
].db_filename
);
729 dbg_log (_("cannot write to database file %s: %s"),
730 dbs
[cnt
].db_filename
, strerror (errno
));
731 dbs
[cnt
].persistent
= 0;
737 dbs
[cnt
].data
= (char *)
738 &dbs
[cnt
].head
->array
[roundup (dbs
[cnt
].head
->module
,
739 ALIGN
/ sizeof (ref_t
))];
740 dbs
[cnt
].memsize
= total
;
741 dbs
[cnt
].mmap_used
= true;
743 /* Remember the descriptors. */
745 dbs
[cnt
].ro_fd
= ro_fd
;
757 if (dbs
[cnt
].head
== NULL
)
759 /* We do not use the persistent database. Just
760 create an in-memory data structure. */
761 assert (! dbs
[cnt
].persistent
);
763 dbs
[cnt
].head
= xmalloc (sizeof (struct database_pers_head
)
764 + (dbs
[cnt
].suggested_module
766 memset (dbs
[cnt
].head
, '\0', sizeof (struct database_pers_head
));
767 assert (~ENDREF
== 0);
768 memset (dbs
[cnt
].head
->array
, '\xff',
769 dbs
[cnt
].suggested_module
* sizeof (ref_t
));
770 dbs
[cnt
].head
->module
= dbs
[cnt
].suggested_module
;
771 dbs
[cnt
].head
->data_size
= (DEFAULT_DATASIZE_PER_BUCKET
772 * dbs
[cnt
].head
->module
);
773 dbs
[cnt
].data
= xmalloc (dbs
[cnt
].head
->data_size
);
774 dbs
[cnt
].head
->first_free
= 0;
777 assert (dbs
[cnt
].ro_fd
== -1);
781 /* Create the socket. */
782 sock
= socket (AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, 0);
785 dbg_log (_("cannot open socket: %s"), strerror (errno
));
786 do_exit (errno
== EACCES
? 4 : 1, 0, NULL
);
788 /* Bind a name to the socket. */
789 struct sockaddr_un sock_addr
;
790 sock_addr
.sun_family
= AF_UNIX
;
791 strcpy (sock_addr
.sun_path
, _PATH_NSCDSOCKET
);
792 if (bind (sock
, (struct sockaddr
*) &sock_addr
, sizeof (sock_addr
)) < 0)
794 dbg_log ("%s: %s", _PATH_NSCDSOCKET
, strerror (errno
));
795 do_exit (errno
== EACCES
? 4 : 1, 0, NULL
);
798 /* Set permissions for the socket. */
799 chmod (_PATH_NSCDSOCKET
, DEFFILEMODE
);
801 /* Set the socket up to accept connections. */
802 if (listen (sock
, SOMAXCONN
) < 0)
804 dbg_log (_("cannot enable socket to accept connections: %s"),
806 do_exit (1, 0, NULL
);
810 if (dbs
[hstdb
].enabled
)
812 /* Try to open netlink socket to monitor network setting changes. */
813 nl_status_fd
= socket (AF_NETLINK
,
814 SOCK_RAW
| SOCK_CLOEXEC
| SOCK_NONBLOCK
,
816 if (nl_status_fd
!= -1)
818 struct sockaddr_nl snl
;
819 memset (&snl
, '\0', sizeof (snl
));
820 snl
.nl_family
= AF_NETLINK
;
821 /* XXX Is this the best set to use? */
822 snl
.nl_groups
= (RTMGRP_IPV4_IFADDR
| RTMGRP_TC
| RTMGRP_IPV4_MROUTE
823 | RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_RULE
824 | RTMGRP_IPV6_IFADDR
| RTMGRP_IPV6_MROUTE
825 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFINFO
826 | RTMGRP_IPV6_PREFIX
);
828 if (bind (nl_status_fd
, (struct sockaddr
*) &snl
, sizeof (snl
)) != 0)
830 close (nl_status_fd
);
835 /* Start the timestamp process. */
836 dbs
[hstdb
].head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
]
837 = __bump_nl_timestamp ();
843 /* Change to unprivileged uid/gid/groups if specified in config file */
844 if (server_user
!= NULL
)
845 finish_drop_privileges ();
849 #define TRACED_FILE_MASK (IN_DELETE_SELF | IN_CLOSE_WRITE | IN_MOVE_SELF)
850 #define TRACED_DIR_MASK (IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MOVE_SELF)
852 install_watches (struct traced_file
*finfo
)
854 /* Use inotify support if we have it. */
855 if (finfo
->inotify_descr
[TRACED_FILE
] < 0)
856 finfo
->inotify_descr
[TRACED_FILE
] = inotify_add_watch (inotify_fd
,
859 if (finfo
->inotify_descr
[TRACED_FILE
] < 0)
861 dbg_log (_("disabled inotify-based monitoring for file `%s': %s"),
862 finfo
->fname
, strerror (errno
));
865 dbg_log (_("monitoring file `%s` (%d)"),
866 finfo
->fname
, finfo
->inotify_descr
[TRACED_FILE
]);
867 /* Additionally listen for events in the file's parent directory.
868 We do this because the file to be watched might be
869 deleted and then added back again. When it is added back again
870 we must re-add the watch. We must also cover IN_MOVED_TO to
871 detect a file being moved into the directory. */
872 if (finfo
->inotify_descr
[TRACED_DIR
] < 0)
873 finfo
->inotify_descr
[TRACED_DIR
] = inotify_add_watch (inotify_fd
,
876 if (finfo
->inotify_descr
[TRACED_DIR
] < 0)
878 dbg_log (_("disabled inotify-based monitoring for directory `%s': %s"),
879 finfo
->fname
, strerror (errno
));
882 dbg_log (_("monitoring directory `%s` (%d)"),
883 finfo
->dname
, finfo
->inotify_descr
[TRACED_DIR
]);
887 /* Register the file in FINFO as a traced file for the database DBS[DBIX].
889 We support registering multiple files per database. Each call to
890 register_traced_file adds to the list of registered files.
892 When we prune the database, either through timeout or a request to
893 invalidate, we will check to see if any of the registered files has changed.
894 When we accept new connections to handle a cache request we will also
895 check to see if any of the registered files has changed.
897 If we have inotify support then we install an inotify fd to notify us of
898 file deletion or modification, both of which will require we invalidate
899 the cache for the database. Without inotify support we stat the file and
900 store st_mtime to determine if the file has been modified. */
902 register_traced_file (size_t dbidx
, struct traced_file
*finfo
)
904 /* If the database is disabled or file checking is disabled
905 then ignore the registration. */
906 if (! dbs
[dbidx
].enabled
|| ! dbs
[dbidx
].check_file
)
909 if (__glibc_unlikely (debug_level
> 0))
910 dbg_log (_("monitoring file %s for database %s"),
911 finfo
->fname
, dbnames
[dbidx
]);
914 install_watches (finfo
);
917 if (stat64 (finfo
->fname
, &st
) < 0)
919 /* We cannot stat() the file. Set mtime to zero and try again later. */
920 dbg_log (_("stat failed for file `%s'; will try again later: %s"),
921 finfo
->fname
, strerror (errno
));
925 finfo
->mtime
= st
.st_mtime
;
927 /* Queue up the file name. */
928 finfo
->next
= dbs
[dbidx
].traced_files
;
929 dbs
[dbidx
].traced_files
= finfo
;
933 /* Close the connections. */
942 invalidate_cache (char *key
, int fd
)
947 for (number
= pwddb
; number
< lastdb
; ++number
)
948 if (strcmp (key
, dbnames
[number
]) == 0)
950 struct traced_file
*runp
= dbs
[number
].traced_files
;
953 /* Make sure we reload from file when checking mtime. */
956 /* During an invalidation we try to reload the traced
957 file watches. This allows the user to re-sync if
958 inotify events were lost. Similar to what we do during
960 install_watches (runp
);
962 if (runp
->call_res_init
)
972 if (number
== lastdb
)
975 writeall (fd
, &resp
, sizeof (resp
));
979 if (dbs
[number
].enabled
)
981 pthread_mutex_lock (&dbs
[number
].prune_run_lock
);
982 prune_cache (&dbs
[number
], LONG_MAX
, fd
);
983 pthread_mutex_unlock (&dbs
[number
].prune_run_lock
);
988 writeall (fd
, &resp
, sizeof (resp
));
995 send_ro_fd (struct database_dyn
*db
, char *key
, int fd
)
997 /* If we do not have an read-only file descriptor do nothing. */
1001 /* We need to send some data along with the descriptor. */
1002 uint64_t mapsize
= (db
->head
->data_size
1003 + roundup (db
->head
->module
* sizeof (ref_t
), ALIGN
)
1004 + sizeof (struct database_pers_head
));
1005 struct iovec iov
[2];
1006 iov
[0].iov_base
= key
;
1007 iov
[0].iov_len
= strlen (key
) + 1;
1008 iov
[1].iov_base
= &mapsize
;
1009 iov
[1].iov_len
= sizeof (mapsize
);
1011 /* Prepare the control message to transfer the descriptor. */
1015 char bytes
[CMSG_SPACE (sizeof (int))];
1017 struct msghdr msg
= { .msg_iov
= iov
, .msg_iovlen
= 2,
1018 .msg_control
= buf
.bytes
,
1019 .msg_controllen
= sizeof (buf
) };
1020 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR (&msg
);
1022 cmsg
->cmsg_level
= SOL_SOCKET
;
1023 cmsg
->cmsg_type
= SCM_RIGHTS
;
1024 cmsg
->cmsg_len
= CMSG_LEN (sizeof (int));
1026 int *ip
= (int *) CMSG_DATA (cmsg
);
1029 msg
.msg_controllen
= cmsg
->cmsg_len
;
1031 /* Send the control message. We repeat when we are interrupted but
1032 everything else is ignored. */
1033 #ifndef MSG_NOSIGNAL
1034 # define MSG_NOSIGNAL 0
1036 (void) TEMP_FAILURE_RETRY (sendmsg (fd
, &msg
, MSG_NOSIGNAL
));
1038 if (__glibc_unlikely (debug_level
> 0))
1039 dbg_log (_("provide access to FD %d, for %s"), db
->ro_fd
, key
);
1041 #endif /* SCM_RIGHTS */
1044 /* Handle new request. */
1046 handle_request (int fd
, request_header
*req
, void *key
, uid_t uid
, pid_t pid
)
1048 if (__builtin_expect (req
->version
, NSCD_VERSION
) != NSCD_VERSION
)
1050 if (debug_level
> 0)
1052 cannot handle old request version %d; current version is %d"),
1053 req
->version
, NSCD_VERSION
);
1057 /* Perform the SELinux check before we go on to the standard checks. */
1058 if (selinux_enabled
&& nscd_request_avc_has_perm (fd
, req
->type
) != 0)
1060 if (debug_level
> 0)
1063 char pbuf
[sizeof ("/proc//exe") + 3 * sizeof (long int)];
1070 snprintf (pbuf
, sizeof (pbuf
), "/proc/%ld/exe", (long int) pid
);
1071 ssize_t n
= readlink (pbuf
, buf
, sizeof (buf
) - 1);
1075 request from %ld not handled due to missing permission"), (long int) pid
);
1080 request from '%s' [%ld] not handled due to missing permission"),
1081 buf
, (long int) pid
);
1084 dbg_log (_("request not handled due to missing permission"));
1090 struct database_dyn
*db
= reqinfo
[req
->type
].db
;
1092 /* See whether we can service the request from the cache. */
1093 if (__builtin_expect (reqinfo
[req
->type
].data_request
, true))
1095 if (__builtin_expect (debug_level
, 0) > 0)
1097 if (req
->type
== GETHOSTBYADDR
|| req
->type
== GETHOSTBYADDRv6
)
1099 char buf
[INET6_ADDRSTRLEN
];
1101 dbg_log ("\t%s (%s)", serv2str
[req
->type
],
1102 inet_ntop (req
->type
== GETHOSTBYADDR
1103 ? AF_INET
: AF_INET6
,
1104 key
, buf
, sizeof (buf
)));
1107 dbg_log ("\t%s (%s)", serv2str
[req
->type
], (char *) key
);
1110 /* Is this service enabled? */
1111 if (__glibc_unlikely (!db
->enabled
))
1113 /* No, sent the prepared record. */
1114 if (TEMP_FAILURE_RETRY (send (fd
, db
->disabled_iov
->iov_base
,
1115 db
->disabled_iov
->iov_len
,
1117 != (ssize_t
) db
->disabled_iov
->iov_len
1118 && __builtin_expect (debug_level
, 0) > 0)
1120 /* We have problems sending the result. */
1122 dbg_log (_("cannot write result: %s"),
1123 strerror_r (errno
, buf
, sizeof (buf
)));
1129 /* Be sure we can read the data. */
1130 if (__glibc_unlikely (pthread_rwlock_tryrdlock (&db
->lock
) != 0))
1132 ++db
->head
->rdlockdelayed
;
1133 pthread_rwlock_rdlock (&db
->lock
);
1136 /* See whether we can handle it from the cache. */
1137 struct datahead
*cached
;
1138 cached
= (struct datahead
*) cache_search (req
->type
, key
, req
->key_len
,
1142 /* Hurray it's in the cache. */
1143 if (writeall (fd
, cached
->data
, cached
->recsize
) != cached
->recsize
1144 && __glibc_unlikely (debug_level
> 0))
1146 /* We have problems sending the result. */
1148 dbg_log (_("cannot write result: %s"),
1149 strerror_r (errno
, buf
, sizeof (buf
)));
1152 pthread_rwlock_unlock (&db
->lock
);
1157 pthread_rwlock_unlock (&db
->lock
);
1159 else if (__builtin_expect (debug_level
, 0) > 0)
1161 if (req
->type
== INVALIDATE
)
1162 dbg_log ("\t%s (%s)", serv2str
[req
->type
], (char *) key
);
1164 dbg_log ("\t%s", serv2str
[req
->type
]);
1167 /* Handle the request. */
1171 addpwbyname (db
, fd
, req
, key
, uid
);
1175 addpwbyuid (db
, fd
, req
, key
, uid
);
1179 addgrbyname (db
, fd
, req
, key
, uid
);
1183 addgrbygid (db
, fd
, req
, key
, uid
);
1187 addhstbyname (db
, fd
, req
, key
, uid
);
1190 case GETHOSTBYNAMEv6
:
1191 addhstbynamev6 (db
, fd
, req
, key
, uid
);
1195 addhstbyaddr (db
, fd
, req
, key
, uid
);
1198 case GETHOSTBYADDRv6
:
1199 addhstbyaddrv6 (db
, fd
, req
, key
, uid
);
1203 addhstai (db
, fd
, req
, key
, uid
);
1207 addinitgroups (db
, fd
, req
, key
, uid
);
1211 addservbyname (db
, fd
, req
, key
, uid
);
1215 addservbyport (db
, fd
, req
, key
, uid
);
1219 addgetnetgrent (db
, fd
, req
, key
, uid
);
1223 addinnetgr (db
, fd
, req
, key
, uid
);
1230 /* Get the callers credentials. */
1232 struct ucred caller
;
1233 socklen_t optlen
= sizeof (caller
);
1235 if (getsockopt (fd
, SOL_SOCKET
, SO_PEERCRED
, &caller
, &optlen
) < 0)
1239 dbg_log (_("error getting caller's id: %s"),
1240 strerror_r (errno
, buf
, sizeof (buf
)));
1246 /* Some systems have no SO_PEERCRED implementation. They don't
1247 care about security so we don't as well. */
1252 /* Accept shutdown, getstat and invalidate only from root. For
1253 the stat call also allow the user specified in the config file. */
1254 if (req
->type
== GETSTAT
)
1256 if (uid
== 0 || uid
== stat_uid
)
1257 send_stats (fd
, dbs
);
1261 if (req
->type
== INVALIDATE
)
1262 invalidate_cache (key
, fd
);
1264 termination_handler (0);
1274 send_ro_fd (reqinfo
[req
->type
].db
, key
, fd
);
1279 /* Ignore the command, it's nothing we know. */
1285 read_cmdline (size_t *size
)
1287 int fd
= open ("/proc/self/cmdline", O_RDONLY
);
1291 size_t limit
= 1024;
1292 char *buffer
= malloc (limit
);
1301 if (current
== limit
)
1304 if (2 * limit
< limit
1305 || (newptr
= realloc (buffer
, 2 * limit
)) == NULL
)
1316 ssize_t n
= TEMP_FAILURE_RETRY (read (fd
, buffer
+ current
,
1337 /* Restart the process. */
1341 /* First determine the parameters. We do not use the parameters
1342 passed to main because then nscd would use the system libc after
1343 restarting even if it was started by a non-system dynamic linker
1344 during glibc testing. */
1346 char *cmdline
= read_cmdline (&readlen
);
1347 if (cmdline
== NULL
)
1350 cannot open /proc/self/cmdline: %m; disabling paranoia mode"));
1355 /* Parse the command line. Worst case scenario: every two
1356 characters form one parameter (one character plus NUL). */
1357 char **argv
= alloca ((readlen
/ 2 + 1) * sizeof (argv
[0]));
1360 for (char *cp
= cmdline
; cp
< cmdline
+ readlen
;)
1363 cp
= (char *) rawmemchr (cp
, '\0') + 1;
1367 /* Second, change back to the old user if we changed it. */
1368 if (server_user
!= NULL
)
1370 if (setresuid (old_uid
, old_uid
, old_uid
) != 0)
1373 cannot change to old UID: %s; disabling paranoia mode"),
1381 if (setresgid (old_gid
, old_gid
, old_gid
) != 0)
1384 cannot change to old GID: %s; disabling paranoia mode"),
1387 ignore_value (setuid (server_uid
));
1394 /* Next change back to the old working directory. */
1395 if (chdir (oldcwd
) == -1)
1398 cannot change to old working directory: %s; disabling paranoia mode"),
1401 if (server_user
!= NULL
)
1403 ignore_value (setuid (server_uid
));
1404 ignore_value (setgid (server_gid
));
1411 /* Synchronize memory. */
1412 int32_t certainly
[lastdb
];
1413 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
1414 if (dbs
[cnt
].enabled
)
1416 /* Make sure nobody keeps using the database. */
1417 dbs
[cnt
].head
->timestamp
= 0;
1418 certainly
[cnt
] = dbs
[cnt
].head
->nscd_certainly_running
;
1419 dbs
[cnt
].head
->nscd_certainly_running
= 0;
1421 if (dbs
[cnt
].persistent
)
1423 msync (dbs
[cnt
].head
, dbs
[cnt
].memsize
, MS_ASYNC
);
1426 /* The preparations are done. */
1428 char pathbuf
[PATH_MAX
];
1432 /* Try to exec the real nscd program so the process name (as reported
1433 in /proc/PID/status) will be 'nscd', but fall back to /proc/self/exe
1434 if readlink or the exec with the result of the readlink call fails. */
1435 ssize_t n
= readlink ("/proc/self/exe", pathbuf
, sizeof (pathbuf
) - 1);
1439 execv (pathbuf
, argv
);
1441 execv ("/proc/self/exe", argv
);
1443 /* If we come here, we will never be able to re-exec. */
1444 dbg_log (_("re-exec failed: %s; disabling paranoia mode"),
1447 if (server_user
!= NULL
)
1449 ignore_value (setuid (server_uid
));
1450 ignore_value (setgid (server_gid
));
1452 if (chdir ("/") != 0)
1453 dbg_log (_("cannot change current working directory to \"/\": %s"),
1458 /* Reenable the databases. */
1459 time_t now
= time (NULL
);
1460 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
1461 if (dbs
[cnt
].enabled
)
1463 dbs
[cnt
].head
->timestamp
= now
;
1464 dbs
[cnt
].head
->nscd_certainly_running
= certainly
[cnt
];
1469 /* List of file descriptors. */
1473 struct fdlist
*next
;
1475 /* Memory allocated for the list. */
1476 static struct fdlist
*fdlist
;
1477 /* List of currently ready-to-read file descriptors. */
1478 static struct fdlist
*readylist
;
1480 /* Conditional variable and mutex to signal availability of entries in
1481 READYLIST. The condvar is initialized dynamically since we might
1482 use a different clock depending on availability. */
1483 static pthread_cond_t readylist_cond
= PTHREAD_COND_INITIALIZER
;
1484 static pthread_mutex_t readylist_lock
= PTHREAD_MUTEX_INITIALIZER
;
1486 /* The clock to use with the condvar. */
1487 static clockid_t timeout_clock
= CLOCK_REALTIME
;
1489 /* Number of threads ready to handle the READYLIST. */
1490 static unsigned long int nready
;
1493 /* Function for the clean-up threads. */
1495 __attribute__ ((__noreturn__
))
1496 nscd_run_prune (void *p
)
1498 const long int my_number
= (long int) p
;
1499 assert (dbs
[my_number
].enabled
);
1501 int dont_need_update
= setup_thread (&dbs
[my_number
]);
1503 time_t now
= time (NULL
);
1505 /* We are running. */
1506 dbs
[my_number
].head
->timestamp
= now
;
1508 struct timespec prune_ts
;
1509 if (__glibc_unlikely (clock_gettime (timeout_clock
, &prune_ts
) == -1))
1510 /* Should never happen. */
1513 /* Compute the initial timeout time. Prevent all the timers to go
1514 off at the same time by adding a db-based value. */
1515 prune_ts
.tv_sec
+= CACHE_PRUNE_INTERVAL
+ my_number
;
1516 dbs
[my_number
].wakeup_time
= now
+ CACHE_PRUNE_INTERVAL
+ my_number
;
1518 pthread_mutex_t
*prune_lock
= &dbs
[my_number
].prune_lock
;
1519 pthread_mutex_t
*prune_run_lock
= &dbs
[my_number
].prune_run_lock
;
1520 pthread_cond_t
*prune_cond
= &dbs
[my_number
].prune_cond
;
1522 pthread_mutex_lock (prune_lock
);
1525 /* Wait, but not forever. */
1527 if (! dbs
[my_number
].clear_cache
)
1528 e
= pthread_cond_timedwait (prune_cond
, prune_lock
, &prune_ts
);
1529 assert (__builtin_expect (e
== 0 || e
== ETIMEDOUT
, 1));
1533 if (e
== ETIMEDOUT
|| now
>= dbs
[my_number
].wakeup_time
1534 || dbs
[my_number
].clear_cache
)
1536 /* We will determine the new timout values based on the
1537 cache content. Should there be concurrent additions to
1538 the cache which are not accounted for in the cache
1539 pruning we want to know about it. Therefore set the
1540 timeout to the maximum. It will be descreased when adding
1541 new entries to the cache, if necessary. */
1542 dbs
[my_number
].wakeup_time
= MAX_TIMEOUT_VALUE
;
1544 /* Unconditionally reset the flag. */
1545 time_t prune_now
= dbs
[my_number
].clear_cache
? LONG_MAX
: now
;
1546 dbs
[my_number
].clear_cache
= 0;
1548 pthread_mutex_unlock (prune_lock
);
1550 /* We use a separate lock for running the prune function (instead
1551 of keeping prune_lock locked) because this enables concurrent
1552 invocations of cache_add which might modify the timeout value. */
1553 pthread_mutex_lock (prune_run_lock
);
1554 next_wait
= prune_cache (&dbs
[my_number
], prune_now
, -1);
1555 pthread_mutex_unlock (prune_run_lock
);
1557 next_wait
= MAX (next_wait
, CACHE_PRUNE_INTERVAL
);
1558 /* If clients cannot determine for sure whether nscd is running
1559 we need to wake up occasionally to update the timestamp.
1560 Wait 90% of the update period. */
1561 #define UPDATE_MAPPING_TIMEOUT (MAPPING_TIMEOUT * 9 / 10)
1562 if (__glibc_unlikely (! dont_need_update
))
1564 next_wait
= MIN (UPDATE_MAPPING_TIMEOUT
, next_wait
);
1565 dbs
[my_number
].head
->timestamp
= now
;
1568 pthread_mutex_lock (prune_lock
);
1570 /* Make it known when we will wake up again. */
1571 if (now
+ next_wait
< dbs
[my_number
].wakeup_time
)
1572 dbs
[my_number
].wakeup_time
= now
+ next_wait
;
1574 next_wait
= dbs
[my_number
].wakeup_time
- now
;
1577 /* The cache was just pruned. Do not do it again now. Just
1578 use the new timeout value. */
1579 next_wait
= dbs
[my_number
].wakeup_time
- now
;
1581 if (clock_gettime (timeout_clock
, &prune_ts
) == -1)
1582 /* Should never happen. */
1585 /* Compute next timeout time. */
1586 prune_ts
.tv_sec
+= next_wait
;
1591 /* This is the main loop. It is replicated in different threads but
1592 the use of the ready list makes sure only one thread handles an
1593 incoming connection. */
1595 __attribute__ ((__noreturn__
))
1596 nscd_run_worker (void *p
)
1600 /* Initial locking. */
1601 pthread_mutex_lock (&readylist_lock
);
1603 /* One more thread available. */
1608 while (readylist
== NULL
)
1609 pthread_cond_wait (&readylist_cond
, &readylist_lock
);
1611 struct fdlist
*it
= readylist
->next
;
1612 if (readylist
->next
== readylist
)
1613 /* Just one entry on the list. */
1616 readylist
->next
= it
->next
;
1618 /* Extract the information and mark the record ready to be used
1623 /* One more thread available. */
1626 /* We are done with the list. */
1627 pthread_mutex_unlock (&readylist_lock
);
1629 /* Now read the request. */
1631 if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd
, &req
, sizeof (req
)))
1632 != sizeof (req
), 0))
1634 /* We failed to read data. Note that this also might mean we
1635 failed because we would have blocked. */
1636 if (debug_level
> 0)
1637 dbg_log (_("short read while reading request: %s"),
1638 strerror_r (errno
, buf
, sizeof (buf
)));
1642 /* Check whether this is a valid request type. */
1643 if (req
.type
< GETPWBYNAME
|| req
.type
>= LASTREQ
)
1646 /* Some systems have no SO_PEERCRED implementation. They don't
1647 care about security so we don't as well. */
1652 if (__glibc_unlikely (debug_level
> 0))
1654 struct ucred caller
;
1655 socklen_t optlen
= sizeof (caller
);
1657 if (getsockopt (fd
, SOL_SOCKET
, SO_PEERCRED
, &caller
, &optlen
) == 0)
1661 const pid_t pid
= 0;
1664 /* It should not be possible to crash the nscd with a silly
1665 request (i.e., a terribly large key). We limit the size to 1kb. */
1666 if (__builtin_expect (req
.key_len
, 1) < 0
1667 || __builtin_expect (req
.key_len
, 1) > MAXKEYLEN
)
1669 if (debug_level
> 0)
1670 dbg_log (_("key length in request too long: %d"), req
.key_len
);
1675 char keybuf
[MAXKEYLEN
+ 1];
1677 if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd
, keybuf
,
1681 /* Again, this can also mean we would have blocked. */
1682 if (debug_level
> 0)
1683 dbg_log (_("short read while reading request key: %s"),
1684 strerror_r (errno
, buf
, sizeof (buf
)));
1687 keybuf
[req
.key_len
] = '\0';
1689 if (__builtin_expect (debug_level
, 0) > 0)
1694 handle_request: request received (Version = %d) from PID %ld"),
1695 req
.version
, (long int) pid
);
1699 handle_request: request received (Version = %d)"), req
.version
);
1702 /* Phew, we got all the data, now process it. */
1703 handle_request (fd
, &req
, keybuf
, uid
, pid
);
1711 pthread_mutex_lock (&readylist_lock
);
1713 /* One more thread available. */
1720 static unsigned int nconns
;
1725 pthread_mutex_lock (&readylist_lock
);
1727 /* Find an empty entry in FDLIST. */
1729 for (inner
= 0; inner
< nconns
; ++inner
)
1730 if (fdlist
[inner
].next
== NULL
)
1732 assert (inner
< nconns
);
1734 fdlist
[inner
].fd
= fd
;
1736 if (readylist
== NULL
)
1737 readylist
= fdlist
[inner
].next
= &fdlist
[inner
];
1740 fdlist
[inner
].next
= readylist
->next
;
1741 readylist
= readylist
->next
= &fdlist
[inner
];
1744 bool do_signal
= true;
1745 if (__glibc_unlikely (nready
== 0))
1750 /* Try to start another thread to help out. */
1752 if (nthreads
< max_nthreads
1753 && pthread_create (&th
, &attr
, nscd_run_worker
,
1754 (void *) (long int) nthreads
) == 0)
1756 /* We got another thread. */
1758 /* The new thread might need a kick. */
1764 pthread_mutex_unlock (&readylist_lock
);
1766 /* Tell one of the worker threads there is work to do. */
1768 pthread_cond_signal (&readylist_cond
);
1772 /* Check whether restarting should happen. */
1774 restart_p (time_t now
)
1776 return (paranoia
&& readylist
== NULL
&& nready
== nthreads
1777 && now
>= restart_time
);
1781 /* Array for times a connection was accepted. */
1782 static time_t *starttime
;
1785 /* Inotify event for changed file. */
1788 struct inotify_event i
;
1790 # define PATH_MAX 1024
1792 char buf
[sizeof (struct inotify_event
) + PATH_MAX
];
1795 /* Returns 0 if the file is there otherwise -1. */
1797 check_file (struct traced_file
*finfo
)
1800 /* We could check mtime and if different re-add
1801 the watches, and invalidate the database, but we
1802 don't because we are called from inotify_check_files
1803 which should be doing that work. If sufficient inotify
1804 events were lost then the next pruning or invalidation
1805 will do the stat and mtime check. We don't do it here to
1806 keep the logic simple. */
1807 if (stat64 (finfo
->fname
, &st
) < 0)
1812 /* Process the inotify event in INEV. If the event matches any of the files
1813 registered with a database then mark that database as requiring its cache
1814 to be cleared. We indicate the cache needs clearing by setting
1815 TO_CLEAR[DBCNT] to true for the matching database. */
1817 inotify_check_files (bool *to_clear
, union __inev
*inev
)
1819 /* Check which of the files changed. */
1820 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
1822 struct traced_file
*finfo
= dbs
[dbcnt
].traced_files
;
1824 while (finfo
!= NULL
)
1826 /* The configuration file was moved or deleted.
1827 We stop watching it at that point, and reinitialize. */
1828 if (finfo
->inotify_descr
[TRACED_FILE
] == inev
->i
.wd
1829 && ((inev
->i
.mask
& IN_MOVE_SELF
)
1830 || (inev
->i
.mask
& IN_DELETE_SELF
)
1831 || (inev
->i
.mask
& IN_IGNORED
)))
1834 bool moved
= (inev
->i
.mask
& IN_MOVE_SELF
) != 0;
1836 if (check_file (finfo
) == 0)
1838 dbg_log (_("ignored inotify event for `%s` (file exists)"),
1843 dbg_log (_("monitored file `%s` was %s, removing watch"),
1844 finfo
->fname
, moved
? "moved" : "deleted");
1845 /* File was moved out, remove the watch. Watches are
1846 automatically removed when the file is deleted. */
1849 ret
= inotify_rm_watch (inotify_fd
, inev
->i
.wd
);
1851 dbg_log (_("failed to remove file watch `%s`: %s"),
1852 finfo
->fname
, strerror (errno
));
1854 finfo
->inotify_descr
[TRACED_FILE
] = -1;
1855 to_clear
[dbcnt
] = true;
1856 if (finfo
->call_res_init
)
1860 /* The configuration file was open for writing and has just closed.
1861 We reset the cache and reinitialize. */
1862 if (finfo
->inotify_descr
[TRACED_FILE
] == inev
->i
.wd
1863 && inev
->i
.mask
& IN_CLOSE_WRITE
)
1865 /* Mark cache as needing to be cleared and reinitialize. */
1866 dbg_log (_("monitored file `%s` was written to"), finfo
->fname
);
1867 to_clear
[dbcnt
] = true;
1868 if (finfo
->call_res_init
)
1872 /* The parent directory was moved or deleted. We trigger one last
1873 invalidation. At the next pruning or invalidation we may add
1874 this watch back if the file is present again. */
1875 if (finfo
->inotify_descr
[TRACED_DIR
] == inev
->i
.wd
1876 && ((inev
->i
.mask
& IN_DELETE_SELF
)
1877 || (inev
->i
.mask
& IN_MOVE_SELF
)
1878 || (inev
->i
.mask
& IN_IGNORED
)))
1880 bool moved
= (inev
->i
.mask
& IN_MOVE_SELF
) != 0;
1881 /* The directory watch may have already been removed
1882 but we don't know so we just remove it again and
1883 ignore the error. Then we remove the file watch.
1884 Note: watches are automatically removed for deleted
1887 inotify_rm_watch (inotify_fd
, inev
->i
.wd
);
1888 if (finfo
->inotify_descr
[TRACED_FILE
] != -1)
1890 dbg_log (_("monitored parent directory `%s` was %s, removing watch on `%s`"),
1891 finfo
->dname
, moved
? "moved" : "deleted", finfo
->fname
);
1892 if (inotify_rm_watch (inotify_fd
, finfo
->inotify_descr
[TRACED_FILE
]) < 0)
1893 dbg_log (_("failed to remove file watch `%s`: %s"),
1894 finfo
->dname
, strerror (errno
));
1896 finfo
->inotify_descr
[TRACED_FILE
] = -1;
1897 finfo
->inotify_descr
[TRACED_DIR
] = -1;
1898 to_clear
[dbcnt
] = true;
1899 if (finfo
->call_res_init
)
1901 /* Continue to the next entry since this might be the
1902 parent directory for multiple registered files and
1903 we want to remove watches for all registered files. */
1906 /* The parent directory had a create or moved to event. */
1907 if (finfo
->inotify_descr
[TRACED_DIR
] == inev
->i
.wd
1908 && ((inev
->i
.mask
& IN_MOVED_TO
)
1909 || (inev
->i
.mask
& IN_CREATE
))
1910 && strcmp (inev
->i
.name
, finfo
->sfname
) == 0)
1912 /* We detected a directory change. We look for the creation
1913 of the file we are tracking or the move of the same file
1914 into the directory. */
1916 dbg_log (_("monitored file `%s` was %s, adding watch"),
1918 inev
->i
.mask
& IN_CREATE
? "created" : "moved into place");
1919 /* File was moved in or created. Regenerate the watch. */
1920 if (finfo
->inotify_descr
[TRACED_FILE
] != -1)
1921 inotify_rm_watch (inotify_fd
,
1922 finfo
->inotify_descr
[TRACED_FILE
]);
1924 ret
= inotify_add_watch (inotify_fd
,
1928 dbg_log (_("failed to add file watch `%s`: %s"),
1929 finfo
->fname
, strerror (errno
));
1931 finfo
->inotify_descr
[TRACED_FILE
] = ret
;
1933 /* The file is new or moved so mark cache as needing to
1934 be cleared and reinitialize. */
1935 to_clear
[dbcnt
] = true;
1936 if (finfo
->call_res_init
)
1939 /* Done re-adding the watch. Don't return, we may still
1940 have other files in this same directory, same watch
1941 descriptor, and need to process them. */
1943 /* Other events are ignored, and we move on to the next file. */
1944 finfo
= finfo
->next
;
1949 /* If an entry in the array of booleans TO_CLEAR is TRUE then clear the cache
1950 for the associated database, otherwise do nothing. The TO_CLEAR array must
1951 have LASTDB entries. */
1953 clear_db_cache (bool *to_clear
)
1955 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
1956 if (to_clear
[dbcnt
])
1958 pthread_mutex_lock (&dbs
[dbcnt
].prune_lock
);
1959 dbs
[dbcnt
].clear_cache
= 1;
1960 pthread_mutex_unlock (&dbs
[dbcnt
].prune_lock
);
1961 pthread_cond_signal (&dbs
[dbcnt
].prune_cond
);
1966 handle_inotify_events (void)
1968 bool to_clear
[lastdb
] = { false, };
1971 /* Read all inotify events for files registered via
1972 register_traced_file(). */
1975 /* Potentially read multiple events into buf. */
1976 ssize_t nb
= TEMP_FAILURE_RETRY (read (inotify_fd
,
1979 if (nb
< (ssize_t
) sizeof (struct inotify_event
))
1981 /* Not even 1 event. */
1982 if (__glibc_unlikely (nb
== -1 && errno
!= EAGAIN
))
1984 /* Done reading events that are ready. */
1987 /* Process all events. The normal inotify interface delivers
1988 complete events on a read and never a partial event. */
1989 char *eptr
= &inev
.buf
[0];
1993 /* Check which of the files changed. */
1994 inotify_check_files (to_clear
, &inev
);
1995 count
= sizeof (struct inotify_event
) + inev
.i
.len
;
1998 if (nb
>= (ssize_t
) sizeof (struct inotify_event
))
1999 memcpy (&inev
, eptr
, nb
);
2005 /* Actually perform the cache clearing. */
2006 clear_db_cache (to_clear
);
2013 __attribute__ ((__noreturn__
))
2014 main_loop_poll (void)
2016 struct pollfd
*conns
= (struct pollfd
*) xmalloc (nconns
2017 * sizeof (conns
[0]));
2020 conns
[0].events
= POLLRDNORM
;
2022 size_t firstfree
= 1;
2025 if (inotify_fd
!= -1)
2027 conns
[1].fd
= inotify_fd
;
2028 conns
[1].events
= POLLRDNORM
;
2035 size_t idx_nl_status_fd
= 0;
2036 if (nl_status_fd
!= -1)
2038 idx_nl_status_fd
= nused
;
2039 conns
[nused
].fd
= nl_status_fd
;
2040 conns
[nused
].events
= POLLRDNORM
;
2048 /* Wait for any event. We wait at most a couple of seconds so
2049 that we can check whether we should close any of the accepted
2050 connections since we have not received a request. */
2051 #define MAX_ACCEPT_TIMEOUT 30
2052 #define MIN_ACCEPT_TIMEOUT 5
2053 #define MAIN_THREAD_TIMEOUT \
2054 (MAX_ACCEPT_TIMEOUT * 1000 \
2055 - ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * 1000 * nused) / (2 * nconns))
2057 int n
= poll (conns
, nused
, MAIN_THREAD_TIMEOUT
);
2059 time_t now
= time (NULL
);
2061 /* If there is a descriptor ready for reading or there is a new
2062 connection, process this now. */
2065 if (conns
[0].revents
!= 0)
2067 /* We have a new incoming connection. Accept the connection. */
2068 int fd
= TEMP_FAILURE_RETRY (accept4 (sock
, NULL
, NULL
,
2071 /* Use the descriptor if we have not reached the limit. */
2074 if (firstfree
< nconns
)
2076 conns
[firstfree
].fd
= fd
;
2077 conns
[firstfree
].events
= POLLRDNORM
;
2078 starttime
[firstfree
] = now
;
2079 if (firstfree
>= nused
)
2080 nused
= firstfree
+ 1;
2084 while (firstfree
< nused
&& conns
[firstfree
].fd
!= -1);
2087 /* We cannot use the connection so close it. */
2096 if (inotify_fd
!= -1 && conns
[1].fd
== inotify_fd
)
2098 if (conns
[1].revents
!= 0)
2101 ret
= handle_inotify_events ();
2104 /* Something went wrong when reading the inotify
2105 data. Better disable inotify. */
2106 dbg_log (_("disabled inotify-based monitoring after read error %d"), errno
);
2122 if (idx_nl_status_fd
!= 0 && conns
[idx_nl_status_fd
].revents
!= 0)
2125 /* Read all the data. We do not interpret it here. */
2126 while (TEMP_FAILURE_RETRY (read (nl_status_fd
, buf
,
2127 sizeof (buf
))) != -1)
2130 dbs
[hstdb
].head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
]
2131 = __bump_nl_timestamp ();
2135 for (size_t cnt
= first
; cnt
< nused
&& n
> 0; ++cnt
)
2136 if (conns
[cnt
].revents
!= 0)
2138 fd_ready (conns
[cnt
].fd
);
2140 /* Clean up the CONNS array. */
2142 if (cnt
< firstfree
)
2144 if (cnt
== nused
- 1)
2147 while (conns
[nused
- 1].fd
== -1);
2153 /* Now find entries which have timed out. */
2156 /* We make the timeout length depend on the number of file
2157 descriptors currently used. */
2158 #define ACCEPT_TIMEOUT \
2159 (MAX_ACCEPT_TIMEOUT \
2160 - ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * nused) / nconns)
2161 time_t laststart
= now
- ACCEPT_TIMEOUT
;
2163 for (size_t cnt
= nused
- 1; cnt
> 0; --cnt
)
2165 if (conns
[cnt
].fd
!= -1 && starttime
[cnt
] < laststart
)
2167 /* Remove the entry, it timed out. */
2168 (void) close (conns
[cnt
].fd
);
2171 if (cnt
< firstfree
)
2173 if (cnt
== nused
- 1)
2176 while (conns
[nused
- 1].fd
== -1);
2180 if (restart_p (now
))
2188 main_loop_epoll (int efd
)
2190 struct epoll_event ev
= { 0, };
2194 /* Add the socket. */
2195 ev
.events
= EPOLLRDNORM
;
2197 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, sock
, &ev
) == -1)
2198 /* We cannot use epoll. */
2201 # ifdef HAVE_INOTIFY
2202 if (inotify_fd
!= -1)
2204 ev
.events
= EPOLLRDNORM
;
2205 ev
.data
.fd
= inotify_fd
;
2206 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, inotify_fd
, &ev
) == -1)
2207 /* We cannot use epoll. */
2213 # ifdef HAVE_NETLINK
2214 if (nl_status_fd
!= -1)
2216 ev
.events
= EPOLLRDNORM
;
2217 ev
.data
.fd
= nl_status_fd
;
2218 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, nl_status_fd
, &ev
) == -1)
2219 /* We cannot use epoll. */
2226 struct epoll_event revs
[100];
2227 # define nrevs (sizeof (revs) / sizeof (revs[0]))
2229 int n
= epoll_wait (efd
, revs
, nrevs
, MAIN_THREAD_TIMEOUT
);
2231 time_t now
= time (NULL
);
2233 for (int cnt
= 0; cnt
< n
; ++cnt
)
2234 if (revs
[cnt
].data
.fd
== sock
)
2236 /* A new connection. */
2237 int fd
= TEMP_FAILURE_RETRY (accept4 (sock
, NULL
, NULL
,
2240 /* Use the descriptor if we have not reached the limit. */
2243 /* Try to add the new descriptor. */
2246 || epoll_ctl (efd
, EPOLL_CTL_ADD
, fd
, &ev
) == -1)
2247 /* The descriptor is too large or something went
2248 wrong. Close the descriptor. */
2252 /* Remember when we accepted the connection. */
2253 starttime
[fd
] = now
;
2262 # ifdef HAVE_INOTIFY
2263 else if (revs
[cnt
].data
.fd
== inotify_fd
)
2266 ret
= handle_inotify_events ();
2269 /* Something went wrong when reading the inotify
2270 data. Better disable inotify. */
2271 dbg_log (_("disabled inotify-based monitoring after read error %d"), errno
);
2272 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, inotify_fd
, NULL
);
2279 # ifdef HAVE_NETLINK
2280 else if (revs
[cnt
].data
.fd
== nl_status_fd
)
2283 /* Read all the data. We do not interpret it here. */
2284 while (TEMP_FAILURE_RETRY (read (nl_status_fd
, buf
,
2285 sizeof (buf
))) != -1)
2288 __bump_nl_timestamp ();
2293 /* Remove the descriptor from the epoll descriptor. */
2294 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, revs
[cnt
].data
.fd
, NULL
);
2296 /* Get a worker to handle the request. */
2297 fd_ready (revs
[cnt
].data
.fd
);
2299 /* Reset the time. */
2300 starttime
[revs
[cnt
].data
.fd
] = 0;
2301 if (revs
[cnt
].data
.fd
== highest
)
2304 while (highest
> 0 && starttime
[highest
] == 0);
2309 /* Now look for descriptors for accepted connections which have
2310 no reply in too long of a time. */
2311 time_t laststart
= now
- ACCEPT_TIMEOUT
;
2312 assert (starttime
[sock
] == 0);
2313 # ifdef HAVE_INOTIFY
2314 assert (inotify_fd
== -1 || starttime
[inotify_fd
] == 0);
2316 assert (nl_status_fd
== -1 || starttime
[nl_status_fd
] == 0);
2317 for (int cnt
= highest
; cnt
> STDERR_FILENO
; --cnt
)
2318 if (starttime
[cnt
] != 0 && starttime
[cnt
] < laststart
)
2320 /* We are waiting for this one for too long. Close it. */
2321 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, cnt
, NULL
);
2329 else if (cnt
!= sock
&& starttime
[cnt
] == 0 && cnt
== highest
)
2332 if (restart_p (now
))
2339 /* Start all the threads we want. The initial process is thread no. 1. */
2341 start_threads (void)
2343 /* Initialize the conditional variable we will use. The only
2344 non-standard attribute we might use is the clock selection. */
2345 pthread_condattr_t condattr
;
2346 pthread_condattr_init (&condattr
);
2348 #if defined _POSIX_CLOCK_SELECTION && _POSIX_CLOCK_SELECTION >= 0 \
2349 && defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
2350 /* Determine whether the monotonous clock is available. */
2351 struct timespec dummy
;
2352 # if _POSIX_MONOTONIC_CLOCK == 0
2353 if (sysconf (_SC_MONOTONIC_CLOCK
) > 0)
2355 # if _POSIX_CLOCK_SELECTION == 0
2356 if (sysconf (_SC_CLOCK_SELECTION
) > 0)
2358 if (clock_getres (CLOCK_MONOTONIC
, &dummy
) == 0
2359 && pthread_condattr_setclock (&condattr
, CLOCK_MONOTONIC
) == 0)
2360 timeout_clock
= CLOCK_MONOTONIC
;
2363 /* Create the attribute for the threads. They are all created
2365 pthread_attr_init (&attr
);
2366 pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
);
2367 /* Use 1MB stacks, twice as much for 64-bit architectures. */
2368 pthread_attr_setstacksize (&attr
, NSCD_THREAD_STACKSIZE
);
2370 /* We allow less than LASTDB threads only for debugging. */
2371 if (debug_level
== 0)
2372 nthreads
= MAX (nthreads
, lastdb
);
2374 /* Create the threads which prune the databases. */
2375 // XXX Ideally this work would be done by some of the worker threads.
2376 // XXX But this is problematic since we would need to be able to wake
2377 // XXX them up explicitly as well as part of the group handling the
2378 // XXX ready-list. This requires an operation where we can wait on
2379 // XXX two conditional variables at the same time. This operation
2380 // XXX does not exist (yet).
2381 for (long int i
= 0; i
< lastdb
; ++i
)
2383 /* Initialize the conditional variable. */
2384 if (pthread_cond_init (&dbs
[i
].prune_cond
, &condattr
) != 0)
2386 dbg_log (_("could not initialize conditional variable"));
2387 do_exit (1, 0, NULL
);
2392 && pthread_create (&th
, &attr
, nscd_run_prune
, (void *) i
) != 0)
2394 dbg_log (_("could not start clean-up thread; terminating"));
2395 do_exit (1, 0, NULL
);
2399 pthread_condattr_destroy (&condattr
);
2401 for (long int i
= 0; i
< nthreads
; ++i
)
2404 if (pthread_create (&th
, &attr
, nscd_run_worker
, NULL
) != 0)
2408 dbg_log (_("could not start any worker thread; terminating"));
2409 do_exit (1, 0, NULL
);
2416 /* Now it is safe to let the parent know that we're doing fine and it can
2420 /* Determine how much room for descriptors we should initially
2421 allocate. This might need to change later if we cap the number
2423 const long int nfds
= sysconf (_SC_OPEN_MAX
);
2425 #define MAXCONN 16384
2426 if (nfds
== -1 || nfds
> MAXCONN
)
2428 else if (nfds
< MINCONN
)
2433 /* We need memory to pass descriptors on to the worker threads. */
2434 fdlist
= (struct fdlist
*) xcalloc (nconns
, sizeof (fdlist
[0]));
2435 /* Array to keep track when connection was accepted. */
2436 starttime
= (time_t *) xcalloc (nconns
, sizeof (starttime
[0]));
2438 /* In the main thread we execute the loop which handles incoming
2441 int efd
= epoll_create (100);
2444 main_loop_epoll (efd
);
2453 /* Look up the uid, gid, and supplementary groups to run nscd as. When
2454 this function is called, we are not listening on the nscd socket yet so
2455 we can just use the ordinary lookup functions without causing a lockup */
2457 begin_drop_privileges (void)
2459 struct passwd
*pwd
= getpwnam (server_user
);
2463 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2464 do_exit (EXIT_FAILURE
, 0,
2465 _("Failed to run nscd as user '%s'"), server_user
);
2468 server_uid
= pwd
->pw_uid
;
2469 server_gid
= pwd
->pw_gid
;
2471 /* Save the old UID/GID if we have to change back. */
2474 old_uid
= getuid ();
2475 old_gid
= getgid ();
2478 if (getgrouplist (server_user
, server_gid
, NULL
, &server_ngroups
) == 0)
2480 /* This really must never happen. */
2481 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2482 do_exit (EXIT_FAILURE
, errno
,
2483 _("initial getgrouplist failed"));
2486 server_groups
= (gid_t
*) xmalloc (server_ngroups
* sizeof (gid_t
));
2488 if (getgrouplist (server_user
, server_gid
, server_groups
, &server_ngroups
)
2491 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2492 do_exit (EXIT_FAILURE
, errno
, _("getgrouplist failed"));
2497 /* Call setgroups(), setgid(), and setuid() to drop root privileges and
2498 run nscd as the user specified in the configuration file. */
2500 finish_drop_privileges (void)
2502 #if defined HAVE_LIBAUDIT && defined HAVE_LIBCAP
2503 /* We need to preserve the capabilities to connect to the audit daemon. */
2504 cap_t new_caps
= preserve_capabilities ();
2507 if (setgroups (server_ngroups
, server_groups
) == -1)
2509 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2510 do_exit (EXIT_FAILURE
, errno
, _("setgroups failed"));
2515 res
= setresgid (server_gid
, server_gid
, old_gid
);
2517 res
= setgid (server_gid
);
2520 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2521 do_exit (4, errno
, "setgid");
2525 res
= setresuid (server_uid
, server_uid
, old_uid
);
2527 res
= setuid (server_uid
);
2530 dbg_log (_("Failed to run nscd as user '%s'"), server_user
);
2531 do_exit (4, errno
, "setuid");
2534 #if defined HAVE_LIBAUDIT && defined HAVE_LIBCAP
2535 /* Remove the temporary capabilities. */
2536 install_real_capabilities (new_caps
);