/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/*
* Utility routines
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <libintl.h>
#include <assert.h>
#include <ucontext.h>
#include <pthread.h>
#include "idmap_impl.h"
struct idmap_handle {
};
NULL, /* client */
B_TRUE, /* failed */
DEFAULTRWLOCK, /* lock */
};
static idmap_stat _idmap_clnt_connect(void);
static void _idmap_clnt_disconnect(void);
{
_UDT_SIZE_INCR) * sizeof (*tmplist);
return (IDMAP_ERR_MEMORY);
sizeof (*tmplist)), 0,
_UDT_SIZE_INCR * sizeof (*tmplist));
}
return (IDMAP_SUCCESS);
}
{
/* extend the request array */
return (IDMAP_ERR_MEMORY);
_GET_IDS_SIZE_INCR * sizeof (*t1));
/* extend the return list */
return (IDMAP_ERR_MEMORY);
_GET_IDS_SIZE_INCR * sizeof (*t2));
}
return (IDMAP_SUCCESS);
}
{
/* init the result */
if (*list) {
} else {
return (IDMAP_ERR_MEMORY);
}
}
TIMEOUT);
if (rc != IDMAP_SUCCESS) {
return (rc);
}
return (IDMAP_SUCCESS);
}
/*
* Convert the return values from an RPC request into an idmap return code.
* Set errno on error.
*/
static
{
/*
* We only deal with door_call(3C) errors here. We look at
* r_err.re_errno instead of r_err.re_status because we need
* to differentiate between RPC failures caused by bad door fd
* and others.
*/
if (clntstat == RPC_SUCCESS)
return (IDMAP_SUCCESS);
case ENOMEM:
return (IDMAP_ERR_MEMORY);
case EBADF:
return (IDMAP_ERR_RPC_HANDLE);
default:
return (IDMAP_ERR_RPC);
}
}
/*
* Management of the connection to idmapd.
*
* The intent is that connections to idmapd are automatically maintained,
* reconnecting if necessary. No attempt is made to retry connnection
* attempts; a failure to connect yields an immediate error return.
*
* State of the connection is maintained through the "client" and "failed"
* elements of the handle structure:
*
* client failed
* NULL true Failed on a previous request and was not recovered.
* NULL false Should never happen.
* nonNULL true Structure exists, but an error has occurred. Waiting
* for a chance to attempt to reconnect.
* nonNULL false Connection is good.
*
* will establish the initial connection.
*
* Concurrency is managed through the rw lock "lock". Only the writer is
* allowed to connect or disconnect, and thus only the writer can set
* "failed" to "false". Readers are allowed to use the "client" pointer,
* and to set "failed" to "true", indicating that they have encountered a
* failure. The "client" pointer is only valid while one holds a reader
* lock. Once "failed" has been set to "true", all requests (including
* the retry of the failing request) will attempt to gain the writer lock.
* When they succeed, indicating that there are no requests in flight and
* thus no outstanding references to the CLIENT structure, they check
* again to see if the connection is still failed (since another thread
* might have fixed it), and then if it is still failed they disconnect
* and reconnect.
*/
/*
* Make an RPC call. Automatically reconnect if the connection to idmapd
* fails. Convert RPC results to idmap return codes.
*/
{
for (;;) {
if (idmap_handle.failed) {
/* No connection. Bid to see if we should fix it. */
/* Somebody else might fix it here. */
/*
* At this point, everybody else is asleep waiting
* for us. Check to see if somebody else has already
* fixed the problem.
*/
if (idmap_handle.failed) {
/* It's our job to fix. */
rc = _idmap_clnt_connect();
if (rc != IDMAP_SUCCESS) {
/* We couldn't fix it. */
break;
}
/* We fixed it. */
}
/* It's fixed now. */
/*
* Starting here, somebody might declare it failed
* again.
*/
continue;
}
if (rc == IDMAP_ERR_RPC_HANDLE) {
/* Failed. Needs to be reconnected. */
continue;
}
/* Success or unrecoverable failure. */
break;
}
return (rc);
}
/*
* Connect to idmapd.
* Must be single-threaded through rw_wrlock(&idmap_handle.lock).
*/
static
_idmap_clnt_connect(void)
{
/*
* clnt_door_call() alloca()s sendsz bytes (twice too, once for
* the call args buffer and once for the call result buffer), so
* we want to pick a sendsz that will be large enough, but not
* too large.
*/
if (stack_getbounds(&st) == 0) {
/*
* Estimate how much stack space is left;
* st.ss_sp is the top of stack.
*/
/* stack grows up */
else
/* stack grows down */
if (sendsz <= MIN_STACK_NEEDS) {
sendsz = 0; /* RPC call may fail */
} else {
/* Leave 64Kb (just a guess) for our needs */
/* Divide the stack space left by two */
/* Limit sendsz to 256KB */
if (sendsz > IDMAP_MAX_DOOR_RPC)
}
}
return (IDMAP_ERR_RPC);
return (IDMAP_SUCCESS);
}
/*
* Disconnect from idmapd, if we're connected.
*/
static
void
_idmap_clnt_disconnect(void)
{
}
}