Edward Roper wrote:

> Okay I'm still lost. :)
> 
> What type of filelocking should be used when dealing with mailboxes. Does
> Sendmail honor a particular "lock file" or do I use flock() or fcntl()?

sendmail doesn't touch mail spools; it invokes an MDA such as
procmail, deliver, mail.local etc to deliver mail.

The standard way to lock a mailbox is to create a file whose name is
that of the mailbox with `.lock' appended. The main subtlety is that
you have to create it with link(), as this is the only approach that
is guaranteed to be atomic on an NFS filesystem.

IOW:

        int lock_file(const char *name)
        {
                size_t n = strlen(name);
                char *buff1 = alloca(n + 6);
                char *buff2 = alloca(n + 6);
                int fd;

                sprintf(buff1, "%s.%04x", name, getpid());

                fd = open(buff1, O_WRONLY | O_CREAT | O_EXCL, 0);
                if (fd < 0)
                {
                        perror("lock_file: open");
                        return -1;
                }

                if (close(fd) < 0)
                {
                        perror("lock_file: close");
                        return -1;
                }

                sprintf(buff2, "%s.lock", name);

                if (link(buff1, buff2) < 0)
                {
                        if (errno != EEXIST)
                        {
                                perror("lock_file: link");
                                status = -1;
                        }
                        else
                                status = 0;
                }
                else
                        status = 1;

                if (unlink(buff1) < 0)
                        perror("lock_file: unlink");

                return status;
        }

The function returns 1 if the file was locked successfully, 0 if the
file was already locked, and -1 if an error occurred.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to