ns_cache_door.c revision f3c070b2c81ee102c30722d6805ad034eb38803e
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/types.h>
2N/A#include <pwd.h>
2N/A#include <stdio.h>
2N/A#include <synch.h>
2N/A#include <sys/param.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include "ns_cache_door.h"
2N/A#include <door.h>
2N/A
2N/A#if defined(PIC) || defined(lint)
2N/A
2N/A/*
2N/A *
2N/A * Routine that actually performs the door call.
2N/A * Note that we cache a file descriptor. We do
2N/A * the following to prevent disasters:
2N/A *
2N/A * 1) Never use 0,1 or 2; if we get this from the open
2N/A * we dup it upwards.
2N/A *
2N/A * 2) Set the close on exec flags so descriptor remains available
2N/A * to child processes.
2N/A *
2N/A * 3) Verify that the door is still the same one we had before
2N/A * by using door_info on the client side.
2N/A *
2N/A * Note that we never close the file descriptor if it isn't one
2N/A * we allocated; we check this with door info. The rather tricky
2N/A * logic is designed to be fast in the normal case (fd is already
2N/A * allocated and is ok) while handling the case where the application
2N/A * closed it underneath us or where the nscd dies or re-execs itself
2N/A * and we're a multi-threaded application. Note that we cannot protect
2N/A * the application if it closes the fd and it is multi-threaded.
2N/A *
2N/A * int _cache_trydoorcall(void *dptr, int *bufsize, int *actualsize);
2N/A *
2N/A * *dptr IN: points to arg buffer OUT: points to results buffer
2N/A * *bufsize IN: overall size of buffer OUT: overall size of buffer
2N/A * *actualsize IN: size of call data OUT: size of return data
2N/A *
2N/A * Note that *dptr may change if provided space as defined by *bufsize is
2N/A * inadequate. In this case the door call mmaps more space and places
2N/A * the answer there and sets dptr to contain a pointer to the space, which
2N/A * should be freed with munmap.
2N/A *
2N/A * Returns 0 if the door call reached the server, -1 if contact was not made.
2N/A *
2N/A */
2N/A
2N/Aextern int errno;
2N/Astatic mutex_t _door_lock = DEFAULTMUTEX;
2N/Astatic int doorfd = -1;
2N/A
2N/Aint
2N/A__ns_ldap_trydoorcall(ldap_data_t **dptr, int *ndata, int *adata)
2N/A{
2N/A static door_info_t real_door;
2N/A door_info_t my_door;
2N/A door_arg_t param;
2N/A
2N/A /*
2N/A * the first time in we try and open and validate the door.
2N/A * the validations are that the door must have been
2N/A * created with the name service door cookie and
2N/A * that the file attached to the door is owned by root
2N/A * and readonly by user, group and other. If any of these
2N/A * validations fail we refuse to use the door.
2N/A */
2N/A
2N/A (void) mutex_lock(&_door_lock);
2N/A
2N/Atry_again:
2N/A
2N/A if (doorfd == -1) {
2N/A
2N/A int tbc[3];
2N/A int i;
2N/A if ((doorfd = open(LDAP_CACHE_DOOR, O_RDONLY, 0))
2N/A == -1) {
2N/A (void) mutex_unlock(&_door_lock);
2N/A return (NOSERVER);
2N/A }
2N/A
2N/A /*
2N/A * dup up the file descriptor if we have 0 - 2
2N/A * to avoid problems with shells stdin/out/err
2N/A */
2N/A i = 0;
2N/A
2N/A while (doorfd < 3) { /* we have a reserved fd */
2N/A tbc[i++] = doorfd;
2N/A if ((doorfd = dup(doorfd)) < 0) {
2N/A while (i--)
2N/A (void) close(tbc[i]);
2N/A doorfd = -1;
2N/A (void) mutex_unlock(&_door_lock);
2N/A return (NOSERVER);
2N/A }
2N/A }
2N/A
2N/A while (i--)
2N/A (void) close(tbc[i]);
2N/A
2N/A /*
2N/A * mark this door descriptor as close on exec
2N/A */
2N/A (void) fcntl(doorfd, F_SETFD, FD_CLOEXEC);
2N/A if (door_info(doorfd, &real_door) == -1 ||
2N/A (real_door.di_attributes & DOOR_REVOKED) ||
2N/A real_door.di_data != (uintptr_t)LDAP_CACHE_DOOR_COOKIE) {
2N/A /*
2N/A * we should close doorfd because we just opened it
2N/A */
2N/A (void) close(doorfd);
2N/A doorfd = -1;
2N/A (void) mutex_unlock(&_door_lock);
2N/A return (NOSERVER);
2N/A }
2N/A } else {
2N/A if (door_info(doorfd, &my_door) == -1 ||
2N/A my_door.di_data != (uintptr_t)LDAP_CACHE_DOOR_COOKIE ||
2N/A my_door.di_uniquifier != real_door.di_uniquifier) {
2N/A /*
2N/A * don't close it -
2N/A * someone else has clobbered fd
2N/A */
2N/A doorfd = -1;
2N/A goto try_again;
2N/A }
2N/A
2N/A if (my_door.di_attributes & DOOR_REVOKED) {
2N/A (void) close(doorfd);
2N/A doorfd = -1; /* try and restart connection */
2N/A goto try_again;
2N/A }
2N/A }
2N/A
2N/A (void) mutex_unlock(&_door_lock);
2N/A
2N/A param.rbuf = (char *)*dptr;
2N/A param.rsize = *ndata;
2N/A param.data_ptr = (char *)*dptr;
2N/A param.data_size = *adata;
2N/A param.desc_ptr = NULL;
2N/A param.desc_num = 0;
2N/A if (door_call(doorfd, &param) == -1) {
2N/A return (NOSERVER);
2N/A }
2N/A *adata = (int)param.data_size;
2N/A *ndata = (int)param.rsize;
2N/A *dptr = (ldap_data_t *)param.data_ptr;
2N/A if (*adata == 0 || *dptr == NULL) {
2N/A return (NOSERVER);
2N/A }
2N/A
2N/A return ((*dptr)->ldap_ret.ldap_return_code);
2N/A}
2N/A
2N/A#pragma fini(_doorfd_close)
2N/Astatic void
2N/A_doorfd_close()
2N/A{
2N/A (void) mutex_lock(&_door_lock);
2N/A if (doorfd != -1) {
2N/A (void) close(doorfd);
2N/A }
2N/A (void) mutex_unlock(&_door_lock);
2N/A}
2N/A
2N/A/*
2N/A * routine to check if server is already running
2N/A */
2N/A
2N/Aint
2N/A__ns_ldap_cache_ping()
2N/A{
2N/A ldap_data_t data;
2N/A ldap_data_t *dptr;
2N/A int ndata;
2N/A int adata;
2N/A
2N/A data.ldap_call.ldap_callnumber = NULLCALL;
2N/A ndata = sizeof (data);
2N/A adata = sizeof (data);
2N/A dptr = &data;
2N/A return (__ns_ldap_trydoorcall(&dptr, &ndata, &adata));
2N/A}
2N/A
2N/A#endif /* PIC */
2N/A