/*
* 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
*/
/*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <door.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <synch.h>
#include <time.h>
#include <unistd.h>
#include "labeld.h"
#ifndef DEBUG
#define perror(e)
#endif /* !DEBUG */
/*
* This is cloned from _nsc_trydoorcall used by the nscd client.
*
* Routine that actually performs the door call.
* Note that we cache a file descriptor. We do
* the following to prevent disasters:
*
* 1) Never use 0, 1 or 2; if we get this from the open
* we dup it upwards.
*
* 2) Set the close on exec flags so descriptor remains available
* to child processes.
*
* 3) Verify that the door is still the same one we had before
* by using door_info on the client side.
*
* Note that we never close the file descriptor if it isn't one
* we allocated; we check this with door info. The rather tricky
* logic is designed to be fast in the normal case (fd is already
* allocated and is ok) while handling the case where the application
* closed it underneath us or where the nscd dies or re-execs itself
* and we're a multi-threaded application. Note that we cannot protect
* the application if it closes the fd and it is multi-threaded.
*
* int __call_labeld(label_door_op **dptr, int *ndata, int *adata);
*
* *dptr IN: points to arg buffer OUT: points to results buffer
* *ndata IN: overall size of buffer OUT: overall size of buffer
* *adata IN: size of call data OUT: size of return data
*
* Note that *dptr may change if provided space as defined by *bufsize is
* inadequate. In this case the door call mmaps more space and places
* the answer there and sets dptr to contain a pointer to the space, which
* should be freed with munmap.
*
* Returns 0 if the door call reached the server, -1 if contact was not made.
*
*/
int
{
#ifdef DEBUG
#endif /* DEBUG */
/*
* the first time in we try and open and validate the door.
* the validations are that the door must have been
* created with the label service door cookie and
* that it has the same door ID. If any of these
* validations fail we refuse to use the door.
*/
(void) mutex_lock(&_door_lock);
if (doorfd == -1) {
int i;
(void) mutex_unlock(&_door_lock);
perror("server door open");
return (NOSERVER);
}
/*
* dup up the file descriptor if we have 0 - 2
*/
i = 0;
perror("couldn't dup");
while (i--)
doorfd = -1;
(void) mutex_unlock(&_door_lock);
return (NOSERVER);
}
}
while (i--)
/*
* mark this door descriptor as close on exec
*/
/*
* we should close doorfd because we just opened it
*/
perror("real door door_info");
doorfd = -1;
(void) mutex_unlock(&_door_lock);
return (NOSERVER);
}
perror("real door fstat");
return (NOSERVER);
}
#ifdef DEBUG
(void) printf("\t\t attributes = %x\n",
(void) printf("\t\t\t UNREF\n");
(void) printf("\t\t\t PRIVATE\n");
(void) printf("\t\t\t LOCAL\n");
(void) printf("\t\t\t REVOKED\n");
(void) printf("\t\t\t DESCRIPTOR\n");
(void) printf("\t\t\t RELEASE\n");
(void) printf("\t\t\t DELAY\n");
#endif /* DEBUG */
#ifdef DEBUG
(void) printf("real door revoked\n");
#endif /* DEBUG */
doorfd = -1;
(void) mutex_unlock(&_door_lock);
return (NOSERVER);
}
} else {
perror("my door door_info");
/*
* don't close it - someone else has clobbered fd
*/
doorfd = -1;
goto try_again;
}
perror("my door fstat");
goto try_again;
}
#ifdef DEBUG
(void) printf("\t\t\t UNREF\n");
(void) printf("\t\t\t PRIVATE\n");
(void) printf("\t\t\t LOCAL\n");
(void) printf("\t\t\t REVOKED\n");
(void) printf("\t\t\t DESCRIPTOR\n");
(void) printf("\t\t\t RELEASE\n");
(void) printf("\t\t\t DELAY\n");
#endif /* DEBUG */
#ifdef DEBUG
(void) printf("my door revoked\n");
#endif /* DEBUG */
goto try_again;
}
}
(void) mutex_unlock(&_door_lock);
/* adjust backoff */
}
#ifdef DEBUG
#endif /* DEBUG */
(void) mutex_lock(&_door_lock);
goto try_again;
}
perror("door call");
return (NOSERVER);
}
/*LINTED*/
#ifdef DEBUG
(void) printf("\tNo data returned, size = %lu, dptr = %p\n",
#endif /* DEBUG */
return (NOSERVER);
}
#ifdef DEBUG
(void) printf("call buf = %x, buf size = %d, call size = %d\n",
(void) printf("retn buf = %x, buf size = %d, retn size = %d\n",
#endif /* DEBUG */
} /* __call_labeld */