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