2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
15 * st.c - routines for managing the status structure, and for reading and
16 * writing status information to disk.
19 #define DISABLE_BRIDGE
23 #include <ac/string.h>
29 #ifndef DECL_SYS_ERRLIST
30 extern char *sys_errlist[];
31 #endif /* DECL_SYS_ERRLIST */
34 * Add information about replica host specified by Ri to list
45 if ( st == NULL || ri == NULL ) {
49 /* Serialize access to the St struct */
50 pthread_mutex_lock( &(st->st_mutex ));
53 ind = st->st_nreplicas - 1;
54 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
55 ( st->st_nreplicas * sizeof( Stel * )));
56 if ( st->st_data == NULL ) {
57 pthread_mutex_unlock( &(st->st_mutex ));
60 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
61 if ( st->st_data[ ind ] == NULL ) {
62 pthread_mutex_unlock( &(st->st_mutex ));
66 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
67 st->st_data[ ind ]->port = ri->ri_port;
68 memset( st->st_data[ ind ]->last, 0, sizeof( st->st_data[ ind ]->last ));
69 st->st_data[ ind ]->seq = 0;
71 pthread_mutex_unlock( &(st->st_mutex ));
72 return st->st_data[ ind ];
78 * Write the contents of an St to disk.
92 pthread_mutex_lock( &(st->st_mutex ));
93 if ( st->st_fp == NULL ) {
95 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
96 &(st->st_lfp))) < 0 ) {
97 if ( !st->st_err_logged ) {
98 Debug( LDAP_DEBUG_ANY,
99 "Error: cannot open status file \"%s\": %s\n",
100 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
101 st->st_err_logged = 1;
102 pthread_mutex_unlock( &(st->st_mutex ));
106 st->st_err_logged = 0;
110 /* Write data to the file */
111 truncate( sglob->slurpd_status_file, 0L );
112 fseek( st->st_fp, 0L, 0 );
113 for ( i = 0; i < st->st_nreplicas; i++ ) {
114 stel = st->st_data[ i ];
115 fprintf( st->st_fp, "%s:%d:%s:%d\n", stel->hostname, stel->port,
116 stel->last, stel->seq );
120 pthread_mutex_unlock( &(st->st_mutex ));
129 * Update the entry for a given host.
138 if ( stel == NULL || re == NULL ) {
142 pthread_mutex_lock( &(st->st_mutex ));
143 strcpy( stel->last, re->re_timestamp );
144 stel->seq = re->re_seq;
145 pthread_mutex_unlock( &(st->st_mutex ));
153 * Read status information from disk file.
165 char *hostname, *port, *timestamp, *seq, *p, *t;
171 pthread_mutex_lock( &(st->st_mutex ));
172 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
174 * File doesn't exist, so create it and return.
176 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
177 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
178 sglob->slurpd_status_file, 0, 0 );
179 pthread_mutex_unlock( &(st->st_mutex ));
183 pthread_mutex_unlock( &(st->st_mutex ));
184 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
188 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
191 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
194 if (( t = strchr( p, ':' )) == NULL ) {
200 if (( t = strchr( p, ':' )) == NULL ) {
206 if (( t = strchr( p, ':' )) == NULL ) {
211 if (( t = strchr( seq, '\n' )) != NULL ) {
216 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
217 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
218 atoi( port ) == sglob->st->st_data[ i ]->port ) {
220 strcpy( sglob->st->st_data[ i ]->last, timestamp );
221 sglob->st->st_data[ i ]->seq = atoi( seq );
227 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
229 Debug( LDAP_DEBUG_ARGS,
230 "Retrieved state information for %s\n", tbuf, 0, 0 );
232 Debug( LDAP_DEBUG_ANY,
233 "Warning: saved state for %s:%s, not a known replica\n",
237 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
238 pthread_mutex_unlock( &(st->st_mutex ));
242 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
243 pthread_mutex_unlock( &(st->st_mutex ));
258 return( pthread_mutex_lock( &st->st_mutex ));
272 return( pthread_mutex_unlock( &st->st_mutex ));
279 * Allocate and initialize an St struct.
290 (*st) = (St *) malloc( sizeof( St ));
295 pthread_mutex_init( &((*st)->st_mutex), pthread_mutexattr_default );
296 (*st)->st_data = NULL;
298 (*st)->st_lfp = NULL;
299 (*st)->st_nreplicas = 0;
300 (*st)->st_err_logged = 0;
301 (*st)->st_update = St_update;
302 (*st)->st_add = St_add;
303 (*st)->st_write = St_write;
304 (*st)->st_read = St_read;
305 (*st)->st_lock = St_lock;
306 (*st)->st_unlock = St_unlock;