10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3431Scarlsonj * Common Development and Distribution License (the "License").
6*3431Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stropts.h>
320Sstevel@tonic-gate #include <synch.h>
330Sstevel@tonic-gate #include <fcntl.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <dhcp_svc_private.h>
370Sstevel@tonic-gate #include <sys/time.h>
380Sstevel@tonic-gate #include <dhcpmsg.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include "dsvclockd.h"
410Sstevel@tonic-gate #include "datastore.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate static uint32_t ds_hash(const char *);
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Create a datastore named `ds_name' and a door which will service requests
470Sstevel@tonic-gate * for this datastore. When the door is called, callback `ds_callback'.
480Sstevel@tonic-gate * Returns the created datastore.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate dsvcd_datastore_t *
ds_create(const char * ds_name,dsvcd_svc_t * ds_callback)510Sstevel@tonic-gate ds_create(const char *ds_name, dsvcd_svc_t *ds_callback)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate char door_path[MAXPATHLEN];
540Sstevel@tonic-gate dsvcd_datastore_t *ds = NULL;
550Sstevel@tonic-gate int fd;
560Sstevel@tonic-gate unsigned int i;
570Sstevel@tonic-gate door_info_t info;
580Sstevel@tonic-gate
590Sstevel@tonic-gate dhcpmsg(MSG_VERBOSE, "managing locks for datastore `%s'", ds_name);
600Sstevel@tonic-gate
610Sstevel@tonic-gate ds = malloc(sizeof (dsvcd_datastore_t));
620Sstevel@tonic-gate if (ds == NULL) {
630Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot manage locks for datastore `%s'",
640Sstevel@tonic-gate ds_name);
650Sstevel@tonic-gate return (NULL);
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
680Sstevel@tonic-gate ds->ds_name = strdup(ds_name);
690Sstevel@tonic-gate if (ds->ds_name == NULL) {
700Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot manage locks for datastore `%s'",
710Sstevel@tonic-gate ds_name);
720Sstevel@tonic-gate free(ds);
730Sstevel@tonic-gate return (NULL);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate
760Sstevel@tonic-gate ds->ds_doorfd = door_create((void (*)())ds_callback, ds,
770Sstevel@tonic-gate DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
780Sstevel@tonic-gate if (ds->ds_doorfd == -1) {
790Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot create door for datastore `%s'",
800Sstevel@tonic-gate ds_name);
810Sstevel@tonic-gate free(ds->ds_name);
820Sstevel@tonic-gate free(ds);
830Sstevel@tonic-gate return (NULL);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate for (i = 0; i < DSVCD_DS_HASH_SIZE; i++) {
870Sstevel@tonic-gate ds->ds_hash[i].cl_head = NULL;
880Sstevel@tonic-gate (void) mutex_init(&ds->ds_hash[i].cl_lock, USYNC_THREAD, 0);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * Create the door name in the filesystem. First, check to see if
930Sstevel@tonic-gate * a door already exists at the specified pathname. If it does,
940Sstevel@tonic-gate * and the server process (no doubt another copy of us) is already
950Sstevel@tonic-gate * running, then fail. Otherwise, unlink the old door and fattach
960Sstevel@tonic-gate * a new one.
970Sstevel@tonic-gate */
980Sstevel@tonic-gate (void) snprintf(door_path, sizeof (door_path), DSVCD_DOOR_FMT, ds_name);
990Sstevel@tonic-gate
1000Sstevel@tonic-gate fd = open(door_path, O_RDWR);
1010Sstevel@tonic-gate if (fd != -1) {
1020Sstevel@tonic-gate if (door_info(fd, &info) == 0 && info.di_target != -1) {
103*3431Scarlsonj dhcpmsg(MSG_ERROR, "%s is in use by process %lu",
1040Sstevel@tonic-gate door_path, info.di_target);
1050Sstevel@tonic-gate (void) close(fd);
1060Sstevel@tonic-gate (void) close(ds->ds_doorfd);
1070Sstevel@tonic-gate free(ds->ds_name);
1080Sstevel@tonic-gate free(ds);
1090Sstevel@tonic-gate return (NULL);
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate (void) close(fd);
1120Sstevel@tonic-gate (void) unlink(door_path);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate fd = open(door_path, O_CREAT|O_EXCL|O_RDWR, 0644);
1160Sstevel@tonic-gate if (fd == -1) {
1170Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot create door rendezvous for datastore "
1180Sstevel@tonic-gate "`%s'", ds_name);
1190Sstevel@tonic-gate (void) close(ds->ds_doorfd);
1200Sstevel@tonic-gate free(ds->ds_name);
1210Sstevel@tonic-gate free(ds);
1220Sstevel@tonic-gate return (NULL);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate (void) close(fd);
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate * Attach the door onto the name
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate if (fattach(ds->ds_doorfd, door_path) == -1) {
1300Sstevel@tonic-gate dhcpmsg(MSG_ERR, "cannot fattach door rendezvous for datastore "
1310Sstevel@tonic-gate "`%s'", ds_name);
1320Sstevel@tonic-gate (void) close(ds->ds_doorfd);
1330Sstevel@tonic-gate free(ds->ds_name);
1340Sstevel@tonic-gate free(ds);
1350Sstevel@tonic-gate return (NULL);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate return (ds);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * Destroy a datastore `ds' and its associated containers, and remove
1430Sstevel@tonic-gate * its door from the filesystem.
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate void
ds_destroy(dsvcd_datastore_t * ds)1460Sstevel@tonic-gate ds_destroy(dsvcd_datastore_t *ds)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate unsigned int i;
1490Sstevel@tonic-gate char door_path[MAXPATHLEN];
1500Sstevel@tonic-gate dsvcd_container_t *cn, *cn_next;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate dhcpmsg(MSG_VERBOSE, "stopping lock management for datastore `%s'",
1530Sstevel@tonic-gate ds->ds_name);
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * Detach and revoke access to the door. The detach makes it so
1570Sstevel@tonic-gate * new callers who open the door will fail; the revoke makes it
1580Sstevel@tonic-gate * so that callers that already have a door descriptor will fail.
1590Sstevel@tonic-gate * We do this prior to calling cn_destroy() to make it easier for
1600Sstevel@tonic-gate * the container lockcount to drain.
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate (void) snprintf(door_path, MAXPATHLEN, DSVCD_DOOR_FMT, ds->ds_name);
1630Sstevel@tonic-gate (void) fdetach(door_path);
1640Sstevel@tonic-gate (void) unlink(door_path);
1650Sstevel@tonic-gate (void) door_revoke(ds->ds_doorfd);
1660Sstevel@tonic-gate (void) close(ds->ds_doorfd);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * Destroy all the underlying containers. We're single-threaded at
1700Sstevel@tonic-gate * this point, so don't worry about locks.
1710Sstevel@tonic-gate */
1720Sstevel@tonic-gate for (i = 0; i < DSVCD_DS_HASH_SIZE; i++) {
1730Sstevel@tonic-gate for (cn = ds->ds_hash[i].cl_head; cn != NULL; cn = cn_next) {
1740Sstevel@tonic-gate cn_next = cn->cn_next;
1750Sstevel@tonic-gate cn_destroy(cn);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate (void) mutex_destroy(&ds->ds_hash[i].cl_lock);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate free(ds->ds_name);
1810Sstevel@tonic-gate free(ds);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * Get a container with id `cn_id' from datastore `ds'; create the
1860Sstevel@tonic-gate * container if it does not exist. If `crosshost' is set and the container
1870Sstevel@tonic-gate * does not yet exist, then the container will synchronize across hosts. .
1880Sstevel@tonic-gate * If the container cannot be found or created, NULL is returned. When the
1890Sstevel@tonic-gate * calling thread is done with the container, ds_release_container() must
1900Sstevel@tonic-gate * be called.
1910Sstevel@tonic-gate */
1920Sstevel@tonic-gate dsvcd_container_t *
ds_get_container(dsvcd_datastore_t * ds,const char * cn_id,boolean_t crosshost)1930Sstevel@tonic-gate ds_get_container(dsvcd_datastore_t *ds, const char *cn_id, boolean_t crosshost)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate dsvcd_container_list_t *cn_list;
1960Sstevel@tonic-gate dsvcd_container_t *cn;
1970Sstevel@tonic-gate uint32_t idhash = ds_hash(cn_id);
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate cn_list = &ds->ds_hash[idhash % DSVCD_DS_HASH_SIZE];
2000Sstevel@tonic-gate (void) mutex_lock(&cn_list->cl_lock);
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate for (cn = cn_list->cl_head; cn != NULL; cn = cn->cn_next) {
2030Sstevel@tonic-gate if (idhash == cn->cn_idhash && strcmp(cn_id, cn->cn_id) == 0)
2040Sstevel@tonic-gate break;
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate if (cn == NULL) {
2080Sstevel@tonic-gate cn = cn_create(cn_id, crosshost);
2090Sstevel@tonic-gate if (cn != NULL) {
2100Sstevel@tonic-gate if (cn_list->cl_head != NULL)
2110Sstevel@tonic-gate cn_list->cl_head->cn_prev = cn;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate cn->cn_next = cn_list->cl_head;
2140Sstevel@tonic-gate cn->cn_prev = NULL;
2150Sstevel@tonic-gate cn_list->cl_head = cn;
2160Sstevel@tonic-gate cn->cn_idhash = idhash;
2170Sstevel@tonic-gate cn->cn_nout = 0;
2180Sstevel@tonic-gate cn->cn_lastrel = 0;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate if (cn != NULL)
2230Sstevel@tonic-gate cn->cn_nout++;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate (void) mutex_unlock(&cn_list->cl_lock);
2260Sstevel@tonic-gate return (cn);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate * Release a container `cn' belonging to datastore `ds'. Once a container
2310Sstevel@tonic-gate * has been released, it can no longer be used by the releasing thread.
2320Sstevel@tonic-gate * Used to track the number of active instances of a container.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate void
ds_release_container(dsvcd_datastore_t * ds,dsvcd_container_t * cn)2350Sstevel@tonic-gate ds_release_container(dsvcd_datastore_t *ds, dsvcd_container_t *cn)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate dsvcd_container_list_t *cn_list;
2380Sstevel@tonic-gate uint32_t idhash = ds_hash(cn->cn_id);
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate cn_list = &ds->ds_hash[idhash % DSVCD_DS_HASH_SIZE];
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate (void) mutex_lock(&cn_list->cl_lock);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate cn->cn_nout--;
2450Sstevel@tonic-gate cn->cn_lastrel = time(NULL);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate (void) mutex_unlock(&cn_list->cl_lock);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate * Destroy any containers in datastore `ds' that have not been accessed in
2520Sstevel@tonic-gate * the last `idle' seconds. Return the number of destroyed (reaped)
2530Sstevel@tonic-gate * containers.
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate unsigned int
ds_reap_containers(dsvcd_datastore_t * ds,unsigned int idle)2560Sstevel@tonic-gate ds_reap_containers(dsvcd_datastore_t *ds, unsigned int idle)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate dsvcd_container_list_t *cn_list;
2590Sstevel@tonic-gate dsvcd_container_t *cn, *cn_next;
2600Sstevel@tonic-gate unsigned int i, nreaped = 0;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate for (i = 0; i < DSVCD_DS_HASH_SIZE; i++) {
2630Sstevel@tonic-gate cn_list = &ds->ds_hash[i];
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate (void) mutex_lock(&cn_list->cl_lock);
2660Sstevel@tonic-gate for (cn = cn_list->cl_head; cn != NULL; cn = cn_next) {
2670Sstevel@tonic-gate cn_next = cn->cn_next;
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate * Since a container is not checked out across a
2710Sstevel@tonic-gate * lock operation, we must check if the lock is
2720Sstevel@tonic-gate * held as well as the number of instances checked
2730Sstevel@tonic-gate * out.
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate if (cn->cn_nout != 0 ||
2760Sstevel@tonic-gate cn_locktype(cn) != DSVCD_NOLOCK ||
2770Sstevel@tonic-gate cn->cn_lastrel + idle >= time(NULL))
2780Sstevel@tonic-gate continue;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if (cn == cn_list->cl_head)
2810Sstevel@tonic-gate cn_list->cl_head = cn->cn_next;
2820Sstevel@tonic-gate else
2830Sstevel@tonic-gate cn->cn_prev->cn_next = cn->cn_next;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if (cn->cn_next != NULL)
2860Sstevel@tonic-gate cn->cn_next->cn_prev = cn->cn_prev;
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate cn_destroy(cn);
2890Sstevel@tonic-gate nreaped++;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate (void) mutex_unlock(&cn_list->cl_lock);
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate return (nreaped);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate * Hash a container identified by `cn_id' into a 32-bit unsigned integer
2990Sstevel@tonic-gate * suitable for use as a key in a hash table.
3000Sstevel@tonic-gate */
3010Sstevel@tonic-gate static uint32_t
ds_hash(const char * cn_id)3020Sstevel@tonic-gate ds_hash(const char *cn_id)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate uint32_t result = 0;
3050Sstevel@tonic-gate unsigned int i;
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate for (i = 0; cn_id[i] != '\0'; i++)
3080Sstevel@tonic-gate result += cn_id[i] << i;
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate return (result);
3110Sstevel@tonic-gate }
312