maximilian attems
2008-Sep-07 08:32 UTC
[klibc] [git pull v2] initial mntent.h, mount features, ipconfig fixes
[ gsi.de pretends this is massmailing so removed from CC's ] hello hpa, please review and on ack merge belows patchset. changes from v1, thanks to review by kzak: * add strtok_r() * use it in getmntent_r() * getment_r() parses mnt_freq and mnt_passno too isn't that hard. thanks maks git pull git://git.debian.org/~maks/klibc.git maks for the changes: maximilian attems (9): [klibc] add strtok_r() [klibc] Add initial mntent.h with setmntent() and endmntent() [klibc] add getmntent() [klibc] mount: add help arg for usage() [klibc] mount: list all mounted file systems [klibc] mount: list fs of particular type [klibc] mount: read /proc/mounts preferably [klibc] ipconfig: raise field length for rootpath DHCP option [klibc] ipconfig: set null ciaddr on DHCPREQUEST during SELECTING state git diff --stat --summary master...maks usr/include/mntent.h | 19 +++++++++++ usr/include/string.h | 1 + usr/kinit/ipconfig/dhcp_proto.c | 3 +- usr/kinit/ipconfig/netdev.h | 2 +- usr/klibc/Kbuild | 4 ++- usr/klibc/endmntent.c | 9 +++++ usr/klibc/getmntent.c | 61 +++++++++++++++++++++++++++++++++++++ usr/klibc/setmntent.c | 7 ++++ usr/klibc/strtok.c | 9 +----- usr/klibc/strtok_r.c | 13 ++++++++ usr/utils/mount_main.c | 64 +++++++++++++++++++++++++++++++------- 11 files changed, 169 insertions(+), 23 deletions(-) create mode 100644 usr/include/mntent.h create mode 100644 usr/klibc/endmntent.c create mode 100644 usr/klibc/getmntent.c create mode 100644 usr/klibc/setmntent.c create mode 100644 usr/klibc/strtok_r.c commit cfc8d649a959f845983aae4c0fc33a0a0c995c92 Author: maximilian attems <max at stro.at> Date: Fri Sep 5 22:48:27 2008 +0200 [klibc] ipconfig: set null ciaddr on DHCPREQUEST during SELECTING state RFC 2131, Section 4.3.2 states: Clients send DHCPREQUEST messages as follows: o DHCPREQUEST generated during SELECTING state: Client inserts the address of the selected server in 'server identifier', 'ciaddr' MUST be zero, 'requested IP address' MUST be filled in with the yiaddr value from the chosen DHCPOFFER. fixes: http://bugs.debian.org/497879 my test dhcpd seem all not that picky, ipconfig worked before and after this RFC 2131 conformal change. Reported-by: Craig Bernstein <cbernstein at stanford.edu> Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/kinit/ipconfig/dhcp_proto.c b/usr/kinit/ipconfig/dhcp_proto.c index d4f2c09..21448f7 100644 --- a/usr/kinit/ipconfig/dhcp_proto.c +++ b/usr/kinit/ipconfig/dhcp_proto.c @@ -171,7 +171,8 @@ static int dhcp_send(struct netdev *dev, struct iovec *vec) bootp.htype = dev->hwtype; bootp.hlen = dev->hwlen; bootp.xid = dev->bootp.xid; - bootp.ciaddr = dev->ip_addr; + bootp.ciaddr = INADDR_NONE; + bootp.yiaddr = dev->ip_addr; bootp.giaddr = INADDR_ANY; bootp.secs = htons(time(NULL) - dev->open_time); memcpy(bootp.chaddr, dev->hwaddr, 16); commit 848a8ce349331cd74021cc3b8ea43f9e47c14e50 Author: maximilian attems <max at stro.at> Date: Fri Sep 5 22:38:22 2008 +0200 [klibc] ipconfig: raise field length for rootpath DHCP option ipconfig would cut off after 40 bytes, use 256 bytes. This is indeed way too short for dotted-decimal IP adresses up to 15 byes long and several NFS mount options. fixes http://bugs.debian.org/497800 Reported-by: "Christopher Huhn, GSI" <C.Huhn at gsi.de> Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/kinit/ipconfig/netdev.h b/usr/kinit/ipconfig/netdev.h index fb6640a..1091943 100644 --- a/usr/kinit/ipconfig/netdev.h +++ b/usr/kinit/ipconfig/netdev.h @@ -4,7 +4,7 @@ #include <sys/utsname.h> #include <net/if.h> -#define BPLEN 40 +#define BPLEN 256 #define FNLEN 128 /* from DHCP RFC 2131 */ struct netdev { commit 6fa665bc70d5f87282a3518595e39cc6cac99050 Author: maximilian attems <max at stro.at> Date: Fri Sep 5 22:18:09 2008 +0200 [klibc] mount: read /proc/mounts preferably as klibc mount doesn't write into /etc/mtab (no addmntent() too), better checkout the kernel view first. may be interchanged again later.. Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/utils/mount_main.c b/usr/utils/mount_main.c index 89e75d7..ee08720 100644 --- a/usr/utils/mount_main.c +++ b/usr/utils/mount_main.c @@ -31,9 +31,9 @@ static __noreturn print_mount(char *type) FILE *mfp; struct mntent *mnt; - mfp = setmntent(_PATH_MOUNTED, "r"); + mfp = setmntent(_PATH_PROC_MOUNTS, "r"); if (!mfp) - mfp = setmntent(_PATH_PROC_MOUNTS, "r"); + mfp = setmntent(_PATH_MOUNTED, "r"); if (!mfp) perror("setmntent"); commit 8b2442a927d0141dbf06289d20486e0e631ebec7 Author: maximilian attems <max at stro.at> Date: Fri Sep 5 22:13:21 2008 +0200 [klibc] mount: list fs of particular type makes possible: mount -t squashfs pass type of the fs down to print_mount(). fixes http://bugs.debian.org/491067 requested for casper live cd initramfs. Cc: Colin Watson <cjwatson at ubuntu.com> Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/utils/mount_main.c b/usr/utils/mount_main.c index 2616e46..89e75d7 100644 --- a/usr/utils/mount_main.c +++ b/usr/utils/mount_main.c @@ -26,7 +26,7 @@ static __noreturn usage(void) exit(1); } -static __noreturn print_mount(void) +static __noreturn print_mount(char *type) { FILE *mfp; struct mntent *mnt; @@ -40,6 +40,8 @@ static __noreturn print_mount(void) while ((mnt = getmntent(mfp)) != NULL) { if (mnt->mnt_fsname && !strncmp(mnt->mnt_fsname, "no", 2)) continue; + if (type && mnt->mnt_type && strcmp(type, mnt->mnt_type)) + continue; printf("%s on %s", mnt->mnt_fsname, mnt->mnt_dir); if (mnt->mnt_type != NULL && mnt->mnt_type != '\0') printf (" type %s", mnt->mnt_type); @@ -136,6 +138,9 @@ int main(int argc, char *argv[]) } } while (1); + if (optind == argc) + print_mount(type); + /* * If remount, bind or move was specified, then we don't * have a "type" as such. Use the dummy "none" type. @@ -143,9 +148,6 @@ int main(int argc, char *argv[]) if (rwflag & MS_TYPE) type = "none"; - if (optind == argc) - print_mount(); - if (optind + 2 != argc || type == NULL) usage(); commit f33a64fe457ccfe74f5d993d025a71ebf7a417c4 Author: maximilian attems <max at stro.at> Date: Fri Sep 5 22:02:21 2008 +0200 [klibc] mount: list all mounted file systems plain mount(8) invocation without arguments. Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/utils/mount_main.c b/usr/utils/mount_main.c index a15ae3d..2616e46 100644 --- a/usr/utils/mount_main.c +++ b/usr/utils/mount_main.c @@ -7,9 +7,13 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <mntent.h> #include "mount_opts.h" +#define _PATH_MOUNTED "/etc/mtab" +#define _PATH_PROC_MOUNTS "/proc/mounts" + char *progname; static struct extra_opts extra; @@ -22,6 +26,31 @@ static __noreturn usage(void) exit(1); } +static __noreturn print_mount(void) +{ + FILE *mfp; + struct mntent *mnt; + + mfp = setmntent(_PATH_MOUNTED, "r"); + if (!mfp) + mfp = setmntent(_PATH_PROC_MOUNTS, "r"); + if (!mfp) + perror("setmntent"); + + while ((mnt = getmntent(mfp)) != NULL) { + if (mnt->mnt_fsname && !strncmp(mnt->mnt_fsname, "no", 2)) + continue; + printf("%s on %s", mnt->mnt_fsname, mnt->mnt_dir); + if (mnt->mnt_type != NULL && mnt->mnt_type != '\0') + printf (" type %s", mnt->mnt_type); + if (mnt->mnt_opts != NULL && mnt->mnt_opts != '\0') + printf (" (%s)", mnt->mnt_opts); + printf("\n"); + } + endmntent(mfp); + exit(0); +} + static int do_mount(char *dev, char *dir, char *type, unsigned long rwflag, void *data) { @@ -114,6 +143,9 @@ int main(int argc, char *argv[]) if (rwflag & MS_TYPE) type = "none"; + if (optind == argc) + print_mount(); + if (optind + 2 != argc || type == NULL) usage(); commit ccc39e2fb2b0133e56ba95217ee5ffed9ca901ac Author: maximilian attems <max at stro.at> Date: Fri Sep 5 09:18:27 2008 +0200 [klibc] mount: add help arg for usage() extract current usage() print out of main() and use it for help arg. as bonus alphabeticaly sort the arguments. Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/utils/mount_main.c b/usr/utils/mount_main.c index b5993cc..a15ae3d 100644 --- a/usr/utils/mount_main.c +++ b/usr/utils/mount_main.c @@ -15,6 +15,13 @@ char *progname; static struct extra_opts extra; static unsigned long rwflag; +static __noreturn usage(void) +{ + fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] [-f] [-i] " + "[-n] device directory\n", progname); + exit(1); +} + static int do_mount(char *dev, char *dir, char *type, unsigned long rwflag, void *data) { @@ -66,10 +73,18 @@ int main(int argc, char *argv[]) rwflag = MS_VERBOSE; do { - c = getopt(argc, argv, "no:rt:wfi"); + c = getopt(argc, argv, "fhino:rt:w"); if (c == EOF) break; switch (c) { + case 'f': + /* we can't edit /etc/mtab yet anyway; exit */ + exit(0); + case 'i': + /* ignore for now; no support for mount helpers */ + break; + case 'h': + usage(); case 'n': /* no mtab writing */ break; @@ -85,12 +100,6 @@ int main(int argc, char *argv[]) case 'w': rwflag &= ~MS_RDONLY; break; - case 'f': - /* we can't edit /etc/mtab yet anyway; exit */ - exit(0); - case 'i': - /* ignore for now; no support for mount helpers */ - break; case '?': fprintf(stderr, "%s: invalid option -%c\n", progname, optopt); @@ -105,11 +114,8 @@ int main(int argc, char *argv[]) if (rwflag & MS_TYPE) type = "none"; - if (optind + 2 != argc || type == NULL) { - fprintf(stderr, "Usage: %s [-r] [-w] [-o options] [-t type] [-f] [-i] " - "[-n] device directory\n", progname); - exit(1); - } + if (optind + 2 != argc || type == NULL) + usage(); return do_mount(argv[optind], argv[optind + 1], type, rwflag, extra.str); commit d2ad1cf968af5c938a0262914f764667a449ffe1 Author: maximilian attems <max at stro.at> Date: Sun Aug 24 00:48:12 2008 +0200 [klibc] add getmntent() internaly use reentrant getmntent_r(), can be exported on need. take care to use reentrant strtok_r() functions too. Thanks to Karel Zak <kzak at redhat.com> for pointing that out. getmntent is used in several places to check that a news fs won't be created on an already mounted partition. it is also needed for mount(8) to list mounted fs. Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/include/mntent.h b/usr/include/mntent.h index ca6b211..210610c 100644 --- a/usr/include/mntent.h +++ b/usr/include/mntent.h @@ -12,6 +12,8 @@ struct mntent { extern FILE *setmntent(const char *, const char *); +extern struct mntent *getmntent(FILE *); + extern int endmntent(FILE *fp); #endif /* mntent.h */ diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild index 842a79f..1f505c2 100644 --- a/usr/klibc/Kbuild +++ b/usr/klibc/Kbuild @@ -57,7 +57,7 @@ klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \ ctype/isxdigit.o ctype/tolower.o ctype/toupper.o \ userdb/getgrgid.o userdb/getgrnam.o userdb/getpwnam.o \ userdb/getpwuid.o userdb/root_group.o userdb/root_user.o \ - setmntent.o endmntent.o + setmntent.o endmntent.o getmntent.o klib-$(CONFIG_KLIBC_ERRLIST) += errlist.o diff --git a/usr/klibc/getmntent.c b/usr/klibc/getmntent.c new file mode 100644 index 0000000..8af27f3 --- /dev/null +++ b/usr/klibc/getmntent.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <mntent.h> + +#define BUFLEN 1024 + +struct mntent *getmntent_r(FILE *fp, struct mntent *mntbuf, char *buf, + int buflen) +{ + char *line = NULL, *saveptr = NULL; + const char *sep = " \t\n"; + + if (!fp || !mntbuf || !buf) + return NULL; + + while ((line = fgets(buf, buflen, fp)) != NULL) { + if (buf[0] == '#' || buf[0] == '\n') + continue; + break; + } + + if (!line) + return NULL; + + mntbuf->mnt_fsname = strtok_r(buf, sep, &saveptr); + if (!mntbuf->mnt_fsname) + return NULL; + + mntbuf->mnt_dir = strtok_r(NULL, sep, &saveptr); + if (!mntbuf->mnt_fsname) + return NULL; + + mntbuf->mnt_type = strtok_r(NULL, sep, &saveptr); + if (!mntbuf->mnt_type) + return NULL; + + mntbuf->mnt_opts = strtok_r(NULL, sep, &saveptr); + if (!mntbuf->mnt_opts) + mntbuf->mnt_opts = ""; + + line = strtok_r(NULL, sep, &saveptr); + mntbuf->mnt_freq = !line ? 0 : atoi(line); + + line = strtok_r(NULL, sep, &saveptr); + mntbuf->mnt_passno = !line ? 0 : atoi(line); + + return mntbuf; +} + +struct mntent *getmntent(FILE *fp) +{ + static char *buf = NULL; + static struct mntent mntbuf; + + buf = malloc(BUFLEN); + if (!buf) + perror("malloc"); + + return getmntent_r(fp, &mntbuf, buf, BUFLEN); +} commit 9452ae7af97f5d3af466104570a6a1dea5e973ad Author: maximilian attems <max at stro.at> Date: Sat Aug 16 00:47:10 2008 +0200 [klibc] Add initial mntent.h with setmntent() and endmntent() Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/include/mntent.h b/usr/include/mntent.h new file mode 100644 index 0000000..ca6b211 --- /dev/null +++ b/usr/include/mntent.h @@ -0,0 +1,17 @@ +#ifndef _MNTENT_H +#define _MNTENT_H 1 + +struct mntent { + char *mnt_fsname; /* name of mounted file system */ + char *mnt_dir; /* file system path prefix */ + char *mnt_type; /* mount type (see mntent.h) */ + char *mnt_opts; /* mount options (see mntent.h) */ + int mnt_freq; /* dump frequency in days */ + int mnt_passno; /* pass number on parallel fsck */ +}; + +extern FILE *setmntent(const char *, const char *); + +extern int endmntent(FILE *fp); + +#endif /* mntent.h */ diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild index eb04267..842a79f 100644 --- a/usr/klibc/Kbuild +++ b/usr/klibc/Kbuild @@ -56,7 +56,8 @@ klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \ ctype/ispunct.o ctype/isspace.o ctype/isupper.o \ ctype/isxdigit.o ctype/tolower.o ctype/toupper.o \ userdb/getgrgid.o userdb/getgrnam.o userdb/getpwnam.o \ - userdb/getpwuid.o userdb/root_group.o userdb/root_user.o + userdb/getpwuid.o userdb/root_group.o userdb/root_user.o \ + setmntent.o endmntent.o klib-$(CONFIG_KLIBC_ERRLIST) += errlist.o diff --git a/usr/klibc/endmntent.c b/usr/klibc/endmntent.c new file mode 100644 index 0000000..419c317 --- /dev/null +++ b/usr/klibc/endmntent.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <mntent.h> + +int endmntent(FILE *fp) +{ + if (fp) + fclose(fp); + return 1; +} diff --git a/usr/klibc/setmntent.c b/usr/klibc/setmntent.c new file mode 100644 index 0000000..d23e141 --- /dev/null +++ b/usr/klibc/setmntent.c @@ -0,0 +1,7 @@ +#include <stdio.h> +#include <mntent.h> + +FILE *setmntent(const char *filename, const char *type) +{ + return fopen(filename, type); +} commit 6f42216923b6d1bb4924d6e2c9117a226d40c2f3 Author: maximilian attems <max at stro.at> Date: Sun Sep 7 10:01:28 2008 +0200 [klibc] add strtok_r() reentrant version of strtok() was missing in klibc, use the current strtok() implementation and make use of it in strtok() itself. Signed-off-by: maximilian attems <max at stro.at> diff --git a/usr/include/string.h b/usr/include/string.h index ae8270e..0c8c046 100644 --- a/usr/include/string.h +++ b/usr/include/string.h @@ -44,5 +44,6 @@ __extern char *strsep(char **, const char *); __extern size_t strspn(const char *, const char *); __extern char *strstr(const char *, const char *); __extern char *strtok(char *, const char *); +__extern char *strtok_r(char *, const char *, char **); #endif /* _STRING_H */ diff --git a/usr/klibc/Kbuild b/usr/klibc/Kbuild index c8eabf9..eb04267 100644 --- a/usr/klibc/Kbuild +++ b/usr/klibc/Kbuild @@ -36,6 +36,7 @@ klib-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \ strncat.o strlcpy.o strlcat.o \ strstr.o strncmp.o strncpy.o strrchr.o \ strxspn.o strspn.o strcspn.o strpbrk.o strsep.o strtok.o \ + strtok_r.o \ fnmatch.o \ gethostname.o getdomainname.o getcwd.o \ seteuid.o setegid.o \ diff --git a/usr/klibc/strtok.c b/usr/klibc/strtok.c index c2671af..6b169a1 100644 --- a/usr/klibc/strtok.c +++ b/usr/klibc/strtok.c @@ -8,12 +8,5 @@ char *strtok(char *s, const char *delim) { static char *holder; - if (s) - holder = s; - - do { - s = strsep(&holder, delim); - } while (s && !*s); - - return s; + return strtok_r(s, delim, &holder); } diff --git a/usr/klibc/strtok_r.c b/usr/klibc/strtok_r.c new file mode 100644 index 0000000..695d516 --- /dev/null +++ b/usr/klibc/strtok_r.c @@ -0,0 +1,13 @@ +#include <string.h> + +char *strtok_r(char *s, const char *delim, char **holder) +{ + if (s) + *holder = s; + + do { + s = strsep(holder, delim); + } while (s && !*s); + + return s; +}
H. Peter Anvin
2008-Sep-08 01:37 UTC
[klibc] [git pull v2] initial mntent.h, mount features, ipconfig fixes
maximilian attems wrote:> [ gsi.de pretends this is massmailing so removed from CC's ] > > > hello hpa, > > please review and on ack merge belows patchset. > changes from v1, thanks to review by kzak: > * add strtok_r() > * use it in getmntent_r() > * getment_r() parses mnt_freq and mnt_passno too > isn't that hard. >What is the motivation for the reentrant function (since klibc doesn't support multithreading?) -hpa