Imported Upstream version 0.66.1
[hcoop/debian/courier-authlib.git] / libs / liblock / liblock.h
CommitLineData
d9898ee8 1
2#ifndef liblock_h
3#define liblock_h
4
5/*
6** Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
7** distribution information.
8*/
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
d9898ee8 14#if HAVE_CONFIG_H
b0322a85 15#include "liblock/config.h"
d9898ee8 16#endif
b0322a85 17#include <sys/types.h>
d9898ee8 18
19#define ll_whence_start 0
20#define ll_whence_curpos 1
21#define ll_whence_end 2
22
23#define ll_readlock 0
24#define ll_writelock 4
25#define ll_unlock 8
26#define ll_wait 16
27
28int ll_lockfd(int, /* File descriptor */
29 int, /* ll_ bitmask */
30 LL_OFFSET_TYPE, /* Start */
31 LL_OFFSET_TYPE); /* Length */
32
33
34/* Some useful macros: ll_lock_ex - exclusive lock on a file,
35 ll_lock_ex_test - attempt an exclusive lock on a file
36 ll_unlock_ex - unlock a file
37*/
38
39#define ll_lock_ex(f) \
40 ll_lockfd( (f), ll_writelock|ll_whence_start|ll_wait, 0, 0)
41
42#define ll_lock_ex_test(f) \
43 ll_lockfd( (f), ll_writelock|ll_whence_start, 0, 0)
44
45#define ll_unlock_ex(f) \
46 ll_lockfd( (f), ll_unlock|ll_whence_start, 0, 0)
47
48
49/*
50** Value-added: functions that reliably start and stop a daemon process,
51** permitting only one daemon process running. Utilizes a lock file, and a
52** pidfile.
53*/
54
55int ll_daemon_start(const char *lockfile);
56void ll_daemon_started(const char *pidfile, int fd);
57int ll_daemon_resetio();
58int ll_daemon_stop(const char *lockfile, const char *pidfile);
59int ll_daemon_restart(const char *lockfile, const char *pidfile);
60
61/*
62 The basic scenario
63
64main()
65{
66 if ((fd=ll_daemon_start(lockfilename)) < 0)
67 {
68 error(); exit(1);
69 }
70
71 ... Some custom initialization here ...
72
73 ll_daemon_started(pidfile, fd);
74
75 ll_daemon_resetio(); ... this one is optional
76}
77
78To stop this daemon:
79
80ll_daemon_stop (lockfilename, pidfile)
81
82
83ll_daemon_start attempts to start a daemon process going. It does only
84a partial setup. If it detects that the daemon process is already
85running, it itself does an exit(0), not returning to the parent.
86
87If there was a failure starting a daemon process, -1 is return, else
88we return a transparent file descriptor, which will have to be passed as
89the secodn argument to ll_daemon_started().
90
91When ll_daemon_start returns, we're already running in a partially set-up
92daemon process. The setup isn't complete just yet. The parent function
93can perform any other custom initialization. If initialization fails,
94the parent function can simply exit. Otherwise, if the initialization
95completes, ll_daemon_started must be called in order to save this daemon
96process's pid in the pid file (2nd arg must be the return from ll_daemon_start.
97
98To stop a daemon process, simply call ll_daemon_stop. Nothing too
99sophisticated here.
100
101To send the daemon process a SIGHUP, call ll_daemon_restart.
102*/
103
104#ifdef __cplusplus
8d138742 105}
d9898ee8 106#endif
107
108#endif