]> git.sur5r.net Git - openldap/blob - servers/slurpd/st.c
Add NO_THREADS fix from tih@nhh.no
[openldap] / servers / slurpd / st.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13
14 /*
15  * st.c - routines for managing the status structure, and for reading and
16  * writing status information to disk.
17  */
18
19
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "slurp.h"
26 #include "globals.h"
27
28 #ifndef SYSERRLIST_IN_STDIO
29 extern char *sys_errlist[];
30 #endif /* SYSERRLIST_IN_STDIO */
31
32 /*
33  * Add information about replica host specified by Ri to list
34  * of hosts.
35  */
36 static Stel *
37 St_add(
38     St  *st,
39     Ri  *ri
40 )
41 {
42     int ind;
43
44     if ( st == NULL || ri == NULL ) {
45         return NULL;
46     }
47
48     /* Serialize access to the St struct */
49     pthread_mutex_lock( &(st->st_mutex ));
50
51     st->st_nreplicas++;
52     ind = st->st_nreplicas - 1;
53     st->st_data = ( Stel ** ) ch_realloc( st->st_data, 
54             ( st->st_nreplicas * sizeof( Stel * )));
55     if ( st->st_data == NULL ) {
56         pthread_mutex_unlock( &(st->st_mutex ));
57         return NULL;
58     }
59     st->st_data[ ind ]  = ( Stel * ) ch_malloc( sizeof( Stel ) );
60     if ( st->st_data[ ind ] == NULL ) {
61         pthread_mutex_unlock( &(st->st_mutex ));
62         return NULL;
63     }
64
65     st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
66     st->st_data[ ind ]->port = ri->ri_port;
67     memset( st->st_data[ ind ]->last, 0, sizeof( st->st_data[ ind ]->last )); 
68     st->st_data[ ind ]->seq = 0;
69
70     pthread_mutex_unlock( &(st->st_mutex ));
71     return st->st_data[ ind ];
72 }
73
74
75
76 /*
77  * Write the contents of an St to disk.
78  */
79 static int
80 St_write (
81     St  *st
82 )
83 {
84     int         rc;
85     Stel        *stel;
86     int         i;
87
88     if ( st == NULL ) {
89         return -1;
90     }
91     pthread_mutex_lock( &(st->st_mutex ));
92     if ( st->st_fp == NULL ) {
93         /* Open file */
94         if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
95                 &(st->st_lfp))) < 0 ) {
96             if ( !st->st_err_logged ) {
97                 Debug( LDAP_DEBUG_ANY,
98                         "Error: cannot open status file \"%s\": %s\n",
99                         sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
100                 st->st_err_logged = 1;
101                 pthread_mutex_unlock( &(st->st_mutex ));
102                 return -1;
103             }
104         } else {
105             st->st_err_logged = 0;
106         }
107     }
108
109     /* Write data to the file */
110     truncate( sglob->slurpd_status_file, 0L );
111     fseek( st->st_fp, 0L, 0 );
112     for ( i = 0; i < st->st_nreplicas; i++ ) {
113         stel = st->st_data[ i ];
114         fprintf( st->st_fp, "%s:%d:%s:%d\n", stel->hostname, stel->port,
115                 stel->last, stel->seq );
116     }
117     fflush( st->st_fp );
118
119     pthread_mutex_unlock( &(st->st_mutex ));
120
121     return 0;
122 }
123     
124
125
126
127 /*
128  * Update the entry for a given host.
129  */
130 static int
131 St_update(
132     St          *st,
133     Stel        *stel,
134     Re          *re
135 )
136 {
137     if ( stel == NULL || re == NULL ) {
138         return -1;
139     }
140
141     pthread_mutex_lock( &(st->st_mutex ));
142     strcpy( stel->last, re->re_timestamp );
143     stel->seq = re->re_seq;
144     pthread_mutex_unlock( &(st->st_mutex ));
145     return 0;
146 }
147
148
149
150
151 /*
152  * Read status information from disk file.
153  */
154 static int
155 St_read(
156     St  *st
157 )
158 {
159     FILE        *fp;
160     FILE        *lfp;
161     char        buf[ 255 ];
162     int         i;
163     int         rc;
164     char        *hostname, *port, *timestamp, *seq, *p, *t;
165     int         found;
166
167     if ( st == NULL ) {
168         return -1;
169     }
170     pthread_mutex_lock( &(st->st_mutex ));
171     if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
172         /*
173          * File doesn't exist, so create it and return.
174          */
175         if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
176             Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
177                     sglob->slurpd_status_file, 0, 0 );
178             pthread_mutex_unlock( &(st->st_mutex ));
179             return -1;
180         }
181         (void) fclose( fp );
182         pthread_mutex_unlock( &(st->st_mutex ));
183         Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
184                 0, 0, 0 );
185         return 0;
186     }
187     if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
188         return 0;
189     }
190     while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
191         p = buf;
192         hostname = p;
193         if (( t = strchr( p, ':' )) == NULL ) {
194             goto bad;
195         }
196         *t++ = '\0';
197         p = t;
198         port = p;
199         if (( t = strchr( p, ':' )) == NULL ) {
200             goto bad;
201         }
202         *t++ = '\0';
203         p = t;
204         timestamp = p;
205         if (( t = strchr( p, ':' )) == NULL ) {
206             goto bad;
207         }
208         *t++ = '\0';
209         seq = t;
210         if (( t = strchr( seq, '\n' )) != NULL ) {
211             *t = '\0';
212         }
213
214         found = 0;
215         for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
216             if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
217                     atoi( port ) == sglob->st->st_data[ i ]->port ) {
218                 found = 1;
219                 strcpy( sglob->st->st_data[ i ]->last, timestamp );
220                 sglob->st->st_data[ i ]->seq = atoi( seq );
221                 break;
222             }
223         }
224         if ( found ) {
225             char tbuf[ 255 ];
226             sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
227                     timestamp, seq );
228             Debug( LDAP_DEBUG_ARGS,
229                     "Retrieved state information for %s\n", tbuf, 0, 0 );
230         } else {
231             Debug(  LDAP_DEBUG_ANY,
232                     "Warning: saved state for %s:%s, not a known replica\n",
233                     hostname, port, 0 );
234         }
235     }
236     (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
237     pthread_mutex_unlock( &(st->st_mutex ));
238     return 0;
239
240 bad:
241     (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
242     pthread_mutex_unlock( &(st->st_mutex ));
243     return -1;
244 }
245     
246
247
248
249 /*
250  * Lock an St struct.
251  */
252 static int
253 St_lock(
254     St *st
255 )
256 {
257     return( pthread_mutex_lock( &st->st_mutex ));
258 }
259
260
261
262
263 /*
264  * Lock an St struct.
265  */
266 static int
267 St_unlock(
268     St *st
269 )
270 {
271     return( pthread_mutex_unlock( &st->st_mutex ));
272 }
273
274
275
276
277 /*
278  * Allocate and initialize an St struct.
279  */
280 int
281 St_init(
282     St **st
283 )
284 {
285     if ( st == NULL ) {
286         return -1;
287     }
288
289     (*st) = (St *) malloc( sizeof( St ));
290     if ( *st == NULL ) {
291         return -1;
292     }
293
294     pthread_mutex_init( &((*st)->st_mutex), pthread_mutexattr_default );
295     (*st)->st_data = NULL;
296     (*st)->st_fp = NULL;
297     (*st)->st_lfp = NULL;
298     (*st)->st_nreplicas = 0;
299     (*st)->st_err_logged = 0;
300     (*st)->st_update = St_update;
301     (*st)->st_add = St_add;
302     (*st)->st_write = St_write;
303     (*st)->st_read = St_read;
304     (*st)->st_lock = St_lock;
305     (*st)->st_unlock = St_unlock;
306     return 0;
307 }
308