From owner-svn-soc-all@FreeBSD.ORG Mon Aug 12 14:00:56 2013 Return-Path: Delivered-To: svn-soc-all@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 24FE9F7C for ; Mon, 12 Aug 2013 14:00:56 +0000 (UTC) (envelope-from oleksandr@FreeBSD.org) Received: from socsvn.freebsd.org (socsvn.freebsd.org [IPv6:2001:1900:2254:206a::50:2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 114002072 for ; Mon, 12 Aug 2013 14:00:56 +0000 (UTC) Received: from socsvn.freebsd.org ([127.0.1.124]) by socsvn.freebsd.org (8.14.7/8.14.7) with ESMTP id r7CE0tj5075659 for ; Mon, 12 Aug 2013 14:00:55 GMT (envelope-from oleksandr@FreeBSD.org) Received: (from www@localhost) by socsvn.freebsd.org (8.14.7/8.14.6/Submit) id r7CE0tG8075647 for svn-soc-all@FreeBSD.org; Mon, 12 Aug 2013 14:00:55 GMT (envelope-from oleksandr@FreeBSD.org) Date: Mon, 12 Aug 2013 14:00:55 GMT Message-Id: <201308121400.r7CE0tG8075647@socsvn.freebsd.org> X-Authentication-Warning: socsvn.freebsd.org: www set sender to oleksandr@FreeBSD.org using -f From: oleksandr@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r255842 - soc2013/oleksandr/SharedFolder-head/sbin/mount_vboxfs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Aug 2013 14:00:56 -0000 Author: oleksandr Date: Mon Aug 12 14:00:55 2013 New Revision: 255842 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=255842 Log: Correct mount_vboxfs utility, add double alternative way to mount vfs file system, if first is fault and correct usage() function Modified: soc2013/oleksandr/SharedFolder-head/sbin/mount_vboxfs/mount_vboxfs.c Modified: soc2013/oleksandr/SharedFolder-head/sbin/mount_vboxfs/mount_vboxfs.c ============================================================================== --- soc2013/oleksandr/SharedFolder-head/sbin/mount_vboxfs/mount_vboxfs.c Mon Aug 12 13:52:15 2013 (r255841) +++ soc2013/oleksandr/SharedFolder-head/sbin/mount_vboxfs/mount_vboxfs.c Mon Aug 12 14:00:55 2013 (r255842) @@ -43,24 +43,45 @@ #include "mntopts.h" +#define MAX_HOST_NAME 256 static char mount_point[MAXPATHLEN + 1]; -static char vboxfs_vfsname[] = "vboxfs"; +static char vboxfs_vfsname[] = "vboxvfs"; +static struct mntopt mopts[] = { + MOPT_STDOPTS, + MOPT_END +}; + static void usage(void) __dead2; +/** + * Print out a usage message and exit. + * + * @param name The name of the application + */ +static void +usage(void) +{ + printf("Usage: [OPTIONS] NAME MOUNTPOINT\n" + "Mount the VirtualBox shared folder NAME from the host system to MOUNTPOINT.\n" + "\n" + " -w mount the shared folder writable \n" + " -r mount the shared folder read-only (the default)\n"); + exit(1); +} int main(int argc, char *argv[]) { struct iovec *iov; struct stat st; - // struct xvfsconf vfc; char *host_name; char errmsg[255]; uid_t uid; gid_t gid; mode_t dir_mode, file_mode; int iovlen; - int error, ch; - + int error, ch, ronly = 0; + int mntflags = MNT_RDONLY; + iov = NULL; iovlen = 0; errmsg[0] = '\0'; @@ -69,40 +90,43 @@ file_mode = 0; dir_mode = 0; - #if 0 - error = getvfsbyname(vboxfs_vfsname, &vfc); - - if (error) { - if (kldload(vboxfs_vfsname) < 0) - err(EX_OSERR, "kldload(%s)", vboxfs_vfsname); - error = getvfsbyname(vboxfs_vfsname, &vfc); - } - - if (error) - errx(EX_OSERR, "VBOXFS filesystem is not available"); - #endif - while ((ch = getopt(argc, argv, "o:")) != -1) + while ((ch = getopt(argc, argv, "rwo:h")) != -1) switch(ch) { - case 'o': - case '?': - usage(); - /*NOTREACHED*/ - default: - usage(); + default: + fprintf(stderr, "unknown option `%c:%#x'\n", ch, ch); + case '?': + case 'h': + usage(); + case 'r': + ronly= 1; + break; + case 'w': + ronly = 0; + break; + case 'o': + getmntopts(optarg, mopts, &mntflags, 0); + break; + break; } if (argc - optind < 2) - usage(); + usage(); host_name = argv[optind]; realpath(argv[optind+1], mount_point); - + if (stat(mount_point, &st) == -1) err(EX_OSERR, "could not find mount point %s", mount_point); + if (!S_ISDIR(st.st_mode)) { errno = ENOTDIR; err(EX_OSERR, "can't mount on %s", mount_point); } + if (strlen(host_name) > MAX_HOST_NAME - 1) + err(EX_OSERR, "host name is too big %s", host_name); + + if (!ronly) + mntflags |= MNT_ASYNC; if (uid == (uid_t)-1) uid = st.st_uid; if (gid == (gid_t)-1) @@ -129,7 +153,26 @@ build_iovec_argf(&iov, &iovlen, "dir_mode", "%d", dir_mode); build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); - error = nmount(iov, iovlen, MNT_RDONLY); + error = nmount(iov, iovlen, mntflags); + + if (error == -1 && errno == EPROTO) + { + /* Sometimes the mount utility messes up the share name. Try to + * un-mangle it again. */ + char szCWD[4096]; + size_t cchCWD; + if (!getcwd(szCWD, sizeof(szCWD))) + printf("%s: failed to get the current working directory", argv[0]); + cchCWD = strlen(szCWD); + if (!strncmp(host_name, szCWD, cchCWD)) + { + while (host_name[cchCWD] == '/') + ++cchCWD; + /* We checked before that we have enough space */ + strcpy(host_name, host_name + cchCWD); + } + error = nmount(iov, iovlen, mntflags); + } if (error) { if (errmsg[0] != 0) { @@ -143,11 +186,3 @@ return 0; } - -static void -usage(void) -{ - (void)fprintf(stderr, - "usage: mount_vboxfs [-o options] name mount-point\n"); - exit(1); -}