3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
20 * st.c - routines for managing the status structure, and for reading and
21 * writing status information to disk.
28 #include <ac/stdlib.h>
29 #include <ac/string.h>
30 #include <ac/unistd.h>
36 * Add information about replica host specified by Ri to list
47 if ( st == NULL || ri == NULL ) {
51 /* Serialize access to the St struct */
52 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
55 ind = st->st_nreplicas - 1;
56 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
57 ( st->st_nreplicas * sizeof( Stel * )));
58 if ( st->st_data == NULL ) {
59 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
62 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
63 if ( st->st_data[ ind ] == NULL ) {
64 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
68 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
69 st->st_data[ ind ]->port = ri->ri_port;
70 st->st_data[ ind ]->last = 0;
71 st->st_data[ ind ]->seq = 0;
73 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
74 return st->st_data[ ind ];
80 * Write the contents of an St to disk.
94 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
95 if ( st->st_fp == NULL ) {
97 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
98 &(st->st_lfp))) < 0 ) {
99 if ( !st->st_err_logged ) {
100 Debug( LDAP_DEBUG_ANY,
101 "Error: cannot open status file \"%s\": %s\n",
102 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
103 st->st_err_logged = 1;
104 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
108 st->st_err_logged = 0;
112 /* Write data to the file */
113 truncate( sglob->slurpd_status_file, 0L );
114 fseek( st->st_fp, 0L, 0 );
115 for ( i = 0; i < st->st_nreplicas; i++ ) {
116 stel = st->st_data[ i ];
117 fprintf( st->st_fp, "%s:%d:%ld:%d\n",
118 stel->hostname, stel->port,
119 (long) stel->last, stel->seq );
123 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
132 * Update the entry for a given host.
141 if ( stel == NULL || re == NULL ) {
145 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
146 stel->last = re->re_timestamp;
147 stel->seq = re->re_seq;
148 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
156 * Read status information from disk file.
168 char *hostname, *port, *timestamp, *seq, *p, *t;
174 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
175 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
177 * File doesn't exist, so create it and return.
179 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
180 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
181 sglob->slurpd_status_file, 0, 0 );
182 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
186 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
187 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
191 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
192 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
195 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
198 if (( t = strchr( p, ':' )) == NULL ) {
204 if (( t = strchr( p, ':' )) == NULL ) {
210 if (( t = strchr( p, ':' )) == NULL ) {
215 if (( t = strchr( seq, '\n' )) != NULL ) {
220 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
221 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
222 atoi( port ) == sglob->st->st_data[ i ]->port ) {
224 sglob->st->st_data[ i ]->last = atol( timestamp );
225 sglob->st->st_data[ i ]->seq = atoi( seq );
231 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
233 Debug( LDAP_DEBUG_ARGS,
234 "Retrieved state information for %s\n", tbuf, 0, 0 );
236 Debug( LDAP_DEBUG_ANY,
237 "Warning: saved state for %s:%s, not a known replica\n",
241 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
242 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
246 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
247 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
262 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
276 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
283 * Allocate and initialize an St struct.
294 (*st) = (St *) malloc( sizeof( St ));
299 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
300 (*st)->st_data = NULL;
302 (*st)->st_lfp = NULL;
303 (*st)->st_nreplicas = 0;
304 (*st)->st_err_logged = 0;
305 (*st)->st_update = St_update;
306 (*st)->st_add = St_add;
307 (*st)->st_write = St_write;
308 (*st)->st_read = St_read;
309 (*st)->st_lock = St_lock;
310 (*st)->st_unlock = St_unlock;