maximilian attems
2008-Sep-05 21:21 UTC
[klibc] initial mntent.h, mount features, ipconfig fixes
hello hpa, please review and on ack merge belows patchset. thanks maks git pull git://git.debian.org/~maks/klibc.git maks for the changes: maximilian attems (8): [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/kinit/ipconfig/dhcp_proto.c | 3 +- usr/kinit/ipconfig/netdev.h | 2 +- usr/klibc/Kbuild | 3 +- usr/klibc/endmntent.c | 9 +++++ usr/klibc/getmntent.c | 60 ++++++++++++++++++++++++++++++++++++ usr/klibc/setmntent.c | 7 ++++ usr/utils/mount_main.c | 64 +++++++++++++++++++++++++++++++------- 8 files changed, 152 insertions(+), 15 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 commit 4b7aa738aa922e321c367ea8dd8f4e1577522e36 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 959ed5fced3403cba814c819146e8d8251c3d7b1 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 5c5809f953c2182d9bad39932eaa6a1de4231e5f 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 473fc6d9d516a0dd8201496b51a3dcb7d5763837 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 4fb0643145f08b1ad54f1ad3ff832e519de477bc 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 cd87134e14675eca448cbcd9ea3da44b461aaafa 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 b03fbfcbfbc687a9870105a5cefe938f24a76939 Author: maximilian attems <max at stro.at> Date: Sun Aug 24 00:48:12 2008 +0200 [klibc] add getmntent() internaly use getmntent_r(), can be exported on need. 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 9edc57f..7945a56 100644 --- a/usr/klibc/Kbuild +++ b/usr/klibc/Kbuild @@ -56,7 +56,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..c84f1ce --- /dev/null +++ b/usr/klibc/getmntent.c @@ -0,0 +1,60 @@ +#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; + 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(buf, sep); + if (!mntbuf->mnt_fsname) + return NULL; + + mntbuf->mnt_dir = strtok(NULL, sep); + if (!mntbuf->mnt_fsname) + return NULL; + + mntbuf->mnt_type = strtok(NULL, sep); + if (!mntbuf->mnt_type) + return NULL; + + mntbuf->mnt_opts = strtok(NULL, sep); + if (!mntbuf->mnt_opts) + mntbuf->mnt_opts = ""; + + mntbuf->mnt_freq = 0; + + /* FIXME: might be interesting */ + mntbuf->mnt_passno = 1; + + 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 3c78cb98b2634db5abb9e9cc743efbf893c37c3a 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 c8eabf9..9edc57f 100644 --- a/usr/klibc/Kbuild +++ b/usr/klibc/Kbuild @@ -55,7 +55,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); +}