2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
16 * All rights reserved.
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.
26 * This work was originally developed by the University of Michigan
27 * (as part of U-MICH LDAP).
32 * st.c - routines for managing the status structure, and for reading and
33 * writing status information to disk.
40 #include <ac/stdlib.h>
41 #include <ac/string.h>
42 #include <ac/unistd.h>
49 * Add information about replica host specified by Ri to list
60 if ( st == NULL || ri == NULL ) {
64 /* Serialize access to the St struct */
65 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
68 ind = st->st_nreplicas - 1;
69 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
70 ( st->st_nreplicas * sizeof( Stel * )));
71 if ( st->st_data == NULL ) {
72 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
75 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
76 if ( st->st_data[ ind ] == NULL ) {
77 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
81 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
82 st->st_data[ ind ]->port = ri->ri_port;
83 st->st_data[ ind ]->last = 0;
84 st->st_data[ ind ]->seq = 0;
86 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
87 return st->st_data[ ind ];
93 * Write the contents of an St to disk.
107 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
108 if ( st->st_fp == NULL ) {
110 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
111 &(st->st_lfp))) < 0 ) {
112 if ( !st->st_err_logged ) {
113 Debug( LDAP_DEBUG_ANY,
114 "Error: cannot open status file \"%s\": %s\n",
115 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
116 st->st_err_logged = 1;
117 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
121 st->st_err_logged = 0;
125 /* Write data to the file */
126 truncate( sglob->slurpd_status_file, 0L );
127 fseek( st->st_fp, 0L, 0 );
128 for ( i = 0; i < st->st_nreplicas; i++ ) {
129 stel = st->st_data[ i ];
130 fprintf( st->st_fp, "%s:%d:%ld:%d\n",
131 stel->hostname, stel->port,
132 (long) stel->last, stel->seq );
136 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
145 * Update the entry for a given host.
154 if ( stel == NULL || re == NULL ) {
158 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
159 stel->last = re->re_timestamp;
160 stel->seq = re->re_seq;
161 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
169 * Read status information from disk file.
181 char *hostname, *port, *timestamp, *seq, *p, *t;
187 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
188 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
190 * File doesn't exist, so create it and return.
192 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
193 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
194 sglob->slurpd_status_file, 0, 0 );
195 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
199 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
200 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
204 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
205 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
208 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
211 if (( t = strchr( p, ':' )) == NULL ) {
217 if (( t = strchr( p, ':' )) == NULL ) {
223 if (( t = strchr( p, ':' )) == NULL ) {
228 if (( t = strchr( seq, '\n' )) != NULL ) {
233 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
235 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
236 lutil_atoi( &p, port ) == 0 && p == sglob->st->st_data[ i ]->port )
239 if ( lutil_atol( &sglob->st->st_data[ i ]->last, timestamp ) != 0
240 || lutil_atoi( &sglob->st->st_data[ i ]->seq, seq ) != 0 )
249 sprintf( tbuf, "%s.%s", timestamp, seq );
250 Debug( LDAP_DEBUG_ARGS,
251 "Retrieved state information for %s:%s (timestamp %s)\n",
252 hostname, port, tbuf );
254 Debug( LDAP_DEBUG_ANY,
255 "Warning: saved state for %s:%s, not a known replica\n",
259 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
260 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
264 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
265 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
280 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
294 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
301 * Allocate and initialize an St struct.
312 (*st) = (St *) malloc( sizeof( St ));
317 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
318 (*st)->st_data = NULL;
320 (*st)->st_lfp = NULL;
321 (*st)->st_nreplicas = 0;
322 (*st)->st_err_logged = 0;
323 (*st)->st_update = St_update;
324 (*st)->st_add = St_add;
325 (*st)->st_write = St_write;
326 (*st)->st_read = St_read;
327 (*st)->st_lock = St_lock;
328 (*st)->st_unlock = St_unlock;