Skip to content

Commit

Permalink
Merge pull request #16 from ajs124/master
Browse files Browse the repository at this point in the history
Fix stuff
  • Loading branch information
dasJ committed Nov 30, 2016
2 parents 577cae5 + 34494b0 commit c96a1de
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 44 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ generator: $(GENERATOR_OUT)

shutdown: $(SHUTDOWN_OUT)

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

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

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

clean:
$(RM) $(MOUNT_OUT) $(ZFS_GENERATOR_OUT) $(ZFS_SHUTDOWN_OUT)
$(RM) $(MOUNT_OUT) $(GENERATOR_OUT) $(SHUTDOWN_OUT)

.PHONY: all mount generator shutdown clean
4 changes: 2 additions & 2 deletions src/cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ int getCmdline(char **cmdline) {

fd = fopen("/proc/cmdline", "r");
if (fd == NULL) {
fprintf(stderr, "Can not open kernel command line\n");
perror("Cannot open kernel command line\n");
return -1;
}
if (getline(cmdline, &linelen, fd) < 0) {
fprintf(stderr, "Can not read kernel command line\n");
perror("Cannot read kernel command line\n");
fclose(fd);
return -2;
}
Expand Down
45 changes: 20 additions & 25 deletions src/mount.initrd_zfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ int handleBootfs(char **pool) {
}

ret = zfs_get_bootfs(rpool, pool);
if (rpool != NULL) {
free(rpool);
}
free(rpool);
return ret;
}

Expand All @@ -43,7 +41,7 @@ int main(int argc, char *argv[]) {
char *snapshot;
char *at;
// For mounting
char *snap;
char *snap = NULL;
char *lines = NULL;
char *endLine;
char *lineToken;
Expand All @@ -70,9 +68,8 @@ int main(int argc, char *argv[]) {
}
strcat(newoptions, argv[i]);
strcat(newoptions, ",");
if (options != NULL) {
free(options);
}
free(options);

options = newoptions;
isOption = 0;
continue;
Expand Down Expand Up @@ -100,6 +97,11 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
exit(1);
}
// Check if all parameters are supplied
if (dataset == NULL || mountpoint == NULL) {
fprintf(stderr, "Call like: %s <dataset> <mount_point> [-o option[,...]]\n", argv[0]);
exit(1);
}
// Get rid of the trailing comma
if (options != NULL) {
newoptions = malloc(sizeof(options) - sizeof(char));
Expand All @@ -118,9 +120,8 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Can not get bootfs value\n");
free(dataset);
free(mountpoint);
if (options != NULL) {
free(options);
}
free(options);
exit(1);
}
}

Expand Down Expand Up @@ -157,7 +158,7 @@ int main(int argc, char *argv[]) {
}
snapshot = strtok(snaps, "\n");
while (snapshot != NULL) {
snapPath = malloc((strlen(snapDS) + strlen(&snapshot[strlen(dataset)] + 1) * sizeof(char)));
snapPath = malloc((strlen(snapDS) + strlen(&snapshot[strlen(dataset)]) + 1) * sizeof(char));
strcpy(snapPath, snapDS);
strcat(snapPath, &(snapshot[strlen(dataset)]));
at = strrchr(snapPath, '@');
Expand Down Expand Up @@ -193,18 +194,15 @@ int main(int argc, char *argv[]) {
}
}
aftersnap:
if (snapDS != NULL) {
free(snapDS);
}
free(snapDS);

// Mount the dataset(s)
ret = zfs_list_datasets_with_mp(dataset, &lines);
if (ret != 0) {
free(dataset);
free(mountpoint);
if (options != NULL) {
free(options);
}
free(options);
exit(1);
}

lineToken = strtok_r(lines, "\n", &endLine);
Expand All @@ -225,10 +223,10 @@ int main(int argc, char *argv[]) {
strcat(where, (where_tmp == NULL) ? mountpointToken : where_tmp);
// Mount
ret = zfs_mount(what, where, options);
if (where_tmp != NULL) {
free(where_tmp);
where_tmp = NULL;
}

free(where_tmp);
where_tmp = NULL;

if (ret != 0) {
fprintf(stderr, "ZFS command failed with exit code %d, bailing out\n", ret);
free(where);
Expand All @@ -241,10 +239,7 @@ int main(int argc, char *argv[]) {
}

free(lines);

free(dataset);
free(mountpoint);
if (options != NULL) {
free(options);
}
free(options);
}
42 changes: 32 additions & 10 deletions src/zfs-generator.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -28,7 +29,7 @@ int getRootOptions(char **options) {
optionsval = malloc((strlen("zfsutil") + 1) * sizeof(char));
strcpy(optionsval, "zfsutil");
} else if (ret != 0) {
fprintf(stderr, "Unknown thing happened while reading the rootflags= paremter\n");
fprintf(stderr, "Unknown thing happened while reading the rootflags= paramter\n");
return 2;
}
len = strlen(optionsval) + 1;
Expand Down Expand Up @@ -86,6 +87,7 @@ int getForce(char **forceParam) {
*forceParam = malloc(4 * sizeof(char));
strcpy(*forceParam, " -f");
}
free(forceval);
return 0;
}

