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 ) {
101 LDAP_LOG ( SLURPD, ERR, "St_write: "
102 "Error: cannot open status file \"%s\":%s\n",
103 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
105 Debug( LDAP_DEBUG_ANY,
106 "Error: cannot open status file \"%s\": %s\n",
107 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
109 st->st_err_logged = 1;
110 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
114 st->st_err_logged = 0;
118 /* Write data to the file */
119 truncate( sglob->slurpd_status_file, 0L );
120 fseek( st->st_fp, 0L, 0 );
121 for ( i = 0; i < st->st_nreplicas; i++ ) {
122 stel = st->st_data[ i ];
123 fprintf( st->st_fp, "%s:%d:%ld:%d\n",
124 stel->hostname, stel->port,
125 (long) stel->last, stel->seq );
129 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
138 * Update the entry for a given host.
147 if ( stel == NULL || re == NULL ) {
151 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
152 stel->last = re->re_timestamp;
153 stel->seq = re->re_seq;
154 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
162 * Read status information from disk file.
174 char *hostname, *port, *timestamp, *seq, *p, *t;
180 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
181 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
183 * File doesn't exist, so create it and return.
185 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
187 LDAP_LOG ( SLURPD, ERR, "St_write: "
188 "Error: cannot create status file \"%s\"\n",
189 sglob->slurpd_status_file, 0, 0 );
191 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
192 sglob->slurpd_status_file, 0, 0 );
194 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
198 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
200 LDAP_LOG ( SLURPD, DETAIL1, "St_write: "
201 "No status file found, defaulting values\n", 0, 0, 0 );
203 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
208 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
209 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
212 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
215 if (( t = strchr( p, ':' )) == NULL ) {
221 if (( t = strchr( p, ':' )) == NULL ) {
227 if (( t = strchr( p, ':' )) == NULL ) {
232 if (( t = strchr( seq, '\n' )) != NULL ) {
237 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
238 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
239 atoi( port ) == sglob->st->st_data[ i ]->port ) {
241 sglob->st->st_data[ i ]->last = atol( timestamp );
242 sglob->st->st_data[ i ]->seq = atoi( seq );
248 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
251 LDAP_LOG ( SLURPD, DETAIL1, "St_write: "
252 "Retrieved state information for %s\n", tbuf, 0, 0 );
254 Debug( LDAP_DEBUG_ARGS,
255 "Retrieved state information for %s\n", tbuf, 0, 0 );
259 LDAP_LOG ( SLURPD, WARNING, "St_write: "
260 "Warning: saved state for %s:%s, not a known replica\n",
263 Debug( LDAP_DEBUG_ANY,
264 "Warning: saved state for %s:%s, not a known replica\n",
269 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
270 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
274 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
275 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
290 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
304 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
311 * Allocate and initialize an St struct.
322 (*st) = (St *) malloc( sizeof( St ));
327 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
328 (*st)->st_data = NULL;
330 (*st)->st_lfp = NULL;
331 (*st)->st_nreplicas = 0;
332 (*st)->st_err_logged = 0;
333 (*st)->st_update = St_update;
334 (*st)->st_add = St_add;
335 (*st)->st_write = St_write;
336 (*st)->st_read = St_read;
337 (*st)->st_lock = St_lock;
338 (*st)->st_unlock = St_unlock;