]> git.sur5r.net Git - openldap/blob - servers/slurpd/st.c
db5035a1f45d40de058ca2be2bba7dd3443446b4
[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         pthread_mutex_unlock( &(st->st_mutex ));
185         return 0;
186     }
187     while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
188         p = buf;
189         hostname = p;
190         if (( t = strchr( p, ':' )) == NULL ) {
191             goto bad;
192         }
193         *t++ = '\0';
194         p = t;
195         port = p;
196         if (( t = strchr( p, ':' )) == NULL ) {
197             goto bad;
198         }
199         *t++ = '\0';
200         p = t;
201         timestamp = p;
202         if (( t = strchr( p, ':' )) == NULL ) {
203             goto bad;
204         }
205         *t++ = '\0';
206         seq = t;
207         if (( t = strchr( seq, '\n' )) != NULL ) {
208             *t = '\0';
209         }
210
211         found = 0;
212         for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
213             if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
214                     atoi( port ) == sglob->st->st_data[ i ]->port ) {
215                 found = 1;
216                 strcpy( sglob->st->st_data[ i ]->last, timestamp );
217                 sglob->st->st_data[ i ]->seq = atoi( seq );
218                 break;
219             }
220         }
221         if ( found ) {
222             char tbuf[ 255 ];
223             sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
224                     timestamp, seq );
225             Debug( LDAP_DEBUG_ARGS,
226                     "Retrieved state information for %s\n", tbuf, 0, 0 );
227         } else {
228             Debug(  LDAP_DEBUG_ANY,
229                     "Warning: saved state for %s:%s, not a known replica\n",
230                     hostname, port, 0 );
231         }
232     }
233     (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
234     pthread_mutex_unlock( &(st->st_mutex ));
235     return 0;
236
237 bad:
238     (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
239     pthread_mutex_unlock( &(st->st_mutex ));
240     return -1;
241 }
242     
243
244
245
246 /*
247  * Lock an St struct.
248  */
249 static int
250 St_lock(
251     St *st
252 )
253 {
254     return( pthread_mutex_lock( &st->st_mutex ));
255 }
256
257
258
259
260 /*
261  * Lock an St struct.
262  */
263 static int
264 St_unlock(
265     St *st
266 )
267 {
268     return( pthread_mutex_unlock( &st->st_mutex ));
269 }
270
271
272
273
274 /*
275  * Allocate and initialize an St struct.
276  */
277 int
278 St_init(
279     St **st
280 )
281 {
282     if ( st == NULL ) {
283         return -1;
284     }
285
286     (*st) = (St *) malloc( sizeof( St ));
287     if ( *st == NULL ) {
288         return -1;
289     }
290
291     pthread_mutex_init( &((*st)->st_mutex), pthread_mutexattr_default );
292     (*st)->st_data = NULL;
293     (*st)->st_fp = NULL;
294     (*st)->st_lfp = NULL;
295     (*st)->st_nreplicas = 0;
296     (*st)->st_err_logged = 0;
297     (*st)->st_update = St_update;
298     (*st)->st_add = St_add;
299     (*st)->st_write = St_write;
300     (*st)->st_read = St_read;
301     (*st)->st_lock = St_lock;
302     (*st)->st_unlock = St_unlock;
303     return 0;
304 }
305