xref: /illumos-gate/usr/src/cmd/cmd-inet/lib/ipmgmtd/ipmgmt_util.c (revision 1007fd6fd24227460e77ce89f5ca85641a85a576)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Utility functions used by the ipmgmtd daemon.
28  */
29 
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <syslog.h>
34 #include <stdarg.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include "ipmgmt_impl.h"
38 
39 #define	IPMGMT_BUFSIZ	1024
40 
41 void
42 ipmgmt_log(int pri, const char *fmt, ...)
43 {
44 	va_list alist;
45 
46 	va_start(alist, fmt);
47 	vsyslog(pri, fmt, alist);
48 	va_end(alist);
49 }
50 
51 /*
52  * Copy a source file to a new destination. The source file will be
53  * removed if rdonly is false (i.e., used when the source file resides
54  * on a read-only file system).
55  *
56  * Returns 0 on success and errno on failure.
57  */
58 int
59 ipmgmt_cpfile(const char *src, const char *dst, boolean_t rdonly)
60 {
61 	struct stat statbuf;
62 	FILE *sfp, *dfp;
63 	char buf[IPMGMT_BUFSIZ];
64 	size_t bytes;
65 	int err = 0;
66 
67 	/*
68 	 * Attempt to open the destination file first since we
69 	 * want to optimize for the case where it is read-only
70 	 * and will return EROFS.
71 	 */
72 	if ((dfp = fopen(dst, "w+")) == NULL)
73 		return (errno);
74 
75 	/*
76 	 * Require that the source file exists.
77 	 */
78 	if (stat(src, &statbuf) != 0) {
79 		err = errno;
80 		(void) fclose(dfp);
81 		return (err);
82 	}
83 	if ((sfp = fopen(src, "r")) == NULL) {
84 		err = errno;
85 		(void) fclose(dfp);
86 		return (err);
87 	}
88 
89 	/*
90 	 * Copy the file.
91 	 */
92 	while (((bytes = fread(buf, 1, sizeof (buf), sfp)) != 0) &&
93 	    (errno == 0)) {
94 		(void) fwrite(buf, bytes, 1, dfp);
95 		if (errno != 0)
96 			break;
97 	}
98 	if (errno != 0)
99 		err = errno;
100 
101 	(void) fclose(sfp);
102 	(void) fclose(dfp);
103 
104 	/*
105 	 * If any error occurred, then remove the destination file.
106 	 */
107 	if (err != 0) {
108 		(void) unlink(dst);
109 		return (err);
110 	}
111 
112 	/*
113 	 * Make sure the file attributes are correct.
114 	 */
115 	if (chmod(dst, IPADM_FILE_MODE) != 0 ||
116 	    chown(dst, UID_NETADM, GID_NETADM) != 0) {
117 		err = errno;
118 		(void) unlink(dst);
119 		return (err);
120 	}
121 
122 	/*
123 	 * If the source file does not reside on a read-only file system
124 	 * then remove it.
125 	 */
126 	if (!rdonly)
127 		(void) unlink(src);
128 
129 	return (0);
130 }
131