Lines Matching defs:cm
55 static int close_conn_mt_when_nouser(ns_conn_mt_t *cm);
59 static void close_conn_mt_by_procchg(ns_conn_mt_t *cm, int rc, char *errmsg);
91 #define NS_CONN_UNLOCK_AND_FREE(free, cm, cmg) \
93 (void) mutex_unlock(&(cm)->lock); \
95 cmg = free_conn_mt((cm), 1); \
904 ns_conn_mt_t *cm;
907 cm = (ns_conn_mt_t *)calloc(1, sizeof (*cm));
908 if (cm == NULL) {
914 cm->conn_server = NULL;
916 cm->conn_server = strdup(server);
917 if (cm->conn_server == NULL) {
920 free(cm);
925 cm->conn_cred = NULL;
927 cm->conn_cred = __ns_ldap_dupAuth(cred);
928 if (cm->conn_cred == NULL) {
931 if (cm->conn_server)
932 free(cm->conn_server);
933 free(cm);
948 (void) __ns_ldap_freeCred(&cm->conn_cred);
949 if (cm->conn_server)
950 free(cm->conn_server);
951 free(cm);
955 (void) mutex_init(&cm->lock, USYNC_THREAD, NULL);
956 cm->state = NS_CONN_MT_CONNECTING;
957 cm->tid = thr_self();
958 cm->pid = getpid();
959 cm->next = NULL;
960 cm->cu_head = NULL;
961 cm->cu_tail = NULL;
962 cm->conn = NULL;
963 cm->conn_mgmt = cmg;
965 return (cm);
974 free_conn_mt(ns_conn_mt_t *cm, int unlock_cmg)
978 if (cm == NULL)
980 if (cm->ns_error != NULL)
981 (void) __ns_ldap_freeError(&cm->ns_error);
982 if (cm->conn_server != NULL)
983 free(cm->conn_server);
984 (void) __ns_ldap_freeCred(&cm->conn_cred);
985 if (cm->conn != NULL) {
986 if (cm->conn->ld != NULL)
987 (void) ldap_unbind(cm->conn->ld);
988 __s_api_freeConnection(cm->conn);
990 cmg = cm->conn_mgmt;
991 (void) mutex_destroy(&cm->lock);
992 free(cm);
998 add_cu2cm(ns_conn_user_t *cu, ns_conn_mt_t *cm)
1001 if (cm->cu_head == NULL) {
1002 cm->cu_head = cu;
1003 cm->cu_tail = cu;
1005 cm->cu_tail->next = cu;
1006 cm->cu_tail = cu;
1008 cm->cu_cnt++;
1013 add_cm2cmg(ns_conn_mt_t *cm, ns_conn_mgmt_t *cmg)
1022 cmg->cm_head = cm;
1023 cmg->cm_tail = cm;
1025 if (cm->opened_for == NS_CONN_USER_WRITE) {
1026 cm->next = cmg->cm_head;
1027 cmg->cm_head = cm;
1029 cmg->cm_tail->next = cm;
1030 cmg->cm_tail = cm;
1038 del_cu4cm(ns_conn_user_t *cu, ns_conn_mt_t *cm)
1042 if (cu == NULL || cm->cu_head == NULL || cm->cu_cnt == 0)
1046 if (cm->cu_head == cm->cu_tail) {
1047 if (cu == cm->cu_head) {
1048 cm->cu_head = cm->cu_tail = NULL;
1049 cm->cu_cnt = 0;
1056 if (cu == cm->cu_head) {
1057 cm->cu_head = cu->next;
1058 cm->cu_cnt--;
1063 pu = cm->cu_head;
1064 for (u = cm->cu_head->next; u; u = u->next) {
1069 if (pu != cm->cu_tail) {
1072 cm->cu_tail = pu;
1073 cm->cu_cnt--;
1083 del_cm4cmg(ns_conn_mt_t *cm, ns_conn_mgmt_t *cmg)
1087 if (cm == NULL || cmg->cm_head == NULL || cmg->cm_cnt == 0)
1092 if (cm == cmg->cm_head) {
1095 cm->next = NULL;
1100 /* more than one and cm is the first one */
1101 if (cm == cmg->cm_head) {
1102 cmg->cm_head = cm->next;
1104 cm->next = NULL;
1110 if (cm == m)
1115 pm->next = cm->next;
1119 cm->next = NULL;
1132 ns_conn_mt_t *cm)
1134 Connection *cp = cm->conn;
1140 if (cm->state == NS_CONN_MT_CONNECTING) {
1141 if (cm->conn_server == NULL ||
1142 strcasecmp(server, cm->conn_server) != 0)
1154 if (cm->state == NS_CONN_MT_CONNECTING)
1155 cr = cm->conn_cred;
1171 wait_for_conn_mt(ns_conn_user_t *cu, ns_conn_mt_t *cm)
1175 add_cu2cm(cu, cm);
1176 cu->conn_mt = cm;
1178 (void) mutex_unlock(&cm->lock);
1181 * cm->conn_mgmt across the wait
1183 (void) mutex_unlock(&(cm->conn_mgmt)->lock);
1185 (void) mutex_lock(&cm->lock);
1187 if (cm->state == NS_CONN_MT_CONNECTING) {
1188 (void) conn_wait(cm, cu);
1190 /* cm->lock is locked again at this point */
1195 if (cm->state == NS_CONN_MT_CONNECTED)
1202 * Check and see if the input MT connection '*cm' should be closed.
1207 * to be closed), otherwise unlock mutex (*cm)->lock and return B_TRUE.
1208 * This function assumes conn_mgmt cmg and conn_mt *cm are locked.
1211 check_and_close_conn(ns_conn_mgmt_t *cmg, ns_conn_mt_t **cm,
1255 if (strcasecmp(cmg->pservers[j], (*cm)->conn->serverAddr) != 0)
1290 if ((*cm)->opened_for == NS_CONN_USER_WRITE ||
1291 (*cm)->referral == B_TRUE) {
1295 free_cm = close_conn_mt_when_nouser(*cm);
1302 /* cu has not been attached to cm yet, use NULL as cu pointer */
1303 free_cm = close_conn_mt(*cm, LDAP_SERVER_DOWN, &ep, NULL);
1309 (void) mutex_unlock(&(*cm)->lock);
1311 (void) free_conn_mt(*cm, 0);
1312 *cm = NULL;
1325 * conn_mt *cmt and conn_mgmt cm->conn_mgmt are assumed locked.
1326 * cm->lock is unlocked at exit if rc is B_FALSE.
1336 ns_conn_mt_t *cm = *cmt;
1337 ns_conn_mgmt_t *cmg = cm->conn_mgmt;
1339 if (cm->state != st || cm->close_when_nouser == B_TRUE ||
1340 cm->detached == B_TRUE || cm->pid != getpid() ||
1341 cm->referral != cu->referral) {
1342 (void) mutex_unlock(&cm->lock);
1357 if (cm->opened_for == NS_CONN_USER_WRITE &&
1358 cu->type != NS_CONN_USER_WRITE && cm->cu_cnt == 0 &&
1359 ((time(NULL) - cm->access_time) > 60)) {
1364 free_cm = close_conn_mt(cm, NS_LDAP_INTERNAL, NULL, NULL);
1365 (void) mutex_unlock(&cm->lock);
1367 (void) free_conn_mt(cm, 0);
1376 if (cm->opened_for == NS_CONN_USER_SEARCH ||
1377 cm->opened_for == NS_CONN_USER_GETENT)
1382 if (cm->opened_for == NS_CONN_USER_WRITE)
1392 is_server_cred_matched(server, cred, cm) == B_FALSE))
1404 if (cm->state == NS_CONN_MT_CONNECTED &&
1406 drop_conn = check_and_close_conn(cmg, &cm, cu);
1408 if (cm == NULL)
1415 if ((cm->state == NS_CONN_MT_CONNECTED &&
1416 cm->cu_max != NS_CONN_MT_USER_NO_MAX &&
1417 cm->cu_cnt >= cm->cu_max) ||
1418 (cm->state == NS_CONN_MT_CONNECTING &&
1419 cm->cu_max != NS_CONN_MT_USER_NO_MAX &&
1420 cm->waiter_cnt >= cm->cu_max - 1))
1425 (void) mutex_unlock(&cm->lock);
1599 ns_conn_mt_t *cm = cu->conn_mt;
1613 (void) mutex_lock(&cm->lock);
1614 cm->conn = con;
1615 cm->state = NS_CONN_MT_CONNECTED;
1616 cm->pid = getpid();
1617 cm->create_time = time(NULL);
1618 cm->access_time = cm->create_time;
1619 cm->opened_for = cu->type;
1620 add_cu2cm(cu, cm);
1621 cu->conn_mt = cm;
1624 cm->cu_max = NS_CONN_MT_USER_MAX;
1626 cm->cu_max = 1;
1629 (void) conn_signal(cm);
1631 (void) mutex_unlock(&cm->lock);
1647 ns_conn_mt_t *cm;
1652 cm = cu->conn_mt;
1653 if (cm == NULL)
1657 (void) mutex_lock(&cm->lock);
1658 del_cu4cm(cu, cm);
1668 if ((cm->close_when_nouser == B_TRUE ||
1669 cm->state != NS_CONN_MT_CONNECTED) && cm->cu_cnt == 0) {
1670 (void) mutex_unlock(&cm->lock);
1672 (void) mutex_lock(&cm->lock);
1673 del_cm4cmg(cm, cmg);
1675 NS_CONN_UNLOCK_AND_FREE(ns_conn_free, cm, cmg);
1677 if (cm->state == NS_CONN_MT_CONNECTED && cm->cu_cnt == 0 &&
1678 cm->conn != NULL && cm->conn->ld != NULL) {
1684 while (ldap_result(cm->conn->ld, LDAP_RES_ANY,
1690 (void) mutex_unlock(&cm->lock);
1696 err2cm(ns_conn_mt_t *cm, int rc, ns_ldap_error_t **errorp) {
1699 cm->ns_rc = rc;
1700 cm->ns_error = NULL;
1704 cm->ns_rc = NS_LDAP_MEMORY;
1706 cm->ns_error = ep;
1712 err_from_cm(ns_conn_user_t *cu, ns_conn_mt_t *cm) {
1715 cu->ns_rc = cm->ns_rc;
1719 if (cm->ns_rc != NS_LDAP_SUCCESS && cm->ns_error != NULL) {
1720 ep = __s_api_copy_error(cm->ns_error);
1750 * errorp : set to NULL, if none NULL cm, callers do not need to free it
1756 ns_conn_mt_t *cm;
1761 if ((cm = cu->conn_mt) == NULL)
1766 (void) mutex_lock(&cm->lock);
1767 if (cm->state != NS_CONN_MT_CONNECT_ERROR) {
1768 cm->state = NS_CONN_MT_CONNECT_ERROR;
1769 cm->ns_rc = rc;
1771 cm->ns_error = *errorp;
1775 (void) conn_signal(cm);
1779 err_from_cm(cu, cm);
1781 del_cu4cm(cu, cm);
1786 if (cm->cu_cnt == 0) {
1787 del_cm4cmg(cm, cmg);
1791 NS_CONN_UNLOCK_AND_FREE(free_cm, cm, cmg);
1841 * Assume cm not null and locked, assume conn_mgmt is also locked.
1842 * Return -1 if error, 1 if the cm should be freed, otherwise 0.
1845 close_conn_mt(ns_conn_mt_t *cm, int rc, ns_ldap_error_t **errorp,
1848 ns_conn_mgmt_t *cmg = cm->conn_mgmt;
1852 if ((cm->state != NS_CONN_MT_CONNECTED && cm->state !=
1858 if (cm == m)
1864 if (cm->state == NS_CONN_MT_CONNECTED) { /* first time in here */
1873 (void) __s_api_removeServer(cm->conn->serverAddr);
1875 cm->state = NS_CONN_MT_CLOSING;
1878 * it to the cm. If the caller calls on behalf of
1885 if ((cu != NULL && cm->cu_cnt > 1) ||
1886 (cu == NULL && cm->cu_cnt > 0)) {
1887 err2cm(cm, rc, errorp);
1889 (void) conn_signal(cm);
1893 if (cm->cu_head != NULL) {
1894 for (u = cm->cu_head; u; u = u->next) {
1903 if ((cu != NULL && cm->cu_cnt == 1) ||
1904 (cu == NULL && cm->cu_cnt == 0)) {
1905 del_cm4cmg(cm, cmg);
1906 cm->detached = B_TRUE;
1929 ns_conn_mt_t *cm;
1935 if (cu->state != NS_CONN_USER_CONNECTED || (cm = cu->conn_mt) == NULL)
1940 (void) mutex_lock(&cm->lock);
1943 free_cm = close_conn_mt(cm, rc, errorp, cu);
1945 (void) mutex_unlock(&cm->lock);
1953 err_from_cm(cu, cm);
1957 del_cu4cm(cu, cm);
1962 NS_CONN_UNLOCK_AND_FREE(free_cm, cm, cmg);
1972 close_conn_mt_by_procchg(ns_conn_mt_t *cm, int rc, char *errmsg)
1978 if (cm == NULL)
1980 cmg = cm->conn_mgmt;
1990 (void) mutex_lock(&cm->lock);
1993 free_cm = close_conn_mt(cm, LDAP_SERVER_DOWN, &ep, NULL);
1995 (void) mutex_unlock(&cm->lock);
2001 NS_CONN_UNLOCK_AND_FREE(free_cm, cm, cmg);
2012 close_conn_mt_when_nouser(ns_conn_mt_t *cm)
2016 if (cm->cu_cnt == 0) {
2017 del_cm4cmg(cm, cm->conn_mgmt);
2020 cm->close_when_nouser = B_TRUE;
2070 ns_conn_mt_t *cm;
2104 for (cm = cmg->cm_head; cm; cm = cm->next) {
2105 (void) mutex_lock(&cm->lock);
2107 if (cm->state == NS_CONN_MT_CONNECTED &&
2108 cm->conn != NULL &&
2109 strcasecmp(cm->conn->serverAddr, s) == 0) {
2110 (void) mutex_unlock(&cm->lock);
2114 (void) mutex_unlock(&cm->lock);
2116 if (cm != NULL) {
2119 close_conn_mt_by_procchg(cm, LDAP_SERVER_DOWN,
2122 * Process the next cm using server s.
2123 * Start from the head of the cm linked
2124 * list again, as the cm list may change
2191 for (cm = cmg->cm_head; cm; cm = cm->next) {
2192 (void) mutex_lock(&cm->lock);
2199 if (cm->state == NS_CONN_MT_CONNECTED &&
2200 cm->close_when_nouser == B_FALSE &&
2201 cm->conn != NULL && cm->opened_for !=
2203 cm->referral == B_FALSE) {
2209 * again, but cm is bound prior to
2215 cm->conn->serverAddr,
2225 * used by the cm (i.e., no match
2231 cm);
2233 cm, cmg);
2238 * check the entire cm
2241 * cm list.
2246 (void) mutex_unlock(&cm->lock);
2248 /* if no (more) cm using s, check next server */
2249 if (cm == NULL)
2263 ns_conn_mt_t *cm;
2275 for (cm = cmg->cm_head; cm; cm = cm->next) {
2276 (void) mutex_lock(&cm->lock);
2277 if (cm->next == NULL)
2280 free_cm = close_conn_mt(cm, LDAP_OTHER, &ep, NULL);
2281 (void) mutex_unlock(&cm->lock);
2283 (void) free_conn_mt(cm, 0);