comparison src/lock.c @ 43:2842f50feb55 noffle

[svn] * client.c, client.h, common.h, config.c, config.h, content.c, content.h, control.c, control.h, database.c, database.h, dynamicstring.c, dynamicstring.h, fetch.c, fetch.h, fetchlist.c, fetchlist.h, group.c, group.h, itemlist.c, itemlist.h, lock.c, lock.h, log.c, log.h, noffle.c, online.c, online.h, outgoing.c, outgoing.h, over.c, over.h, post.c, post.h, protocol.c, protocol.h, pseudo.c, pseudo.h, request.c, request.h, server.c, server.h, util.c, util.h, wildmat.c, wildmat.h: Moved files to the subdirectory src/ * Makefile.am, acconfig.h, configure.in, docs/Makefile.am, src/Makefile.am, Makefile.in, aclocal.m4, config.h.in, configure, install-sh, missing, mkinstalldirs, stamp-h.in, docs/Makefile.in, src/Makefile.in: Added files. They are used by aclocal, autoheader, autoconf and automake. * src/config.c, src/config.h: Renamed to configfile.c and configfile.h, because configure will generate a config.h file itself. * src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c, src/group.c, src/lock.c, src/noffle.c, src/online.c, src/outgoing.c, src/over.c, src/pseudo.c, src/request.c, src/server.c, src/util.c: Changed '#include "config.h"' to '#include "configfile.h"'. * src/client.c, src/content.c, src/database.c, src/fetch.c, src/fetchlist.c, src/group.c, src/lock.c, src/online.c, src/outgoing.c, src/post.c, src/protocol.c, src/request.c, src/server.c: Files now #include <config.h>. Added missing <stdio.h>. This removes the warnings about snprintf() not being declared. * Makefile: Removed. This is now generated by configure.
author uh1763
date Fri, 05 May 2000 22:45:56 +0100
parents
children 125d79c9e586
comparison
equal deleted inserted replaced
42:2467ff423c15 43:2842f50feb55
1 /*
2 lock.c
3
4 $Id: lock.c 49 2000-05-05 21:45:56Z uh1763 $
5 */
6
7 #if HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdio.h>
12 #include "lock.h"
13 #include <errno.h>
14 #include <ctype.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include "configfile.h"
21 #include "log.h"
22 #include "database.h"
23 #include "group.h"
24 #include "request.h"
25
26 struct Lock
27 {
28 int lockFd;
29 Str lockFile;
30 } lock = { -1, "" };
31
32
33 #ifdef DEBUG
34 static Bool
35 testLock( void )
36 {
37 return ( lock.lockFd != -1 );
38 }
39 #endif
40
41 static Bool
42 waitLock( void )
43 {
44 int fd;
45 struct flock l;
46
47 ASSERT( ! testLock() );
48 Log_dbg( "Waiting for lock ..." );
49 snprintf( lock.lockFile, MAXCHAR, "%s/lock/global", Cfg_spoolDir() );
50 if ( ( fd = open( lock.lockFile, O_WRONLY | O_CREAT, 0644 ) ) < 0 )
51 {
52 Log_err( "Cannot open %s (%s)", lock.lockFile, strerror( errno ) );
53 return FALSE;
54 }
55 l.l_type = F_WRLCK;
56 l.l_start = 0;
57 l.l_whence = SEEK_SET;
58 l.l_len = 0;
59 if ( fcntl( fd, F_SETLKW, &l ) < 0 )
60 {
61 Log_err( "Cannot lock %s: %s", lock.lockFile, strerror( errno ) );
62 return FALSE;
63 }
64 lock.lockFd = fd;
65 Log_dbg( "Lock successful" );
66 return TRUE;
67 }
68
69 static void
70 releaseLock( void )
71 {
72 struct flock l;
73
74 ASSERT( testLock() );
75 l.l_type = F_UNLCK;
76 l.l_start = 0;
77 l.l_whence = SEEK_SET;
78 l.l_len = 0;
79 if ( fcntl( lock.lockFd, F_SETLK, &l ) < 0 )
80 Log_err( "Cannot release %s: %s", lock.lockFile,
81 strerror( errno ) );
82 close( lock.lockFd );
83 lock.lockFd = -1;
84 Log_dbg( "Releasing lock" );
85 }
86
87
88 /* Open all databases and set global lock. */
89 Bool
90 Lock_openDatabases( void )
91 {
92 if ( ! waitLock() )
93 {
94 Log_err( "Could not get write lock" );
95 return FALSE;
96 }
97 if ( ! Db_open() )
98 {
99 Log_err( "Could not open database" );
100 releaseLock();
101 return FALSE;
102 }
103 if ( ! Grp_open() )
104 {
105 Log_err( "Could not open groupinfo" );
106 Db_close();
107 releaseLock();
108 return FALSE;
109 }
110 if ( ! Req_open() )
111 {
112 Log_err( "Could not initialize request database" );
113 Grp_close();
114 Db_close();
115 releaseLock();
116 return FALSE;
117 }
118
119 return TRUE;
120 }
121
122
123 /* Close all databases and release global lock. */
124 void
125 Lock_closeDatabases( void )
126 {
127 Grp_close();
128 Db_close();
129 Req_close();
130 releaseLock();
131 }