3 * Copyright 1998-2000 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 memset( st->st_data[ ind ]->last, '\0', sizeof( st->st_data[ ind ]->last ));
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:%s:%d\n", stel->hostname, stel->port,
118 stel->last, stel->seq );
122 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
131 * Update the entry for a given host.
140 if ( stel == NULL || re == NULL ) {
144 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
145 strcpy( stel->last, re->re_timestamp );
146 stel->seq = re->re_seq;
147 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
155 * Read status information from disk file.
167 char *hostname, *port, *timestamp, *seq, *p, *t;
173 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
174 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
176 * File doesn't exist, so create it and return.
178 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
179 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
180 sglob->slurpd_status_file, 0, 0 );
181 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
185 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
186 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
190 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
191 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
194 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
197 if (( t = strchr( p, ':' )) == NULL ) {
203 if (( t = strchr( p, ':' )) == NULL ) {
209 if (( t = strchr( p, ':' )) == NULL ) {
214 if (( t = strchr( seq, '\n' )) != NULL ) {
219 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
220 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
221 atoi( port ) == sglob->st->st_data[ i ]->port ) {
223 strcpy( sglob->st->st_data[ i ]->last, timestamp );
224 sglob->st->st_data[ i ]->seq = atoi( seq );
230 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
232 Debug( LDAP_DEBUG_ARGS,
233 "Retrieved state information for %s\n", tbuf, 0, 0 );
235 Debug( LDAP_DEBUG_ANY,
236 "Warning: saved state for %s:%s, not a known replica\n",
240 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
241 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
245 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
246 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
261 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
275 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
282 * Allocate and initialize an St struct.
293 (*st) = (St *) malloc( sizeof( St ));
298 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
299 (*st)->st_data = NULL;
301 (*st)->st_lfp = NULL;
302 (*st)->st_nreplicas = 0;
303 (*st)->st_err_logged = 0;
304 (*st)->st_update = St_update;
305 (*st)->st_add = St_add;
306 (*st)->st_write = St_write;
307 (*st)->st_read = St_read;
308 (*st)->st_lock = St_lock;
309 (*st)->st_unlock = St_unlock;