Merge commit '37e84ab74e939caf52150fc3352081786ecc0c29' into merges
[unleashed.git] / usr / src / cmd / ldapcachemgr / cachemgr_change.c
blob221a98f7f7591280461becbdb9ba30e26c81a12f
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <strings.h>
27 #include <stdlib.h>
28 #include <syslog.h>
29 #include <errno.h>
30 #include <libintl.h>
31 #include <door.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <procfs.h>
36 #include "cachemgr.h"
38 extern admin_t current_admin;
40 #define CLEANUP_WAIT_TIME 60
42 typedef enum cleanup_type {
43 CLEANUP_ALL = 1,
44 CLEANUP_BY_PID = 2
45 } cleanup_type_t;
47 typedef struct cleanup_op {
48 pid_t pid;
49 cleanup_type_t type;
50 } cleanup_op_t;
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 */
57 /* copying data */
58 int is_waiting_cleanup; /* A main nscd thread is */
59 /* waiting for another main */
60 /* nscd thread to be cleaned */
61 /* up */
62 } main_nscd_t;
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);
78 ldap_get_chg_cookie_t
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);
85 return (cookie);
88 static void
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);
96 void
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);
104 static boolean_t
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)
108 return (B_TRUE);
109 else
110 return (B_FALSE);
113 * Create a node in the list and output the node. The caller can NOT free it.
115 static int
116 waiting_list_add(chg_info_t *info, pid_t pid, thread_t tid,
117 waiting_list_t **wlp)
120 waiting_list_t *wl;
122 *wlp = NULL;
124 if ((wl = (waiting_list_t *)calloc(1, sizeof (waiting_list_t)))
125 == NULL) {
126 logit("waiting_list_add: No memory. pid %ld tid %d\n",
127 pid, tid);
128 return (CHG_NO_MEMORY);
131 wl->pid = pid;
132 wl->tid = tid;
134 if (info->chg_w_first == NULL) {
135 info->chg_w_first = wl;
136 info->chg_w_last = wl;
137 } else {
138 info->chg_w_last->next = wl;
139 wl->prev = info->chg_w_last;
140 info->chg_w_last = wl;
142 *wlp = wl;
143 return (CHG_SUCCESS);
147 * Find a node with matching tid in the list and remove it from the list.
149 static int
150 waiting_list_delete(chg_info_t *info, thread_t tid)
152 waiting_list_t *wl;
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;
160 } else {
161 wl->prev->next = NULL;
162 info->chg_w_last = wl->prev;
164 } else {
165 if (wl->prev == NULL) {
166 wl->next->prev = NULL;
167 info->chg_w_first = wl->next;
168 } else {
169 wl->prev->next = wl->next;
170 wl->next->prev = wl->prev;
173 free(wl);
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
182 * is empty.
184 static void
185 waiting_list_cleanup(chg_info_t *chg, thread_t tid)
187 int rc;
189 rc = waiting_list_delete(chg, tid);
191 if (rc == CHG_SUCCESS && chg->chg_w_first == NULL) {
192 free(chg->chg_data);
193 chg->chg_data = NULL;
194 chg->chg_wakeup = 0;
199 * Set flag by pid so it can be cleaned up.
201 static void
202 waiting_list_set_cleanup(chg_info_t *info, pid_t pid)
204 waiting_list_t *wl;
206 for (wl = info->chg_w_first; wl != NULL; wl = wl->next) {
207 if (wl->pid == pid) {
208 wl->cleanup = 1;
209 break;
215 * Return: 1 - door client is dead, 0 - door client is alive
217 static int
218 door_client_dead(void)
220 ucred_t *uc = NULL;
221 int rc;
223 if (door_ucred(&uc) == -1 && errno == EINVAL) {
224 rc = 1;
225 } else {
226 rc = 0;
228 if (uc)
229 ucred_free(uc);
231 return (rc);
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
253 * door return.
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;
259 int len, return_now;
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;
265 info->str = NULL;
266 info->len = 0;
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(
287 chg_main_nscd.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) {
331 rc = CHG_NO_MEMORY;
332 } else {
333 cout->type = NS_STATUS_CHANGE_TYPE_CONFIG;
334 cout->cookie = cookie;
335 info->str = (char *)cout;
336 info->len = len;
339 } else {
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) {
345 return_now = 0;
346 while (!chg.chg_wakeup) {
347 if (wl->cleanup ||
348 door_client_dead()) {
349 return_now = 1;
350 break;
352 (void) cond_wait(&chg.chg_cv,
353 &chg.chg_lock);
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) {
361 rc = CHG_NO_MEMORY;
362 } else {
363 (void) memcpy(info->str,
364 chg.chg_data,
365 chg.chg_data_size);
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);
390 rc = CHG_SUCCESS;
392 } else {
393 rc = CHG_INVALID_PARAM;
395 if (rc == CHG_EXCEED_MAX_THREADS)
396 cleanup_thread_by_pid(0);
398 return (rc);
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) {
415 if (chg.chg_data) {
416 free(chg.chg_data);
417 chg.chg_data = NULL;
420 chg.chg_data = str;
422 if (cout->type == NS_STATUS_CHANGE_TYPE_CONFIG)
423 chg.chg_data_size = sizeof (ldap_get_change_out_t);
424 else
425 /* NS_STATUS_CHANGE_TYPE_SERVER */
426 chg.chg_data_size = sizeof (ldap_get_change_out_t) -
427 sizeof (int) + cout->data_size;
429 chg.chg_wakeup = 1;
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.
442 void
443 chg_test_config_change(ns_config_t *new, int *change_status)
445 int changed = 0;
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 */
460 changed = 1;
461 } else if ((errp = __ns_ldap_LoadDoorInfo(&cur_cfg, NULL, NULL, 0))
462 != NULL) {
463 __ns_ldap_freeError(&errp);
464 /* error, assume the config is changed */
465 changed = 1;
467 if (changed == 0) {
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) {
471 changed = 1;
472 if (current_admin.debug_level >= DBG_PROFILE_REFRESH) {
473 logit("config changed.\n");
477 free(cur_cfg.str);
478 free(new_cfg.str);
480 if (changed) {
482 if ((cout = calloc(1, sizeof (ldap_get_change_out_t)))
483 == NULL) {
484 logit("chg_test_config_change: No Memory\n");
485 } else {
487 * Replace the currentdefault config with the new
488 * config
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);
499 } else {
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.
511 static void
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.
528 void *
529 chg_cleanup_waiting_threads(void *arg)
531 cleanup_op_t *op = (cleanup_op_t *)arg;
532 cleanup_type_t type = 0;
533 pid_t pid;
534 int always = 1, waiting;
536 if (op == NULL) {
537 waiting = 1;
538 type = CLEANUP_ALL;
539 pid = 0;
540 } else {
541 waiting = 0;
542 type = op->type;
543 pid = op->pid;
546 while (always) {
547 if (waiting)
548 (void) sleep(CLEANUP_WAIT_TIME);
549 cleanup_threads(&chg, pid, type);
550 if (!waiting)
551 break;
554 free(op);
556 thr_exit(NULL);
557 return (NULL);
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.
563 static void
564 cleanup_thread_by_pid(pid_t pid)
566 cleanup_op_t *op;
568 if ((op = malloc(sizeof (cleanup_op_t))) == NULL)
569 return;
571 op->pid = pid;
572 /* clean up all if pid is 0 */
573 if (pid == 0)
574 op->type = CLEANUP_ALL;
575 else
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) {
580 free(op);
581 logit("thr_create failed for cleanup_thread_by_pid(%ld)\n",
582 pid);
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
589 * 1 - psinfo found
590 * Note: If info is NULL, returns 0 or 1 only and no output from info.
592 static int
593 get_nscd_psinfo(pid_t pid, psinfo_t *info)
595 psinfo_t pinfo;
596 char fname[MAXPATHLEN];
597 ssize_t ret;
598 int fd;
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));
603 (void) close(fd);
604 if ((ret == sizeof (psinfo_t)) &&
605 (strcmp(pinfo.pr_fname, "nscd") == 0)) {
606 if (info)
607 *info = pinfo;
608 return (1);
612 return (0);
615 * If the parent process is nscd and euid is 0, it's a peruser nscd.
617 static int
618 is_peruser_nscd(pid_t pid)
620 pid_t ppid;
621 psinfo_t pinfo;
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
629 return (1);
630 else
631 return (0);
632 } else {
633 return (0);
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)
643 int rc;
644 uid_t euid;
645 pid_t pid;
646 ucred_t *uc = NULL;
648 if (door_ucred(&uc) != 0) {
649 rc = errno;
650 logit("door_ucred() call failed %s\n", strerror(rc));
651 return (0);
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);
662 rc = 1;
663 } else {
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);
669 rc = 0;
672 ucred_free(uc);
674 return (rc);