fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* The following are used by update_fabric_wwn_list() */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define COPY_EXT ".cpy." /* Extn used in naming backup file */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define TMP_EXT ".tmp." /* Extn used in naming temp file */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"# fabric_WWN_map\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"# The physical ap_id list of configured fabric devices.\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"# Do NOT edit this file by hand -- refer to the cfgadm_fp(1M)\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"# man page and use cfgadm(1m) instead.\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function searches for "srch_str" (of length "slen") in "buf" (of length
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "buflen"). If it is not found, "write_offset" has the offset in "buf" where
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "srch_str" would have to be added in "buf". If "srch_str" is found in "buf",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "write_offset" has its offset in "buf"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ARGUMENTS :
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * buf - buffer to search in
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * buflen - length of buffer
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * srch_str - string to search
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * slen - length of srch_str
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * write_offset - Set in function on exit
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - It is the offset in buf where srch_str is or should be
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * bytes_left - Set in function on exit
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - It is the # of bytes left beyond write_offset in buf
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - This function assumes "buf" is sorted in ascending order
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - If 'buflen' is > 0, it assumes it has a header on top and skips it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - "srch_str" has '\n' at the end, but when update_fabric_wwn_list() calls
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * this function, 'slen' does not include the last `\n'
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * RETURN VALUES :
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Zero - "srch_str" found in "buf"... "write_offset" has offset in "buf"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * > 0 - "srch_str" NOT found in "buf" ... "write_offset" has offset in "buf"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * where "srch_str" can fit in.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "buf" had contents > "srch_str"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * < 0 - "srch_str" NOT found in "buf" ... "write_offset" has offset in "buf"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * where "srch_str" can fit in.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "buf" had contents < "srch_str"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesearch_line(char *buf, int buflen, char *srch_str, int slen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-2); /* Arbitrary -ve val. srch_str not found */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (srch_str == NULL || *srch_str == NULL || slen <= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0); /* This says srch_str was found */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* skip header */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((retval = strncmp(sol, srch_str, slen)) >= 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* strncmp will pass if srch_str is a substring */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* retval < 0 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((cur_pos = strchr(sol, (int)'\n')) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Get the length of this line */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cur_pos = '\0'; /* kludge to get string length */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cur_pos = '\n'; /* Put back the original char */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* In this case the bytes left will be less than slen */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((retval = strncmp(sol, srch_str, *bytes_left)) >= 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Should return a value < 0 to show that search string goes to eof */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function sets an advisory lock on the file pointed to by the argument
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fd, which is a file descriptor. The lock is set using fcntl() which uses
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * flock structure.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelock_register(int fd, int cmd, int type, off_t offset, int whence, off_t len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Lot of places to cleanup - Less chance of missing out using this macro */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * cmd - ADD_ENTRY or REMOVE_ENTRY
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * update_str - string for repository operation
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - Assumed NOT to have a '\n' and that it is null terminated
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * errstring - Pointer that will be updated by this function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * - Any error msgs that has to be sent back to caller
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * FPCFGA_OK on success
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * FPCFGA_LIB_ERR on error
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function adds or deletes 'update_str' from FAB_REPOSITORY based on
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * value of 'cmd'. The repository has a warning line on the top to disallow
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * manual editing of the file. If the repository is being created fresh or if
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * it is of zero length or if it has only warning lines in it, the operation
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * speicified by 'cmd' is performed and returned. If the repository exists
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and has some data, it is expected to be of atleast the size of the lenght
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * of the warning header. This is the only check that is performed on the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * validity of the file. No other checks are performed. On a valid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * repository, to perform the update, this function basically makes use of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 3 buffers - the original buffer (repbuf), a copy buffer (c_repbuf) and a
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * temp buffer (t_repbuf).
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of the repository are mmap-ed into the repbuf and then
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * copied into the c_repbuf. All further operations are done using the copy.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * t_repbuf is created to be the size of c_repbuf +/- 'slen' (based on
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * whether it is add or remove operation). After adding/removing the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 'update_str', the c_repbuf is copied to a OLD_FAB_REPOSITORY and t_repbuf
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is made FAB_REPOSITORY.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteupdate_fabric_wwn_list(int cmd, const char *update_str, char **errstring)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Do some initializations */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Set the mode to read only. Root user can still open as RDWR.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We ignore errors in general here. But, just notice ENOENTs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((chmod(FAB_REPOSITORY, S_IRUSR|S_IRGRP|S_IROTH) == -1) &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Create the repository if its not there */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open(FAB_REPOSITORY, O_RDWR | O_CREAT)) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now try to chmod again. This time we dont ignore errors */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_register(fd, F_SETLKW, F_WRLCK, 0, SEEK_SET, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* A very Minimal check on repository */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If there is some data, it should be atleast the size of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the header
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We are trying to add/remove a NULL string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Just return success
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strcat(upd_str, "\n"); /* Append a new line char */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((copy_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sprintf(copy_rep, "%s%s%ld", FAB_REPOSITORY, COPY_EXT,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((copy_fd = open(copy_rep, O_RDWR | O_CREAT | O_TRUNC,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((repbuf = (char *)mmap(0, filesize, PROT_READ,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lseek(copy_fd, filesize - 1, SEEK_SET) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We cannot close the repository since we hold a lock
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * But we'll free up the mmap-ed area.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If we just created this file, or it was an empty repository file
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * add a header to the beginning of file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If it had was a repository file with just the header,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_file_flag != 0 || filesize == 0 || filesize == sizeof_rep_hdr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (write(fd, HDR, sizeof_rep_hdr) != sizeof_rep_hdr)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We know its a new file, empty file or a file with only a
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * header so lets get the update operation done with
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* If there is a header, we have to skip it */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now create the '.old' file */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * So, the side effect of a remove on an empty or
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * non-existing repository is that the repository got
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now, size and filesize are > sizeof_rep_hdr */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We'll search the full repository, header included, since
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we dont expect upd_str to match anything in the header.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* line already exists in repository or len == 0 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* construct temp file name using pid. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmp_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sprintf(tmp_rep, "%s%s%ld", FAB_REPOSITORY,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Open tmp repository file in absolute mode */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmp_fd = open(tmp_rep, O_RDWR|O_CREAT|O_TRUNC,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((t_repbuf = (char *)mmap(0, size, PROT_READ|PROT_WRITE,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we are using the copy of FAB_REPOSITORY and will
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * do msync first since it will be renamed to '.old' file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(copy_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(tmp_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* here we do rename and rename before close fd */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_register(fd, F_SETLK, F_UNLCK, 0, SEEK_SET, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * No need to init the 'else' part (size < len) because
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * in that case, there will be nothing to delete from
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the file and so 'size' will not be used in the code
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * below since search_line() will not find upd_str.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (search_line(c_repbuf, filesize, upd_str, len - 1,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* this line does not exists - nothing to remove */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* construct temp file name using pid. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmp_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sprintf(tmp_rep, "%s%s%ld", FAB_REPOSITORY,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Open tmp repository file in absolute mode */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmp_fd = open(tmp_rep, O_RDWR|O_CREAT|O_TRUNC,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(tmp_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we are using the copy of FAB_REPOSITORY and will
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * do msync first since it will be renamed to bak file.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(copy_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Close and invalidate the fd's */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* here we do rename and rename before close fd */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_register(fd, F_SETLK, F_UNLCK, 0, SEEK_SET, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Unexpected - just getout */