1 /* Inner loops of cache daemon.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
34 #include <arpa/inet.h>
36 # include <linux/netlink.h>
37 # include <linux/rtnetlink.h>
40 # include <sys/epoll.h>
43 # include <sys/inotify.h>
46 #include <sys/param.h>
48 #include <sys/socket.h>
55 #include <resolv/resolv.h>
57 #include <kernel-features.h>
58 #include <libc-diag.h>
61 /* Support to run nscd as an unprivileged user */
62 const char *server_user
;
63 static uid_t server_uid
;
64 static gid_t server_gid
;
65 const char *stat_user
;
67 static gid_t
*server_groups
;
71 static int server_ngroups
;
73 static pthread_attr_t attr
;
75 static void begin_drop_privileges (void);
76 static void finish_drop_privileges (void);
78 /* Map request type to a string. */
79 const char *const serv2str
[LASTREQ
] =
81 [GETPWBYNAME
] = "GETPWBYNAME",
82 [GETPWBYUID
] = "GETPWBYUID",
83 [GETGRBYNAME
] = "GETGRBYNAME",
84 [GETGRBYGID
] = "GETGRBYGID",
85 [GETHOSTBYNAME
] = "GETHOSTBYNAME",
86 [GETHOSTBYNAMEv6
] = "GETHOSTBYNAMEv6",
87 [GETHOSTBYADDR
] = "GETHOSTBYADDR",
88 [GETHOSTBYADDRv6
] = "GETHOSTBYADDRv6",
89 [SHUTDOWN
] = "SHUTDOWN",
90 [GETSTAT
] = "GETSTAT",
91 [INVALIDATE
] = "INVALIDATE",
92 [GETFDPW
] = "GETFDPW",
93 [GETFDGR
] = "GETFDGR",
94 [GETFDHST
] = "GETFDHST",
96 [INITGROUPS
] = "INITGROUPS",
97 [GETSERVBYNAME
] = "GETSERVBYNAME",
98 [GETSERVBYPORT
] = "GETSERVBYPORT",
99 [GETFDSERV
] = "GETFDSERV",
100 [GETNETGRENT
] = "GETNETGRENT",
101 [INNETGR
] = "INNETGR",
102 [GETFDNETGR
] = "GETFDNETGR"
105 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
106 # define RWLOCK_INITIALIZER PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
108 # define RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
111 /* The control data structures for the services. */
112 struct database_dyn dbs
[lastdb
] =
115 .lock
= RWLOCK_INITIALIZER
,
116 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
117 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
123 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
124 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
125 .db_filename
= _PATH_NSCD_PASSWD_DB
,
126 .disabled_iov
= &pwd_iov_disabled
,
134 .lock
= RWLOCK_INITIALIZER
,
135 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
136 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
142 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
143 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
144 .db_filename
= _PATH_NSCD_GROUP_DB
,
145 .disabled_iov
= &grp_iov_disabled
,
153 .lock
= RWLOCK_INITIALIZER
,
154 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
155 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
159 .propagate
= 0, /* Not used. */
161 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
162 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
163 .db_filename
= _PATH_NSCD_HOSTS_DB
,
164 .disabled_iov
= &hst_iov_disabled
,
172 .lock
= RWLOCK_INITIALIZER
,
173 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
174 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
178 .propagate
= 0, /* Not used. */
180 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
181 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
182 .db_filename
= _PATH_NSCD_SERVICES_DB
,
183 .disabled_iov
= &serv_iov_disabled
,
191 .lock
= RWLOCK_INITIALIZER
,
192 .prune_lock
= PTHREAD_MUTEX_INITIALIZER
,
193 .prune_run_lock
= PTHREAD_MUTEX_INITIALIZER
,
197 .propagate
= 0, /* Not used. */
199 .max_db_size
= DEFAULT_MAX_DB_SIZE
,
200 .suggested_module
= DEFAULT_SUGGESTED_MODULE
,
201 .db_filename
= _PATH_NSCD_NETGROUP_DB
,
202 .disabled_iov
= &netgroup_iov_disabled
,
212 /* Mapping of request type to database. */
216 struct database_dyn
*db
;
217 } const reqinfo
[LASTREQ
] =
219 [GETPWBYNAME
] = { true, &dbs
[pwddb
] },
220 [GETPWBYUID
] = { true, &dbs
[pwddb
] },
221 [GETGRBYNAME
] = { true, &dbs
[grpdb
] },
222 [GETGRBYGID
] = { true, &dbs
[grpdb
] },
223 [GETHOSTBYNAME
] = { true, &dbs
[hstdb
] },
224 [GETHOSTBYNAMEv6
] = { true, &dbs
[hstdb
] },
225 [GETHOSTBYADDR
] = { true, &dbs
[hstdb
] },
226 [GETHOSTBYADDRv6
] = { true, &dbs
[hstdb
] },
227 [SHUTDOWN
] = { false, NULL
},
228 [GETSTAT
] = { false, NULL
},
229 [GETFDPW
] = { false, &dbs
[pwddb
] },
230 [GETFDGR
] = { false, &dbs
[grpdb
] },
231 [GETFDHST
] = { false, &dbs
[hstdb
] },
232 [GETAI
] = { true, &dbs
[hstdb
] },
233 [INITGROUPS
] = { true, &dbs
[grpdb
] },
234 [GETSERVBYNAME
] = { true, &dbs
[servdb
] },
235 [GETSERVBYPORT
] = { true, &dbs
[servdb
] },
236 [GETFDSERV
] = { false, &dbs
[servdb
] },
237 [GETNETGRENT
] = { true, &dbs
[netgrdb
] },
238 [INNETGR
] = { true, &dbs
[netgrdb
] },
239 [GETFDNETGR
] = { false, &dbs
[netgrdb
] }
243 /* Initial number of threads to use. */
245 /* Maximum number of threads to use. */
246 int max_nthreads
= 32;
248 /* Socket for incoming connections. */
252 /* Inotify descriptor. */
257 /* Descriptor for netlink status updates. */
258 static int nl_status_fd
= -1;
261 /* Number of times clients had to wait. */
262 unsigned long int client_queued
;
266 writeall (int fd
, const void *buf
, size_t len
)
272 ret
= TEMP_FAILURE_RETRY (send (fd
, buf
, n
, MSG_NOSIGNAL
));
275 buf
= (const char *) buf
+ ret
;
279 return ret
< 0 ? ret
: len
- n
;
286 /* The following three are not really used, they are symbolic constants. */
292 use_he_begin
= use_he
| use_begin
,
293 use_he_end
= use_he
| use_end
,
295 use_data_begin
= use_data
| use_begin
,
296 use_data_end
= use_data
| use_end
,
297 use_data_first
= use_data_begin
| use_first
302 check_use (const char *data
, nscd_ssize_t first_free
, uint8_t *usemap
,
303 enum usekey use
, ref_t start
, size_t len
)
308 if (start
> first_free
|| start
+ len
> first_free
309 || (start
& BLOCK_ALIGN_M1
))
312 if (usemap
[start
] == use_not
)
314 /* Add the start marker. */
315 usemap
[start
] = use
| use_begin
;
319 if (usemap
[++start
] != use_not
)
324 /* Add the end marker. */
325 usemap
[start
] = use
| use_end
;
327 else if ((usemap
[start
] & ~use_first
) == ((use
| use_begin
) & ~use_first
))
329 /* Hash entries can't be shared. */
333 usemap
[start
] |= (use
& use_first
);
337 if (usemap
[++start
] != use
)
340 if (usemap
[++start
] != (use
| use_end
))
344 /* Points to a wrong object or somewhere in the middle. */
351 /* Verify data in persistent database. */
353 verify_persistent_db (void *mem
, struct database_pers_head
*readhead
, int dbnr
)
355 assert (dbnr
== pwddb
|| dbnr
== grpdb
|| dbnr
== hstdb
|| dbnr
== servdb
358 time_t now
= time (NULL
);
360 struct database_pers_head
*head
= mem
;
361 struct database_pers_head head_copy
= *head
;
363 /* Check that the header that was read matches the head in the database. */
364 if (memcmp (head
, readhead
, sizeof (*head
)) != 0)
367 /* First some easy tests: make sure the database header is sane. */
368 if (head
->version
!= DB_VERSION
369 || head
->header_size
!= sizeof (*head
)
370 /* We allow a timestamp to be one hour ahead of the current time.
371 This should cover daylight saving time changes. */
372 || head
->timestamp
> now
+ 60 * 60 + 60
373 || (head
->gc_cycle
& 1)
375 || (size_t) head
->module
> INT32_MAX
/ sizeof (ref_t
)
376 || (size_t) head
->data_size
> INT32_MAX
- head
->module
* sizeof (ref_t
)
377 || head
->first_free
< 0
378 || head
->first_free
> head
->data_size
379 || (head
->first_free
& BLOCK_ALIGN_M1
) != 0
380 || head
->maxnentries
< 0
381 || head
->maxnsearched
< 0)
384 uint8_t *usemap
= calloc (head
->first_free
, 1);
388 const char *data
= (char *) &head
->array
[roundup (head
->module
,
389 ALIGN
/ sizeof (ref_t
))];
391 nscd_ssize_t he_cnt
= 0;
392 for (nscd_ssize_t cnt
= 0; cnt
< head
->module
; ++cnt
)
394 ref_t trail
= head
->array
[cnt
];
398 while (work
!= ENDREF
)
400 if (! check_use (data
, head
->first_free
, usemap
, use_he
, work
,
401 sizeof (struct hashentry
)))
404 /* Now we know we can dereference the record. */
405 struct hashentry
*here
= (struct hashentry
*) (data
+ work
);
409 /* Make sure the record is for this type of service. */
410 if (here
->type
>= LASTREQ
411 || reqinfo
[here
->type
].db
!= &dbs
[dbnr
])
414 /* Validate boolean field value. */
415 if (here
->first
!= false && here
->first
!= true)
423 || here
->packet
> head
->first_free
424 || here
->packet
+ sizeof (struct datahead
) > head
->first_free
)
427 struct datahead
*dh
= (struct datahead
*) (data
+ here
->packet
);
429 if (! check_use (data
, head
->first_free
, usemap
,
430 use_data
| (here
->first
? use_first
: 0),
431 here
->packet
, dh
->allocsize
))
434 if (dh
->allocsize
< sizeof (struct datahead
)
435 || dh
->recsize
> dh
->allocsize
436 || (dh
->notfound
!= false && dh
->notfound
!= true)
437 || (dh
->usable
!= false && dh
->usable
!= true))
440 if (here
->key
< here
->packet
+ sizeof (struct datahead
)
441 || here
->key
> here
->packet
+ dh
->allocsize
442 || here
->key
+ here
->len
> here
->packet
+ dh
->allocsize
)
448 /* A circular list, this must not happen. */
451 trail
= ((struct hashentry
*) (data
+ trail
))->next
;
456 if (he_cnt
!= head
->nentries
)
459 /* See if all data and keys had at least one reference from
460 he->first == true hashentry. */
461 for (ref_t idx
= 0; idx
< head
->first_free
; ++idx
)
463 if (usemap
[idx
] == use_data_begin
)
467 /* Finally, make sure the database hasn't changed since the first test. */
468 if (memcmp (mem
, &head_copy
, sizeof (*head
)) != 0)
480 /* Initialize database information structures. */
484 /* Look up unprivileged uid/gid/groups before we start listening on the
486 if (server_user
!= NULL
)
487 begin_drop_privileges ();
490 /* No configuration for this value, assume a default. */
493 for (size_t cnt
= 0; cnt
< lastdb
; ++cnt
)
494 if (dbs
[cnt
].enabled
)
496 pthread_rwlock_init (&dbs
[cnt
].lock
, NULL
);
497 pthread_mutex_init (&dbs
[cnt
].memlock
, NULL
);
499 if (dbs
[cnt
].persistent
)
501 /* Try to open the appropriate file on disk. */
502 int fd
= open (dbs
[cnt
].db_filename
, O_RDWR
| O_CLOEXEC
);
509 struct database_pers_head head
;
510 ssize_t n
= TEMP_FAILURE_RETRY (read (fd
, &head
,
512 if (n
!= sizeof (head
) || fstat64 (fd
, &st
) != 0)
515 /* The code is single-threaded at this point so
516 using strerror is just fine. */
517 msg
= strerror (errno
);
519 dbg_log (_("invalid persistent database file \"%s\": %s"),
520 dbs
[cnt
].db_filename
, msg
);
521 unlink (dbs
[cnt
].db_filename
);
523 else if (head
.module
== 0 && head
.data_size
== 0)
525 /* The file has been created, but the head has not
526 been initialized yet. */
527 msg
= _("uninitialized header");
530 else if (head
.header_size
!= (int) sizeof (head
))
532 msg
= _("header size does not match");
535 else if ((total
= (sizeof (head
)
536 + roundup (head
.module
* sizeof (ref_t
),
540 || total
< sizeof (head
))
542 msg
= _("file size does not match");
545 /* Note we map with the maximum size allowed for the
546 database. This is likely much larger than the
547 actual file size. This is OK on most OSes since
548 extensions of the underlying file will
549 automatically translate more pages available for
551 else if ((mem
= mmap (NULL
, dbs
[cnt
].max_db_size
,
552 PROT_READ
| PROT_WRITE
,
556 else if (!verify_persistent_db (mem
, &head
, cnt
))
559 msg
= _("verification failed");
564 /* Success. We have the database. */
566 dbs
[cnt
].memsize
= total
;
567 dbs
[cnt
].data
= (char *)
568 &dbs
[cnt
].head
->array
[roundup (dbs
[cnt
].head
->module
,
569 ALIGN
/ sizeof (ref_t
))];
570 dbs
[cnt
].mmap_used
= true;
572 if (dbs
[cnt
].suggested_module
> head
.module
)
573 dbg_log (_("suggested size of table for database %s larger than the persistent database's table"),
578 /* We also need a read-only descriptor. */
581 dbs
[cnt
].ro_fd
= open (dbs
[cnt
].db_filename
,
582 O_RDONLY
| O_CLOEXEC
);
583 if (dbs
[cnt
].ro_fd
== -1)
585 cannot create read-only descriptor for \"%s\"; no mmap"),
586 dbs
[cnt
].db_filename
);
589 // XXX Shall we test whether the descriptors actually
590 // XXX point to the same file?
593 /* Close the file descriptors in case something went
594 wrong in which case the variable have not been
599 else if (errno
== EACCES
)
600 do_exit (EXIT_FAILURE
, 0, _("cannot access '%s'"),
601 dbs
[cnt
].db_filename
);
604 if (dbs
[cnt
].head
== NULL
)
606 /* No database loaded. Allocate the data structure,
608 struct database_pers_head head
;
609 size_t total
= (sizeof (head
)
610 + roundup (dbs
[cnt
].suggested_module
611 * sizeof (ref_t
), ALIGN
)
612 + (dbs
[cnt
].suggested_module
613 * DEFAULT_DATASIZE_PER_BUCKET
));
615 /* Try to create the database. If we do not need a
616 persistent database create a temporary file. */
619 if (dbs
[cnt
].persistent
)
621 fd
= open (dbs
[cnt
].db_filename
,
622 O_RDWR
| O_CREAT
| O_EXCL
| O_TRUNC
| O_CLOEXEC
,
624 if (fd
!= -1 && dbs
[cnt
].shared
)
625 ro_fd
= open (dbs
[cnt
].db_filename
,
626 O_RDONLY
| O_CLOEXEC
);
630 char fname
[] = _PATH_NSCD_XYZ_DB_TMP
;
631 fd
= mkostemp (fname
, O_CLOEXEC
);
633 /* We do not need the file name anymore after we
634 opened another file descriptor in read-only mode. */
638 ro_fd
= open (fname
, O_RDONLY
| O_CLOEXEC
);
648 dbg_log (_("database for %s corrupted or simultaneously used; remove %s manually if necessary and restart"),
649 dbnames
[cnt
], dbs
[cnt
].db_filename
);
650 do_exit (1, 0, NULL
);
653 if (dbs
[cnt
].persistent
)
654 dbg_log (_("cannot create %s; no persistent database used"),
655 dbs
[cnt
].db_filename
);
657 dbg_log (_("cannot create %s; no sharing possible"),
658 dbs
[cnt
].db_filename
);
660 dbs
[cnt
].persistent
= 0;
661 // XXX remember: no mmap
665 /* Tell the user if we could not create the read-only
667 if (ro_fd
== -1 && dbs
[cnt
].shared
)
669 cannot create read-only descriptor for \"%s\"; no mmap"),
670 dbs
[cnt
].db_filename
);
672 /* Before we create the header, initialize the hash
673 table. That way if we get interrupted while writing
674 the header we can recognize a partially initialized
676 size_t ps
= sysconf (_SC_PAGESIZE
);
678 assert (~ENDREF
== 0);
679 memset (tmpbuf
, '\xff', ps
);
681 size_t remaining
= dbs
[cnt
].suggested_module
* sizeof (ref_t
);
682 off_t offset
= sizeof (head
);
685 if (offset
% ps
!= 0)
687 towrite
= MIN (remaining
, ps
- (offset
% ps
));
688 if (pwrite (fd
, tmpbuf
, towrite
, offset
) != towrite
)
691 remaining
-= towrite
;
694 while (remaining
> ps
)
696 if (pwrite (fd
, tmpbuf
, ps
, offset
) == -1)
703 && pwrite (fd
, tmpbuf
, remaining
, offset
) != remaining
)
706 /* Create the header of the file. */
707 struct database_pers_head head
=
709 .version
= DB_VERSION
,
710 .header_size
= sizeof (head
),
711 .module
= dbs
[cnt
].suggested_module
,
712 .data_size
= (dbs
[cnt
].suggested_module
713 * DEFAULT_DATASIZE_PER_BUCKET
),
718 if ((TEMP_FAILURE_RETRY (write (fd
, &head
, sizeof (head
)))
720 || (TEMP_FAILURE_RETRY_VAL (posix_fallocate (fd
, 0, total
))
722 || (mem
= mmap (NULL
, dbs
[cnt
].max_db_size
,
723 PROT_READ
| PROT_WRITE
,
724 MAP_SHARED
, fd
, 0)) == MAP_FAILED
)
727 unlink (dbs
[cnt
].db_filename
);
728 dbg_log (_("cannot write to database file %s: %s"),
729 dbs
[cnt
].db_filename
, strerror (errno
));
730 dbs
[cnt
].persistent
= 0;
736 dbs
[cnt
].data
= (char *)
737 &dbs
[cnt
].head
->array
[roundup (dbs
[cnt
].head
->module
,
738 ALIGN
/ sizeof (ref_t
))];
739 dbs
[cnt
].memsize
= total
;
740 dbs
[cnt
].mmap_used
= true;
742 /* Remember the descriptors. */
744 dbs
[cnt
].ro_fd
= ro_fd
;
756 if (dbs
[cnt
].head
== NULL
)
758 /* We do not use the persistent database. Just
759 create an in-memory data structure. */
760 assert (! dbs
[cnt
].persistent
);
762 dbs
[cnt
].head
= xmalloc (sizeof (struct database_pers_head
)
763 + (dbs
[cnt
].suggested_module
765 memset (dbs
[cnt
].head
, '\0', sizeof (struct database_pers_head
));
766 assert (~ENDREF
== 0);
767 memset (dbs
[cnt
].head
->array
, '\xff',
768 dbs
[cnt
].suggested_module
* sizeof (ref_t
));
769 dbs
[cnt
].head
->module
= dbs
[cnt
].suggested_module
;
770 dbs
[cnt
].head
->data_size
= (DEFAULT_DATASIZE_PER_BUCKET
771 * dbs
[cnt
].head
->module
);
772 dbs
[cnt
].data
= xmalloc (dbs
[cnt
].head
->data_size
);
773 dbs
[cnt
].head
->first_free
= 0;
776 assert (dbs
[cnt
].ro_fd
== -1);
780 /* Create the socket. */
781 sock
= socket (AF_UNIX
, SOCK_STREAM
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, 0);
784 dbg_log (_("cannot open socket: %s"), strerror (errno
));
785 do_exit (errno
== EACCES
? 4 : 1, 0, NULL
);
787 /* Bind a name to the socket. */
788 struct sockaddr_un sock_addr
;
789 sock_addr
.sun_family
= AF_UNIX
;
790 strcpy (sock_addr
.sun_path
, _PATH_NSCDSOCKET
);
791 if (bind (sock
, (struct sockaddr
*) &sock_addr
, sizeof (sock_addr
)) < 0)
793 dbg_log ("%s: %s", _PATH_NSCDSOCKET
, strerror (errno
));
794 do_exit (errno
== EACCES
? 4 : 1, 0, NULL
);
797 /* Set permissions for the socket. */
798 chmod (_PATH_NSCDSOCKET
, DEFFILEMODE
);
800 /* Set the socket up to accept connections. */
801 if (listen (sock
, SOMAXCONN
) < 0)
803 dbg_log (_("cannot enable socket to accept connections: %s"),
805 do_exit (1, 0, NULL
);
809 if (dbs
[hstdb
].enabled
)
811 /* Try to open netlink socket to monitor network setting changes. */
812 nl_status_fd
= socket (AF_NETLINK
,
813 SOCK_RAW
| SOCK_CLOEXEC
| SOCK_NONBLOCK
,
815 if (nl_status_fd
!= -1)
817 struct sockaddr_nl snl
;
818 memset (&snl
, '\0', sizeof (snl
));
819 snl
.nl_family
= AF_NETLINK
;
820 /* XXX Is this the best set to use? */
821 snl
.nl_groups
= (RTMGRP_IPV4_IFADDR
| RTMGRP_TC
| RTMGRP_IPV4_MROUTE
822 | RTMGRP_IPV4_ROUTE
| RTMGRP_IPV4_RULE
823 | RTMGRP_IPV6_IFADDR
| RTMGRP_IPV6_MROUTE
824 | RTMGRP_IPV6_ROUTE
| RTMGRP_IPV6_IFINFO
825 | RTMGRP_IPV6_PREFIX
);
827 if (bind (nl_status_fd
, (struct sockaddr
*) &snl
, sizeof (snl
)) != 0)
829 close (nl_status_fd
);
834 /* Start the timestamp process. */
835 dbs
[hstdb
].head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
]
836 = __bump_nl_timestamp ();
842 /* Change to unprivileged uid/gid/groups if specified in config file */
843 if (server_user
!= NULL
)
844 finish_drop_privileges ();
848 #define TRACED_FILE_MASK (IN_DELETE_SELF | IN_CLOSE_WRITE | IN_MOVE_SELF)
849 #define TRACED_DIR_MASK (IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MOVE_SELF)
851 install_watches (struct traced_file
*finfo
)
853 /* Use inotify support if we have it. */
854 if (finfo
->inotify_descr
[TRACED_FILE
] < 0)
855 finfo
->inotify_descr
[TRACED_FILE
] = inotify_add_watch (inotify_fd
,
858 if (finfo
->inotify_descr
[TRACED_FILE
] < 0)
860 dbg_log (_("disabled inotify-based monitoring for file `%s': %s"),
861 finfo
->fname
, strerror (errno
));
864 dbg_log (_("monitoring file `%s` (%d)"),
865 finfo
->fname
, finfo
->inotify_descr
[TRACED_FILE
]);
866 /* Additionally listen for events in the file's parent directory.
867 We do this because the file to be watched might be
868 deleted and then added back again. When it is added back again
869 we must re-add the watch. We must also cover IN_MOVED_TO to
870 detect a file being moved into the directory. */
871 if (finfo
->inotify_descr
[TRACED_DIR
] < 0)
872 finfo
->inotify_descr
[TRACED_DIR
] = inotify_add_watch (inotify_fd
,
875 if (finfo
->inotify_descr
[TRACED_DIR
] < 0)
877 dbg_log (_("disabled inotify-based monitoring for directory `%s': %s"),
878 finfo
->fname
, strerror (errno
));
881 dbg_log (_("monitoring directory `%s` (%d)"),
882 finfo
->dname
, finfo
->inotify_descr
[TRACED_DIR
]);
886 /* Register the file in FINFO as a traced file for the database DBS[DBIX].
888 We support registering multiple files per database. Each call to
889 register_traced_file adds to the list of registered files.
891 When we prune the database, either through timeout or a request to
892 invalidate, we will check to see if any of the registered files has changed.
893 When we accept new connections to handle a cache request we will also
894 check to see if any of the registered files has changed.
896 If we have inotify support then we install an inotify fd to notify us of
897 file deletion or modification, both of which will require we invalidate
898 the cache for the database. Without inotify support we stat the file and
899 store st_mtime to determine if the file has been modified. */
901 register_traced_file (size_t dbidx
, struct traced_file
*finfo
)
903 /* If the database is disabled or file checking is disabled
904 then ignore the registration. */
905 if (! dbs
[dbidx
].enabled
|| ! dbs
[dbidx
].check_file
)
908 if (__glibc_unlikely (debug_level
> 0))
909 dbg_log (_("monitoring file %s for database %s"),
910 finfo
->fname
, dbnames
[dbidx
]);
913 install_watches (finfo
);
916 if (stat64 (finfo
->fname
, &st
) < 0)
918 /* We cannot stat() the file. Set mtime to zero and try again later. */
919 dbg_log (_("stat failed for file `%s'; will try again later: %s"),
920 finfo
->fname
, strerror (errno
));
924 finfo
->mtime
= st
.st_mtime
;
926 /* Queue up the file name. */
927 finfo
->next
= dbs
[dbidx
].traced_files
;
928 dbs
[dbidx
].traced_files
= finfo
;
932 /* Close the connections. */
941 invalidate_cache (char *key
, int fd
)
946 for (number
= pwddb
; number
< lastdb
; ++number
)
947 if (strcmp (key
, dbnames
[number
]) == 0)
949 struct traced_file
*runp
= dbs
[number
].traced_files
;
952 /* Make sure we reload from file when checking mtime. */
955 /* During an invalidation we try to reload the traced
956 file watches. This allows the user to re-sync if
957 inotify events were lost. Similar to what we do during
959 install_watches (runp
);
961 if (runp
->call_res_init
)
971 if (number
== lastdb
)
974 writeall (fd
, &resp
, sizeof (resp
));
978 if (dbs
[number
].enabled
)
980 pthread_mutex_lock (&dbs
[number
].prune_run_lock
);
981 prune_cache (&dbs
[number
], LONG_MAX
, fd
);
982 pthread_mutex_unlock (&dbs
[number
].prune_run_lock
);
987 writeall (fd
, &resp
, sizeof (resp
));
994 send_ro_fd (struct database_dyn
*db
, char *key
, int fd
)
996 /* If we do not have an read-only file descriptor do nothing. */
1000 /* We need to send some data along with the descriptor. */
1001 uint64_t mapsize
= (db
->head
->data_size
1002 + roundup (db
->head
->module
* sizeof (ref_t
), ALIGN
)
1003 + sizeof (struct database_pers_head
));
1004 struct iovec iov
[2];
1005 iov
[0].iov_base
= key
;
1006 iov
[0].iov_len
= strlen (key
) + 1;
1007 iov
[1].iov_base
= &mapsize
;
1008 iov
[1].iov_len
= sizeof (mapsize
);
1010 /* Prepare the control message to transfer the descriptor. */
1014 char bytes
[CMSG_SPACE (sizeof (int))];
1016 struct msghdr msg
= { .msg_iov
= iov
, .msg_iovlen
= 2,
1017 .msg_control
= buf
.bytes
,
1018 .msg_controllen
= sizeof (buf
) };
1019 struct cmsghdr
*cmsg
= CMSG_FIRSTHDR (&msg
);
1021 cmsg
->cmsg_level
= SOL_SOCKET
;
1022 cmsg
->cmsg_type
= SCM_RIGHTS
;
1023 cmsg
->cmsg_len
= CMSG_LEN (sizeof (int));
1025 int *ip
= (int *) CMSG_DATA (cmsg
);
1028 msg
.msg_controllen
= cmsg
->cmsg_len
;
1030 /* Send the control message. We repeat when we are interrupted but
1031 everything else is ignored. */
1032 #ifndef MSG_NOSIGNAL
1033 # define MSG_NOSIGNAL 0
1035 (void) TEMP_FAILURE_RETRY (sendmsg (fd
, &msg
, MSG_NOSIGNAL
));
1037 if (__glibc_unlikely (debug_level
> 0))
1038 dbg_log (_("provide access to FD %d, for %s"), db
->ro_fd
, key
);
1040 #endif /* SCM_RIGHTS */
1043 /* Handle new request. */
1045 handle_request (int fd
, request_header
*req
, void *key
, uid_t uid
, pid_t pid
)
1047 if (__builtin_expect (req
->version
, NSCD_VERSION
) != NSCD_VERSION
)
1049 if (debug_level
> 0)
1051 cannot handle old request version %d; current version is %d"),
1052 req
->version
, NSCD_VERSION
);
1056 /* Perform the SELinux check before we go on to the standard checks. */
1057 if (selinux_enabled
&& nscd_request_avc_has_perm (fd
, req
->type
) != 0)
1059 if (debug_level
> 0)
1062 char pbuf
[sizeof ("/proc//exe") + 3 * sizeof (long int)];
1069 snprintf (pbuf
, sizeof (pbuf
), "/proc/%ld/exe", (long int) pid
);
1070 ssize_t n
= readlink (pbuf
, buf
, sizeof (buf
) - 1);
1074 request from %ld not handled due to missing permission"), (long int) pid
);
1079 request from '%s' [%ld] not handled due to missing permission"),
1080 buf
, (long int) pid
);
1083 dbg_log (_("request not handled due to missing permission"));
1089 struct database_dyn
*db
= reqinfo
[req
->type
].db
;
1091 /* See whether we can service the request from the cache. */
1092 if (__builtin_expect (reqinfo
[req
->type
].data_request
, true))
1094 if (__builtin_expect (debug_level
, 0) > 0)
1096 if (req
->type
== GETHOSTBYADDR
|| req
->type
== GETHOSTBYADDRv6
)
1098 char buf
[INET6_ADDRSTRLEN
];
1100 dbg_log ("\t%s (%s)", serv2str
[req
->type
],
1101 inet_ntop (req
->type
== GETHOSTBYADDR
1102 ? AF_INET
: AF_INET6
,
1103 key
, buf
, sizeof (buf
)));
1106 dbg_log ("\t%s (%s)", serv2str
[req
->type
], (char *) key
);
1109 /* Is this service enabled? */
1110 if (__glibc_unlikely (!db
->enabled
))
1112 /* No, sent the prepared record. */
1113 if (TEMP_FAILURE_RETRY (send (fd
, db
->disabled_iov
->iov_base
,
1114 db
->disabled_iov
->iov_len
,
1116 != (ssize_t
) db
->disabled_iov
->iov_len
1117 && __builtin_expect (debug_level
, 0) > 0)
1119 /* We have problems sending the result. */
1121 dbg_log (_("cannot write result: %s"),
1122 strerror_r (errno
, buf
, sizeof (buf
)));
1128 /* Be sure we can read the data. */
1129 if (__glibc_unlikely (pthread_rwlock_tryrdlock (&db
->lock
) != 0))
1131 ++db
->head
->rdlockdelayed
;
1132 pthread_rwlock_rdlock (&db
->lock
);
1135 /* See whether we can handle it from the cache. */
1136 struct datahead
*cached
;
1137 cached
= (struct datahead
*) cache_search (req
->type
, key
, req
->key_len
,
1141 /* Hurray it's in the cache. */
1142 if (writeall (fd
, cached
->data
, cached
->recsize
) != cached
->recsize
1143 && __glibc_unlikely (debug_level
> 0))
1145 /* We have problems sending the result. */
1147 dbg_log (_("cannot write result: %s"),
1148 strerror_r (errno
, buf
, sizeof (buf
)));
1151 pthread_rwlock_unlock (&db
->lock
);
1156 pthread_rwlock_unlock (&db
->lock
);
1158 else if (__builtin_expect (debug_level
, 0) > 0)
1160 if (req
->type
== INVALIDATE
)
1161 dbg_log ("\t%s (%s)", serv2str
[req
->type
], (char *) key
);
1163 dbg_log ("\t%s", serv2str
[req
->type
]);
1166 /* Handle the request. */
1170 addpwbyname (db
, fd
, req
, key
, uid
);
1174 addpwbyuid (db
, fd
, req
, key
, uid
);
1178 addgrbyname (db
, fd
, req
, key
, uid
);
1182 addgrbygid (db
, fd
, req
, key
, uid
);
1186 addhstbyname (db
, fd
, req
, key
, uid
);
1189 case GETHOSTBYNAMEv6
:
1190 addhstbynamev6 (db
, fd
, req
, key
, uid
);
1194 addhstbyaddr (db
, fd
, req
, key
, uid
);
1197 case GETHOSTBYADDRv6
:
1198 addhstbyaddrv6 (db
, fd
, req
, key
, uid
);
1202 addhstai (db
, fd
, req
, key
, uid
);
1206 addinitgroups (db
, fd
, req
, key
, uid
);
1210 addservbyname (db
, fd
, req
, key
, uid
);
1214 addservbyport (db
, fd
, req
, key
, uid
);
1218 addgetnetgrent (db
, fd
, req
, key
, uid
);
1222 addinnetgr (db
, fd
, req
, key
, uid
);
1229 /* Get the callers credentials. */
1231 struct ucred caller
;
1232 socklen_t optlen
= sizeof (caller
);
1234 if (getsockopt (fd
, SOL_SOCKET
, SO_PEERCRED
, &caller
, &optlen
) < 0)
1238 dbg_log (_("error getting caller's id: %s"),
1239 strerror_r (errno
, buf
, sizeof (buf
)));
1245 /* Some systems have no SO_PEERCRED implementation. They don't
1246 care about security so we don't as well. */
1251 /* Accept shutdown, getstat and invalidate only from root. For
1252 the stat call also allow the user specified in the config file. */
1253 if (req
->type
== GETSTAT
)
1255 if (uid
== 0 || uid
== stat_uid
)
1256 send_stats (fd
, dbs
);
1260 if (req
->type
== INVALIDATE
)
1261 invalidate_cache (key
, fd
);
1263 termination_handler (0);
1273 send_ro_fd (reqinfo
[req
->type
].db
, key
, fd
);
1278 /* Ignore the command, it's nothing we know. */
1284 read_cmdline (size_t *size
)
1286 int fd
= open ("/proc/self/cmdline", O_RDONLY
);
1290 size_t limit
= 1024;
1291 char *buffer
= malloc (limit
);
1300 if (current
== limit
)
1303 if (2 * limit
< limit
1304 || (newptr
= realloc (buffer
, 2 * limit
)) == NULL
)
1315 ssize_t n
= TEMP_FAILURE_RETRY (read (fd
, buffer
+ current
,
1336 /* Restart the process. */
1340 /* First determine the parameters. We do not use the parameters
1341 passed to main because then nscd would use the system libc after
1342 restarting even if it was started by a non-system dynamic linker
1343 during glibc testing. */
1345 char *cmdline
= read_cmdline (&readlen
);
1346 if (cmdline
== NULL
)
1349 cannot open /proc/self/cmdline: %m; disabling paranoia mode"));
1354 /* Parse the command line. Worst case scenario: every two
1355 characters form one parameter (one character plus NUL). */
1356 char **argv
= alloca ((readlen
/ 2 + 1) * sizeof (argv
[0]));
1359 for (char *cp
= cmdline
; cp
< cmdline
+ readlen
;)
1362 cp
= (char *) rawmemchr (cp
, '\0') + 1;
1366 /* Second, change back to the old user if we changed it. */
1367 if (server_user
!= NULL
)
1369 if (setresuid (old_uid
, old_uid
, old_uid
) != 0)
1372 cannot change to old UID: %s; disabling paranoia mode"),
1380 if (setresgid (old_gid
, old_gid
, old_gid
) != 0)
1383 cannot change to old GID: %s; disabling paranoia mode"),
1386 ignore_value (setuid (server_uid
));
1393 /* Next change back to the old working directory. */
1394 if (chdir (oldcwd
) == -1)
1397 cannot change to old working directory: %s; disabling paranoia mode"),
1400 if (server_user
!= NULL
)
1402 ignore_value (setuid (server_uid
));
1403 ignore_value (setgid (server_gid
));
1410 /* Synchronize memory. */
1411 int32_t certainly
[lastdb
];
1412 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
1413 if (dbs
[cnt
].enabled
)
1415 /* Make sure nobody keeps using the database. */
1416 dbs
[cnt
].head
->timestamp
= 0;
1417 certainly
[cnt
] = dbs
[cnt
].head
->nscd_certainly_running
;
1418 dbs
[cnt
].head
->nscd_certainly_running
= 0;
1420 if (dbs
[cnt
].persistent
)
1422 msync (dbs
[cnt
].head
, dbs
[cnt
].memsize
, MS_ASYNC
);
1425 /* The preparations are done. */
1427 char pathbuf
[PATH_MAX
];
1431 /* Try to exec the real nscd program so the process name (as reported
1432 in /proc/PID/status) will be 'nscd', but fall back to /proc/self/exe
1433 if readlink or the exec with the result of the readlink call fails. */
1434 ssize_t n
= readlink ("/proc/self/exe", pathbuf
, sizeof (pathbuf
) - 1);
1438 execv (pathbuf
, argv
);
1440 execv ("/proc/self/exe", argv
);
1442 /* If we come here, we will never be able to re-exec. */
1443 dbg_log (_("re-exec failed: %s; disabling paranoia mode"),
1446 if (server_user
!= NULL
)
1448 ignore_value (setuid (server_uid
));
1449 ignore_value (setgid (server_gid
));
1451 if (chdir ("/") != 0)
1452 dbg_log (_("cannot change current working directory to \"/\": %s"),
1457 /* Reenable the databases. */
1458 time_t now
= time (NULL
);
1459 for (int cnt
= 0; cnt
< lastdb
; ++cnt
)
1460 if (dbs
[cnt
].enabled
)
1462 dbs
[cnt
].head
->timestamp
= now
;
1463 dbs
[cnt
].head
->nscd_certainly_running
= certainly
[cnt
];
1468 /* List of file descriptors. */
1472 struct fdlist
*next
;
1474 /* Memory allocated for the list. */
1475 static struct fdlist
*fdlist
;
1476 /* List of currently ready-to-read file descriptors. */
1477 static struct fdlist
*readylist
;
1479 /* Conditional variable and mutex to signal availability of entries in
1480 READYLIST. The condvar is initialized dynamically since we might
1481 use a different clock depending on availability. */
1482 static pthread_cond_t readylist_cond
= PTHREAD_COND_INITIALIZER
;
1483 static pthread_mutex_t readylist_lock
= PTHREAD_MUTEX_INITIALIZER
;
1485 /* The clock to use with the condvar. */
1486 static clockid_t timeout_clock
= CLOCK_REALTIME
;
1488 /* Number of threads ready to handle the READYLIST. */
1489 static unsigned long int nready
;
1492 /* Function for the clean-up threads. */
1494 __attribute__ ((__noreturn__
))
1495 nscd_run_prune (void *p
)
1497 const long int my_number
= (long int) p
;
1498 assert (dbs
[my_number
].enabled
);
1500 int dont_need_update
= setup_thread (&dbs
[my_number
]);
1502 time_t now
= time (NULL
);
1504 /* We are running. */
1505 dbs
[my_number
].head
->timestamp
= now
;
1507 struct timespec prune_ts
;
1508 if (__glibc_unlikely (clock_gettime (timeout_clock
, &prune_ts
) == -1))
1509 /* Should never happen. */
1512 /* Compute the initial timeout time. Prevent all the timers to go
1513 off at the same time by adding a db-based value. */
1514 prune_ts
.tv_sec
+= CACHE_PRUNE_INTERVAL
+ my_number
;
1515 dbs
[my_number
].wakeup_time
= now
+ CACHE_PRUNE_INTERVAL
+ my_number
;
1517 pthread_mutex_t
*prune_lock
= &dbs
[my_number
].prune_lock
;
1518 pthread_mutex_t
*prune_run_lock
= &dbs
[my_number
].prune_run_lock
;
1519 pthread_cond_t
*prune_cond
= &dbs
[my_number
].prune_cond
;
1521 pthread_mutex_lock (prune_lock
);
1524 /* Wait, but not forever. */
1526 if (! dbs
[my_number
].clear_cache
)
1527 e
= pthread_cond_timedwait (prune_cond
, prune_lock
, &prune_ts
);
1528 assert (__builtin_expect (e
== 0 || e
== ETIMEDOUT
, 1));
1532 if (e
== ETIMEDOUT
|| now
>= dbs
[my_number
].wakeup_time
1533 || dbs
[my_number
].clear_cache
)
1535 /* We will determine the new timout values based on the
1536 cache content. Should there be concurrent additions to
1537 the cache which are not accounted for in the cache
1538 pruning we want to know about it. Therefore set the
1539 timeout to the maximum. It will be descreased when adding
1540 new entries to the cache, if necessary. */
1541 dbs
[my_number
].wakeup_time
= MAX_TIMEOUT_VALUE
;
1543 /* Unconditionally reset the flag. */
1544 time_t prune_now
= dbs
[my_number
].clear_cache
? LONG_MAX
: now
;
1545 dbs
[my_number
].clear_cache
= 0;
1547 pthread_mutex_unlock (prune_lock
);
1549 /* We use a separate lock for running the prune function (instead
1550 of keeping prune_lock locked) because this enables concurrent
1551 invocations of cache_add which might modify the timeout value. */
1552 pthread_mutex_lock (prune_run_lock
);
1553 next_wait
= prune_cache (&dbs
[my_number
], prune_now
, -1);
1554 pthread_mutex_unlock (prune_run_lock
);
1556 next_wait
= MAX (next_wait
, CACHE_PRUNE_INTERVAL
);
1557 /* If clients cannot determine for sure whether nscd is running
1558 we need to wake up occasionally to update the timestamp.
1559 Wait 90% of the update period. */
1560 #define UPDATE_MAPPING_TIMEOUT (MAPPING_TIMEOUT * 9 / 10)
1561 if (__glibc_unlikely (! dont_need_update
))
1563 next_wait
= MIN (UPDATE_MAPPING_TIMEOUT
, next_wait
);
1564 dbs
[my_number
].head
->timestamp
= now
;
1567 pthread_mutex_lock (prune_lock
);
1569 /* Make it known when we will wake up again. */
1570 if (now
+ next_wait
< dbs
[my_number
].wakeup_time
)
1571 dbs
[my_number
].wakeup_time
= now
+ next_wait
;
1573 next_wait
= dbs
[my_number
].wakeup_time
- now
;
1576 /* The cache was just pruned. Do not do it again now. Just
1577 use the new timeout value. */
1578 next_wait
= dbs
[my_number
].wakeup_time
- now
;
1580 if (clock_gettime (timeout_clock
, &prune_ts
) == -1)
1581 /* Should never happen. */
1584 /* Compute next timeout time. */
1585 prune_ts
.tv_sec
+= next_wait
;
1590 /* This is the main loop. It is replicated in different threads but
1591 the use of the ready list makes sure only one thread handles an
1592 incoming connection. */
1594 __attribute__ ((__noreturn__
))
1595 nscd_run_worker (void *p
)
1599 /* Initial locking. */
1600 pthread_mutex_lock (&readylist_lock
);
1602 /* One more thread available. */
1607 while (readylist
== NULL
)
1608 pthread_cond_wait (&readylist_cond
, &readylist_lock
);
1610 struct fdlist
*it
= readylist
->next
;
1611 if (readylist
->next
== readylist
)
1612 /* Just one entry on the list. */
1615 readylist
->next
= it
->next
;
1617 /* Extract the information and mark the record ready to be used
1622 /* One more thread available. */
1625 /* We are done with the list. */
1626 pthread_mutex_unlock (&readylist_lock
);
1628 /* Now read the request. */
1630 if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd
, &req
, sizeof (req
)))
1631 != sizeof (req
), 0))
1633 /* We failed to read data. Note that this also might mean we
1634 failed because we would have blocked. */
1635 if (debug_level
> 0)
1636 dbg_log (_("short read while reading request: %s"),
1637 strerror_r (errno
, buf
, sizeof (buf
)));
1641 /* Check whether this is a valid request type. */
1642 if (req
.type
< GETPWBYNAME
|| req
.type
>= LASTREQ
)
1645 /* Some systems have no SO_PEERCRED implementation. They don't
1646 care about security so we don't as well. */
1651 if (__glibc_unlikely (debug_level
> 0))
1653 struct ucred caller
;
1654 socklen_t optlen
= sizeof (caller
);
1656 if (getsockopt (fd
, SOL_SOCKET
, SO_PEERCRED
, &caller
, &optlen
) == 0)
1660 const pid_t pid
= 0;
1663 /* It should not be possible to crash the nscd with a silly
1664 request (i.e., a terribly large key). We limit the size to 1kb. */
1665 if (__builtin_expect (req
.key_len
, 1) < 0
1666 || __builtin_expect (req
.key_len
, 1) > MAXKEYLEN
)
1668 if (debug_level
> 0)
1669 dbg_log (_("key length in request too long: %d"), req
.key_len
);
1674 char keybuf
[MAXKEYLEN
+ 1];
1676 if (__builtin_expect (TEMP_FAILURE_RETRY (read (fd
, keybuf
,
1680 /* Again, this can also mean we would have blocked. */
1681 if (debug_level
> 0)
1682 dbg_log (_("short read while reading request key: %s"),
1683 strerror_r (errno
, buf
, sizeof (buf
)));
1686 keybuf
[req
.key_len
] = '\0';
1688 if (__builtin_expect (debug_level
, 0) > 0)
1693 handle_request: request received (Version = %d) from PID %ld"),
1694 req
.version
, (long int) pid
);
1698 handle_request: request received (Version = %d)"), req
.version
);
1701 /* Phew, we got all the data, now process it. */
1702 handle_request (fd
, &req
, keybuf
, uid
, pid
);
1710 pthread_mutex_lock (&readylist_lock
);
1712 /* One more thread available. */
1719 static unsigned int nconns
;
1724 pthread_mutex_lock (&readylist_lock
);
1726 /* Find an empty entry in FDLIST. */
1728 for (inner
= 0; inner
< nconns
; ++inner
)
1729 if (fdlist
[inner
].next
== NULL
)
1731 assert (inner
< nconns
);
1733 fdlist
[inner
].fd
= fd
;
1735 if (readylist
== NULL
)
1736 readylist
= fdlist
[inner
].next
= &fdlist
[inner
];
1739 fdlist
[inner
].next
= readylist
->next
;
1740 readylist
= readylist
->next
= &fdlist
[inner
];
1743 bool do_signal
= true;
1744 if (__glibc_unlikely (nready
== 0))
1749 /* Try to start another thread to help out. */
1751 if (nthreads
< max_nthreads
1752 && pthread_create (&th
, &attr
, nscd_run_worker
,
1753 (void *) (long int) nthreads
) == 0)
1755 /* We got another thread. */
1757 /* The new thread might need a kick. */
1763 pthread_mutex_unlock (&readylist_lock
);
1765 /* Tell one of the worker threads there is work to do. */
1767 pthread_cond_signal (&readylist_cond
);
1771 /* Check whether restarting should happen. */
1773 restart_p (time_t now
)
1775 return (paranoia
&& readylist
== NULL
&& nready
== nthreads
1776 && now
>= restart_time
);
1780 /* Array for times a connection was accepted. */
1781 static time_t *starttime
;
1784 /* Inotify event for changed file. */
1787 struct inotify_event i
;
1789 # define PATH_MAX 1024
1791 char buf
[sizeof (struct inotify_event
) + PATH_MAX
];
1794 /* Returns 0 if the file is there otherwise -1. */
1796 check_file (struct traced_file
*finfo
)
1799 /* We could check mtime and if different re-add
1800 the watches, and invalidate the database, but we
1801 don't because we are called from inotify_check_files
1802 which should be doing that work. If sufficient inotify
1803 events were lost then the next pruning or invalidation
1804 will do the stat and mtime check. We don't do it here to
1805 keep the logic simple. */
1806 if (stat64 (finfo
->fname
, &st
) < 0)
1811 /* Process the inotify event in INEV. If the event matches any of the files
1812 registered with a database then mark that database as requiring its cache
1813 to be cleared. We indicate the cache needs clearing by setting
1814 TO_CLEAR[DBCNT] to true for the matching database. */
1816 inotify_check_files (bool *to_clear
, union __inev
*inev
)
1818 /* Check which of the files changed. */
1819 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
1821 struct traced_file
*finfo
= dbs
[dbcnt
].traced_files
;
1823 while (finfo
!= NULL
)
1825 /* The configuration file was moved or deleted.
1826 We stop watching it at that point, and reinitialize. */
1827 if (finfo
->inotify_descr
[TRACED_FILE
] == inev
->i
.wd
1828 && ((inev
->i
.mask
& IN_MOVE_SELF
)
1829 || (inev
->i
.mask
& IN_DELETE_SELF
)
1830 || (inev
->i
.mask
& IN_IGNORED
)))
1833 bool moved
= (inev
->i
.mask
& IN_MOVE_SELF
) != 0;
1835 if (check_file (finfo
) == 0)
1837 dbg_log (_("ignored inotify event for `%s` (file exists)"),
1842 dbg_log (_("monitored file `%s` was %s, removing watch"),
1843 finfo
->fname
, moved
? "moved" : "deleted");
1844 /* File was moved out, remove the watch. Watches are
1845 automatically removed when the file is deleted. */
1848 ret
= inotify_rm_watch (inotify_fd
, inev
->i
.wd
);
1850 dbg_log (_("failed to remove file watch `%s`: %s"),
1851 finfo
->fname
, strerror (errno
));
1853 finfo
->inotify_descr
[TRACED_FILE
] = -1;
1854 to_clear
[dbcnt
] = true;
1855 if (finfo
->call_res_init
)
1859 /* The configuration file was open for writing and has just closed.
1860 We reset the cache and reinitialize. */
1861 if (finfo
->inotify_descr
[TRACED_FILE
] == inev
->i
.wd
1862 && inev
->i
.mask
& IN_CLOSE_WRITE
)
1864 /* Mark cache as needing to be cleared and reinitialize. */
1865 dbg_log (_("monitored file `%s` was written to"), finfo
->fname
);
1866 to_clear
[dbcnt
] = true;
1867 if (finfo
->call_res_init
)
1871 /* The parent directory was moved or deleted. We trigger one last
1872 invalidation. At the next pruning or invalidation we may add
1873 this watch back if the file is present again. */
1874 if (finfo
->inotify_descr
[TRACED_DIR
] == inev
->i
.wd
1875 && ((inev
->i
.mask
& IN_DELETE_SELF
)
1876 || (inev
->i
.mask
& IN_MOVE_SELF
)
1877 || (inev
->i
.mask
& IN_IGNORED
)))
1879 bool moved
= (inev
->i
.mask
& IN_MOVE_SELF
) != 0;
1880 /* The directory watch may have already been removed
1881 but we don't know so we just remove it again and
1882 ignore the error. Then we remove the file watch.
1883 Note: watches are automatically removed for deleted
1886 inotify_rm_watch (inotify_fd
, inev
->i
.wd
);
1887 if (finfo
->inotify_descr
[TRACED_FILE
] != -1)
1889 dbg_log (_("monitored parent directory `%s` was %s, removing watch on `%s`"),
1890 finfo
->dname
, moved
? "moved" : "deleted", finfo
->fname
);
1891 if (inotify_rm_watch (inotify_fd
, finfo
->inotify_descr
[TRACED_FILE
]) < 0)
1892 dbg_log (_("failed to remove file watch `%s`: %s"),
1893 finfo
->dname
, strerror (errno
));
1895 finfo
->inotify_descr
[TRACED_FILE
] = -1;
1896 finfo
->inotify_descr
[TRACED_DIR
] = -1;
1897 to_clear
[dbcnt
] = true;
1898 if (finfo
->call_res_init
)
1900 /* Continue to the next entry since this might be the
1901 parent directory for multiple registered files and
1902 we want to remove watches for all registered files. */
1905 /* The parent directory had a create or moved to event. */
1906 if (finfo
->inotify_descr
[TRACED_DIR
] == inev
->i
.wd
1907 && ((inev
->i
.mask
& IN_MOVED_TO
)
1908 || (inev
->i
.mask
& IN_CREATE
))
1909 && strcmp (inev
->i
.name
, finfo
->sfname
) == 0)
1911 /* We detected a directory change. We look for the creation
1912 of the file we are tracking or the move of the same file
1913 into the directory. */
1915 dbg_log (_("monitored file `%s` was %s, adding watch"),
1917 inev
->i
.mask
& IN_CREATE
? "created" : "moved into place");
1918 /* File was moved in or created. Regenerate the watch. */
1919 if (finfo
->inotify_descr
[TRACED_FILE
] != -1)
1920 inotify_rm_watch (inotify_fd
,
1921 finfo
->inotify_descr
[TRACED_FILE
]);
1923 ret
= inotify_add_watch (inotify_fd
,
1927 dbg_log (_("failed to add file watch `%s`: %s"),
1928 finfo
->fname
, strerror (errno
));
1930 finfo
->inotify_descr
[TRACED_FILE
] = ret
;
1932 /* The file is new or moved so mark cache as needing to
1933 be cleared and reinitialize. */
1934 to_clear
[dbcnt
] = true;
1935 if (finfo
->call_res_init
)
1938 /* Done re-adding the watch. Don't return, we may still
1939 have other files in this same directory, same watch
1940 descriptor, and need to process them. */
1942 /* Other events are ignored, and we move on to the next file. */
1943 finfo
= finfo
->next
;
1948 /* If an entry in the array of booleans TO_CLEAR is TRUE then clear the cache
1949 for the associated database, otherwise do nothing. The TO_CLEAR array must
1950 have LASTDB entries. */
1952 clear_db_cache (bool *to_clear
)
1954 for (size_t dbcnt
= 0; dbcnt
< lastdb
; ++dbcnt
)
1955 if (to_clear
[dbcnt
])
1957 pthread_mutex_lock (&dbs
[dbcnt
].prune_lock
);
1958 dbs
[dbcnt
].clear_cache
= 1;
1959 pthread_mutex_unlock (&dbs
[dbcnt
].prune_lock
);
1960 pthread_cond_signal (&dbs
[dbcnt
].prune_cond
);
1965 handle_inotify_events (void)
1967 bool to_clear
[lastdb
] = { false, };
1970 /* Read all inotify events for files registered via
1971 register_traced_file(). */
1974 /* Potentially read multiple events into buf. */
1975 ssize_t nb
= TEMP_FAILURE_RETRY (read (inotify_fd
,
1978 if (nb
< (ssize_t
) sizeof (struct inotify_event
))
1980 /* Not even 1 event. */
1981 if (__glibc_unlikely (nb
== -1 && errno
!= EAGAIN
))
1983 /* Done reading events that are ready. */
1986 /* Process all events. The normal inotify interface delivers
1987 complete events on a read and never a partial event. */
1988 char *eptr
= &inev
.buf
[0];
1992 /* Check which of the files changed. */
1993 inotify_check_files (to_clear
, &inev
);
1994 count
= sizeof (struct inotify_event
) + inev
.i
.len
;
1997 if (nb
>= (ssize_t
) sizeof (struct inotify_event
))
1998 memcpy (&inev
, eptr
, nb
);
2004 /* Actually perform the cache clearing. */
2005 clear_db_cache (to_clear
);
2012 __attribute__ ((__noreturn__
))
2013 main_loop_poll (void)
2015 struct pollfd
*conns
= (struct pollfd
*) xmalloc (nconns
2016 * sizeof (conns
[0]));
2019 conns
[0].events
= POLLRDNORM
;
2021 size_t firstfree
= 1;
2024 if (inotify_fd
!= -1)
2026 conns
[1].fd
= inotify_fd
;
2027 conns
[1].events
= POLLRDNORM
;
2034 size_t idx_nl_status_fd
= 0;
2035 if (nl_status_fd
!= -1)
2037 idx_nl_status_fd
= nused
;
2038 conns
[nused
].fd
= nl_status_fd
;
2039 conns
[nused
].events
= POLLRDNORM
;
2047 /* Wait for any event. We wait at most a couple of seconds so
2048 that we can check whether we should close any of the accepted
2049 connections since we have not received a request. */
2050 #define MAX_ACCEPT_TIMEOUT 30
2051 #define MIN_ACCEPT_TIMEOUT 5
2052 #define MAIN_THREAD_TIMEOUT \
2053 (MAX_ACCEPT_TIMEOUT * 1000 \
2054 - ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * 1000 * nused) / (2 * nconns))
2056 int n
= poll (conns
, nused
, MAIN_THREAD_TIMEOUT
);
2058 time_t now
= time (NULL
);
2060 /* If there is a descriptor ready for reading or there is a new
2061 connection, process this now. */
2064 if (conns
[0].revents
!= 0)
2066 /* We have a new incoming connection. Accept the connection. */
2067 int fd
= TEMP_FAILURE_RETRY (accept4 (sock
, NULL
, NULL
,
2070 /* Use the descriptor if we have not reached the limit. */
2073 if (firstfree
< nconns
)
2075 conns
[firstfree
].fd
= fd
;
2076 conns
[firstfree
].events
= POLLRDNORM
;
2077 starttime
[firstfree
] = now
;
2078 if (firstfree
>= nused
)
2079 nused
= firstfree
+ 1;
2083 while (firstfree
< nused
&& conns
[firstfree
].fd
!= -1);
2086 /* We cannot use the connection so close it. */
2095 if (inotify_fd
!= -1 && conns
[1].fd
== inotify_fd
)
2097 if (conns
[1].revents
!= 0)
2100 ret
= handle_inotify_events ();
2103 /* Something went wrong when reading the inotify
2104 data. Better disable inotify. */
2105 dbg_log (_("disabled inotify-based monitoring after read error %d"), errno
);
2121 if (idx_nl_status_fd
!= 0 && conns
[idx_nl_status_fd
].revents
!= 0)
2124 /* Read all the data. We do not interpret it here. */
2125 while (TEMP_FAILURE_RETRY (read (nl_status_fd
, buf
,
2126 sizeof (buf
))) != -1)
2129 dbs
[hstdb
].head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
]
2130 = __bump_nl_timestamp ();
2134 for (size_t cnt
= first
; cnt
< nused
&& n
> 0; ++cnt
)
2135 if (conns
[cnt
].revents
!= 0)
2137 fd_ready (conns
[cnt
].fd
);
2139 /* Clean up the CONNS array. */
2141 if (cnt
< firstfree
)
2143 if (cnt
== nused
- 1)
2146 while (conns
[nused
- 1].fd
== -1);
2152 /* Now find entries which have timed out. */
2155 /* We make the timeout length depend on the number of file
2156 descriptors currently used. */
2157 #define ACCEPT_TIMEOUT \
2158 (MAX_ACCEPT_TIMEOUT \
2159 - ((MAX_ACCEPT_TIMEOUT - MIN_ACCEPT_TIMEOUT) * nused) / nconns)
2160 time_t laststart
= now
- ACCEPT_TIMEOUT
;
2162 for (size_t cnt
= nused
- 1; cnt
> 0; --cnt
)
2164 if (conns
[cnt
].fd
!= -1 && starttime
[cnt
] < laststart
)
2166 /* Remove the entry, it timed out. */
2167 (void) close (conns
[cnt
].fd
);
2170 if (cnt
< firstfree
)
2172 if (cnt
== nused
- 1)
2175 while (conns
[nused
- 1].fd
== -1);
2179 if (restart_p (now
))
2187 main_loop_epoll (int efd
)
2189 struct epoll_event ev
= { 0, };
2193 /* Add the socket. */
2194 ev
.events
= EPOLLRDNORM
;
2196 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, sock
, &ev
) == -1)
2197 /* We cannot use epoll. */
2200 # ifdef HAVE_INOTIFY
2201 if (inotify_fd
!= -1)
2203 ev
.events
= EPOLLRDNORM
;
2204 ev
.data
.fd
= inotify_fd
;
2205 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, inotify_fd
, &ev
) == -1)
2206 /* We cannot use epoll. */
2212 # ifdef HAVE_NETLINK
2213 if (nl_status_fd
!= -1)
2215 ev
.events
= EPOLLRDNORM
;
2216 ev
.data
.fd
= nl_status_fd
;
2217 if (epoll_ctl (efd
, EPOLL_CTL_ADD
, nl_status_fd
, &ev
) == -1)
2218 /* We cannot use epoll. */
2225 struct epoll_event revs
[100];
2226 # define nrevs (sizeof (revs) / sizeof (revs[0]))
2228 int n
= epoll_wait (efd
, revs
, nrevs
, MAIN_THREAD_TIMEOUT
);
2230 time_t now
= time (NULL
);
2232 for (int cnt
= 0; cnt
< n
; ++cnt
)
2233 if (revs
[cnt
].data
.fd
== sock
)
2235 /* A new connection. */
2236 int fd
= TEMP_FAILURE_RETRY (accept4 (sock
, NULL
, NULL
,
2239 /* Use the descriptor if we have not reached the limit. */
2242 /* Try to add the new descriptor. */
2245 || epoll_ctl (efd
, EPOLL_CTL_ADD
, fd
, &ev
) == -1)
2246 /* The descriptor is too large or something went
2247 wrong. Close the descriptor. */
2251 /* Remember when we accepted the connection. */
2252 starttime
[fd
] = now
;
2261 # ifdef HAVE_INOTIFY
2262 else if (revs
[cnt
].data
.fd
== inotify_fd
)
2265 ret
= handle_inotify_events ();
2268 /* Something went wrong when reading the inotify
2269 data. Better disable inotify. */
2270 dbg_log (_("disabled inotify-based monitoring after read error %d"), errno
);
2271 (void) epoll_ctl (efd
, EPOLL_CTL_DEL
, inotify_fd
, NULL
);
2278 # ifdef HAVE_NETLINK
2279 else if (revs
[cnt
].data
.fd
== nl_status_fd
)
2282 /* Read all the data. We do not interpret it here. */
2283 while (TEMP_FAILURE_RETRY (read (nl_status_fd
, buf
,
2284 sizeof (buf
))) != -1)
2287 dbs
[hstdb
].head
->extra_data
[NSCD_HST_IDX_CONF_TIMESTAMP
]
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
);