dlmgmt_db.c revision 0dc974a9a2e66d676505db23524ebff105fb36a9
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * CDDL HEADER START
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * The contents of this file are subject to the terms of the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Common Development and Distribution License (the "License").
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * You may not use this file except in compliance with the License.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * or http://www.opensolaris.org/os/licensing.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * See the License for the specific language governing permissions
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * and limitations under the License.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * When distributing Covered Code, include this CDDL HEADER in each
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If applicable, add the following below this CDDL HEADER, with the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * fields enclosed by brackets "[]" replaced with your own identifying
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * information: Portions Copyright [yyyy] [name of copyright owner]
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * CDDL HEADER END
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Use is subject to license terms.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <assert.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <ctype.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <errno.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <fcntl.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <stdio.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <stdlib.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <strings.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <syslog.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <sys/stat.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <pthread.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <unistd.h>
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include "dlmgmt_impl.h"
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byztypedef enum dlmgmt_db_op {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz DLMGMT_DB_OP_WRITE,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz DLMGMT_DB_OP_DELETE,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz DLMGMT_DB_OP_READ
d62bc4badc1c1f1549c961cfb8b420e650e1272byz} dlmgmt_db_op_t;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byztypedef struct dlmgmt_db_req_s {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz struct dlmgmt_db_req_s *ls_next;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_op_t ls_op;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz datalink_id_t ls_linkid;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint32_t ls_flags; /* Either DLMGMT_ACTIVE or */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* DLMGMT_PERSIST, not both. */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz} dlmgmt_db_req_t;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * List of pending db updates (e.g., because of a read-only filesystem).
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic dlmgmt_db_req_t *dlmgmt_db_req_head = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic dlmgmt_db_req_t *dlmgmt_db_req_tail = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int dlmgmt_db_update(dlmgmt_db_op_t, datalink_id_t,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint32_t);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int dlmgmt_process_db_req(dlmgmt_db_req_t *);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int dlmgmt_process_db_onereq(dlmgmt_db_req_t *, boolean_t);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void *dlmgmt_db_update_thread(void *);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic boolean_t process_link_line(char *, dlmgmt_link_t **);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int process_db_write(dlmgmt_db_req_t *, FILE *, FILE *);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int process_db_read(dlmgmt_db_req_t *, FILE *, FILE *);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void generate_link_line(dlmgmt_link_t *, boolean_t, char *);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define BUFLEN(lim, ptr) (((lim) > (ptr)) ? ((lim) - (ptr)) : 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define MAXLINELEN 1024
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Translator functions to go from dladm_datatype_t to character strings.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Each function takes a pointer to a buffer, the size of the buffer,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the name of the attribute, and the value to be written. The functions
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * return the number of bytes written to the buffer. If the buffer is not big
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * enough to hold the string representing the value, then nothing is written
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * and 0 is returned.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byztypedef size_t write_func_t(char *, size_t, char *, void *);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Translator functions to read from a NULL terminated string buffer into
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * something of the given DLADM_TYPE_*. The functions each return the number
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * of bytes read from the string buffer. If there is an error reading data
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * from the buffer, then 0 is returned. It is the caller's responsibility
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * to free the data allocated by these functions.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byztypedef size_t read_func_t(char *, void **);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byztypedef struct translator_s {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz const char *type_name;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz write_func_t *write_func;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz read_func_t *read_func;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz} translator_t;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Translator functions, defined later but declared here so that
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the translator table can be defined.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic write_func_t write_str, write_boolean, write_uint64;
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic read_func_t read_str, read_boolean, read_int64;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Translator table, indexed by dladm_datatype_t.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic translator_t translators[] = {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz { "string", write_str, read_str },
d62bc4badc1c1f1549c961cfb8b420e650e1272byz { "boolean", write_boolean, read_boolean },
d62bc4badc1c1f1549c961cfb8b420e650e1272byz { "int", write_uint64, read_int64 }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz};
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t ntranslators = sizeof (translators) / sizeof (translator_t);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define LINK_PROPERTY_DELIMINATOR ";"
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define LINK_PROPERTY_TYPE_VALUE_SEP ","
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define BASE_PROPERTY_LENGTH(t, n) (strlen(translators[(t)].type_name) +\
d62bc4badc1c1f1549c961cfb8b420e650e1272byz strlen(LINK_PROPERTY_TYPE_VALUE_SEP) +\
d62bc4badc1c1f1549c961cfb8b420e650e1272byz strlen(LINK_PROPERTY_DELIMINATOR) +\
d62bc4badc1c1f1549c961cfb8b420e650e1272byz strlen((n)))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define GENERATE_PROPERTY_STRING(buf, length, conv, name, type, val) \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (snprintf((buf), (length), "%s=%s%s" conv "%s", (name), \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz translators[(type)].type_name, \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LINK_PROPERTY_TYPE_VALUE_SEP, (val), LINK_PROPERTY_DELIMINATOR))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Name of the cache file to keep the active <link name, linkid> mapping
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic char cachefile[MAXPATHLEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define DLMGMT_PERSISTENT_DB_PATH "/etc/dladm/datalink.conf"
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define DLMGMT_MAKE_FILE_DB_PATH(buffer, persistent) \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf((buffer), MAXPATHLEN, "%s", \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (persistent) ? DLMGMT_PERSISTENT_DB_PATH : cachefile);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzwrite_str(char *buffer, size_t buffer_length, char *name, void *value)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *ptr = value;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz size_t data_length = strnlen(ptr, buffer_length);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Strings are assumed to be NULL terminated. In order to fit in
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the buffer, the string's length must be less then buffer_length.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If the value is empty, there's no point in writing it, in fact,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * we shouldn't even see that case.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (data_length + BASE_PROPERTY_LENGTH(DLADM_TYPE_STR, name) ==
d62bc4badc1c1f1549c961cfb8b420e650e1272byz buffer_length || data_length == 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Since we know the string will fit in the buffer, snprintf will
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * always return less than buffer_length, so we can just return
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * whatever snprintf returns.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (GENERATE_PROPERTY_STRING(buffer, buffer_length, "%s",
d62bc4badc1c1f1549c961cfb8b420e650e1272byz name, DLADM_TYPE_STR, ptr));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzwrite_boolean(char *buffer, size_t buffer_length, char *name, void *value)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boolean_t *ptr = value;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Booleans are either zero or one, so we only need room for two
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * characters in the buffer.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (buffer_length <= 1 + BASE_PROPERTY_LENGTH(DLADM_TYPE_BOOLEAN, name))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (GENERATE_PROPERTY_STRING(buffer, buffer_length, "%d",
d62bc4badc1c1f1549c961cfb8b420e650e1272byz name, DLADM_TYPE_BOOLEAN, *ptr));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzwrite_uint64(char *buffer, size_t buffer_length, char *name, void *value)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t *ptr = value;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Limit checking for uint64_t is a little trickier.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (snprintf(NULL, 0, "%lld", *ptr) +
d62bc4badc1c1f1549c961cfb8b420e650e1272byz BASE_PROPERTY_LENGTH(DLADM_TYPE_UINT64, name) >= buffer_length)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (GENERATE_PROPERTY_STRING(buffer, buffer_length, "%lld",
d62bc4badc1c1f1549c961cfb8b420e650e1272byz name, DLADM_TYPE_UINT64, *ptr));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzread_str(char *buffer, void **value)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
024b0a258461f282a92b1b1283c3b8b083f9f33fseb char *ptr = calloc(MAXLINKATTRVALLEN, sizeof (char));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ssize_t len;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
024b0a258461f282a92b1b1283c3b8b083f9f33fseb if (ptr == NULL || (len = strlcpy(ptr, buffer, MAXLINKATTRVALLEN))
024b0a258461f282a92b1b1283c3b8b083f9f33fseb >= MAXLINKATTRVALLEN) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(ptr);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *(char **)value = ptr;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* Account for NULL terminator */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (len + 1);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzread_boolean(char *buffer, void **value)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boolean_t *ptr = calloc(1, sizeof (boolean_t));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ptr == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ptr = atoi(buffer);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *(boolean_t **)value = ptr;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (sizeof (boolean_t));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic size_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzread_int64(char *buffer, void **value)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int64_t *ptr = calloc(1, sizeof (int64_t));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ptr == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ptr = (int64_t)atoll(buffer);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *(int64_t **)value = ptr;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (sizeof (int64_t));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_db_update(dlmgmt_db_op_t op, datalink_id_t linkid, uint32_t flags)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_t *req;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * It is either a persistent request or an active request, not both.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert((flags == DLMGMT_PERSIST) || (flags == DLMGMT_ACTIVE));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((req = malloc(sizeof (dlmgmt_db_req_t))) == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (ENOMEM);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req->ls_next = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req->ls_op = op;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req->ls_linkid = linkid;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req->ls_flags = flags;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If the return error is EINPROGRESS, this request is handled
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * asynchronously; return success.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = dlmgmt_process_db_req(req);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err != EINPROGRESS)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(req);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz else
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define DLMGMT_DB_OP_STR(op) \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (((op) == DLMGMT_DB_OP_READ) ? "read" : \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (((op) == DLMGMT_DB_OP_WRITE) ? "write" : "delete"))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#define DLMGMT_DB_CONF_STR(flag) \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (((flag) == DLMGMT_ACTIVE) ? "active" : \
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (((flag) == DLMGMT_PERSIST) ? "persistent" : ""))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_process_db_req(dlmgmt_db_req_t *req)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz pthread_t tid;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boolean_t writeop;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If there are already pending "write" requests, queue this request in
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the pending list. Note that this function is called while the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * dlmgmt_rw_lock is held, so it is safe to access the global variables.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz writeop = (req->ls_op != DLMGMT_DB_OP_READ);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (writeop && (req->ls_flags == DLMGMT_PERSIST) &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (dlmgmt_db_req_head != NULL)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_tail->ls_next = req;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_tail = req;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (EINPROGRESS);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = dlmgmt_process_db_onereq(req, writeop);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err != EINPROGRESS && err != 0 &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (req->ls_flags != DLMGMT_ACTIVE || errno != ENOENT)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Log the error unless the request processing:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * - is successful;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * - is still in progress;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * - has failed with ENOENT because the active configuration
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * file is not created yet;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_log(LOG_WARNING, "dlmgmt_process_db_onereq() %s "
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "operation on %s configuration failed: %s",
d62bc4badc1c1f1549c961cfb8b420e650e1272byz DLMGMT_DB_OP_STR(req->ls_op),
d62bc4badc1c1f1549c961cfb8b420e650e1272byz DLMGMT_DB_CONF_STR(req->ls_flags), strerror(err));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err == EINPROGRESS) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(req->ls_flags == DLMGMT_PERSIST);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(writeop && dlmgmt_db_req_head == NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_tail = dlmgmt_db_req_head = req;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = pthread_create(&tid, NULL, dlmgmt_db_update_thread, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err == 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (EINPROGRESS);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_process_db_onereq(dlmgmt_db_req_t *req, boolean_t writeop)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz FILE *fp, *nfp = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char file[MAXPATHLEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char newfile[MAXPATHLEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int nfd;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz DLMGMT_MAKE_FILE_DB_PATH(file, (req->ls_flags == DLMGMT_PERSIST));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((fp = fopen(file, (writeop ? "r+" : "r"))) == NULL) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (writeop && errno == EROFS) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * This can happen at boot when the file system is
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * read-only. So add this request to the pending
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * request list and start a retry thread.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (EINPROGRESS);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (req->ls_flags == DLMGMT_ACTIVE && errno == ENOENT) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * It is fine if the file keeping active configuration
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * does not exist. This happens during a new reboot.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!writeop)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (ENOENT);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If this is an update request for the active
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * configuration, create the file.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((fp = fopen(file, "w")) == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (errno == EROFS ? EINPROGRESS : errno);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (errno);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (writeop) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(newfile, MAXPATHLEN, "%s.new", file);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) fclose(fp);
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((nfp = fdopen(nfd, "w")) == NULL) {
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) close(nfd);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) fclose(fp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) unlink(newfile);
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (writeop)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = process_db_write(req, fp, nfp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz else
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = process_db_read(req, fp, nfp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!writeop || err != 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto done;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (fflush(nfp) == EOF) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto done;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) fclose(fp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) fclose(nfp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (rename(newfile, file) < 0) {
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) unlink(newfile);
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdone:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (nfp != NULL) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) fclose(nfp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err != 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) unlink(newfile);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) fclose(fp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*ARGSUSED*/
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void *
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_db_update_thread(void *arg)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_t *req;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_table_lock(B_TRUE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(dlmgmt_db_req_head != NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz while ((req = dlmgmt_db_req_head) != NULL) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(req->ls_flags == DLMGMT_PERSIST);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = dlmgmt_process_db_onereq(req, B_TRUE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err == EINPROGRESS) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * The filesystem is still read only. Go to sleep and
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * try again.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_table_unlock();
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) sleep(5);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_table_lock(B_TRUE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * The filesystem is no longer read only. Continue processing
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * and remove the request from the pending list.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_head = req->ls_next;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (dlmgmt_db_req_tail == req) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(dlmgmt_db_req_head == NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_tail = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(req);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_table_unlock();
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int
d62bc4badc1c1f1549c961cfb8b420e650e1272byzparse_linkprops(char *buf, dlmgmt_link_t *linkp)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boolean_t found_type = B_FALSE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dladm_datatype_t type = DLADM_TYPE_STR;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int i, len;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *curr;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char attr_name[MAXLINKATTRLEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz size_t attr_buf_len = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz void *attr_buf = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz curr = buf;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz len = strlen(buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz attr_name[0] = '\0';
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (i = 0; i < len && err == 0; i++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char c = buf[i];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boolean_t match = (c == '=' ||
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (c == ',' && !found_type) || c == ';');
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Move to the next character if there is no match and
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * if we have not reached the last character.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!match && i != len - 1)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (match) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * NUL-terminate the string pointed to by 'curr'.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz buf[i] = '\0';
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (*curr == '\0')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto parse_fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (attr_name[0] != '\0' && found_type) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * We get here after we have processed the "<prop>="
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * pattern. The pattern we are now interested in is
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * "<val>;".
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (c == '=')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto parse_fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (strcmp(attr_name, "name") == 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) read_str(curr, &attr_buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(linkp->ll_link,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz MAXLINKNAMELEN, "%s", attr_buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (strcmp(attr_name, "class") == 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) read_int64(curr, &attr_buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp->ll_class =
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (datalink_class_t)*(int64_t *)attr_buf;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (strcmp(attr_name, "media") == 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) read_int64(curr, &attr_buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp->ll_media =
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (uint32_t)*(int64_t *)attr_buf;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz attr_buf_len = translators[type].read_func(curr,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz &attr_buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = linkattr_set(&(linkp->ll_head), attr_name,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz attr_buf, attr_buf_len, type);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(attr_buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz attr_name[0] = '\0';
d62bc4badc1c1f1549c961cfb8b420e650e1272byz found_type = B_FALSE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (attr_name[0] != '\0') {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Non-zero length attr_name and found_type of false
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * indicates that we have not found the type for this
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * attribute. The pattern now is "<type>,<val>;", we
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * want the <type> part of the pattern.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (type = 0; type < ntranslators; type++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (strcmp(curr,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz translators[type].type_name) == 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz found_type = B_TRUE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!found_type)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto parse_fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * A zero length attr_name indicates we are looking
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * at the beginning of a link attribute.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (c != '=')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto parse_fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(attr_name, MAXLINKATTRLEN, "%s", curr);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz curr = buf + i + 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzparse_fail:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (-1);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic boolean_t
d62bc4badc1c1f1549c961cfb8b420e650e1272byzprocess_link_line(char *buf, dlmgmt_link_t **linkpp)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_link_t *linkp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int i, len, llen;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *str, *lasts;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char tmpbuf[MAXLINELEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Use a copy of buf for parsing so that we can do whatever we want.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) strlcpy(tmpbuf, buf, MAXLINELEN);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Skip leading spaces, blank lines, and comments.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz len = strlen(tmpbuf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (i = 0; i < len; i++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!isspace(tmpbuf[i]))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (i == len || tmpbuf[i] == '#') {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *linkpp = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (B_TRUE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp = calloc(1, sizeof (dlmgmt_link_t));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (linkp == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz str = tmpbuf + i;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Find the link id and assign it to the link structure.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (strtok_r(str, " \n\t", &lasts) == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz llen = strlen(str);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp->ll_linkid = atoi(str);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz str += llen + 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (str >= tmpbuf + len)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Now find the list of link properties.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((str = strtok_r(str, " \n\t", &lasts)) == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (parse_linkprops(str, linkp) < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *linkpp = linkp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (B_TRUE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzfail:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz link_destroy(linkp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Delete corrupted line.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz buf[0] = '\0';
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (B_FALSE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int
d62bc4badc1c1f1549c961cfb8b420e650e1272byzprocess_db_write(dlmgmt_db_req_t *req, FILE *fp, FILE *nfp)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boolean_t done = B_FALSE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_link_t *linkp, *link_in_file, link;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char buf[MAXLINELEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (req->ls_op == DLMGMT_DB_OP_WRITE) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * find the link in the avl tree with the given linkid.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz link.ll_linkid = req->ls_linkid;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp = avl_find(&dlmgmt_id_avl, &link, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (linkp == NULL || (linkp->ll_flags & req->ls_flags) == 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * This link has already been changed. This could
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * happen if the request is pending because of
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * read-only file-system. If so, we are done.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz while (err == 0 && fgets(buf, sizeof (buf), fp) != NULL &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz process_link_line(buf, &link_in_file)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (link_in_file == NULL || done) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
0dc974a9a2e66d676505db23524ebff105fb36a9Cathy Zhou * this is a comment line or we are done updating the
0dc974a9a2e66d676505db23524ebff105fb36a9Cathy Zhou * link of the given link, write the rest of lines out.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (fputs(buf, nfp) == EOF)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = errno;
0dc974a9a2e66d676505db23524ebff105fb36a9Cathy Zhou if (link_in_file != NULL)
0dc974a9a2e66d676505db23524ebff105fb36a9Cathy Zhou link_destroy(link_in_file);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz switch (req->ls_op) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case DLMGMT_DB_OP_WRITE:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * For write operations, if the linkid of the link
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * read from the file does not match the id of what
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * req->ll_linkid points to, write out the buffer.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Otherwise, generate a new line. If we get to the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * end and have not seen what req->ll_linkid points
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * to, write it out then.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (linkp == NULL ||
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp->ll_linkid != link_in_file->ll_linkid) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (fputs(buf, nfp) == EOF)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz generate_link_line(linkp,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req->ls_flags == DLMGMT_PERSIST, buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (fputs(buf, nfp) == EOF)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz done = B_TRUE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case DLMGMT_DB_OP_DELETE:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Delete is simple. If buf does not represent the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * link we're deleting, write it out.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (req->ls_linkid != link_in_file->ll_linkid) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (fputs(buf, nfp) == EOF)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz done = B_TRUE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case DLMGMT_DB_OP_READ:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz default:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz link_destroy(link_in_file);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If we get to the end of the file and have not seen what
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * req->ll_linkid points to, write it out then.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (req->ls_op == DLMGMT_DB_OP_WRITE && !done) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz generate_link_line(linkp, req->ls_flags == DLMGMT_PERSIST, buf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz done = B_TRUE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (fputs(buf, nfp) == EOF)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!done)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = ENOENT;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/* ARGSUSED1 */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int
d62bc4badc1c1f1549c961cfb8b420e650e1272byzprocess_db_read(dlmgmt_db_req_t *req, FILE *fp, FILE *nfp)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz avl_index_t name_where, id_where;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_link_t *link_in_file;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_link_t *linkp1, *linkp2;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char buf[MAXLINELEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * This loop processes each line of the configuration file.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz while (fgets(buf, MAXLINELEN, fp) != NULL) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!process_link_line(buf, &link_in_file)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Skip the comment line.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (link_in_file == NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp1 = avl_find(&dlmgmt_name_avl, link_in_file, &name_where);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp2 = avl_find(&dlmgmt_id_avl, link_in_file, &id_where);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((linkp1 != NULL) || (linkp2 != NULL)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * If any of the following conditions are met, this is
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * a duplicate entry:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * 1. link2 (with the given name) and link2 (with the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * given id) are not the same link;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * 2. This is a persistent req and find the link with
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the given name and id. Note that persistent db
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * is read before the active one.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * 3. Found the link with the given name and id but
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the link is already active.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((linkp1 != linkp2) ||
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (req->ls_flags == DLMGMT_PERSIST) ||
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ((linkp1->ll_flags & DLMGMT_ACTIVE) != 0)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_log(LOG_WARNING, "Duplicate link "
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "entries in repository: link name %s "
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "link id %i", link_in_file->ll_link,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz link_in_file->ll_linkid);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp1->ll_flags |= DLMGMT_ACTIVE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz link_destroy(link_in_file);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz avl_insert(&dlmgmt_name_avl, link_in_file, name_where);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz avl_insert(&dlmgmt_id_avl, link_in_file, id_where);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_advance(link_in_file);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz link_in_file->ll_flags |= req->ls_flags;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Generate an entry in the link database.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Each entry has this format:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * <link id> <prop0>=<type>,<val>;...;<propn>=<type>,<val>;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void
d62bc4badc1c1f1549c961cfb8b420e650e1272byzgenerate_link_line(dlmgmt_link_t *linkp, boolean_t persist, char *buf)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char tmpbuf[MAXLINELEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *ptr;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *lim = tmpbuf + MAXLINELEN;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *name_to_write = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz datalink_id_t id_to_write;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_linkattr_t *cur_p = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t u64;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ptr = tmpbuf;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz id_to_write = linkp->ll_linkid;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ptr += snprintf(ptr, BUFLEN(lim, ptr), "%d\t", id_to_write);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz name_to_write = linkp->ll_link;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ptr += write_str(ptr, BUFLEN(lim, ptr), "name", name_to_write);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz u64 = linkp->ll_class;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ptr += write_uint64(ptr, BUFLEN(lim, ptr), "class", &u64);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz u64 = linkp->ll_media;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ptr += write_uint64(ptr, BUFLEN(lim, ptr), "media", &u64);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * The daemon does not keep any active link attribute. If this request
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * is for active configuration, we are done.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!persist)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto done;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (cur_p = linkp->ll_head; cur_p != NULL; cur_p = cur_p->lp_next) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz ptr += translators[cur_p->lp_type].write_func(ptr,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz BUFLEN(lim, ptr), cur_p->lp_name, cur_p->lp_val);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdone:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ptr > lim)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(buf, MAXLINELEN, "%s\n", tmpbuf);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzint
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_delete_db_entry(datalink_id_t linkid, uint32_t flags)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (dlmgmt_db_update(DLMGMT_DB_OP_DELETE, linkid, flags));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzint
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_write_db_entry(datalink_id_t linkid, uint32_t flags)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (flags & DLMGMT_PERSIST) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((err = dlmgmt_db_update(DLMGMT_DB_OP_WRITE,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkid, DLMGMT_PERSIST)) != 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (flags & DLMGMT_ACTIVE) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (((err = dlmgmt_db_update(DLMGMT_DB_OP_WRITE,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkid, DLMGMT_ACTIVE)) != 0) &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (flags & DLMGMT_PERSIST)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) dlmgmt_db_update(DLMGMT_DB_OP_DELETE,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkid, DLMGMT_PERSIST);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz/*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * Initialize the datalink <link name, linkid> mapping and the link's
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * attributes list based on the configuration file /etc/dladm/datalink.conf
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * and the active configuration cache file
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz * /etc/svc/volatile/dladm/datalink-management:default.cache.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * This function is called when the datalink-management service is started
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * during reboot, and when the dlmgmtd daemon is restarted.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzint
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdlmgmt_db_init()
d62bc4badc1c1f1549c961cfb8b420e650e1272byz{
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char filename[MAXPATHLEN];
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_db_req_t req;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int err;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_link_t *linkp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *fmri, *c;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * First derive the name of the cache file from the FMRI name. This
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * cache name is used to keep active datalink configuration.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (debug) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(cachefile, MAXPATHLEN, "%s/%s%s",
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz DLMGMT_TMPFS_DIR, progname, ".debug.cache");
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((fmri = getenv("SMF_FMRI")) == NULL) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_log(LOG_WARNING, "dlmgmtd is an smf(5) managed "
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "service and should not be run from the command "
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "line.");
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (EINVAL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * The FMRI name is in the form of
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * svc:/service/service:instance. We need to remove the
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * prefix "svc:/" and replace '/' with '-'. The cache file
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * name is in the form of "service:instance.cache".
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((c = strchr(fmri, '/')) != NULL)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz c++;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz else
d62bc4badc1c1f1549c961cfb8b420e650e1272byz c = fmri;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(filename, MAXPATHLEN, "%s.cache", c);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (c = filename; *c != '\0'; c++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (*c == '/')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *c = '-';
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) snprintf(cachefile, MAXPATHLEN, "%s/%s",
b9e076dcc05b713d74073c0d692dfbb0f6f2c594yz DLMGMT_TMPFS_DIR, filename);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_table_lock(B_TRUE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req.ls_next = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req.ls_op = DLMGMT_DB_OP_READ;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req.ls_linkid = DATALINK_INVALID_LINKID;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req.ls_flags = DLMGMT_PERSIST;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((err = dlmgmt_process_db_req(&req)) != 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto done;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz req.ls_flags = DLMGMT_ACTIVE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = dlmgmt_process_db_req(&req);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (err == ENOENT) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /*
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * The temporary datalink.conf does not exist. This is
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * the first boot. Mark all the physical links active.
d62bc4badc1c1f1549c961cfb8b420e650e1272byz */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (linkp = avl_first(&dlmgmt_id_avl); linkp != NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp = AVL_NEXT(&dlmgmt_id_avl, linkp)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (linkp->ll_class == DATALINK_CLASS_PHYS) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp->ll_flags |= DLMGMT_ACTIVE;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (void) dlmgmt_write_db_entry(
d62bc4badc1c1f1549c961cfb8b420e650e1272byz linkp->ll_linkid, DLMGMT_ACTIVE);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz err = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzdone:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dlmgmt_table_unlock();
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return (err);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}