Skip to content

Commit c96a1de

Browse files
authored
Merge pull request #16 from ajs124/master
Fix stuff
2 parents 577cae5 + 34494b0 commit c96a1de

File tree

5 files changed

+70
-44
lines changed

5 files changed

+70
-44
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ generator: $(GENERATOR_OUT)
1212

1313
shutdown: $(SHUTDOWN_OUT)
1414

15-
$(MOUNT_OUT): src/mount.initrd_zfs.c src/zfs-util.c src/zfs-util.h
15+
$(MOUNT_OUT): src/mount.initrd_zfs.c src/zfs-util.c
1616
$(CC) $(CCFLAGS) $(LDFLAGS) -o $@ $^
1717

18-
$(GENERATOR_OUT): src/zfs-generator.c src/cmdline.c src/cmdline.h
18+
$(GENERATOR_OUT): src/zfs-generator.c src/cmdline.c
1919
$(CC) $(CCFLAGS) $(LDFLAGS) -o $@ $^
2020

2121
$(SHUTDOWN_OUT): src/zfs-shutdown.c
2222
$(CC) $(CCFLAGS) $(LDFLAGS) -o $@ $^
2323

2424
clean:
25-
$(RM) $(MOUNT_OUT) $(ZFS_GENERATOR_OUT) $(ZFS_SHUTDOWN_OUT)
25+
$(RM) $(MOUNT_OUT) $(GENERATOR_OUT) $(SHUTDOWN_OUT)
2626

2727
.PHONY: all mount generator shutdown clean

src/cmdline.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ int getCmdline(char **cmdline) {
99

1010
fd = fopen("/proc/cmdline", "r");
1111
if (fd == NULL) {
12-
fprintf(stderr, "Can not open kernel command line\n");
12+
perror("Cannot open kernel command line\n");
1313
return -1;
1414
}
1515
if (getline(cmdline, &linelen, fd) < 0) {
16-
fprintf(stderr, "Can not read kernel command line\n");
16+
perror("Cannot read kernel command line\n");
1717
fclose(fd);
1818
return -2;
1919
}

src/mount.initrd_zfs.c

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ int handleBootfs(char **pool) {
1919
}
2020

2121
ret = zfs_get_bootfs(rpool, pool);
22-
if (rpool != NULL) {
23-
free(rpool);
24-
}
22+
free(rpool);
2523
return ret;
2624
}
2725

