Clean up minor issues in sysinstall(8).

This commit is contained in:
Jonas 'Sortie' Termansen 2021-01-15 21:22:05 +01:00
parent 69513b4396
commit 84b008e455
5 changed files with 45 additions and 23 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2016 Jonas 'Sortie' Termansen. * Copyright (c) 2015, 2016, 2021 Jonas 'Sortie' Termansen.
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -69,7 +69,8 @@ void unscan_filesystem(struct blockdevice* bdev)
void scan_filesystem(struct blockdevice* bdev) void scan_filesystem(struct blockdevice* bdev)
{ {
enum filesystem_error fserr = blockdevice_inspect_filesystem(&bdev->fs, bdev); enum filesystem_error fserr =
blockdevice_inspect_filesystem(&bdev->fs, bdev);
if ( fserr == FILESYSTEM_ERROR_ABSENT || if ( fserr == FILESYSTEM_ERROR_ABSENT ||
fserr == FILESYSTEM_ERROR_UNRECOGNIZED ) fserr == FILESYSTEM_ERROR_UNRECOGNIZED )
return; return;
@ -94,7 +95,8 @@ void scan_device(struct harddisk* hd)
{ {
unscan_device(hd); unscan_device(hd);
struct blockdevice* bdev = &hd->bdev; struct blockdevice* bdev = &hd->bdev;
enum partition_error parterr = blockdevice_get_partition_table(&bdev->pt, bdev); enum partition_error parterr =
blockdevice_get_partition_table(&bdev->pt, bdev);
if ( parterr == PARTITION_ERROR_ABSENT || if ( parterr == PARTITION_ERROR_ABSENT ||
parterr == PARTITION_ERROR_UNRECOGNIZED ) parterr == PARTITION_ERROR_UNRECOGNIZED )
{ {
@ -261,9 +263,14 @@ bool load_mountpoints(const char* fstab_path,
FILE* fp = fopen(fstab_path, "r"); FILE* fp = fopen(fstab_path, "r");
if ( !fp ) if ( !fp )
return false; return false;
struct mountpoint* mountpoints = NULL; struct mountpoint* mountpoints = malloc(sizeof(struct mountpoint));
if ( !mountpoints )
{
fclose(fp);
return false;
}
size_t mountpoints_used = 0; size_t mountpoints_used = 0;
size_t mountpoints_length = 0; size_t mountpoints_length = 1;
char* line = NULL; char* line = NULL;
size_t line_size; size_t line_size;
ssize_t line_length; ssize_t line_length;
@ -276,11 +283,9 @@ bool load_mountpoints(const char* fstab_path,
continue; continue;
if ( mountpoints_used == mountpoints_length ) if ( mountpoints_used == mountpoints_length )
{ {
size_t new_length = 2 * mountpoints_length;
if ( !new_length )
new_length = 16;
struct mountpoint* new_mountpoints = (struct mountpoint*) struct mountpoint* new_mountpoints = (struct mountpoint*)
reallocarray(mountpoints, new_length, sizeof(struct mountpoint)); reallocarray(mountpoints, mountpoints_length,
2 * sizeof(struct mountpoint));
if ( !new_mountpoints ) if ( !new_mountpoints )
{ {
free_mountpoints(mountpoints, mountpoints_used); free_mountpoints(mountpoints, mountpoints_used);
@ -289,7 +294,7 @@ bool load_mountpoints(const char* fstab_path,
return false; return false;
} }
mountpoints = new_mountpoints; mountpoints = new_mountpoints;
mountpoints_length = new_length; mountpoints_length *= 2;
} }
struct mountpoint* mountpoint = &mountpoints[mountpoints_used++]; struct mountpoint* mountpoint = &mountpoints[mountpoints_used++];
memset(mountpoint, 0, sizeof(*mountpoint)); memset(mountpoint, 0, sizeof(*mountpoint));
@ -326,7 +331,7 @@ bool mountpoint_mount(struct mountpoint* mountpoint)
// TODO: It would be ideal to get an exclusive lock so that no other // TODO: It would be ideal to get an exclusive lock so that no other
// processes have currently mounted that filesystem. // processes have currently mounted that filesystem.
struct blockdevice* bdev = fs->bdev; struct blockdevice* bdev = fs->bdev;
const char* bdev_path = bdev->p ? bdev->p->path : bdev->hd->path; const char* bdev_path = path_of_blockdevice(bdev);
assert(bdev_path); assert(bdev_path);
if ( fs->flags & FILESYSTEM_FLAG_FSCK_MUST && !fsck(fs) ) if ( fs->flags & FILESYSTEM_FLAG_FSCK_MUST && !fsck(fs) )
{ {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2016, 2017, 2020 Jonas 'Sortie' Termansen. * Copyright (c) 2015, 2016, 2017, 2020, 2021 Jonas 'Sortie' Termansen.
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -55,7 +55,7 @@ int mkdir_p(const char* path, mode_t mode)
char* prev = strdup(path); char* prev = strdup(path);
if ( !prev ) if ( !prev )
return -1; return -1;
int status = mkdir_p(dirname(prev), mode | 0500); int status = mkdir_p(dirname(prev), mode | 0500);
free(prev); free(prev);
if ( status < 0 ) if ( status < 0 )
return -1; return -1;
@ -92,9 +92,11 @@ void mkdir_or_chmod_or_die(const char* path, mode_t mode)
{ {
if ( chmod(path, mode) == 0 ) if ( chmod(path, mode) == 0 )
return; return;
err(2, "chmod: %s", path); warn("chmod: %s", path);
_exit(2);
} }
err(2, "mkdir: %s", path); warn("mkdir: %s", path);
_exit(2);
} }
void write_random_seed(const char* path) void write_random_seed(const char* path)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, 2016, 2017 Jonas 'Sortie' Termansen. * Copyright (c) 2015, 2016, 2017, 2021 Jonas 'Sortie' Termansen.
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -91,7 +91,12 @@ static char* os_release_eval(const char* string)
doubly_quote = !doubly_quote; doubly_quote = !doubly_quote;
else else
{ {
fputc((unsigned char) c, fp); if ( fputc((unsigned char) c, fp) == EOF )
{
fclose(fp);
free(result);
return NULL;
}
escaped = false; escaped = false;
} }
} }

View File

@ -135,6 +135,7 @@ static bool should_install_bootloader_path(const char* mnt,
} }
free(release_path); free(release_path);
free(release_errpath); free(release_errpath);
release_free(&release);
char* conf_path; char* conf_path;
if ( asprintf(&conf_path, "%s/etc/upgrade.conf", mnt) < 0 ) if ( asprintf(&conf_path, "%s/etc/upgrade.conf", mnt) < 0 )
{ {
@ -402,6 +403,8 @@ int main(void)
if ( !mkdtemp(etc) ) if ( !mkdtemp(etc) )
err(2, "mkdtemp: %s", "/tmp/etc.XXXXXX"); err(2, "mkdtemp: %s", "/tmp/etc.XXXXXX");
etc_made = true; etc_made = true;
// Export for the convenience of users escaping to a shell.
setenv("SYSINSTALL_ETC", fs, 1);
if ( chdir(etc) < 0 ) if ( chdir(etc) < 0 )
err(2, "chdir: %s", etc); err(2, "chdir: %s", etc);
@ -859,6 +862,7 @@ int main(void)
if ( !mkdtemp(fs) ) if ( !mkdtemp(fs) )
err(2, "mkdtemp: %s", "/tmp/fs.XXXXXX"); err(2, "mkdtemp: %s", "/tmp/fs.XXXXXX");
fs_made = true; fs_made = true;
// Export for the convenience of users escaping to a shell.
setenv("SYSINSTALL_TARGET", fs, 1); setenv("SYSINSTALL_TARGET", fs, 1);
for ( size_t i = 0; i < mountpoints_used; i++ ) for ( size_t i = 0; i < mountpoints_used; i++ )

View File

@ -142,6 +142,7 @@ static void search_installation_path(const char* mnt, struct blockdevice* bdev)
if ( asprintf(&machine_path, "%s/etc/machine", mnt) < 0 ) if ( asprintf(&machine_path, "%s/etc/machine", mnt) < 0 )
{ {
warn("%s: malloc", path_of_blockdevice(bdev)); warn("%s: malloc", path_of_blockdevice(bdev));
free_mountpoints(mountpoints, mountpoints_used);
release_free(&release); release_free(&release);
return; return;
} }
@ -150,12 +151,14 @@ static void search_installation_path(const char* mnt, struct blockdevice* bdev)
if ( !machine ) if ( !machine )
{ {
warn("%s/etc/machine", path_of_blockdevice(bdev)); warn("%s/etc/machine", path_of_blockdevice(bdev));
free_mountpoints(mountpoints, mountpoints_used);
release_free(&release); release_free(&release);
return; return;
} }
if ( !add_installation(bdev, &release, mountpoints, mountpoints_used, if ( !add_installation(bdev, &release, mountpoints, mountpoints_used,
machine) ) machine) )
{ {
free(machine);
free_mountpoints(mountpoints, mountpoints_used); free_mountpoints(mountpoints, mountpoints_used);
release_free(&release); release_free(&release);
return; return;
@ -278,7 +281,7 @@ static void preserve_src(const char* where)
if ( mkdir("oldsrc", 0755) < 0 ) if ( mkdir("oldsrc", 0755) < 0 )
{ {
warn("oldsrc"); warn("oldsrc");
_exit(1); _exit(2);
} }
} }
time_t now = time(NULL); time_t now = time(NULL);
@ -298,7 +301,7 @@ static void preserve_src(const char* where)
if ( !mkdtemp(buf) ) if ( !mkdtemp(buf) )
{ {
warnx("failed to find location to store old /%s", where); warnx("failed to find location to store old /%s", where);
_exit(1); _exit(2);
} }
rmdir(buf); rmdir(buf);
} }
@ -307,7 +310,7 @@ static void preserve_src(const char* where)
if ( rename(where, buf) < 0 ) if ( rename(where, buf) < 0 )
{ {
warn("rename: %s -> %s", where, buf); warn("rename: %s -> %s", where, buf);
_exit(1); _exit(2);
} }
} }
@ -502,6 +505,9 @@ int main(void)
if ( !mkdtemp(fs) ) if ( !mkdtemp(fs) )
err(2, "mkdtemp: %s", "/tmp/fs.XXXXXX"); err(2, "mkdtemp: %s", "/tmp/fs.XXXXXX");
fs_made = true;
// Export for the convenience of users escaping to a shell.
setenv("SYSINSTALL_TARGET", fs, 1);
struct installation* target = NULL; struct installation* target = NULL;
while ( true ) while ( true )
@ -817,7 +823,7 @@ int main(void)
if ( rename("src", "src.tmp") < 0 ) if ( rename("src", "src.tmp") < 0 )
{ {
warn("rename: /src -> /src.tmp"); warn("rename: /src -> /src.tmp");
_exit(1); _exit(2);
} }
} }
install_manifest("src", "", ".", (const char*[]){}, 0); install_manifest("src", "", ".", (const char*[]){}, 0);
@ -826,12 +832,12 @@ int main(void)
if ( rename("src", "newsrc") < 0 ) if ( rename("src", "newsrc") < 0 )
{ {
warn("rename: /src -> /newsrc"); warn("rename: /src -> /newsrc");
_exit(1); _exit(2);
} }
if ( rename("src.tmp", "src") < 0 ) if ( rename("src.tmp", "src") < 0 )
{ {
warn("rename: /src.tmp -> /src"); warn("rename: /src.tmp -> /src");
_exit(1); _exit(2);
} }
} }
} }