Back to index

courier  0.68.2
Defines | Functions
liblock.h File Reference
#include <sys/types.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define ll_whence_start   0
#define ll_whence_curpos   1
#define ll_whence_end   2
#define ll_readlock   0
#define ll_writelock   4
#define ll_unlock   8
#define ll_wait   16
#define ll_lock_ex(f)   ll_lockfd( (f), ll_writelock|ll_whence_start|ll_wait, 0, 0)
#define ll_lock_ex_test(f)   ll_lockfd( (f), ll_writelock|ll_whence_start, 0, 0)
#define ll_unlock_ex(f)   ll_lockfd( (f), ll_unlock|ll_whence_start, 0, 0)

Functions

int ll_lockfd (int, int, LL_OFFSET_TYPE, LL_OFFSET_TYPE)
int ll_daemon_start (const char *lockfile)
void ll_daemon_started (const char *pidfile, int fd)
int ll_daemon_resetio ()
int ll_daemon_stop (const char *lockfile, const char *pidfile)
int ll_daemon_restart (const char *lockfile, const char *pidfile)

Define Documentation

#define ll_lock_ex (   f)    ll_lockfd( (f), ll_writelock|ll_whence_start|ll_wait, 0, 0)

Definition at line 39 of file liblock.h.

#define ll_lock_ex_test (   f)    ll_lockfd( (f), ll_writelock|ll_whence_start, 0, 0)

Definition at line 42 of file liblock.h.

#define ll_readlock   0

Definition at line 23 of file liblock.h.

#define ll_unlock   8

Definition at line 25 of file liblock.h.

#define ll_unlock_ex (   f)    ll_lockfd( (f), ll_unlock|ll_whence_start, 0, 0)

Definition at line 45 of file liblock.h.

#define ll_wait   16

Definition at line 26 of file liblock.h.

#define ll_whence_curpos   1

Definition at line 20 of file liblock.h.

#define ll_whence_end   2

Definition at line 21 of file liblock.h.

#define ll_whence_start   0

Definition at line 19 of file liblock.h.

#define ll_writelock   4

Definition at line 24 of file liblock.h.


Function Documentation

Definition at line 195 of file lockdaemon.c.

{
int    i;

       close(0);
       if (open("/dev/null", O_RDONLY) != 0)
              return (-1);

       close(1);
       i=open(CONSOLE, O_WRONLY);
       if (i < 0)    i=open("/dev/null", O_WRONLY);
       if (i != 1)   return (-1);

       close(2);
       i=open(CONSOLE, O_WRONLY);
       if (i < 0)    i=open("/dev/null", O_WRONLY);
       if (i != 2)   return (-1);
       return (0);
}

Here is the caller graph for this function:

int ll_daemon_restart ( const char *  lockfile,
const char *  pidfile 
)

Definition at line 338 of file lockdaemon.c.

{
int    lockfd;
pid_t  p;
FILE   *fp;
int    c;

       if ((lockfd=open(lockfile, O_RDWR|O_CREAT, 0600)) < 0)
       {
              perror(lockfile);
              return (1);
       }

       if ( ll_lock_ex_test(lockfd) == 0) /* No daemon process running */
       {
              close(lockfd);
              return (0);   /* That was easy! */
       }
       close(lockfd);

       if ((fp=fopen(pidfile, "r")) == NULL)
              return (0);

       p=0;

       while ((c=getc(fp)) != EOF && c != '\n')
       {
              if (isdigit(c))
                     p=p*10+(c-'0');
       }

       fclose(fp);
       if (p)
              kill(p, SIGHUP);
       return (0);
}

Here is the caller graph for this function:

int ll_daemon_start ( const char *  lockfile)

Definition at line 41 of file lockdaemon.c.

{
pid_t  p;
int    pipefd[2];
char   c;
int    i;

       /*
       ** Close any open file descriptors.
       */

       for (i=3; i < 256; i++)
              close(i);

       /*
       ** We fork, and set up a pipe from the child process.  If we read
       ** a single 0 byte from the pipe, it means that the child has
       ** successfully initialized, and will return to main, so we exit(0).
       ** If we do not read a single 0 byte from the pipe, it means that
       ** there was an initialization error, so we return -1 to main.
       */

       if (pipe(pipefd) < 0)
       {
              perror("pipe");
              return (-1);
       }

       if ((p=fork()) == -1)
       {
              close(pipefd[0]);
              close(pipefd[1]);
              perror("fork");
              return (-1);
       }

       if (p == 0)
       {
              close(pipefd[0]);

              /*
              ** We fork once more, so that the daemon process will not
              ** be the child process of anyone.
              */

              p=fork();
              if (p == -1)
              {
                     perror("fork");
                     exit(0);
              }
              if (p)
                     exit(0);

              /*
              ** Continue initialization in start1()
              */
              return (start1(lockfile, pipefd[1]));
       }

       close(pipefd[1]);
       if (read(pipefd[0], &c, 1) <= 0)
              c=1;
       close(pipefd[0]);
       if (c == 0)
              exit (0);     /* Successful start of daemon */
       errno=EAGAIN;
       return (-1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ll_daemon_started ( const char *  pidfile,
int  fd 
)

Definition at line 215 of file lockdaemon.c.

{
char   buf[NUMBUFSIZE+1];
char   *p=strcat(libmail_str_pid_t(getpid(), buf), "\n");
FILE   *fp;

       unlink(pidfile); 
       if ((fp=fopen(pidfile, "w")) == NULL ||
              fprintf(fp, "%s", p) < 0 || fflush(fp) < 0 || fclose(fp))
       {
              perror(pidfile);
              exit(1);
       }

       if (write(fd, "", 1) != 1)  /* Signal waiting parent */
              exit(1); /* Shouldn't happen */
       close(fd);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ll_daemon_stop ( const char *  lockfile,
const char *  pidfile 
)

Definition at line 236 of file lockdaemon.c.

{
pid_t  p, p2;
int    waitstat;

       /*
       ** We fork, and the child process attempts to stop the daemon,
       ** then communicates the success to us, via its exit code.
       */

       signal(SIGCHLD, SIG_DFL);
       if ((p=fork()) == -1)
       {
              perror("fork");
              return (1);
       }
       if (p == 0)   stop1(lockfile, pidfile);

       while ((p2=wait(&waitstat)) != p)
              ;

       if (WIFEXITED(waitstat))
              return (WEXITSTATUS(waitstat));
       return (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int ll_lockfd ( int  ,
int  ,
LL_OFFSET_TYPE  ,
LL_OFFSET_TYPE   
)

Definition at line 18 of file lockfcntl.c.

{
#if HAS_FLOCK_T
flock_t       ft;
#else
struct flock ft;
#endif

       ft.l_type=ltype & ll_unlock ? F_UNLCK:
              ltype & ll_writelock ? F_WRLCK:F_RDLCK;
       ft.l_whence=ltype & ll_whence_curpos ? 1:
                     ltype & ll_whence_end ? 2:0;
       ft.l_start=start;
       ft.l_len=len;

       return (fcntl(fd, (ltype & ll_unlock) == 0 && (ltype & ll_wait)
                     ? F_SETLKW:F_SETLK, &ft));
}

Here is the call graph for this function:

Here is the caller graph for this function: