2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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 ) {
113 LDAP_LOG ( SLURPD, ERR, "St_write: "
114 "Error: cannot open status file \"%s\":%s\n",
115 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
117 Debug( LDAP_DEBUG_ANY,
118 "Error: cannot open status file \"%s\": %s\n",
119 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
121 st->st_err_logged = 1;
122 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
126 st->st_err_logged = 0;
130 /* Write data to the file */
131 truncate( sglob->slurpd_status_file, 0L );
132 fseek( st->st_fp, 0L, 0 );
133 for ( i = 0; i < st->st_nreplicas; i++ ) {
134 stel = st->st_data[ i ];
135 fprintf( st->st_fp, "%s:%d:%ld:%d\n",
136 stel->hostname, stel->port,
137 (long) stel->last, stel->seq );
141 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
150 * Update the entry for a given host.
159 if ( stel == NULL || re == NULL ) {
163 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
164 stel->last = re->re_timestamp;
165 stel->seq = re->re_seq;
166 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
174 * Read status information from disk file.
186 char *hostname, *port, *timestamp, *seq, *p, *t;
192 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
193 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
195 * File doesn't exist, so create it and return.
197 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
199 LDAP_LOG ( SLURPD, ERR, "St_write: "
200 "Error: cannot create status file \"%s\"\n",
201 sglob->slurpd_status_file, 0, 0 );
203 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
204 sglob->slurpd_status_file, 0, 0 );
206 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
210 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
212 LDAP_LOG ( SLURPD, DETAIL1, "St_write: "
213 "No status file found, defaulting values\n", 0, 0, 0 );
215 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
220 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
221 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
224 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
227 if (( t = strchr( p, ':' )) == NULL ) {
233 if (( t = strchr( p, ':' )) == NULL ) {
239 if (( t = strchr( p, ':' )) == NULL ) {
244 if (( t = strchr( seq, '\n' )) != NULL ) {
249 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
250 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
251 atoi( port ) == sglob->st->st_data[ i ]->port ) {
253 sglob->st->st_data[ i ]->last = atol( timestamp );
254 sglob->st->st_data[ i ]->seq = atoi( seq );
260 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
263 LDAP_LOG ( SLURPD, DETAIL1, "St_write: "
264 "Retrieved state information for %s\n", tbuf, 0, 0 );
266 Debug( LDAP_DEBUG_ARGS,
267 "Retrieved state information for %s\n", tbuf, 0, 0 );
271 LDAP_LOG ( SLURPD, WARNING, "St_write: "
272 "Warning: saved state for %s:%s, not a known replica\n",
275 Debug( LDAP_DEBUG_ANY,
276 "Warning: saved state for %s:%s, not a known replica\n",
281 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
282 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
286 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
287 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
302 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
316 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
323 * Allocate and initialize an St struct.
334 (*st) = (St *) malloc( sizeof( St ));
339 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
340 (*st)->st_data = NULL;
342 (*st)->st_lfp = NULL;
343 (*st)->st_nreplicas = 0;
344 (*st)->st_err_logged = 0;
345 (*st)->st_update = St_update;
346 (*st)->st_add = St_add;
347 (*st)->st_write = St_write;
348 (*st)->st_read = St_read;
349 (*st)->st_lock = St_lock;
350 (*st)->st_unlock = St_unlock;