b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/* $Id$ */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @file
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * vbsfmount - Commonly used code to mount shared folders on Linux-based
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * systems. Currently used by mount.vboxsf and VBoxService.
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/*
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * Copyright (C) 2010 Oracle Corporation
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync *
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * available from http://www.virtualbox.org. This file is free software;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * General Public License (GPL) as published by the Free Software
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include "vbsfmount.h"
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
a6164d45a25a3602bc99ad1f973c10f7f7a7825avboxsync#ifndef _GNU_SOURCE
a6164d45a25a3602bc99ad1f973c10f7f7a7825avboxsync#define _GNU_SOURCE
a6164d45a25a3602bc99ad1f973c10f7f7a7825avboxsync#endif
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include <ctype.h>
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include <mntent.h>
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include <stdio.h>
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include <stdlib.h>
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include <string.h>
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync#include <sys/mount.h>
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync/** @todo Use defines for return values! */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsyncint vbsfmount_complete(const char *host_name, const char *mount_point,
44699f5668033b169bfc5009587c72f618b642a5vboxsync unsigned long flags, struct vbsf_mount_opts *opts)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync{
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync FILE *f, *m;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync char *buf;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync size_t size;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync struct mntent e;
44699f5668033b169bfc5009587c72f618b642a5vboxsync int rc = 0;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync m = open_memstream(&buf, &size);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (!m)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync return 1; /* Could not update mount table (failed to create memstream). */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (opts->uid)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "uid=%d,", opts->uid);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (opts->gid)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "gid=%d,", opts->gid);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (opts->ttl)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "ttl=%d,", opts->ttl);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (*opts->nls_name)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "iocharset=%s,", opts->nls_name);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (flags & MS_NOSUID)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "%s,", MNTOPT_NOSUID);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (flags & MS_RDONLY)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "%s,", MNTOPT_RO);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync else
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fprintf(m, "%s,", MNTOPT_RW);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync fclose(m);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (size > 0)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync buf[size - 1] = 0;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync else
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync buf = "defaults";
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync f = setmntent(MOUNTED, "a+");
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (!f)
44699f5668033b169bfc5009587c72f618b642a5vboxsync {
44699f5668033b169bfc5009587c72f618b642a5vboxsync rc = 2; /* Could not open mount table for update. */
44699f5668033b169bfc5009587c72f618b642a5vboxsync }
44699f5668033b169bfc5009587c72f618b642a5vboxsync else
44699f5668033b169bfc5009587c72f618b642a5vboxsync {
44699f5668033b169bfc5009587c72f618b642a5vboxsync e.mnt_fsname = (char*)host_name;
44699f5668033b169bfc5009587c72f618b642a5vboxsync e.mnt_dir = (char*)mount_point;
44699f5668033b169bfc5009587c72f618b642a5vboxsync e.mnt_type = "vboxsf";
44699f5668033b169bfc5009587c72f618b642a5vboxsync e.mnt_opts = buf;
44699f5668033b169bfc5009587c72f618b642a5vboxsync e.mnt_freq = 0;
44699f5668033b169bfc5009587c72f618b642a5vboxsync e.mnt_passno = 0;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
44699f5668033b169bfc5009587c72f618b642a5vboxsync if (addmntent(f, &e))
44699f5668033b169bfc5009587c72f618b642a5vboxsync rc = 3; /* Could not add an entry to the mount table. */
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
44699f5668033b169bfc5009587c72f618b642a5vboxsync endmntent(f);
44699f5668033b169bfc5009587c72f618b642a5vboxsync }
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync if (size > 0)
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync {
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync memset(buf, 0, size);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync free(buf);
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync }
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync
44699f5668033b169bfc5009587c72f618b642a5vboxsync return rc;
b6517c5cc3b7a38889416706905a3cf2fd010785vboxsync}