diff --git a/Makefile b/Makefile index e143a12..d950036 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/src/cmdline.c b/src/cmdline.c index 4f628cd..9a147c6 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -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; } diff --git a/src/mount.initrd_zfs.c b/src/mount.initrd_zfs.c index 5df53b0..091c492 100644 --- a/src/mount.initrd_zfs.c +++ b/src/mount.initrd_zfs.c @@ -19,9 +19,7 @@ int handleBootfs(char **pool) { } ret = zfs_get_bootfs(rpool, pool); - if (rpool != NULL) { - free(rpool); - } + free(rpool); return ret; } @@ -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; @@ -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; @@ -100,6 +97,11 @@ int main(int argc, char *argv[]) { fprintf(stderr, "Call like: %s [-o option[,...]]\n", argv[0]); exit(1); } + // Check if all parameters are supplied + if (dataset == NULL || mountpoint == NULL) { + fprintf(stderr, "Call like: %s [-o option[,...]]\n", argv[0]); + exit(1); + } // Get rid of the trailing comma if (options != NULL) { newoptions = malloc(sizeof(options) - sizeof(char)); @@ -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); } } @@ -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, '@'); @@ -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); @@ -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); @@ -241,10 +239,7 @@ int main(int argc, char *argv[]) { } free(lines); - free(dataset); free(mountpoint); - if (options != NULL) { - free(options); - } + free(options); } diff --git a/src/zfs-generator.c b/src/zfs-generator.c index 7e916e3..f67570c 100644 --- a/src/zfs-generator.c +++ b/src/zfs-generator.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -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; @@ -86,6 +87,7 @@ int getForce(char **forceParam) { *forceParam = malloc(4 * sizeof(char)); strcpy(*forceParam, " -f"); } + free(forceval); return 0; } @@ -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 { @@ -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\ @@ -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; } @@ -193,7 +202,12 @@ 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); @@ -201,14 +215,16 @@ int generateCacheUnit(char *directory, const char *targetName, const char *unitN // 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\ @@ -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; } @@ -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; } @@ -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\ diff --git a/src/zfs-util.c b/src/zfs-util.c index ecbcceb..e0c0a72 100644 --- a/src/zfs-util.c +++ b/src/zfs-util.c @@ -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) { @@ -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) { @@ -249,6 +257,7 @@ int zfs_list_snapshots(char *dataset, char *snapshot, char **output) { } tok = strtok(NULL, "\n"); } + free(suffix); free(snaps); return 0; }