Back to index

courier  0.68.2
liblock.h
Go to the documentation of this file.
00001 
00002 #ifndef liblock_h
00003 #define liblock_h
00004 
00005 /*
00006 ** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
00007 ** distribution information.
00008 */
00009 
00010 #ifdef __cplusplus
00011 extern "C" {
00012 #endif
00013 
00014 #if    HAVE_CONFIG_H
00015 #include      "config.h"
00016 #endif
00017 #include      <sys/types.h>
00018 
00019 #define       ll_whence_start      0
00020 #define       ll_whence_curpos 1
00021 #define       ll_whence_end 2
00022 
00023 #define       ll_readlock   0
00024 #define       ll_writelock  4
00025 #define       ll_unlock     8
00026 #define       ll_wait              16
00027 
00028 int    ll_lockfd(int,       /* File descriptor */
00029               int,   /* ll_ bitmask */
00030               LL_OFFSET_TYPE,      /* Start */
00031               LL_OFFSET_TYPE);     /* Length */
00032 
00033 
00034 /* Some useful macros: ll_lock_ex - exclusive lock on a file,
00035               ll_lock_ex_test - attempt an exclusive lock on a file
00036               ll_unlock_ex - unlock a file
00037 */
00038 
00039 #define       ll_lock_ex(f) \
00040        ll_lockfd( (f), ll_writelock|ll_whence_start|ll_wait, 0, 0)
00041 
00042 #define       ll_lock_ex_test(f)   \
00043        ll_lockfd( (f), ll_writelock|ll_whence_start, 0, 0)
00044 
00045 #define       ll_unlock_ex(f)      \
00046        ll_lockfd( (f), ll_unlock|ll_whence_start, 0, 0)
00047 
00048 
00049 /*
00050 ** Value-added: functions that reliably start and stop a daemon process,
00051 ** permitting only one daemon process running.  Utilizes a lock file, and a
00052 ** pidfile.
00053 */
00054 
00055 int ll_daemon_start(const char *lockfile);
00056 void ll_daemon_started(const char *pidfile, int fd);
00057 int ll_daemon_resetio();
00058 int ll_daemon_stop(const char *lockfile, const char *pidfile);
00059 int ll_daemon_restart(const char *lockfile, const char *pidfile);
00060 
00061 /*
00062   The basic scenario
00063 
00064 main()
00065 {
00066     if ((fd=ll_daemon_start(lockfilename)) < 0)
00067     {
00068            error();  exit(1);
00069     }
00070 
00071     ... Some custom initialization here ...
00072 
00073     ll_daemon_started(pidfile, fd);
00074 
00075     ll_daemon_resetio();   ... this one is optional
00076 }
00077 
00078 To stop this daemon:
00079 
00080 ll_daemon_stop (lockfilename, pidfile)
00081 
00082 
00083 ll_daemon_start attempts to start a daemon process going.  It does only
00084 a partial setup.  If it detects that the daemon process is already
00085 running, it itself does an exit(0), not returning to the parent.
00086 
00087 If there was a failure starting a daemon process, -1 is return, else
00088 we return a transparent file descriptor, which will have to be passed as
00089 the secodn argument to ll_daemon_started().
00090 
00091 When ll_daemon_start returns, we're already running in a partially set-up
00092 daemon process.  The setup isn't complete just yet.  The parent function
00093 can perform any other custom initialization.  If initialization fails,
00094 the parent function can simply exit.  Otherwise, if the initialization
00095 completes, ll_daemon_started must be called in order to save this daemon
00096 process's pid in the pid file (2nd arg must be the return from ll_daemon_start.
00097 
00098 To stop a daemon process, simply call ll_daemon_stop.  Nothing too
00099 sophisticated here.
00100 
00101 To send the daemon process a SIGHUP, call ll_daemon_restart.
00102 */
00103 
00104 #ifdef __cplusplus
00105 }
00106 #endif
00107 
00108 #endif