/* movemail foo bar -- move file foo to file bar,
locking file foo the way /bin/mail respects.
-Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2012
- Free Software Foundation, Inc.
+Copyright (C) 1986, 1992-1994, 1996, 1999, 2001-2014 Free Software
+Foundation, Inc.
This file is part of GNU Emacs.
/* Important notice: defining MAIL_USE_FLOCK or MAIL_USE_LOCKF *will
cause loss of mail* if you do it on a system that does not normally
- use flock as its way of interlocking access to inbox files. The
+ use flock/lockf as its way of interlocking access to inbox files. The
setting of MAIL_USE_FLOCK and MAIL_USE_LOCKF *must agree* with the
system's own conventions. It is not a choice that is up to you.
#include <getopt.h>
#include <unistd.h>
-#ifdef HAVE_FCNTL_H
#include <fcntl.h>
-#endif
#include <string.h>
#include "syswait.h"
#ifdef MAIL_USE_POP
#include <fcntl.h>
#endif /* WINDOWSNT */
-#ifndef F_OK
-#define F_OK 0
-#define X_OK 1
-#define W_OK 2
-#define R_OK 4
-#endif
-
#ifdef WINDOWSNT
#include <sys/locking.h>
#endif
+/* If your system uses the `flock' or `lockf' system call for mail locking,
+ define MAIL_USE_SYSTEM_LOCK. If your system type should always define
+ MAIL_USE_LOCKF or MAIL_USE_FLOCK but configure does not do this,
+ please make a bug report. */
+
#ifdef MAIL_USE_LOCKF
#define MAIL_USE_SYSTEM_LOCK
#endif
else
#endif
{
- #ifndef DIRECTORY_SEP
- #define DIRECTORY_SEP '/'
- #endif
- #ifndef IS_DIRECTORY_SEP
- #define IS_DIRECTORY_SEP(_c_) ((_c_) == DIRECTORY_SEP)
- #endif
-
/* Use a lock file named after our first argument with .lock appended:
If it exists, the mail file is locked. */
/* Note: this locking mechanism is *required* by the mailer
so it can create lock files properly.
You might also wish to verify that your system is one which
- uses lock files for this purpose. Some systems use other methods.
-
- If your system uses the `flock' system call for mail locking,
- define MAIL_USE_SYSTEM_LOCK in config.h and recompile movemail.
- If your system type should always define MAIL_USE_SYSTEM_LOCK
- but does not, send a bug report to bug-gnu-emacs@gnu.org so we
- can change the default in configure. */
+ uses lock files for this purpose. Some systems use other methods. */
inname_len = strlen (inname);
lockname = xmalloc (inname_len + sizeof ".lock");
memcpy (tempname, inname, inname_dirlen);
strcpy (tempname + inname_dirlen, "EXXXXXX");
-#ifdef HAVE_MKSTEMP
- desc = mkstemp (tempname);
-#else
- mktemp (tempname);
- if (!*tempname)
- desc = -1;
- else
- {
- unlink (tempname);
- desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600);
- }
-#endif
+ desc = mkostemp (tempname, 0);
if (desc < 0)
{
- int mkstemp_errno = errno;
+ int mkostemp_errno = errno;
error ("error while creating what would become the lock file",
0, 0);
- errno = mkstemp_errno;
+ errno = mkostemp_errno;
pfatal_with_name (tempname);
}
close (desc);
tem = link (tempname, lockname);
-#ifdef EPERM
- if (tem < 0 && errno == EPERM)
- fatal ("Unable to create hard link between %s and %s",
- tempname, lockname);
-#endif
+ if (tem < 0 && errno != EEXIST)
+ pfatal_with_name (lockname);
unlink (tempname);
if (tem >= 0)
if (indesc < 0)
pfatal_with_name (inname);
-#ifdef BSD_SYSTEM
- /* In case movemail is setuid to root, make sure the user can
- read the output file. */
- /* This is desirable for all systems
- but I don't want to assume all have the umask system call */
- umask (umask (0) & 0333);
-#endif /* BSD_SYSTEM */
+ /* Make sure the user can read the output file. */
+ umask (umask (0) & 0377);
+
outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (outdesc < 0)
pfatal_with_name (outname);
for certain failure codes. */
if (status < 0)
{
- if (++lockcount <= 5)
+ if (++lockcount <= 5 && (errno == EAGAIN || errno == EBUSY))
{
-#ifdef EAGAIN
- if (errno == EAGAIN)
- {
- sleep (1);
- goto retry_lock;
- }
-#endif
-#ifdef EBUSY
- if (errno == EBUSY)
- {
- sleep (1);
- goto retry_lock;
- }
-#endif
+ sleep (1);
+ goto retry_lock;
}
pfatal_with_name (inname);
}
}
-#ifdef BSD_SYSTEM
- if (fsync (outdesc) < 0)
+ if (fsync (outdesc) != 0 && errno != EINVAL)
pfatal_and_delete (outname);
-#endif
/* Prevent symlink attacks truncating other users' mailboxes */
if (setregid (-1, real_gid) < 0)
register int i;
int mbfi;
FILE *mbf;
- char *getenv (const char *);
popserver server;
int start, end, increment;
char *user, *hostname;
}
}
- /* On AFS, a call to write only modifies the file in the local
- * workstation's AFS cache. The changes are not written to the server
- * until a call to fsync or close is made. Users with AFS home
- * directories have lost mail when over quota because these checks were
- * not made in previous versions of movemail. */
-
-#ifdef BSD_SYSTEM
- if (fsync (mbfi) < 0)
+ if (fsync (mbfi) != 0 && errno != EINVAL)
{
error ("Error in fsync: %s", strerror (errno), 0);
+ close (mbfi);
return EXIT_FAILURE;
}
-#endif
- if (close (mbfi) == -1)
+ if (close (mbfi) != 0)
{
error ("Error in close: %s", strerror (errno), 0);
return EXIT_FAILURE;