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