Expand Down Expand Up @@ -130,20 +132,25 @@ int generateScanUnit(char *directory, const char *targetName, const char *unitNa
strcat(unitpath, "/");
strcat(unitpath, unitName);
// Make wants directory
mkdir(targetpath, 0775);
if (mkdir(targetpath, 0775) < 0 && errno != EEXIST) {
perror("Can not create unit directory\n");
free(targetpath);
free(unitpath);
return(1);
}
// Make symlink
strcat(targetpath, "/");
strcat(targetpath, unitName);
symlink(unitName, targetpath);
// Check if unit already exists
if (access(unitpath, R_OK) != -1) {
perror("Scanning unit file already exists or cannot be accessed\n");
free(unitpath);
printf("Scanning unit file already exists\n");
free(targetpath);
return 0;
}
// Check if we need to ignore the cache file
if (ignoreCache == 0) {
// Wir möchten die Zeile
cacheLine = malloc((strlen("ConditionPathExists=!/etc/zfs/zpool.cache") + 1) * sizeof(char));
strcpy(cacheLine, "ConditionPathExists=!/etc/zfs/zpool.cache");
} else {
Expand All @@ -153,9 +160,10 @@ int generateScanUnit(char *directory, const char *targetName, const char *unitNa
// Write
fp = fopen(unitpath, "w");
if (fp == NULL) {
perror("Can not write to scanning unit file\n");
free(unitpath);
free(cacheLine);
fprintf(stderr, "Can not write to scanning unit file\n");
free(targetpath);
return 1;
}
fprintf(fp, "[Unit]\n\
Expand All @@ -175,6 +183,7 @@ ExecStart=/usr/bin/zpool import %s -N -o cachefile=none%s\n", cacheLine, poolNam

free(cacheLine);
free(unitpath);
free(targetpath);
return 0;
}

Expand All @@ -193,22 +202,29 @@ int generateCacheUnit(char *directory, const char *targetName, const char *unitN
strcat(unitpath, "/");
strcat(unitpath, unitName);
// Make wants directory
mkdir(targetpath, 0775);
if (mkdir(targetpath, 0775) < 0 && errno != EEXIST) {
perror("Can not create unit directory\n");
free(targetpath);
free(unitpath);
return(1);
}
// Make symlink
strcat(targetpath, "/");
strcat(targetpath, unitName);
symlink(unitName, targetpath);
// Check if unit already exists
if (access(unitpath, R_OK) != -1) {
free(unitpath);
free(targetpath);
printf("Caching unit file already exists\n");
return 0;
}
// Write
fp = fopen(unitpath, "w");
if (fp == NULL) {
perror("Cannot write to scanning unit file\n");
free(unitpath);
fprintf(stderr, "Can not write to scanning unit file\n");
free(targetpath);
return 1;
}
fprintf(fp, "[Unit]\n\
Expand All @@ -226,6 +242,7 @@ RemainAfterExit=yes\n\
ExecStart=/usr/bin/zpool import %s -N -c /etc/zfs/zpool.cache%s\n", poolName, forceParam);
fclose(fp);
free(unitpath);
free(targetpath);

return 0;
}
Expand All @@ -244,13 +261,17 @@ int generateSysrootUnit(char *directory, int bootfs, char *dataset, char *snapsh
strcat(unitpath, "/");
strcat(unitpath, targetName);
// Make dropin directory
mkdir(unitpath, 0775);
if (mkdir(unitpath, 0775) < 0 && errno != EEXIST) {
perror("Can not create unit directory\n");
free(unitpath);
return(1);
}
strcat(unitpath, "/");
strcat(unitpath, unitName);
// Check if unit already exists
if (access(unitpath, R_OK) != -1) {
perror("Mounting unit file already exists\n");
free(unitpath);
printf("Mounting unit file already exists\n");
return 0;
}

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

// Write
fp = fopen(unitpath, "w");
if (fp == NULL) {
perror("Can not write to mounting unit file\n");
free(unitpath);
free(options);
free(what);
fprintf(stderr, "Can not write to mounting unit file\n");
return 1;
}
fprintf(fp, "[Mount]\n\
Expand Down
17 changes: 13 additions & 4 deletions src/zfs-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ int execute(char *command, char needOutput, char **output, char *param[]) {
char *linebuffer;
size_t size;
int nRead;
int fd;

// Execute
if (needOutput == 1) {
Expand All @@ -33,15 +34,22 @@ int execute(char *command, char needOutput, char **output, char *param[]) {
close(2);
if (needOutput == 1) {
close(pip[0]);
dup(pip[1]);
fd = dup(pip[1]);
if (fd < 0) {
perror("Can not duplicate pipe\n");
close(pip[1]);
}
}
// Execute
execv(command, param);
close(fd);
exit(254);
} else if (pid < 0) {
fprintf(stderr, "Can not fork\n");
close(pip[0]);
close(pip[1]);
perror("Can not fork\n");
if (needOutput == 1) {
close(pip[0]);
close(pip[1]);
}
return pid;
}
if (needOutput == 1) {
Expand Down Expand Up @@ -249,6 +257,7 @@ int zfs_list_snapshots(char *dataset, char *snapshot, char **output) {
}
tok = strtok(NULL, "\n");
}
free(suffix);
free(snaps);
return 0;
}
Expand Down

0 comments on commit c96a1de

Please sign in to comment.