-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsupdate.c
304 lines (277 loc) · 10.4 KB
/
fsupdate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright (c) 2018, Exile Heavy Industries
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fstab.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#ifndef DFBEADM_FSUP_H
#include "fsupdate.h"
#endif
extern char *__progname;
extern char **environ;
extern bool dbg;
extern bool noop;
/*
* TODO: This really should just be "activate()" automatically called by create()
* Special activation function, for use by the create() chain of functions
* since we already have all the information necessary to generate and install
* the fstab data
*/
int
autoactivate(bedata *snapfs, int fscount, const char *label) {
/* should probably have an int in there to ensure proper iteration */
int i, efd, retc;
char *efstab;
i = efd = retc = 0;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with snapfs = %p, fscount = %d\n",
__progname,__FILE__,__LINE__,__func__,(void *)snapfs,fscount);
}
if ((efstab = calloc((size_t)512, sizeof(char))) == NULL) {
fprintf(stderr,"ERR: %s [%s:%u] %s: Unable to allocate buffer for *efstab!\n",__progname,__FILE__,__LINE__,__func__);
retc = -1;
} else {
/* generate the name of the ephemeral fstab file */
snprintf(efstab, (size_t)512, "/tmp/.fstab.%u", getpid());
if ((efd = open(efstab, O_RDWR|O_CREAT|O_NONBLOCK|O_APPEND,S_IRUSR|S_IRGRP|S_IWUSR|S_IROTH)) <= 0) {
fprintf(stderr, "ERR: %s [%s:%u] %s: Unable to open %s for writing!\n",__progname,__FILE__,__LINE__,__func__,efstab);
free(efstab);
retc = -2;
} else {
for (i = 0; i < fscount; i++) {
/* XXX: Some tweaking necessary, likely need to bring *label back */
if (snapfs[i].snap) {
dprintf(efd, "%s%c%s\t%s\t%s\t%s\t%d\t%d\n", snapfs[i].fstab.fs_spec, BESEP, label, snapfs[i].fstab.fs_file,
snapfs[i].fstab.fs_vfstype, snapfs[i].fstab.fs_mntops,
snapfs[i].fstab.fs_freq, snapfs[i].fstab.fs_passno);
} else {
dprintf(efd, "%s\t%s\t%s\t%s\t%d\t%d\n", snapfs[i].fstab.fs_spec, snapfs[i].fstab.fs_file,
snapfs[i].fstab.fs_vfstype, snapfs[i].fstab.fs_mntops,
snapfs[i].fstab.fs_freq, snapfs[i].fstab.fs_passno);
}
}
/*
* new funciton, specifically dumps current fstab into a file with a timestamp of when it was created
* ideally will include the BE label in the future a swell, though that would require some extra parsing
*/
fprintf(stdout,"Installing new fstab...\n");
swapfstab("/etc/fstab", &efd);
printfs(efstab);
/* unlink(efstab); Do not unlink, as we can't be sure it's written properly now */
close(efd);
}
free(efstab);
}
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning %d to caller\n",__progname,__FILE__,__LINE__,__func__,retc);
}
return(retc);
}
/*
* activate a given boot environment
*/
int
activate(const char *label) {
/*
* the *label parameter is only used for the pfs lookups, by default this is called by create()
* we'll scan currently mounted filesystems for the given boot environment label, and use that to
* create the ephemeral fstab, which then replaces the current fstab, putting it in fstab.label
* assuming there was a label. If no label exists for the existing fstab, we'll simply call it
* fstab.bak
*/
int retc;
char *efstab;
FILE *efd;
retc = 0;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with label = %s\n",__progname,__FILE__,__LINE__,__func__,label);
}
if ((efstab = calloc((size_t)512, sizeof(char))) == NULL) {
fprintf(stderr,"Error Allocating Ephemeral fstab\n");
}
snprintf(efstab, (size_t)512, "/tmp/.fstab.%d.%s", getpid(), label);
fprintf(stderr, "Created Ephemeral fstab at %s", efstab);
if ((efd = fopen(efstab, "a")) == NULL) {
err(errno, "%s: %s: ", __progname, efstab);
}
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning %d to caller\n",__progname,__FILE__,__LINE__,__func__,retc);
}
fclose(efd);
free(efstab);
return(retc);
}
/*
* deactivate the given boot environment
*/
int
deactivate(const char *label) {
/*
* I'm not sure what this function will do in particular as the
* naming convention described above is fstab.label, making it trivial to swap boot environments
* This function likely only needs to be called when destroying a boot environment
*/
int retc;
retc = 0;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with label = %s\n",__progname,__FILE__,__LINE__,__func__,label);
}
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning %d to caller\n",__progname,__FILE__,__LINE__,__func__,retc);
}
return(retc);
}
/*
* delete a given boot environment
*/
int
rmenv(const char *label) {
/* TODO: Implement PFS deletion for the given label */
int retc;
retc = 0;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with label = %s\n",__progname,__FILE__,__LINE__,__func__,label);
}
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning %d to caller\n",__progname,__FILE__,__LINE__,__func__,retc);
}
return(retc);
}
/*
* delete the given snapshot
*/
int
rmsnap(const char *pfs) {
/* TODO: Implement PFS deletion for the given label */
int retc;
retc = 0;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with pfs = %s\n",__progname,__FILE__,__LINE__,__func__,pfs);
}
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning %d to caller\n",__progname,__FILE__,__LINE__,__func__,retc);
}
return(retc);
}
void
printfs(const char *fstab) {
struct fstab *fsent;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with fstab = %s\n",__progname,__FILE__,__LINE__,__func__,fstab);
}
fsent = NULL;
setfstab(fstab);
while ((fsent = getfsent()) != NULL) {
fprintf(stdout,"%s\t%s\t%s\t%s\t%s\t%d\t%d\n",
fsent->fs_spec, fsent->fs_file, fsent->fs_vfstype,
fsent->fs_mntops, fsent->fs_type, fsent->fs_freq,
fsent->fs_passno);
}
endfsent();
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning to caller\n",__progname,__FILE__,__LINE__,__func__);
}
}
/*
* TODO: Verify proper control logic
*/
int
swapfstab(const char *current, int *newfd) {
/*
* this function will open the old fstab, clean it out after dumpting contents to a new
* backup file, then write the contents of the ephemeral fstab into it
*/
int bfd, cfd, retc;
struct stat curfstab;
ssize_t written;
size_t readsize;
off_t writepoint;
char tmpbuf[PAGESIZE]; /* work with a page of data at a time, defaulting to 4096 if not otherwise defined */
readsize = 0;
written = 0; writepoint = 0;
bfd = cfd = retc = 0;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Entering with current = %s, newfd = %d\n",__progname,__FILE__,__LINE__,__func__,current,*newfd);
}
/* First ensure the fstab even exists, with no dynamic allocations, we can simply bail early */
if ((stat(current,&curfstab)) != 0) {
fprintf(stderr,"ERR: %s [%s:%u] %s: Unable to stat %s (%s)\n",
__progname,__FILE__,__LINE__,__func__,current,strerror(errno));
retc = -1;
}
/* Detect whether or not we should read in a full PAGE */
readsize = (curfstab.st_size > PAGESIZE) ? PAGESIZE : curfstab.st_size;
if ((cfd = open(current, O_RDWR)) <= 0) {
fprintf(stderr,"ERR: %s [%s:%u] %s: Unable to open %s r/w, %s\n",__progname,__FILE__,__LINE__,__func__,current,strerror(errno));
retc = -1;
}
if (retc == 0 && (bfd = open("/etc/fstab.bak", O_TRUNC|O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) <= 0) {
fprintf(stderr, "ERR: %s [%s:%u] %s: /etc/fstab.bak could not be created, verify file and user permissions are set properly!\n",__progname,__FILE__,__LINE__,__func__);
retc = -2;
}
/* read cfd into bfd, close bfd, rewind cfd, dump newfd into it */
for (;!noop && retc == 0 && written < curfstab.st_size;) {
pread(cfd, &tmpbuf, readsize, writepoint);
written += pwrite(bfd, &tmpbuf, readsize, writepoint);
writepoint = written; /* after writing, record the location to read from in the next iteration */
}
writepoint ^= writepoint; written ^= written;
/* Now we can close bfd, and dump newfd into cfd */
close(bfd);
/* stat the efstab, reuising curfstab struct */
if ((fstat(*newfd,&curfstab)) != 0) {
fprintf(stderr,"ERR: %s [%s:%u] %s: Unable to stat new fstab fd %d (%s)\n",__progname,__FILE__,__LINE__,__func__,*newfd,strerror(errno));
retc = -3;
}
for (;!noop && written < curfstab.st_size;) {
pread(*newfd, &tmpbuf, readsize, writepoint);
written += pwrite(cfd, &tmpbuf, readsize, writepoint);
writepoint = written;
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: writepoint = %lu, filesize = %lu\n",__progname,__FILE__,__LINE__,__func__,writepoint,curfstab.st_size);
}
}
if (dbg) {
fprintf(stderr,"DBG: %s [%s:%u] %s: Returning %d to caller\n",__progname,__FILE__,__LINE__,__func__,retc);
}
close(cfd);
return(retc);
}