ipmgmt_util.c revision 9b5bf10ab04b9be5564d70a57980cfb68b6372e7
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk/*
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * CDDL HEADER START
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk *
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * The contents of this file are subject to the terms of the
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * Common Development and Distribution License (the "License").
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * You may not use this file except in compliance with the License.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk *
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * or http://www.opensolaris.org/os/licensing.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * See the License for the specific language governing permissions
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * and limitations under the License.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk *
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * When distributing Covered Code, include this CDDL HEADER in each
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * If applicable, add the following below this CDDL HEADER, with the
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * fields enclosed by brackets "[]" replaced with your own identifying
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * information: Portions Copyright [yyyy] [name of copyright owner]
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk *
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * CDDL HEADER END
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk */
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire/*
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk */
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk/*
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * Utility functions used by the ipmgmtd daemon.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk */
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include <stddef.h>
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include <stdlib.h>
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include <stdio.h>
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include <syslog.h>
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk#include <stdarg.h>
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include <unistd.h>
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include <errno.h>
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire#include "ipmgmt_impl.h"
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk#define IPMGMT_BUFSIZ 1024
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkvoid
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkipmgmt_log(int pri, const char *fmt, ...)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk{
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk va_list alist;
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk va_start(alist, fmt);
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk vsyslog(pri, fmt, alist);
f808c858fa61e7769218966759510a8b1190dfcfraf va_end(alist);
f808c858fa61e7769218966759510a8b1190dfcfraf}
f808c858fa61e7769218966759510a8b1190dfcfraf
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire/*
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire * Copy a source file to a new destination. The source file will be
4201a95e0468170d576f82c3aa63afecf718497aRic Aleshire * removed if rdonly is false (i.e., used when the source file resides
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * on a read-only file system).
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk *
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * Returns 0 on success and errno on failure.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk */
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkint
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpkipmgmt_cpfile(const char *src, const char *dst, boolean_t rdonly)
0bb073995ac5a95bd35f2dd790df1ea3d8c2d507Nick Todd{
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk struct stat statbuf;
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe FILE *sfp, *dfp;
7014882c6a3672fd0e5d60200af8643ae53c5928Richard Lowe char buf[IPMGMT_BUFSIZ];
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk size_t bytes;
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk int err = 0;
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk /*
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * Attempt to open the destination file first since we
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * want to optimize for the case where it is read-only
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk * and will return EROFS.
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk */
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk if ((dfp = fopen(dst, "w+")) == NULL)
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk return (errno);
45916cd2fec6e79bca5dee0421bd39e3c2910d1ejpk
/*
* Require that the source file exists.
*/
if (stat(src, &statbuf) != 0) {
err = errno;
(void) fclose(dfp);
return (err);
}
if ((sfp = fopen(src, "r")) == NULL) {
err = errno;
(void) fclose(dfp);
return (err);
}
/*
* Copy the file.
*/
while (((bytes = fread(buf, 1, sizeof (buf), sfp)) != 0) &&
(errno == 0)) {
(void) fwrite(buf, bytes, 1, dfp);
if (errno != 0)
break;
}
if (errno != 0)
err = errno;
(void) fclose(sfp);
(void) fclose(dfp);
/*
* If any error occurred, then remove the destination file.
*/
if (err != 0) {
(void) unlink(dst);
return (err);
}
/*
* Make sure the file attributes are correct.
*/
if (chmod(dst, IPADM_FILE_MODE) != 0 ||
chown(dst, UID_NETADM, GID_NETADM) != 0) {
err = errno;
(void) unlink(dst);
return (err);
}
/*
* If the source file does not reside on a read-only file system
* then remove it.
*/
if (!rdonly)
(void) unlink(src);
return (0);
}