@@ -43,7 +41,7 @@ int main(int argc, char *argv[]) {
4341
char *snapshot;
4442
char *at;
4543
// For mounting
46-
char *snap;
44+
char *snap = NULL;
4745
char *lines = NULL;
4846
char *endLine;
4947
char *lineToken;
@@ -70,9 +68,8 @@ int main(int argc, char *argv[]) {
7068
}
7169
strcat(newoptions, argv[i]);
7270
strcat(newoptions, ",");
73-
if (options != NULL) {
74-
free(options);
75-
}
71+
free(options);
72+
7673
options = newoptions;
7774
isOption = 0;
7875
continue;
@@ -100,6 +97,11 @@ int main(int argc, char *argv[]) {
10097
fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
10198
exit(1);
10299
}
100+
// Check if all parameters are supplied
101+
if (dataset == NULL || mountpoint == NULL) {
102+
fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
103+
exit(1);
104+
}
103105
// Get rid of the trailing comma
104106
if (options != NULL) {
105107
newoptions = malloc(sizeof(options) - sizeof(char));
@@ -118,9 +120,8 @@ int main(int argc, char *argv[]) {
118120
fprintf(stderr, "Can not get bootfs value\n");
119121
free(dataset);
120122
free(mountpoint);
121-
if (options != NULL) {
122-
free(options);
123-
}
123+
free(options);
124+
exit(1);
124125
}
125126
}
126127

@@ -157,7 +158,7 @@ int main(int argc, char *argv[]) {
157158
}
158159
snapshot = strtok(snaps, "\n");
159160
while (snapshot != NULL) {
160-
snapPath = malloc((strlen(snapDS) + strlen(&snapshot[strlen(dataset)] + 1) * sizeof(char)));
161+
snapPath = malloc((strlen(snapDS) + strlen(&snapshot[strlen(dataset)]) + 1) * sizeof(char));
161162
strcpy(snapPath, snapDS);
162163
strcat(snapPath, &(snapshot[strlen(dataset)]));
163164
at = strrchr(snapPath, '@');
@@ -193,18 +194,15 @@ int main(int argc, char *argv[]) {
193194
}
194195
}
195196
aftersnap:
196-
if (snapDS != NULL) {
197-
free(snapDS);
198-
}
197+
free(snapDS);
199198

200199
// Mount the dataset(s)
201200
ret = zfs_list_datasets_with_mp(dataset, &lines);
202201
if (ret != 0) {
203202
free(dataset);
204203
free(mountpoint);
205-
if (options != NULL) {
206-
free(options);
207-
}
204+
free(options);
205+
exit(1);
208206
}
209207

210208
lineToken = strtok_r(lines, "\n", &endLine);
@@ -225,10 +223,10 @@ int main(int argc, char *argv[]) {
225223
strcat(where, (where_tmp == NULL) ? mountpointToken : where_tmp);
226224
// Mount
227225
ret = zfs_mount(what, where, options);
228-
if (where_tmp != NULL) {
229-
free(where_tmp);
230-
where_tmp = NULL;
231-
}
226+
227+
free(where_tmp);
228+
where_tmp = NULL;
229+
232230
if (ret != 0) {
233231
fprintf(stderr, "ZFS command failed with exit code %d, bailing out\n", ret);
234232
free(where);
@@ -241,10 +239,7 @@ int main(int argc, char *argv[]) {
241239
}
242240

243241
free(lines);
244-
245242
free(dataset);
246243
free(mountpoint);
247-
if (options != NULL) {
248-
free(options);
249-
}
244+
free(options);
250245
}

src/zfs-generator.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <errno.h>
12
#include <unistd.h>
23
#include <stdio.h>
34
#include <stdlib.h>
@@ -28,7 +29,7 @@ int getRootOptions(char **options) {
2829
optionsval = malloc((strlen("zfsutil") + 1) * sizeof(char));
2930
strcpy(optionsval, "zfsutil");
3031
} else if (ret != 0) {
31-
fprintf(stderr, "Unknown thing happened while reading the rootflags= paremter\n");
32+
fprintf(stderr, "Unknown thing happened while reading the rootflags= paramter\n");
3233
return 2;
3334
}
3435
len = strlen(optionsval) + 1;
@@ -86,6 +87,7 @@ int getForce(char **forceParam) {
8687
*forceParam = malloc(4 * sizeof(char));
8788
strcpy(*forceParam, " -f");
8889
}
90+
free(forceval);
8991
return 0;
9092
}
9193

@@ -130,20 +132,25 @@ int generateScanUnit(char *directory, const char *targetName, const char *unitNa
130132
strcat(unitpath, "/");
131133
strcat(unitpath, unitName);
132134
// Make wants directory
133-
mkdir(targetpath, 0775);
135+
if (mkdir(targetpath, 0775) < 0 && errno != EEXIST) {
136+
perror("Can not create unit directory\n");
137+
free(targetpath);
138+
free(unitpath);
139+
return(1);
140+
}
134141
// Make symlink
135142
strcat(targetpath, "/");
136143
strcat(targetpath, unitName);
137144
symlink(unitName, targetpath);
138145
// Check if unit already exists
139146
if (access(unitpath, R_OK) != -1) {
147+
perror("Scanning unit file already exists or cannot be accessed\n");
140148
free(unitpath);
141-
printf("Scanning unit file already exists\n");
149+
free(targetpath);
142150
return 0;
143151
}
144152
// Check if we need to ignore the cache file
145153
if (ignoreCache == 0) {
146-
// Wir möchten die Zeile
147154
cacheLine = malloc((strlen("ConditionPathExists=!/etc/zfs/zpool.cache") + 1) * sizeof(char));
148155
strcpy(cacheLine, "ConditionPathExists=!/etc/zfs/zpool.cache");
149156
} else {
@@ -153,9 +160,10 @@ int generateScanUnit(char *directory, const char *targetName, const char *unitNa
153160
// Write
154161
fp = fopen(unitpath, "w");
155162
if (fp == NULL) {
163+
perror("Can not write to scanning unit file\n");
156164
free(unitpath);
157165
free(cacheLine);
158-
fprintf(stderr, "Can not write to scanning unit file\n");
166+
free(targetpath);
159167
return 1;
160168
}
161169
fprintf(fp, "[Unit]\n\
@@ -175,6 +183,7 @@ ExecStart=/usr/bin/zpool import %s -N -o cachefile=none%s\n", cacheLine, poolNam
175183

176184
free(cacheLine);
177185
free(unitpath);
186+
free(targetpath);
178187
return 0;
179188
}
180189

@@ -193,22 +202,29 @@ int generateCacheUnit(char *directory, const char *targetName, const char *unitN
193202
strcat(unitpath, "/");
194203
strcat(unitpath, unitName);
195204
// Make wants directory
196-
mkdir(targetpath, 0775);
205+
if (mkdir(targetpath, 0775) < 0 && errno != EEXIST) {
206+
perror("Can not create unit directory\n");
207+
free(targetpath);
208+
free(unitpath);
209+
return(1);
210+
}
197211
// Make symlink
198212
strcat(targetpath, "/");
199213
strcat(targetpath, unitName);
200214
symlink(unitName, targetpath);
201215
// Check if unit already exists
202216
if (access(unitpath, R_OK) != -1) {
203217
free(unitpath);
218+
free(targetpath);
204219
printf("Caching unit file already exists\n");
205220
return 0;
206221
}
207222
// Write
208223
fp = fopen(unitpath, "w");
209224
if (fp == NULL) {
225+
perror("Cannot write to scanning unit file\n");
210226
free(unitpath);
211-
fprintf(stderr, "Can not write to scanning unit file\n");
227+
free(targetpath);
212228
return 1;
213229
}
214230
fprintf(fp, "[Unit]\n\
@@ -226,6 +242,7 @@ RemainAfterExit=yes\n\
226242
ExecStart=/usr/bin/zpool import %s -N -c /etc/zfs/zpool.cache%s\n", poolName, forceParam);
227243
fclose(fp);
228244
free(unitpath);
245+
free(targetpath);
229246

230247
return 0;
231248
}
@@ -244,13 +261,17 @@ int generateSysrootUnit(char *directory, int bootfs, char *dataset, char *snapsh
244261
strcat(unitpath, "/");
245262
strcat(unitpath, targetName);
246263
// Make dropin directory
247-
mkdir(unitpath, 0775);
264+
if (mkdir(unitpath, 0775) < 0 && errno != EEXIST) {
265+
perror("Can not create unit directory\n");
266+
free(unitpath);
267+
return(1);
268+
}
248269
strcat(unitpath, "/");
249270
strcat(unitpath, unitName);
250271
// Check if unit already exists
251272
if (access(unitpath, R_OK) != -1) {
273+
perror("Mounting unit file already exists\n");
252274
free(unitpath);
253-
printf("Mounting unit file already exists\n");
254275
return 0;
255276
}
256277

@@ -279,16 +300,17 @@ int generateSysrootUnit(char *directory, int bootfs, char *dataset, char *snapsh
279300
if (getRootOptions(&options) != 0) {
280301
fprintf(stderr, "Can not get root options\n");
281302
free(what);
303+
free(unitpath);
282304
return 1;
283305
}
284306

285307
// Write
286308
fp = fopen(unitpath, "w");
287309
if (fp == NULL) {
310+
perror("Can not write to mounting unit file\n");
288311
free(unitpath);
289312
free(options);
290313
free(what);
291-
fprintf(stderr, "Can not write to mounting unit file\n");
292314
return 1;
293315
}
294316
fprintf(fp, "[Mount]\n\

src/zfs-util.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ int execute(char *command, char needOutput, char **output, char *param[]) {
2121
char *linebuffer;
2222
size_t size;
2323
int nRead;
24+
int fd;
2425

2526
// Execute
2627
if (needOutput == 1) {
@@ -33,15 +34,22 @@ int execute(char *command, char needOutput, char **output, char *param[]) {
3334
close(2);
3435
if (needOutput == 1) {
3536
close(pip[0]);
36-
dup(pip[1]);
37+
fd = dup(pip[1]);
38+
if (fd < 0) {
39+
perror("Can not duplicate pipe\n");
40+
close(pip[1]);
41+
}
3742
}
3843
// Execute
3944
execv(command, param);
45+
close(fd);
4046
exit(254);
4147
} else if (pid < 0) {
42-
fprintf(stderr, "Can not fork\n");
43-
close(pip[0]);
44-
close(pip[1]);
48+
perror("Can not fork\n");
49+
if (needOutput == 1) {
50+
close(pip[0]);
51+
close(pip[1]);
52+
}
4553
return pid;
4654
}
4755
if (needOutput == 1) {
@@ -249,6 +257,7 @@ int zfs_list_snapshots(char *dataset, char *snapshot, char **output) {
249257
}
250258
tok = strtok(NULL, "\n");
251259
}
260+
free(suffix);
252261
free(snaps);
253262
return 0;
254263
}

0 commit comments

Comments
 (0)