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 |
11 | extern "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 | |
28 | int 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 | |
55 | int ll_daemon_start(const char *lockfile); |
56 | void ll_daemon_started(const char *pidfile, int fd); |
57 | int ll_daemon_resetio(); |
58 | int ll_daemon_stop(const char *lockfile, const char *pidfile); |
59 | int ll_daemon_restart(const char *lockfile, const char *pidfile); |
60 | |
61 | /* |
62 | The basic scenario |
63 | |
64 | main() |
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 | |
78 | To stop this daemon: |
79 | |
80 | ll_daemon_stop (lockfilename, pidfile) |
81 | |
82 | |
83 | ll_daemon_start attempts to start a daemon process going. It does only |
84 | a partial setup. If it detects that the daemon process is already |
85 | running, it itself does an exit(0), not returning to the parent. |
86 | |
87 | If there was a failure starting a daemon process, -1 is return, else |
88 | we return a transparent file descriptor, which will have to be passed as |
89 | the secodn argument to ll_daemon_started(). |
90 | |
91 | When ll_daemon_start returns, we're already running in a partially set-up |
92 | daemon process. The setup isn't complete just yet. The parent function |
93 | can perform any other custom initialization. If initialization fails, |
94 | the parent function can simply exit. Otherwise, if the initialization |
95 | completes, ll_daemon_started must be called in order to save this daemon |
96 | process's pid in the pid file (2nd arg must be the return from ll_daemon_start. |
97 | |
98 | To stop a daemon process, simply call ll_daemon_stop. Nothing too |
99 | sophisticated here. |
100 | |
101 | To send the daemon process a SIGHUP, call ll_daemon_restart. |
102 | */ |
103 | |
104 | #ifdef __cplusplus |
8d138742 |
105 | } |
d9898ee8 |
106 | #endif |
107 | |
108 | #endif |