4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
32 #include <sys/types.h>
38 extern admin_t current_admin
;
40 #define CLEANUP_WAIT_TIME 60
42 typedef enum cleanup_type
{
47 typedef struct cleanup_op
{
52 typedef struct main_nscd_struct
{
53 pid_t pid
; /* main nscd pid */
54 thread_t tid
; /* main nscd tid */
55 int in_progress
; /* A main nscd thread is */
56 /* waiting for change or */
58 int is_waiting_cleanup
; /* A main nscd thread is */
59 /* waiting for another main */
60 /* nscd thread to be cleaned */
64 static chg_info_t chg
= { DEFAULTMUTEX
, DEFAULTCV
, 0, NULL
, NULL
, NULL
, 0 };
66 static main_nscd_t chg_main_nscd
= {0, 0, 0, 0};
67 static mutex_t chg_main_nscd_lock
= DEFAULTMUTEX
;
68 static cond_t chg_main_nscd_cv
= DEFAULTCV
;
71 * The cookie of the configuration and its mutex
73 static ldap_get_chg_cookie_t config_cookie
= {0, 0};
74 static mutex_t config_cookie_lock
= DEFAULTMUTEX
;
76 static void cleanup_thread_by_pid(pid_t pid
);
79 chg_config_cookie_get(void)
81 ldap_get_chg_cookie_t cookie
;
82 (void) mutex_lock(&config_cookie_lock
);
83 cookie
= config_cookie
;
84 (void) mutex_unlock(&config_cookie_lock
);
89 chg_config_cookie_increment_seq_num(void)
91 (void) mutex_lock(&config_cookie_lock
);
92 config_cookie
.seq_num
++;
93 (void) mutex_unlock(&config_cookie_lock
);
97 chg_config_cookie_set(ldap_get_chg_cookie_t
*cookie
)
99 (void) mutex_lock(&config_cookie_lock
);
100 config_cookie
.mgr_pid
= cookie
->mgr_pid
;
101 config_cookie
.seq_num
= cookie
->seq_num
;
102 (void) mutex_unlock(&config_cookie_lock
);
105 chg_cookie_equal(ldap_get_chg_cookie_t
*c1
, ldap_get_chg_cookie_t
*c2
)
107 if (c1
->mgr_pid
== c2
->mgr_pid
&& c1
->seq_num
== c2
->seq_num
)
113 * Create a node in the list and output the node. The caller can NOT free it.
116 waiting_list_add(chg_info_t
*info
, pid_t pid
, thread_t tid
,
117 waiting_list_t
**wlp
)
124 if ((wl
= (waiting_list_t
*)calloc(1, sizeof (waiting_list_t
)))
126 logit("waiting_list_add: No memory. pid %ld tid %d\n",
128 return (CHG_NO_MEMORY
);
134 if (info
->chg_w_first
== NULL
) {
135 info
->chg_w_first
= wl
;
136 info
->chg_w_last
= wl
;
138 info
->chg_w_last
->next
= wl
;
139 wl
->prev
= info
->chg_w_last
;
140 info
->chg_w_last
= wl
;
143 return (CHG_SUCCESS
);
147 * Find a node with matching tid in the list and remove it from the list.
150 waiting_list_delete(chg_info_t
*info
, thread_t tid
)
154 for (wl
= info
->chg_w_first
; wl
!= NULL
; wl
= wl
->next
) {
155 if (wl
->tid
== tid
) {
156 if (wl
->next
== NULL
) {
157 if (wl
->prev
== NULL
) {
158 info
->chg_w_first
= NULL
;
159 info
->chg_w_last
= NULL
;
161 wl
->prev
->next
= NULL
;
162 info
->chg_w_last
= wl
->prev
;
165 if (wl
->prev
== NULL
) {
166 wl
->next
->prev
= NULL
;
167 info
->chg_w_first
= wl
->next
;
169 wl
->prev
->next
= wl
->next
;
170 wl
->next
->prev
= wl
->prev
;
174 return (CHG_SUCCESS
);
177 return (CHG_NOT_FOUND_IN_WAITING_LIST
);
181 * Delete the thread from the waiting list and remove data when the list
185 waiting_list_cleanup(chg_info_t
*chg
, thread_t tid
)
189 rc
= waiting_list_delete(chg
, tid
);
191 if (rc
== CHG_SUCCESS
&& chg
->chg_w_first
== NULL
) {
193 chg
->chg_data
= NULL
;
199 * Set flag by pid so it can be cleaned up.
202 waiting_list_set_cleanup(chg_info_t
*info
, pid_t pid
)
206 for (wl
= info
->chg_w_first
; wl
!= NULL
; wl
= wl
->next
) {
207 if (wl
->pid
== pid
) {
215 * Return: 1 - door client is dead, 0 - door client is alive
218 door_client_dead(void)
223 if (door_ucred(&uc
) == -1 && errno
== EINVAL
) {
235 * This function handles GETSTATUSCHANGE call from main nscd.
236 * The call can be a START op or STOP op. A cookie is sent from main nscd too.
237 * The static global variable main_nscd keeps record of pid, tid and some flags.
238 * If the thread is door_return(), main_nscd.pid, main_nscd.tid are set to 0.
239 * When the call is START op, it checks if main_nscd.pid is 0. If it is, it
240 * proceeds to wait for the change notification. If it's not, which means
241 * another main nscd handling thread is still around. It sends broadcast to
242 * clean up that thread and wait until the cleanup is done then proceeds to
243 * wait for the change notification. If same main nscd sends START op
244 * repeatedly, it'll be rejected.
245 * It also checks the cookie from main nscd. If it's not the same as
246 * ldap_cachemgr's cookie, door returns config change.
247 * If the door call is STOP op, it creates a thread to clean up main nscd START
248 * thread so it won't be blocking.
249 * In waiting for the change notification phase, the thread is waken up by
250 * the notification threads or by the cleanup threads.
251 * If it's a notification, it copies data to the stack then door return.
252 * If it's a cleanup, door_client_dead() is called to verify it then
256 chg_get_statusChange(LineBuf
*info
, ldap_call_t
*in
, pid_t nscd_pid
)
258 int rc
= CHG_SUCCESS
, another_main_nscd_thread_alive
= 0;
260 thread_t this_tid
= thr_self();
261 waiting_list_t
*wl
= NULL
;
262 ldap_get_change_out_t
*cout
;
263 ldap_get_chg_cookie_t cookie
;
268 if (in
->ldap_u
.get_change
.op
== NS_STATUS_CHANGE_OP_START
) {
270 (void) mutex_lock(&chg_main_nscd_lock
);
271 if (chg_main_nscd
.pid
!= 0) {
272 if (nscd_pid
!= chg_main_nscd
.pid
) {
274 * This is the case that nscd doesn't shut down
275 * properly(e.g. core) and STOP op is not sent,
276 * the thread handling it is still around and
277 * not cleaned up yet.
278 * Test if the thread is still alive.
279 * If it is, clean it up.
280 * For thr_kill, if sig is 0, a validity check
281 * is done for the existence of the target
282 * thread; no signal is sent.
284 if (thr_kill(chg_main_nscd
.tid
, 0) == 0) {
285 another_main_nscd_thread_alive
= 1;
286 cleanup_thread_by_pid(
289 } else if (chg_main_nscd
.in_progress
||
290 chg_main_nscd
.is_waiting_cleanup
) {
292 * Same nscd pid can only send door call
293 * one at a time and wait for ldap_cachemgr to
294 * return change data. If it's the same pid
295 * again, it's an nscd error.
297 (void) mutex_unlock(&chg_main_nscd_lock
);
298 return (CHG_NSCD_REPEATED_CALL
);
302 * Wait for another thread to be cleaned up if it's alive.
303 * After that this cond var is waken up.
305 if (another_main_nscd_thread_alive
) {
306 while (chg_main_nscd
.in_progress
) {
307 chg_main_nscd
.is_waiting_cleanup
= 1;
308 (void) cond_wait(&chg_main_nscd_cv
,
309 &chg_main_nscd_lock
);
314 * Replace pid and tid and set the flag.
316 chg_main_nscd
.is_waiting_cleanup
= 0;
317 chg_main_nscd
.pid
= nscd_pid
;
318 chg_main_nscd
.tid
= this_tid
;
319 chg_main_nscd
.in_progress
= 1;
320 (void) mutex_unlock(&chg_main_nscd_lock
);
322 cookie
= chg_config_cookie_get();
324 if (!chg_cookie_equal(&cookie
, &in
->ldap_u
.get_change
.cookie
)) {
326 * different cookie, set new cookie and
327 * return door call right away
329 len
= sizeof (ldap_get_change_out_t
);
330 if ((cout
= calloc(1, len
)) == NULL
) {
333 cout
->type
= NS_STATUS_CHANGE_TYPE_CONFIG
;
334 cout
->cookie
= cookie
;
335 info
->str
= (char *)cout
;
340 (void) mutex_lock(&chg
.chg_lock
);
342 /* wait for the change notification */
343 rc
= waiting_list_add(&chg
, nscd_pid
, this_tid
, &wl
);
344 if (rc
== CHG_SUCCESS
) {
346 while (!chg
.chg_wakeup
) {
348 door_client_dead()) {
352 (void) cond_wait(&chg
.chg_cv
,
355 /* Check if door client is still alive again */
356 if (!return_now
&& !wl
->cleanup
&&
357 !door_client_dead()) {
358 /* copy data to buffer */
359 if ((info
->str
= malloc(
360 chg
.chg_data_size
)) == NULL
) {
363 (void) memcpy(info
->str
,
366 info
->len
= chg
.chg_data_size
;
369 waiting_list_cleanup(&chg
, this_tid
);
371 (void) mutex_unlock(&chg
.chg_lock
);
376 * Reset pid, tid and flag, send wakeup signal.
378 (void) mutex_lock(&chg_main_nscd_lock
);
379 chg_main_nscd
.pid
= 0;
380 chg_main_nscd
.tid
= 0;
381 chg_main_nscd
.in_progress
= 0;
382 if (chg_main_nscd
.is_waiting_cleanup
)
383 (void) cond_broadcast(&chg_main_nscd_cv
);
385 (void) mutex_unlock(&chg_main_nscd_lock
);
387 } else if (in
->ldap_u
.get_change
.op
== NS_STATUS_CHANGE_OP_STOP
) {
389 cleanup_thread_by_pid(nscd_pid
);
393 rc
= CHG_INVALID_PARAM
;
395 if (rc
== CHG_EXCEED_MAX_THREADS
)
396 cleanup_thread_by_pid(0);
402 * This function copies the header and data stream to the buffer
403 * then send broadcast to wake up the chg_get_statusChange() threads.
406 chg_notify_statusChange(char *str
)
408 ldap_get_change_out_t
*cout
= (ldap_get_change_out_t
*)str
;
410 cout
->cookie
= chg_config_cookie_get();
412 (void) mutex_lock(&chg
.chg_lock
);
413 if (chg
.chg_w_first
!= NULL
&& chg
.chg_wakeup
== 0) {
422 if (cout
->type
== NS_STATUS_CHANGE_TYPE_CONFIG
)
423 chg
.chg_data_size
= sizeof (ldap_get_change_out_t
);
425 /* NS_STATUS_CHANGE_TYPE_SERVER */
426 chg
.chg_data_size
= sizeof (ldap_get_change_out_t
) -
427 sizeof (int) + cout
->data_size
;
430 (void) cond_broadcast(&chg
.chg_cv
);
432 (void) mutex_unlock(&chg
.chg_lock
);
434 return (CHG_SUCCESS
);
438 * This is called when the configuration is refreshed.
439 * The new configuration is different from the current one, a notification
440 * is sent tochg_get_statusChange() threads.
443 chg_test_config_change(ns_config_t
*new, int *change_status
)
446 LineBuf new_cfg
, cur_cfg
;
447 ns_ldap_error_t
*errp
= NULL
;
448 ldap_config_out_t
*new_out
, *cur_out
;
449 ldap_get_change_out_t
*cout
;
451 (void) memset(&new_cfg
, 0, sizeof (LineBuf
));
452 (void) memset(&cur_cfg
, 0, sizeof (LineBuf
));
454 * Flatten the config data of the newly downloaded config and
455 * current default config and compare both.
457 if ((errp
= __ns_ldap_LoadDoorInfo(&new_cfg
, NULL
, new, 0)) != NULL
) {
458 __ns_ldap_freeError(&errp
);
459 /* error, assume the config is changed */
461 } else if ((errp
= __ns_ldap_LoadDoorInfo(&cur_cfg
, NULL
, NULL
, 0))
463 __ns_ldap_freeError(&errp
);
464 /* error, assume the config is changed */
468 new_out
= (ldap_config_out_t
*)new_cfg
.str
;
469 cur_out
= (ldap_config_out_t
*)cur_cfg
.str
;
470 if (strcmp(new_out
->config_str
, cur_out
->config_str
) != 0) {
472 if (current_admin
.debug_level
>= DBG_PROFILE_REFRESH
) {
473 logit("config changed.\n");
482 if ((cout
= calloc(1, sizeof (ldap_get_change_out_t
)))
484 logit("chg_test_config_change: No Memory\n");
487 * Replace the currentdefault config with the new
490 __s_api_init_config(new);
491 chg_config_cookie_increment_seq_num();
492 cout
->type
= NS_STATUS_CHANGE_TYPE_CONFIG
;
494 * cout->cookie is set by
495 * chg_notify_statusChange
497 (void) chg_notify_statusChange((char *)cout
);
500 __s_api_destroy_config(new);
503 *change_status
= changed
;
507 * Wake up chg_get_statusChange() threads to clean up the threads
508 * that main nscd doesn't exist on the other of door anymore or
509 * the thread is marked as cleanup.
512 cleanup_threads(chg_info_t
*chg
, pid_t pid
, cleanup_type_t type
)
514 (void) mutex_lock(&chg
->chg_lock
);
515 if (type
== CLEANUP_BY_PID
)
516 waiting_list_set_cleanup(chg
, pid
);
518 * wake up threads without setting chg.chg_wakeup.
519 * It's for cleanup purpose, not for notifying changes.
521 (void) cond_broadcast(&chg
->chg_cv
);
522 (void) mutex_unlock(&chg
->chg_lock
);
525 * If arg is NULL, it loops forever,
526 * else it calls cleanup_threads once and exits.
529 chg_cleanup_waiting_threads(void *arg
)
531 cleanup_op_t
*op
= (cleanup_op_t
*)arg
;
532 cleanup_type_t type
= 0;
534 int always
= 1, waiting
;
548 (void) sleep(CLEANUP_WAIT_TIME
);
549 cleanup_threads(&chg
, pid
, type
);
560 * The door server thead which has the door client pid will be marked
561 * as to be clean up. If pid is 0, no marking and just clean up all.
564 cleanup_thread_by_pid(pid_t pid
)
568 if ((op
= malloc(sizeof (cleanup_op_t
))) == NULL
)
572 /* clean up all if pid is 0 */
574 op
->type
= CLEANUP_ALL
;
576 op
->type
= CLEANUP_BY_PID
;
578 if (thr_create(NULL
, 0, chg_cleanup_waiting_threads
,
579 (void *)op
, THR_BOUND
|THR_DETACHED
, NULL
) != 0) {
581 logit("thr_create failed for cleanup_thread_by_pid(%ld)\n",
587 * Output a psinfo of an nscd process with process id pid
588 * Return: 0 - Can't find the process or it's not nscd
590 * Note: If info is NULL, returns 0 or 1 only and no output from info.
593 get_nscd_psinfo(pid_t pid
, psinfo_t
*info
)
596 char fname
[MAXPATHLEN
];
600 if (snprintf(fname
, MAXPATHLEN
, "/proc/%d/psinfo", pid
) > 0) {
601 if ((fd
= open(fname
, O_RDONLY
)) >= 0) {
602 ret
= read(fd
, &pinfo
, sizeof (psinfo_t
));
604 if ((ret
== sizeof (psinfo_t
)) &&
605 (strcmp(pinfo
.pr_fname
, "nscd") == 0)) {
615 * If the parent process is nscd and euid is 0, it's a peruser nscd.
618 is_peruser_nscd(pid_t pid
)
623 if (get_nscd_psinfo(pid
, &pinfo
)) {
624 ppid
= pinfo
.pr_ppid
;
625 if (get_nscd_psinfo(ppid
, &pinfo
) && pinfo
.pr_euid
== 0)
627 * get psinfo of parent forker nscd
637 * Check if the door client making door call is a nscd or peruser nscd and
638 * output door client's pid.
641 chg_is_called_from_nscd_or_peruser_nscd(char *dc_str
, pid_t
*pidp
)
648 if (door_ucred(&uc
) != 0) {
650 logit("door_ucred() call failed %s\n", strerror(rc
));
653 euid
= ucred_geteuid(uc
);
654 pid
= *pidp
= ucred_getpid(uc
);
656 if ((euid
== 0 && is_called_from_nscd(pid
)) ||
657 is_peruser_nscd(pid
)) {
658 if (current_admin
.debug_level
>= DBG_ALL
)
659 logit("ldap_cachemgr received %s call from pid %ld, "
660 "uid %u, euid %u\n", dc_str
, pid
,
661 ucred_getruid(uc
), euid
);
664 if (current_admin
.debug_level
>= DBG_CANT_FIND
)
665 logit("%s call failed(cred): caller pid %ld, uid %u, "
666 "euid %u\n", dc_str
, pid
,
667 ucred_getruid(uc
), euid
);