sshfs-map: fix segfaults under musl.

- line and line_size should be reset each loop, otherwise getline() will
use a freed memory address. This worked under glibc, but broke under
musl for some reason. More specifically, it would segfault when running
setmntent for the second time.

- General clean-up: is_mounted checks shouldn't call continue, otherwise
they will skip the clean-up routines. This would be a memory leak, but
not a functionality issue. The calls to continue were probably the
reason that the program didn't segfault when running for a consecutive
time, after the first map had already been mounted.

- Move printf calls to outside fork block.
This commit is contained in:
Érico Rolim 2020-07-07 17:35:21 -03:00
parent 827ad9943c
commit fed9677104
1 changed files with 22 additions and 22 deletions

View File

@ -31,11 +31,12 @@ int main(int argc, char **argv)
exit(1);
}
char *line = NULL;
size_t line_size = 0;
int i = 0;
while (true) {
char *line = NULL;
size_t line_size = 0;
size_t result = getline(&line, &line_size, map);
// EOF
if (result == -1) {
@ -52,7 +53,6 @@ int main(int argc, char **argv)
// guarantee null-terminated terms
if (line[result - 1] == '\n') line[result - 1] = '\0';
char *source, *dest, *full_dest;
source = dest = full_dest = NULL;
sscanf(line, "%ms %ms", &source, &dest);
@ -85,36 +85,36 @@ int main(int argc, char **argv)
switch (mode) {
pid_t p;
case SSHFS_MAP:
if (is_mounted) {
printf("%s is already mounted\n", full_dest);
continue;
}
p = fork();
if (p == 0) {
printf("%s <=> %s\n", source, full_dest);
fflush(stdout);
char *args[] = {"sshfs", source, full_dest, NULL};
execvp("sshfs", args);
} else {
int wstatus;
waitpid(p, &wstatus, 0);
printf("%s <=> %s\n", source, full_dest);
p = fork();
if (p == 0) {
char *args[] = {"sshfs", "-o", "follow_symlinks", source, full_dest, NULL};
execvp("sshfs", args);
} else {
int wstatus;
waitpid(p, &wstatus, 0);
}
}
break;
case SSHFS_UNMAP:
if (!is_mounted) {
printf("%s isn't mounted\n", full_dest);
continue;
}
p = fork();
if (p == 0) {
char *args[] = {"umount", full_dest, NULL};
execvp("umount", args);
} else {
int wstatus;
waitpid(p, &wstatus, 0);
p = fork();
if (p == 0) {
char *args[] = {"umount", full_dest, NULL};
execvp("umount", args);
} else {
int wstatus;
waitpid(p, &wstatus, 0);
}
}
break;
}