From patchwork Fri Mar 26 14:25:35 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 48645 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.samba.org (fn.samba.org [216.83.154.106]) by ozlabs.org (Postfix) with ESMTP id 74924B7C09 for ; Sat, 27 Mar 2010 01:26:20 +1100 (EST) Received: from fn.samba.org (localhost [127.0.0.1]) by lists.samba.org (Postfix) with ESMTP id 6EA0C46649; Fri, 26 Mar 2010 08:26:20 -0600 (MDT) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on fn.samba.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.8 tests=BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS,SPF_NEUTRAL autolearn=ham version=3.2.5 X-Original-To: linux-cifs-client@lists.samba.org Delivered-To: linux-cifs-client@lists.samba.org Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by lists.samba.org (Postfix) with ESMTP id 18EA546659 for ; Fri, 26 Mar 2010 08:25:49 -0600 (MDT) Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o2QEPm44020154 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 26 Mar 2010 10:25:48 -0400 Received: from localhost.localdomain (vpn-10-105.rdu.redhat.com [10.11.10.105]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o2QEPdCh026868 for ; Fri, 26 Mar 2010 10:25:47 -0400 From: Jeff Layton To: linux-cifs-client@lists.samba.org Date: Fri, 26 Mar 2010 10:25:35 -0400 Message-Id: <1269613542-6402-13-git-send-email-jlayton@samba.org> In-Reply-To: <1269613542-6402-1-git-send-email-jlayton@samba.org> References: <1269613542-6402-1-git-send-email-jlayton@samba.org> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 Subject: [linux-cifs-client] [PATCH 12/19] mount.cifs: move mtab adding code to separate function X-BeenThere: linux-cifs-client@lists.samba.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: The Linux CIFS VFS client List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-cifs-client-bounces@lists.samba.org Errors-To: linux-cifs-client-bounces@lists.samba.org From: Jeff Layton Signed-off-by: Jeff Layton --- mount.cifs.c | 128 ++++++++++++++++++++++++++++++++-------------------------- 1 files changed, 71 insertions(+), 57 deletions(-) diff --git a/mount.cifs.c b/mount.cifs.c index fd110c7..b960de3 100644 --- a/mount.cifs.c +++ b/mount.cifs.c @@ -60,6 +60,9 @@ /* I believe that the kernel limits options data to a page */ #define MAX_OPTIONS_LEN 4096 +/* max length of mtab options */ +#define MTAB_OPTIONS_LEN 220 + /* * Maximum length of "share" portion of a UNC. I have no idea if this is at * all valid. According to MSDN, the typical max length of any component is @@ -994,6 +997,72 @@ static int check_mtab(const char *progname, const char *devname, return 0; } +static int +add_mtab(char *devname, char *mountpoint, unsigned long flags) +{ + int rc = 0; + char *mount_user; + struct mntent mountent; + FILE *pmntfile; + + atexit(unlock_mtab); + rc = lock_mtab(); + if (rc) { + fprintf(stderr, "cannot lock mtab"); + rc = EX_FILEIO; + goto add_mtab_exit; + } + + pmntfile = setmntent(MOUNTED, "a+"); + if (!pmntfile) { + fprintf(stderr, "could not update mount table\n"); + unlock_mtab(); + rc = EX_FILEIO; + goto add_mtab_exit; + } + + mountent.mnt_fsname = devname; + mountent.mnt_dir = mountpoint; + mountent.mnt_type = (char *)(void *)cifs_fstype; + mountent.mnt_opts = (char *)calloc(MTAB_OPTIONS_LEN, 1); + if(mountent.mnt_opts) { + if (flags & MS_RDONLY) + strlcat(mountent.mnt_opts,"ro", MTAB_OPTIONS_LEN); + else + strlcat(mountent.mnt_opts,"rw", MTAB_OPTIONS_LEN); + + if(flags & MS_MANDLOCK) + strlcat(mountent.mnt_opts,",mand", MTAB_OPTIONS_LEN); + if(flags & MS_NOEXEC) + strlcat(mountent.mnt_opts,",noexec", MTAB_OPTIONS_LEN); + if(flags & MS_NOSUID) + strlcat(mountent.mnt_opts,",nosuid", MTAB_OPTIONS_LEN); + if(flags & MS_NODEV) + strlcat(mountent.mnt_opts,",nodev", MTAB_OPTIONS_LEN); + if(flags & MS_SYNCHRONOUS) + strlcat(mountent.mnt_opts,",sync", MTAB_OPTIONS_LEN); + if(getuid() != 0) { + strlcat(mountent.mnt_opts, ",user=", MTAB_OPTIONS_LEN); + mount_user = getusername(); + if (mount_user) + strlcat(mountent.mnt_opts, mount_user, + MTAB_OPTIONS_LEN); + } + } + mountent.mnt_freq = 0; + mountent.mnt_passno = 0; + rc = addmntent(pmntfile, &mountent); + endmntent(pmntfile); + unlock_mtab(); + SAFE_FREE(mountent.mnt_opts); +add_mtab_exit: + if (rc) { + fprintf(stderr, "unable to add mount entry to mtab\n"); + rc = EX_FILEIO; + } + + return rc; +} int main(int argc, char ** argv) { @@ -1009,9 +1078,7 @@ int main(int argc, char ** argv) int already_uppercased = 0; size_t options_size = MAX_OPTIONS_LEN; size_t dev_len; - struct mntent mountent; struct parsed_mount_info *parsed_info = NULL; - FILE * pmntfile; if (check_setuid()) return EX_USAGE; @@ -1299,62 +1366,9 @@ mount_retry: goto mount_exit; } - if (nomtab) - goto mount_exit; + if (!nomtab) + rc = add_mtab(dev_name, mountpoint, parsed_info->flags); - atexit(unlock_mtab); - rc = lock_mtab(); - if (rc) { - fprintf(stderr, "cannot lock mtab"); - goto mount_exit; - } - pmntfile = setmntent(MOUNTED, "a+"); - if (!pmntfile) { - fprintf(stderr, "could not update mount table\n"); - unlock_mtab(); - rc = EX_FILEIO; - goto mount_exit; - } - - mountent.mnt_fsname = dev_name; - mountent.mnt_dir = mountpoint; - mountent.mnt_type = (char *)(void *)cifs_fstype; - mountent.mnt_opts = (char *) calloc(220, 1); - if(mountent.mnt_opts) { - char * mount_user = getusername(); - if(parsed_info->flags & MS_RDONLY) - strlcat(mountent.mnt_opts,"ro",220); - else - strlcat(mountent.mnt_opts,"rw",220); - if(parsed_info->flags & MS_MANDLOCK) - strlcat(mountent.mnt_opts,",mand",220); - if(parsed_info->flags & MS_NOEXEC) - strlcat(mountent.mnt_opts,",noexec",220); - if(parsed_info->flags & MS_NOSUID) - strlcat(mountent.mnt_opts,",nosuid",220); - if(parsed_info->flags & MS_NODEV) - strlcat(mountent.mnt_opts,",nodev",220); - if(parsed_info->flags & MS_SYNCHRONOUS) - strlcat(mountent.mnt_opts,",sync",220); - if(mount_user) { - if(getuid() != 0) { - strlcat(mountent.mnt_opts, - ",user=", 220); - strlcat(mountent.mnt_opts, - mount_user, 220); - } - } - } - mountent.mnt_freq = 0; - mountent.mnt_passno = 0; - rc = addmntent(pmntfile,&mountent); - endmntent(pmntfile); - unlock_mtab(); - SAFE_FREE(mountent.mnt_opts); - if (rc) { - fprintf(stderr, "unable to add mount entry to mtab\n"); - rc = EX_FILEIO; - } mount_exit: if (parsed_info) memset(parsed_info->password, 0, sizeof(parsed_info->password));