2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>
48 * Add information about replica host specified by Ri to list
59 if ( st == NULL || ri == NULL ) {
63 /* Serialize access to the St struct */
64 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
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 ));
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 ));
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;
85 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
86 return st->st_data[ ind ];
92 * Write the contents of an St to disk.
106 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
107 if ( st->st_fp == NULL ) {
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 ));
120 st->st_err_logged = 0;
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 );
135 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
144 * Update the entry for a given host.
153 if ( stel == NULL || re == NULL ) {
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 ));
168 * Read status information from disk file.
180 char *hostname, *port, *timestamp, *seq, *p, *t;
186 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
187 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
189 * File doesn't exist, so create it and return.
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 ));
198 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
199 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
203 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
204 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
207 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
210 if (( t = strchr( p, ':' )) == NULL ) {
216 if (( t = strchr( p, ':' )) == NULL ) {
222 if (( t = strchr( p, ':' )) == NULL ) {
227 if (( t = strchr( seq, '\n' )) != NULL ) {
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 ) {
236 sglob->st->st_data[ i ]->last = atol( timestamp );
237 sglob->st->st_data[ i ]->seq = atoi( seq );
243 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
245 Debug( LDAP_DEBUG_ARGS,
246 "Retrieved state information for %s\n", tbuf, 0, 0 );
248 Debug( LDAP_DEBUG_ANY,
249 "Warning: saved state for %s:%s, not a known replica\n",
253 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
254 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
258 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
259 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
274 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
288 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
295 * Allocate and initialize an St struct.
306 (*st) = (St *) malloc( sizeof( St ));
311 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
312 (*st)->st_data = 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;