1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * Just in case we're not in a build environment, make sure that
31*7c478bd9Sstevel@tonic-gate * TEXT_DOMAIN gets set to something.
32*7c478bd9Sstevel@tonic-gate */
33*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
34*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
35*7c478bd9Sstevel@tonic-gate #endif
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate /*
38*7c478bd9Sstevel@tonic-gate * patch /etc/vfstab file
39*7c478bd9Sstevel@tonic-gate */
40*7c478bd9Sstevel@tonic-gate #include <meta.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate /*
44*7c478bd9Sstevel@tonic-gate * patch filesystem lines into vfstab file, return tempfilename
45*7c478bd9Sstevel@tonic-gate */
46*7c478bd9Sstevel@tonic-gate int
meta_patch_vfstab(char * cmpname,mdname_t * fsnp,char * vname,char * old_bdevname,int doit,int verbose,char ** tname,md_error_t * ep)47*7c478bd9Sstevel@tonic-gate meta_patch_vfstab(
48*7c478bd9Sstevel@tonic-gate char *cmpname, /* filesystem mount point or */
49*7c478bd9Sstevel@tonic-gate /* "swap" if updating swap partition */
50*7c478bd9Sstevel@tonic-gate mdname_t *fsnp, /* filesystem device name */
51*7c478bd9Sstevel@tonic-gate char *vname, /* vfstab file name */
52*7c478bd9Sstevel@tonic-gate char *old_bdevname, /* old name of block device, needed */
53*7c478bd9Sstevel@tonic-gate /* for deciding which of multiple */
54*7c478bd9Sstevel@tonic-gate /* swap file entries to change */
55*7c478bd9Sstevel@tonic-gate /* if NULL then not changing swap */
56*7c478bd9Sstevel@tonic-gate int doit, /* really patch file */
57*7c478bd9Sstevel@tonic-gate int verbose, /* show what we're doing */
58*7c478bd9Sstevel@tonic-gate char **tname, /* returned temp file name */
59*7c478bd9Sstevel@tonic-gate md_error_t *ep /* returned error */
60*7c478bd9Sstevel@tonic-gate )
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate char *chrname = fsnp->rname;
63*7c478bd9Sstevel@tonic-gate char *blkname = fsnp->bname;
64*7c478bd9Sstevel@tonic-gate FILE *fp = NULL;
65*7c478bd9Sstevel@tonic-gate FILE *tfp = NULL;
66*7c478bd9Sstevel@tonic-gate struct stat sbuf;
67*7c478bd9Sstevel@tonic-gate char buf[512];
68*7c478bd9Sstevel@tonic-gate char cdev[512];
69*7c478bd9Sstevel@tonic-gate char bdev[512];
70*7c478bd9Sstevel@tonic-gate char mntpt[512];
71*7c478bd9Sstevel@tonic-gate char fstype[512];
72*7c478bd9Sstevel@tonic-gate char fsckpass[512];
73*7c478bd9Sstevel@tonic-gate char mntboot[512];
74*7c478bd9Sstevel@tonic-gate char mntopt[512];
75*7c478bd9Sstevel@tonic-gate int gotfs = 0;
76*7c478bd9Sstevel@tonic-gate char *cmpstr = &mntpt[0]; /* compare against mntpnt if fs, */
77*7c478bd9Sstevel@tonic-gate /* or fstype if swap */
78*7c478bd9Sstevel@tonic-gate char *char_device = chrname;
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate /* check names */
81*7c478bd9Sstevel@tonic-gate assert(vname != NULL);
82*7c478bd9Sstevel@tonic-gate assert(tname != NULL);
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate /* get temp names */
85*7c478bd9Sstevel@tonic-gate *tname = NULL;
86*7c478bd9Sstevel@tonic-gate *tname = Malloc(strlen(vname) + strlen(".tmp") + 1);
87*7c478bd9Sstevel@tonic-gate (void) strcpy(*tname, vname);
88*7c478bd9Sstevel@tonic-gate (void) strcat(*tname, ".tmp");
89*7c478bd9Sstevel@tonic-gate
90*7c478bd9Sstevel@tonic-gate /* check if going to update swap entry in file */
91*7c478bd9Sstevel@tonic-gate /* if so then compare against file system type */
92*7c478bd9Sstevel@tonic-gate if ((old_bdevname != NULL) && (strcmp("swap", cmpname) == 0)) {
93*7c478bd9Sstevel@tonic-gate cmpstr = &fstype[0];
94*7c478bd9Sstevel@tonic-gate char_device = &cdev[0];
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate /* copy vfstab file, replace filesystem line */
98*7c478bd9Sstevel@tonic-gate if ((fp = fopen(vname, "r")) == NULL) {
99*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname);
100*7c478bd9Sstevel@tonic-gate goto out;
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate if (fstat(fileno(fp), &sbuf) != 0) {
103*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname);
104*7c478bd9Sstevel@tonic-gate goto out;
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate if (doit) {
107*7c478bd9Sstevel@tonic-gate if ((tfp = fopen(*tname, "w")) == NULL) {
108*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname);
109*7c478bd9Sstevel@tonic-gate goto out;
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate if (fchmod(fileno(tfp), (sbuf.st_mode & 0777)) != 0) {
112*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname);
113*7c478bd9Sstevel@tonic-gate goto out;
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate if (fchown(fileno(tfp), sbuf.st_uid, sbuf.st_gid) != 0) {
116*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname);
117*7c478bd9Sstevel@tonic-gate goto out;
118*7c478bd9Sstevel@tonic-gate }
119*7c478bd9Sstevel@tonic-gate }
120*7c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp) != NULL) {
121*7c478bd9Sstevel@tonic-gate
122*7c478bd9Sstevel@tonic-gate /* check that have all required params from vfstab file */
123*7c478bd9Sstevel@tonic-gate /* or that the line isnt a comment */
124*7c478bd9Sstevel@tonic-gate /* or that the fstype/mntpoint match what was passed in */
125*7c478bd9Sstevel@tonic-gate /* or that the block device matches if changing swap */
126*7c478bd9Sstevel@tonic-gate /* the last check is needed since there may be multiple */
127*7c478bd9Sstevel@tonic-gate /* entries of swap in the file, and so the fstype is not */
128*7c478bd9Sstevel@tonic-gate /* a sufficient check */
129*7c478bd9Sstevel@tonic-gate if ((sscanf(buf, "%512s %512s %512s %512s %512s %512s %512s",
130*7c478bd9Sstevel@tonic-gate bdev, cdev, mntpt, fstype, fsckpass,
131*7c478bd9Sstevel@tonic-gate mntboot, mntopt) != 7) ||
132*7c478bd9Sstevel@tonic-gate (bdev[0] == '#') || (strcmp(cmpstr, cmpname) != 0) ||
133*7c478bd9Sstevel@tonic-gate ((old_bdevname != NULL) &&
134*7c478bd9Sstevel@tonic-gate (strstr(bdev, old_bdevname) == NULL))) {
135*7c478bd9Sstevel@tonic-gate if (doit) {
136*7c478bd9Sstevel@tonic-gate if (fputs(buf, tfp) == EOF) {
137*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname);
138*7c478bd9Sstevel@tonic-gate goto out;
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate continue;
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate if (verbose) {
145*7c478bd9Sstevel@tonic-gate (void) printf(dgettext(TEXT_DOMAIN,
146*7c478bd9Sstevel@tonic-gate "Delete the following line from %s:\n\n"),
147*7c478bd9Sstevel@tonic-gate vname);
148*7c478bd9Sstevel@tonic-gate (void) printf("%s\n", buf);
149*7c478bd9Sstevel@tonic-gate (void) printf(
150*7c478bd9Sstevel@tonic-gate dgettext(TEXT_DOMAIN,
151*7c478bd9Sstevel@tonic-gate "Add the following line to %s:\n\n"),
152*7c478bd9Sstevel@tonic-gate vname);
153*7c478bd9Sstevel@tonic-gate (void) printf("%s\t%s\t%s\t%s\t%s\t%s\t%s\n\n",
154*7c478bd9Sstevel@tonic-gate blkname, char_device, mntpt, fstype, fsckpass,
155*7c478bd9Sstevel@tonic-gate mntboot, mntopt);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate if (doit) {
158*7c478bd9Sstevel@tonic-gate if (fprintf(tfp, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
159*7c478bd9Sstevel@tonic-gate blkname, char_device, mntpt, fstype, fsckpass,
160*7c478bd9Sstevel@tonic-gate mntboot, mntopt) == EOF) {
161*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname);
162*7c478bd9Sstevel@tonic-gate goto out;
163*7c478bd9Sstevel@tonic-gate }
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate
166*7c478bd9Sstevel@tonic-gate
167*7c478bd9Sstevel@tonic-gate gotfs = 1;
168*7c478bd9Sstevel@tonic-gate }
169*7c478bd9Sstevel@tonic-gate if (! feof(fp)) {
170*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname);
171*7c478bd9Sstevel@tonic-gate goto out;
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate if (! gotfs) {
174*7c478bd9Sstevel@tonic-gate (void) mderror(ep, MDE_VFSTAB_FILE, vname);
175*7c478bd9Sstevel@tonic-gate goto out;
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate if (fclose(fp) != 0) {
178*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname);
179*7c478bd9Sstevel@tonic-gate goto out;
180*7c478bd9Sstevel@tonic-gate }
181*7c478bd9Sstevel@tonic-gate fp = NULL;
182*7c478bd9Sstevel@tonic-gate if (doit) {
183*7c478bd9Sstevel@tonic-gate if ((fflush(tfp) != 0) ||
184*7c478bd9Sstevel@tonic-gate (fsync(fileno(tfp)) != 0) ||
185*7c478bd9Sstevel@tonic-gate (fclose(tfp) != 0)) {
186*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, *tname);
187*7c478bd9Sstevel@tonic-gate goto out;
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate tfp = NULL;
190*7c478bd9Sstevel@tonic-gate }
191*7c478bd9Sstevel@tonic-gate
192*7c478bd9Sstevel@tonic-gate /* return success */
193*7c478bd9Sstevel@tonic-gate return (0);
194*7c478bd9Sstevel@tonic-gate
195*7c478bd9Sstevel@tonic-gate /* cleanup, return error */
196*7c478bd9Sstevel@tonic-gate out:
197*7c478bd9Sstevel@tonic-gate if (fp != NULL)
198*7c478bd9Sstevel@tonic-gate (void) fclose(fp);
199*7c478bd9Sstevel@tonic-gate if (tfp != NULL)
200*7c478bd9Sstevel@tonic-gate (void) fclose(tfp);
201*7c478bd9Sstevel@tonic-gate if (*tname != NULL) {
202*7c478bd9Sstevel@tonic-gate (void) unlink(*tname);
203*7c478bd9Sstevel@tonic-gate Free(*tname);
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate return (-1);
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate /*
210*7c478bd9Sstevel@tonic-gate * set filesystem device name in vfstab
211*7c478bd9Sstevel@tonic-gate */
212*7c478bd9Sstevel@tonic-gate int
meta_patch_fsdev(char * fsname,mdname_t * fsnp,char * vname,md_error_t * ep)213*7c478bd9Sstevel@tonic-gate meta_patch_fsdev(
214*7c478bd9Sstevel@tonic-gate char *fsname, /* filesystem mount point */
215*7c478bd9Sstevel@tonic-gate mdname_t *fsnp, /* filesystem device */
216*7c478bd9Sstevel@tonic-gate char *vname, /* vfstab file name */
217*7c478bd9Sstevel@tonic-gate md_error_t *ep /* returned error */
218*7c478bd9Sstevel@tonic-gate )
219*7c478bd9Sstevel@tonic-gate {
220*7c478bd9Sstevel@tonic-gate int doit = 1;
221*7c478bd9Sstevel@tonic-gate int verbose = 0;
222*7c478bd9Sstevel@tonic-gate char *tvname = NULL;
223*7c478bd9Sstevel@tonic-gate int rval = -1;
224*7c478bd9Sstevel@tonic-gate
225*7c478bd9Sstevel@tonic-gate /* check names */
226*7c478bd9Sstevel@tonic-gate assert(fsname != NULL);
227*7c478bd9Sstevel@tonic-gate if (vname == NULL)
228*7c478bd9Sstevel@tonic-gate vname = "/etc/vfstab";
229*7c478bd9Sstevel@tonic-gate
230*7c478bd9Sstevel@tonic-gate /* replace lines in vfstab */
231*7c478bd9Sstevel@tonic-gate if (meta_patch_vfstab(fsname, fsnp, vname, NULL, doit, verbose, &tvname,
232*7c478bd9Sstevel@tonic-gate ep) != 0) {
233*7c478bd9Sstevel@tonic-gate goto out;
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate /* rename temp file on top of real one */
237*7c478bd9Sstevel@tonic-gate if (rename(tvname, vname) != 0) {
238*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname);
239*7c478bd9Sstevel@tonic-gate goto out;
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate Free(tvname);
242*7c478bd9Sstevel@tonic-gate tvname = NULL;
243*7c478bd9Sstevel@tonic-gate rval = 0;
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate /* cleanup, return error */
246*7c478bd9Sstevel@tonic-gate out:
247*7c478bd9Sstevel@tonic-gate if (tvname != NULL) {
248*7c478bd9Sstevel@tonic-gate if (doit)
249*7c478bd9Sstevel@tonic-gate (void) unlink(tvname);
250*7c478bd9Sstevel@tonic-gate Free(tvname);
251*7c478bd9Sstevel@tonic-gate }
252*7c478bd9Sstevel@tonic-gate return (rval);
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate /*
257*7c478bd9Sstevel@tonic-gate * set filesystem device name in vfstab
258*7c478bd9Sstevel@tonic-gate */
259*7c478bd9Sstevel@tonic-gate int
meta_patch_swapdev(mdname_t * fsnp,char * vname,char * old_bdevname,md_error_t * ep)260*7c478bd9Sstevel@tonic-gate meta_patch_swapdev(
261*7c478bd9Sstevel@tonic-gate mdname_t *fsnp, /* filesystem device */
262*7c478bd9Sstevel@tonic-gate char *vname, /* vfstab file name */
263*7c478bd9Sstevel@tonic-gate char *old_bdevname, /* block device name to change */
264*7c478bd9Sstevel@tonic-gate md_error_t *ep /* returned error */
265*7c478bd9Sstevel@tonic-gate )
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate int doit = 1;
268*7c478bd9Sstevel@tonic-gate int verbose = 0;
269*7c478bd9Sstevel@tonic-gate char *tvname = NULL;
270*7c478bd9Sstevel@tonic-gate int rval = -1;
271*7c478bd9Sstevel@tonic-gate
272*7c478bd9Sstevel@tonic-gate /* check names */
273*7c478bd9Sstevel@tonic-gate if (vname == NULL)
274*7c478bd9Sstevel@tonic-gate vname = "/etc/vfstab";
275*7c478bd9Sstevel@tonic-gate
276*7c478bd9Sstevel@tonic-gate /* replace lines in vfstab */
277*7c478bd9Sstevel@tonic-gate if (meta_patch_vfstab("swap", fsnp, vname, old_bdevname, doit,
278*7c478bd9Sstevel@tonic-gate verbose, &tvname, ep) != 0) {
279*7c478bd9Sstevel@tonic-gate goto out;
280*7c478bd9Sstevel@tonic-gate }
281*7c478bd9Sstevel@tonic-gate
282*7c478bd9Sstevel@tonic-gate /* rename temp file on top of real one */
283*7c478bd9Sstevel@tonic-gate if (rename(tvname, vname) != 0) {
284*7c478bd9Sstevel@tonic-gate (void) mdsyserror(ep, errno, vname);
285*7c478bd9Sstevel@tonic-gate goto out;
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate Free(tvname);
288*7c478bd9Sstevel@tonic-gate tvname = NULL;
289*7c478bd9Sstevel@tonic-gate rval = 0;
290*7c478bd9Sstevel@tonic-gate
291*7c478bd9Sstevel@tonic-gate /* cleanup, return error */
292*7c478bd9Sstevel@tonic-gate out:
293*7c478bd9Sstevel@tonic-gate if (tvname != NULL) {
294*7c478bd9Sstevel@tonic-gate if (doit)
295*7c478bd9Sstevel@tonic-gate (void) unlink(tvname);
296*7c478bd9Sstevel@tonic-gate Free(tvname);
297*7c478bd9Sstevel@tonic-gate }
298*7c478bd9Sstevel@tonic-gate return (rval);
299*7c478bd9Sstevel@tonic-gate }
300