By the way, for extra bonus points, use code that looks like the following
attachment.
To do this right, you really want to lock the pidfile after opening so that
multiple invocations don't occur.


#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define MAX_PID_SIZE 10

static int
pid_open (const char *filename)
{
    int fd;

    fd = open(filename, O_CREAT|O_WRONLY, 0644);
    if (fd < 0) {
 logging(LOG_ERR, "error opening %s for writing: %m", filename);
 return -1;
    }

    if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
 if (!pid_lkquiet) {
     logging(LOG_ERR, "unable to lock %s: %m", filename);
     logging(LOG_ERR, "is another copy of this program running?");
 }
 close(fd);
 return -1;
    }

    return fd;
}

int
pid_update (int fd)
{
    char pid_buf[MAX_PID_SIZE];

    memset(pid_buf, '\0', sizeof(pid_buf));
    snprintf(pid_buf, sizeof pid_buf, "%d\n", getpid());

    if (lseek(fd, 0, SEEK_SET) < 0) {
 logging(LOG_ERR, "%s: lseek: %m", __FUNCTION__);
 return -1;
    }

    if (write(fd, pid_buf, strlen(pid_buf)) < 0) {
 logging(LOG_ERR, "%s: write: %m", __FUNCTION__);
 return -1;  /* failure */
    }

    return 0;   /* success */
}

int
pid_lock (const char *filename)
{
    int fd;

    fd = pid_open(filename);
    if (fd < 0)
 return fd;

    if (pid_update(fd) < 0) {
 close(fd);
 return -1;
    }
    return fd;
}




----- Original Message -----
From: "Duncan Findlay" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, February 27, 2002 4:09 PM
Subject: Re: Re: [SAtalk] BSD rc.d script and HTML spam


> On Wed, Feb 27, 2002 at 03:49:38PM -0800, Craig R Hughes wrote:
> > Stick it in bugzilla and I'll get to it sometime.  Though isn't it
probably
> > easier (and more flexible) to get the pid in your shell, and have the
shell
> > write it to a file if you want to do that?
> >
>
> easier? no.
> more flexible? maybe.
>
> It'd be much better to have -d write the pid to /var/run/spamd. That's
what
> pretty much every other daemon does.
>
>
> --
> Duncan Findlay
>
> _______________________________________________
> Spamassassin-talk mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/spamassassin-talk
>
>


_______________________________________________
Spamassassin-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/spamassassin-talk

Reply via email to