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 (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <door.h>
2N/A#include <errno.h>
2N/A#include <fcntl.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <synch.h>
2N/A#include <time.h>
2N/A#include <unistd.h>
2N/A
2N/A#include <sys/param.h>
2N/A#include <sys/stat.h>
2N/A#include <sys/types.h>
2N/A
2N/A#include "labeld.h"
2N/A
2N/A#ifndef DEBUG
2N/A#define perror(e)
2N/A#endif /* !DEBUG */
2N/A
2N/A/*
2N/A * This is cloned from _nsc_trydoorcall used by the nscd client.
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 __call_labeld(label_door_op **dptr, int *ndata, int *adata);
2N/A *
2N/A * *dptr IN: points to arg buffer OUT: points to results buffer
2N/A * *ndata IN: overall size of buffer OUT: overall size of buffer
2N/A * *adata 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/A
2N/Astatic mutex_t _door_lock = DEFAULTMUTEX;
2N/A
2N/Aint
2N/A__call_labeld(labeld_data_t **dptr, size_t *ndata, size_t *adata)
2N/A{
2N/A static int doorfd = -1;
2N/A static door_info_t real_door;
2N/A struct stat st;
2N/A door_info_t my_door;
2N/A door_arg_t param;
2N/A char door_name[MAXPATHLEN];
2N/A struct timespec ts;
2N/A int busy = 0; /* number of busy loops */
2N/A
2N/A#ifdef DEBUG
2N/A labeld_data_t *callptr = *dptr;
2N/A int buf_size = *ndata;
2N/A int return_size = *adata;
2N/A#endif /* DEBUG */
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 label service door cookie and
2N/A * that it has the same door ID. If any of these
2N/A * validations fail we refuse to use the door.
2N/A */
2N/A ts.tv_sec = 0; /* initialize nanosecond retry timer */
2N/A ts.tv_nsec = 100;
2N/A (void) mutex_lock(&_door_lock);
2N/A
2N/Atry_again:
2N/A if (doorfd == -1) {
2N/A int tbc[3];
2N/A int i;
2N/A
2N/A (void) snprintf(door_name, sizeof (door_name), "%s%s",
2N/A DOOR_PATH, DOOR_NAME);
2N/A if ((doorfd = open64(door_name, O_RDONLY | O_CLOEXEC, 0)) < 0) {
2N/A (void) mutex_unlock(&_door_lock);
2N/A perror("server door open");
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 while (doorfd < 3) { /* we have a reserved fd */
2N/A tbc[i++] = doorfd;
2N/A if ((doorfd = dup(doorfd)) < 0) {
2N/A perror("couldn't dup");
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 while (i--)
2N/A (void) close(tbc[i]);
2N/A
2N/A if (door_info(doorfd, &real_door) < 0) {
2N/A /*
2N/A * we should close doorfd because we just opened it
2N/A */
2N/A perror("real door door_info");
2N/A (void) close(doorfd);
2N/A doorfd = -1;
2N/A (void) mutex_unlock(&_door_lock);
2N/A return (NOSERVER);
2N/A }
2N/A if (fstat(doorfd, &st) < 0) {
2N/A perror("real door fstat");
2N/A return (NOSERVER);
2N/A }
2N/A#ifdef DEBUG
2N/A (void) printf("\treal door %s\n", door_name);
2N/A (void) printf("\t\tuid = %d, gid = %d, mode = %o\n", st.st_uid,
2N/A st.st_gid, st.st_mode);
2N/A (void) printf("\t\toutstanding requests = %d\n", st.st_nlink-1);
2N/A (void) printf("\t\t pid = %d\n", real_door.di_target);
2N/A (void) printf("\t\t procedure = %llx\n", real_door.di_proc);
2N/A (void) printf("\t\t cookie = %llx\n", real_door.di_data);
2N/A (void) printf("\t\t attributes = %x\n",
2N/A real_door.di_attributes);
2N/A if (real_door.di_attributes & DOOR_UNREF)
2N/A (void) printf("\t\t\t UNREF\n");
2N/A if (real_door.di_attributes & DOOR_PRIVATE)
2N/A (void) printf("\t\t\t PRIVATE\n");
2N/A if (real_door.di_attributes & DOOR_LOCAL)
2N/A (void) printf("\t\t\t LOCAL\n");
2N/A if (real_door.di_attributes & DOOR_REVOKED)
2N/A (void) printf("\t\t\t REVOKED\n");
2N/A if (real_door.di_attributes & DOOR_DESCRIPTOR)
2N/A (void) printf("\t\t\t DESCRIPTOR\n");
2N/A if (real_door.di_attributes & DOOR_RELEASE)
2N/A (void) printf("\t\t\t RELEASE\n");
2N/A if (real_door.di_attributes & DOOR_DELAY)
2N/A (void) printf("\t\t\t DELAY\n");
2N/A (void) printf("\t\t id = %llx\n", real_door.di_uniquifier);
2N/A#endif /* DEBUG */
2N/A if ((real_door.di_attributes & DOOR_REVOKED) ||
2N/A (real_door.di_data != (door_ptr_t)COOKIE)) {
2N/A#ifdef DEBUG
2N/A (void) printf("real door revoked\n");
2N/A#endif /* DEBUG */
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) < 0) ||
2N/A (my_door.di_data != (door_ptr_t)COOKIE) ||
2N/A (my_door.di_uniquifier != real_door.di_uniquifier)) {
2N/A perror("my door door_info");
2N/A /*
2N/A * don't close it - someone else has clobbered fd
2N/A */
2N/A doorfd = -1;
2N/A goto try_again;
2N/A }
2N/A if (fstat(doorfd, &st) < 0) {
2N/A perror("my door fstat");
2N/A goto try_again;
2N/A }
2N/A#ifdef DEBUG
2N/A (void) sprintf(door_name, "%s%s", DOOR_PATH, DOOR_NAME);
2N/A (void) printf("\tmy door %s\n", door_name);
2N/A (void) printf("\t\tuid = %d, gid = %d, mode = %o\n", st.st_uid,
2N/A st.st_gid, st.st_mode);
2N/A (void) printf("\t\toutstanding requests = %d\n", st.st_nlink-1);
2N/A (void) printf("\t\t pid = %d\n", my_door.di_target);
2N/A (void) printf("\t\t procedure = %llx\n", my_door.di_proc);
2N/A (void) printf("\t\t cookie = %llx\n", my_door.di_data);
2N/A (void) printf("\t\t attributes = %x\n", my_door.di_attributes);
2N/A if (my_door.di_attributes & DOOR_UNREF)
2N/A (void) printf("\t\t\t UNREF\n");
2N/A if (my_door.di_attributes & DOOR_PRIVATE)
2N/A (void) printf("\t\t\t PRIVATE\n");
2N/A if (my_door.di_attributes & DOOR_LOCAL)
2N/A (void) printf("\t\t\t LOCAL\n");
2N/A if (my_door.di_attributes & DOOR_REVOKED)
2N/A (void) printf("\t\t\t REVOKED\n");
2N/A if (my_door.di_attributes & DOOR_DESCRIPTOR)
2N/A (void) printf("\t\t\t DESCRIPTOR\n");
2N/A if (my_door.di_attributes & DOOR_RELEASE)
2N/A (void) printf("\t\t\t RELEASE\n");
2N/A if (my_door.di_attributes & DOOR_DELAY)
2N/A (void) printf("\t\t\t DELAY\n");
2N/A (void) printf("\t\t id = %llx\n", my_door.di_uniquifier);
2N/A#endif /* DEBUG */
2N/A if (my_door.di_attributes & DOOR_REVOKED) {
2N/A#ifdef DEBUG
2N/A (void) printf("my door revoked\n");
2N/A#endif /* DEBUG */
2N/A (void) close(doorfd); /* labeld exited .... */
2N/A doorfd = -1; /* try and restart connection */
2N/A goto try_again;
2N/A }
2N/A }
2N/A (void) mutex_unlock(&_door_lock);
2N/A
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 param.rbuf = (char *)*dptr;
2N/A param.rsize = *ndata;
2N/A
2N/A if (door_call(doorfd, &param) < 0) {
2N/A if (errno == EAGAIN && busy++ < 10) {
2N/A /* adjust backoff */
2N/A if ((ts.tv_nsec *= 10) >= NANOSEC) {
2N/A ts.tv_sec++;
2N/A ts.tv_nsec = 100;
2N/A }
2N/A (void) nanosleep(&ts, NULL);
2N/A#ifdef DEBUG
2N/A (void) printf("door_call failed EAGAIN # %d\n", busy);
2N/A#endif /* DEBUG */
2N/A (void) mutex_lock(&_door_lock);
2N/A goto try_again;
2N/A }
2N/A perror("door call");
2N/A return (NOSERVER);
2N/A }
2N/A
2N/A *adata = (int)param.data_size;
2N/A *ndata = (int)param.rsize;
2N/A /*LINTED*/
2N/A *dptr = (labeld_data_t *)param.data_ptr;
2N/A
2N/A if (*adata == 0 || *dptr == NULL) {
2N/A#ifdef DEBUG
2N/A (void) printf("\tNo data returned, size = %lu, dptr = %p\n",
2N/A (unsigned long)*adata, (void *)*dptr);
2N/A#endif /* DEBUG */
2N/A return (NOSERVER);
2N/A }
2N/A#ifdef DEBUG
2N/A (void) printf("call buf = %x, buf size = %d, call size = %d\n",
2N/A callptr, buf_size, return_size);
2N/A (void) printf("retn buf = %x, buf size = %d, retn size = %d\n",
2N/A *dptr, *ndata, *adata);
2N/A (void) printf("\treply status = %d\n", (*dptr)->param.aret.ret);
2N/A#endif /* DEBUG */
2N/A return ((*dptr)->param.aret.ret);
2N/A
2N/A} /* __call_labeld */