From a0e8627a9265725f4ece39516d5b40ea2e7cc8ac Mon Sep 17 00:00:00 2001 From: Ian Kent Date: Thu, 14 Dec 2006 20:34:52 +0900 Subject: [PATCH] nfs-utils - mtab locking needed on add as well as update Hi all, I noticed some mtab corruption the other day when doing some autofs testing but thought nothing of it. When investigating another issue I came across utils/mount.c:add_mtab which looks like it adds an entry to /etc/mtab without performing correct locking. Perhaps this is not needed when adding entries but I think it is. --- utils/mount/mount.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/utils/mount/mount.c b/utils/mount/mount.c index ca87e3d..5e0e599 100644 --- a/utils/mount/mount.c +++ b/utils/mount/mount.c @@ -149,11 +149,7 @@ int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opt ment.mnt_freq = 0; ment.mnt_passno= 0; - if ((fd = open(MOUNTED"~", O_RDWR|O_CREAT|O_EXCL, 0600)) == -1) { - fprintf(stderr, "Can't get "MOUNTED"~ lock file"); - return 1; - } - close(fd); + lock_mtab(); if ((mtab = setmntent(MOUNTED, "a+")) == NULL) { fprintf(stderr, "Can't open " MOUNTED); @@ -161,21 +157,22 @@ int add_mtab(char *fsname, char *mount_point, char *fstype, int flags, char *opt } if (addmntent(mtab, &ment) == 1) { + endmntent(mtab); + unlock_mtab(); fprintf(stderr, "Can't write mount entry"); return 1; } if (fchmod(fileno(mtab), 0644) == -1) { + endmntent(mtab); + unlock_mtab(); fprintf(stderr, "Can't set perms on " MOUNTED); return 1; } endmntent(mtab); - if (unlink(MOUNTED"~") == -1) { - fprintf(stderr, "Can't remove "MOUNTED"~"); - return 1; - } + unlock_mtab(); return 0; } -- 2.39.2