annotate lock.c @ 36:affc3884dfff noffle

[svn] Ignore noffle-1.0pre5
author enz
date Wed, 03 May 2000 08:22:45 +0100
parents 04124a4423d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
1 /*
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
2 lock.c
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
3
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
4 $Id: lock.c 3 2000-01-04 11:35:42Z enz $
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
5 */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
6
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
7 #include "lock.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
8 #include <errno.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
9 #include <ctype.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
10 #include <sys/types.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
11 #include <sys/stat.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
12 #include <fcntl.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
13 #include <time.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
14 #include <unistd.h>
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
15 #include "config.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
16 #include "log.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
17 #include "database.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
18 #include "group.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
19 #include "request.h"
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
20
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
21 struct Lock
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
22 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
23 int lockFd;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
24 Str lockFile;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
25 } lock = { -1, "" };
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
26
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
27
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
28 #ifdef DEBUG
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
29 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
30 testLock( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
31 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
32 return ( lock.lockFd != -1 );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
33 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
34 #endif
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
35
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
36 static Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
37 waitLock( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
38 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
39 int fd;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
40 struct flock l;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
41
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
42 ASSERT( ! testLock() );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
43 Log_dbg( "Waiting for lock ..." );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
44 snprintf( lock.lockFile, MAXCHAR, "%s/lock/global", Cfg_spoolDir() );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
45 if ( ( fd = open( lock.lockFile, O_WRONLY | O_CREAT, 0644 ) ) < 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
46 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
47 Log_err( "Cannot open %s (%s)", lock.lockFile, strerror( errno ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
48 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
49 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
50 l.l_type = F_WRLCK;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
51 l.l_start = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
52 l.l_whence = SEEK_SET;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
53 l.l_len = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
54 if ( fcntl( fd, F_SETLKW, &l ) < 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
55 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
56 Log_err( "Cannot lock %s: %s", lock.lockFile, strerror( errno ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
57 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
58 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
59 lock.lockFd = fd;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
60 Log_dbg( "Lock successful" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
61 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
62 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
63
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
64 static void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
65 releaseLock( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
66 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
67 struct flock l;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
68
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
69 ASSERT( testLock() );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
70 l.l_type = F_UNLCK;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
71 l.l_start = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
72 l.l_whence = SEEK_SET;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
73 l.l_len = 0;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
74 if ( fcntl( lock.lockFd, F_SETLK, &l ) < 0 )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
75 Log_err( "Cannot release %s: %s", lock.lockFile,
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
76 strerror( errno ) );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
77 close( lock.lockFd );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
78 lock.lockFd = -1;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
79 Log_dbg( "Releasing lock" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
80 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
81
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
82
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
83 /* Open all databases and set global lock. */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
84 Bool
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
85 Lock_openDatabases( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
86 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
87 if ( ! waitLock() )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
88 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
89 Log_err( "Could not get write lock" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
90 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
91 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
92 if ( ! Db_open() )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
93 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
94 Log_err( "Could not open database" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
95 releaseLock();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
96 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
97 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
98 if ( ! Grp_open() )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
99 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
100 Log_err( "Could not open groupinfo" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
101 Db_close();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
102 releaseLock();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
103 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
104 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
105 if ( ! Req_open() )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
106 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
107 Log_err( "Could not initialize request database" );
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
108 Grp_close();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
109 Db_close();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
110 releaseLock();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
111 return FALSE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
112 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
113
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
114 return TRUE;
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
115 }
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
116
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
117
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
118 /* Close all databases and release global lock. */
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
119 void
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
120 Lock_closeDatabases( void )
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
121 {
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
122 Grp_close();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
123 Db_close();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
124 Req_close();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
125 releaseLock();
04124a4423d4 [svn] Initial revision
enz
parents:
diff changeset
126 }