Ben Hutchings
2016-Jan-06 01:08 UTC
[klibc] [PATCH klibc 0/3] Changes to support initramfs-tools 0.117
initramfs-tools version 0.117 requires 'readlink -f' and 'mount -o defaults' to work. The first two patches were previously submitted but not applied. Ben. Ben Hutchings (3): Implement realpath() readlink: Add -f option mount: Implement -o defaults usr/include/stdlib.h | 2 ++ usr/klibc/Kbuild | 2 +- usr/klibc/realpath.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ usr/utils/mount_opts.c | 9 +++++++-- usr/utils/readlink.c | 29 +++++++++++++++++++++++++---- 5 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 usr/klibc/realpath.c -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 811 bytes Desc: Digital signature URL: <http://www.zytor.com/pipermail/klibc/attachments/20160106/8fe3dded/attachment.sig>
This is needed as the basis for the readlink -f option. Signed-off-by: Ben Hutchings <ben at decadent.org.uk> --- usr/include/stdlib.h | 2 ++ usr/klibc/Kbuild | 2 +- usr/klibc/realpath.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 usr/klibc/realpath.c diff --git a/usr/include/stdlib.h b/usr/include/stdlib.h index 406f446..856c647 100644 --- a/usr/include/stdlib.h +++ b/usr/include/stdlib.h @@ -92,4 +92,6 @@ static __inline__ int grantpt(int __fd) return 0; /* devpts does this all for us! */ } +__extern char *realpath(const char *, char *); + #endif /* _STDLIB_H */ diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild index d3e2b9f..7d95e87 100644 --- a/usr/klibc/Kbuild +++ b/usr/klibc/Kbuild @@ -61,7 +61,7 @@ klib-y += vsnprintf.o snprintf.o vsprintf.o sprintf.o \ send.o recv.o \ access.o chmod.o chown.o dup2.o mknod.o poll.o rename.o stat.o \ lchown.o link.o rmdir.o unlink.o utimes.o lstat.o mkdir.o \ - readlink.o select.o symlink.o pipe.o \ + readlink.o realpath.o select.o symlink.o pipe.o \ ctype/isalnum.o ctype/isalpha.o ctype/isascii.o \ ctype/isblank.o ctype/iscntrl.o ctype/isdigit.o \ ctype/isgraph.o ctype/islower.o ctype/isprint.o \ diff --git a/usr/klibc/realpath.c b/usr/klibc/realpath.c new file mode 100644 index 0000000..1474b1e --- /dev/null +++ b/usr/klibc/realpath.c @@ -0,0 +1,49 @@ +#include <fcntl.h> +#include <limits.h> +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <unistd.h> + +/* + * Note that this requires name to refer to an existing file. This is + * correct according to POSIX. However, BSD and GNU implementations + * also allow name to refer to a non-existing file in an existing + * directory. + */ + +char *realpath(const char *name, char *resolved_name) +{ + static const char proc_fd_prefix[] = "/proc/self/fd/"; + char proc_fd_name[sizeof(proc_fd_prefix) + sizeof(int) * 3]; + int allocated = 0; + int fd; + ssize_t len; + + /* Open for path lookup only */ + fd = open(name, O_PATH); + if (fd < 0) + return NULL; + + if (!resolved_name) { + resolved_name = malloc(PATH_MAX); + if (!resolved_name) + goto out_close; + allocated = 1; + } + + /* Use procfs to read back the resolved name */ + sprintf(proc_fd_name, "%s%d", proc_fd_prefix, fd); + len = readlink(proc_fd_name, resolved_name, PATH_MAX - 1); + if (len < 0) { + if (allocated) + free(resolved_name); + resolved_name = NULL; + } else { + resolved_name[len] = 0; + } + +out_close: + close(fd); + return resolved_name; +} -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 811 bytes Desc: Digital signature URL: <http://www.zytor.com/pipermail/klibc/attachments/20160106/1d1632c0/attachment.sig>
This is needed to support mounting non-root filesystems in initramfs-tools. Signed-off-by: Ben Hutchings <ben at decadent.org.uk> --- usr/utils/readlink.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/usr/utils/readlink.c b/usr/utils/readlink.c index 4e3cfcb..ffb0b1f 100644 --- a/usr/utils/readlink.c +++ b/usr/utils/readlink.c @@ -7,24 +7,45 @@ const char *progname; static __noreturn usage(void) { - fprintf(stderr, "Usage: %s link...\n", progname); + fprintf(stderr, "Usage: %s [-f] link...\n", progname); exit(1); } int main(int argc, char *argv[]) { + int c, f_flag = 0; const char *name; char link_name[PATH_MAX]; int rv; int i; - progname = *argv++; + progname = argv[0]; - if (argc < 2) + do { + c = getopt(argc, argv, "f"); + if (c == EOF) + break; + switch (c) { + case 'f': + f_flag = 1; + break; + + case '?': + fprintf(stderr, "%s: invalid option -%c\n", + progname, optopt); + usage(); + } + } while (1); + + if (optind == argc) usage(); + argv += optind; while ((name = *argv++)) { - rv = readlink(name, link_name, sizeof link_name - 1); + if (f_flag) + rv = realpath(name, link_name) ? strlen(link_name) : -1; + else + rv = readlink(name, link_name, sizeof link_name - 1); if (rv < 0) { perror(name); exit(1); -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 811 bytes Desc: Digital signature URL: <http://www.zytor.com/pipermail/klibc/attachments/20160106/1d26eb96/attachment.sig>
Ben Hutchings
2016-Jan-06 01:09 UTC
[klibc] [PATCH klibc 3/3] mount: Implement -o defaults
This is needed to support mounting non-root filesystems in initramfs-tools. Signed-off-by: Ben Hutchings <ben at decadent.org.uk> --- usr/utils/mount_opts.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/usr/utils/mount_opts.c b/usr/utils/mount_opts.c index 05d1729..bb26c7d 100644 --- a/usr/utils/mount_opts.c +++ b/usr/utils/mount_opts.c @@ -89,8 +89,13 @@ parse_mount_options(char *arg, unsigned long rwflag, struct extra_opts *extra) break; } - if (res != 0 && s[0]) - add_extra_option(extra, opt); + if (res != 0 && s[0]) { + if (!strcmp(opt, "defaults")) + rwflag &= ~(MS_RDONLY|MS_NOSUID|MS_NODEV| + MS_NOEXEC|MS_SYNCHRONOUS); + else + add_extra_option(extra, opt); + } } return rwflag; -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 811 bytes Desc: Digital signature URL: <http://www.zytor.com/pipermail/klibc/attachments/20160106/56400a52/attachment.sig>