mirror of https://github.com/ericonr/dotfiles.git
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:
parent
827ad9943c
commit
fed9677104
|
@ -31,11 +31,12 @@ int main(int argc, char **argv)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *line = NULL;
|
|
||||||
size_t line_size = 0;
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
char *line = NULL;
|
||||||
|
size_t line_size = 0;
|
||||||
|
|
||||||
size_t result = getline(&line, &line_size, map);
|
size_t result = getline(&line, &line_size, map);
|
||||||
// EOF
|
// EOF
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
|
@ -52,7 +53,6 @@ int main(int argc, char **argv)
|
||||||
// guarantee null-terminated terms
|
// guarantee null-terminated terms
|
||||||
if (line[result - 1] == '\n') line[result - 1] = '\0';
|
if (line[result - 1] == '\n') line[result - 1] = '\0';
|
||||||
|
|
||||||
|
|
||||||
char *source, *dest, *full_dest;
|
char *source, *dest, *full_dest;
|
||||||
source = dest = full_dest = NULL;
|
source = dest = full_dest = NULL;
|
||||||
sscanf(line, "%ms %ms", &source, &dest);
|
sscanf(line, "%ms %ms", &source, &dest);
|
||||||
|
@ -85,36 +85,36 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
pid_t p;
|
pid_t p;
|
||||||
|
|
||||||
case SSHFS_MAP:
|
case SSHFS_MAP:
|
||||||
if (is_mounted) {
|
if (is_mounted) {
|
||||||
printf("%s is already mounted\n", full_dest);
|
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 {
|
} else {
|
||||||
int wstatus;
|
printf("%s <=> %s\n", source, full_dest);
|
||||||
waitpid(p, &wstatus, 0);
|
|
||||||
|
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;
|
break;
|
||||||
|
|
||||||
case SSHFS_UNMAP:
|
case SSHFS_UNMAP:
|
||||||
if (!is_mounted) {
|
if (!is_mounted) {
|
||||||
printf("%s isn't mounted\n", full_dest);
|
printf("%s isn't mounted\n", full_dest);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
p = fork();
|
|
||||||
if (p == 0) {
|
|
||||||
char *args[] = {"umount", full_dest, NULL};
|
|
||||||
execvp("umount", args);
|
|
||||||
} else {
|
} else {
|
||||||
int wstatus;
|
p = fork();
|
||||||
waitpid(p, &wstatus, 0);
|
if (p == 0) {
|
||||||
|
char *args[] = {"umount", full_dest, NULL};
|
||||||
|
execvp("umount", args);
|
||||||
|
} else {
|
||||||
|
int wstatus;
|
||||||
|
waitpid(p, &wstatus, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue