fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
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 *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
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 *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <libgen.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "cfga_fp.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
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 Fortestatic char *HDR =
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"#\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"# fabric_WWN_map\n"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte"#\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"#\n";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
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 *
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 *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Notes :
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 *
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 Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesearch_line(char *buf, int buflen, char *srch_str, int slen,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int *write_offset, int *bytes_left)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int retval, sizeof_rep_hdr = strlen(HDR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *sol; /* Pointer to Start-Of-Line */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *cur_pos; /* current position */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *bytes_left = buflen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *write_offset = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (buf == NULL || *buf == NULL || buflen <= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-2); /* Arbitrary -ve val. srch_str not found */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (srch_str == NULL || *srch_str == NULL || slen <= 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0); /* This says srch_str was found */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sol = cur_pos = buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (buflen >= sizeof_rep_hdr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* skip header */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sol = cur_pos = buf + sizeof_rep_hdr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *bytes_left -= sizeof_rep_hdr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (*bytes_left >= slen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((retval = strncmp(sol, srch_str, slen)) >= 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* strncmp will pass if srch_str is a substring */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((retval == 0) && (*bytes_left > slen) &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*(sol+slen) != '\n'))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retval = 1; /* Force it to be > 0 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *write_offset = sol - buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (retval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* retval < 0 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((cur_pos = strchr(sol, (int)'\n')) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *write_offset = buflen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (retval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Get the length of this line */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cur_pos = '\0'; /* kludge to get string length */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *bytes_left -= (strlen(sol) + 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *cur_pos = '\n'; /* Put back the original char */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sol = cur_pos = cur_pos + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (*bytes_left > 0) {
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 *write_offset = sol - buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *write_offset = buflen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (retval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *write_offset = sol - buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Should return a value < 0 to show that search string goes to eof */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
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 Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelock_register(int fd, int cmd, int type, off_t offset, int whence, off_t len)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct flock lock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_type = type;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_start = offset;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_whence = whence;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock.l_len = len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (fcntl(fd, cmd, &lock));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Lot of places to cleanup - Less chance of missing out using this macro */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define CLEANUP_N_RET(ret) \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fd != -1) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (copy_fd != -1) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(copy_fd); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tmp_fd != -1) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(tmp_fd); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (copy_rep != NULL) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte remove(copy_rep); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(copy_rep); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tmp_rep != NULL) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte remove(tmp_rep); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(tmp_rep); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (upd_str != NULL) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(upd_str); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (repbuf != NULL) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte munmap(repbuf, filesize); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c_repbuf != NULL) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte munmap(c_repbuf, filesize); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (t_repbuf != NULL) { \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte munmap(t_repbuf, size); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ret)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * INPUTS:
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 *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * RETURNS :
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * FPCFGA_OK on success
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * FPCFGA_LIB_ERR on error
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * SYNOPSIS:
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 Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteupdate_fabric_wwn_list(int cmd, const char *update_str, char **errstring)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int fd, copy_fd, tmp_fd, new_file_flag = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int len, write_offset, bytes_left;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int sizeof_rep_hdr = strlen(HDR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *repbuf, *c_repbuf, *t_repbuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *copy_rep, *tmp_rep, *upd_str;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte off_t filesize, size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct stat stbuf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Do some initializations */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fd = copy_fd = tmp_fd = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte repbuf = c_repbuf = t_repbuf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte copy_rep = tmp_rep = upd_str = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size = filesize = write_offset = bytes_left = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((chmod(FAB_REPOSITORY, S_IRUSR|S_IRGRP|S_IROTH) == -1) &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (errno == ENOENT)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_file_flag = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mkdirp(FAB_REPOSITORY_DIR,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Create the repository if its not there */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fd = open(FAB_REPOSITORY, O_RDWR | O_CREAT)) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
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 close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_register(fd, F_SETLKW, F_WRLCK, 0, SEEK_SET, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, 0, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fstat(fd, &stbuf) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte filesize = size = stbuf.st_size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* A very Minimal check on repository */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (filesize && filesize < sizeof_rep_hdr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If there is some data, it should be atleast the size of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the header
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((len = strlen(update_str)) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We are trying to add/remove a NULL string.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Just return success
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_OK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((upd_str = calloc(1, len + 2)) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strcpy(upd_str, update_str);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strcat(upd_str, "\n"); /* Append a new line char */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len = strlen(upd_str);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (filesize > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((copy_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (COPY_EXT) + sizeof (pid_t))) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sprintf(copy_rep, "%s%s%ld", FAB_REPOSITORY, COPY_EXT,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte getpid());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((copy_fd = open(copy_rep, O_RDWR | O_CREAT | O_TRUNC,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte S_IRUSR | S_IWUSR)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((repbuf = (char *)mmap(0, filesize, PROT_READ,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte MAP_SHARED, fd, 0)) == MAP_FAILED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(fd);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(upd_str);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lseek(copy_fd, filesize - 1, SEEK_SET) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (write(copy_fd, "", 1) != 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((c_repbuf = (char *)mmap(0, filesize,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PROT_READ | PROT_WRITE,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte MAP_SHARED, copy_fd, 0)) == MAP_FAILED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(c_repbuf, repbuf, filesize);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte munmap(repbuf, filesize);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte repbuf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_file_flag != 0 || filesize == 0 || filesize == sizeof_rep_hdr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((filesize != sizeof_rep_hdr) &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (write(fd, HDR, sizeof_rep_hdr) != sizeof_rep_hdr)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (cmd) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case ADD_ENTRY:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* If there is a header, we have to skip it */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lseek(fd, 0, SEEK_END) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (write(fd, upd_str, len) != len) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (filesize > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now create the '.old' file */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msync(c_repbuf, filesize, MS_SYNC) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(copy_fd,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rename(copy_rep, OLD_FAB_REPOSITORY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case REMOVE_ENTRY:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 * created
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, 0, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Now, size and filesize are > sizeof_rep_hdr */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (cmd) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case ADD_ENTRY:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size += len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (search_line(c_repbuf, filesize, upd_str,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte len - 1, &write_offset, &bytes_left) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* line already exists in repository or len == 0 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK); /* SUCCESS */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* construct temp file name using pid. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmp_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (TMP_EXT) + sizeof (pid_t))) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sprintf(tmp_rep, "%s%s%ld", FAB_REPOSITORY,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte TMP_EXT, getpid());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
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 S_IRUSR | S_IWUSR)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lseek(tmp_fd, size - 1, SEEK_SET) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (write(tmp_fd, "", 1) != 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((t_repbuf = (char *)mmap(0, size, PROT_READ|PROT_WRITE,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte MAP_SHARED, tmp_fd, 0)) == MAP_FAILED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(t_repbuf, c_repbuf, write_offset);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strncpy(t_repbuf + write_offset, upd_str, len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (write_offset != filesize) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(t_repbuf + write_offset + len,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c_repbuf + write_offset, bytes_left);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msync(c_repbuf, filesize, MS_SYNC) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(copy_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msync(t_repbuf, size, MS_SYNC) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(tmp_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(copy_fd); copy_fd = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(tmp_fd); tmp_fd = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* here we do rename and rename before close fd */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rename(copy_rep, OLD_FAB_REPOSITORY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rename(tmp_rep, FAB_REPOSITORY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_register(fd, F_SETLK, F_UNLCK, 0, SEEK_SET, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case REMOVE_ENTRY:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (size >= sizeof_rep_hdr + len - 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size -= len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (search_line(c_repbuf, filesize, upd_str, len - 1,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &write_offset, &bytes_left) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* this line does not exists - nothing to remove */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK); /* SUCCESS */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* construct temp file name using pid. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((tmp_rep = (char *)calloc(1, strlen(FAB_REPOSITORY) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (TMP_EXT) + sizeof (pid_t))) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sprintf(tmp_rep, "%s%s%ld", FAB_REPOSITORY,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte TMP_EXT, getpid());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
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 S_IRUSR | S_IWUSR)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (size > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lseek(tmp_fd, size - 1, SEEK_SET) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (write(tmp_fd, "", 1) != 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((t_repbuf = (char *)mmap(0, size,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte PROT_READ|PROT_WRITE,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte MAP_SHARED, tmp_fd, 0)) == MAP_FAILED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(t_repbuf, c_repbuf, write_offset);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((bytes_left - len) > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte memcpy(t_repbuf + write_offset,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c_repbuf + write_offset + len,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bytes_left - len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msync(t_repbuf, size, MS_SYNC) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(tmp_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msync(c_repbuf, filesize, MS_SYNC) == -1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (fchmod(copy_fd, S_IRUSR | S_IRGRP | S_IROTH) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Close and invalidate the fd's */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(copy_fd); copy_fd = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte close(tmp_fd); tmp_fd = -1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* here we do rename and rename before close fd */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rename(copy_rep, OLD_FAB_REPOSITORY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rename(tmp_rep, FAB_REPOSITORY);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_register(fd, F_SETLK, F_UNLCK, 0, SEEK_SET, 0) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfga_err(errstring, errno, ERR_UPD_REP, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_LIB_ERR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Unexpected - just getout */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CLEANUP_N_RET(FPCFGA_OK); /* SUCCESS */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}