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.
22 #include <ac/string.h>
28 #ifdef DECL_SYS_ERRLIST
29 extern char *sys_errlist[];
30 #endif /* DECL_SYS_ERRLIST */
33 * Add information about replica host specified by Ri to list
44 if ( st == NULL || ri == NULL ) {
48 /* Serialize access to the St struct */
49 pthread_mutex_lock( &(st->st_mutex ));
52 ind = st->st_nreplicas - 1;
53 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
54 ( st->st_nreplicas * sizeof( Stel * )));
55 if ( st->st_data == NULL ) {
56 pthread_mutex_unlock( &(st->st_mutex ));
59 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
60 if ( st->st_data[ ind ] == NULL ) {
61 pthread_mutex_unlock( &(st->st_mutex ));
65 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
66 st->st_data[ ind ]->port = ri->ri_port;
67 memset( st->st_data[ ind ]->last, 0, sizeof( st->st_data[ ind ]->last ));
68 st->st_data[ ind ]->seq = 0;
70 pthread_mutex_unlock( &(st->st_mutex ));
71 return st->st_data[ ind ];
77 * Write the contents of an St to disk.
91 pthread_mutex_lock( &(st->st_mutex ));
92 if ( st->st_fp == NULL ) {
94 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
95 &(st->st_lfp))) < 0 ) {
96 if ( !st->st_err_logged ) {
97 Debug( LDAP_DEBUG_ANY,
98 "Error: cannot open status file \"%s\": %s\n",
99 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
100 st->st_err_logged = 1;
101 pthread_mutex_unlock( &(st->st_mutex ));
105 st->st_err_logged = 0;
109 /* Write data to the file */
110 truncate( sglob->slurpd_status_file, 0L );
111 fseek( st->st_fp, 0L, 0 );
112 for ( i = 0; i < st->st_nreplicas; i++ ) {
113 stel = st->st_data[ i ];
114 fprintf( st->st_fp, "%s:%d:%s:%d\n", stel->hostname, stel->port,
115 stel->last, stel->seq );
119 pthread_mutex_unlock( &(st->st_mutex ));
128 * Update the entry for a given host.
137 if ( stel == NULL || re == NULL ) {
141 pthread_mutex_lock( &(st->st_mutex ));
142 strcpy( stel->last, re->re_timestamp );
143 stel->seq = re->re_seq;
144 pthread_mutex_unlock( &(st->st_mutex ));
152 * Read status information from disk file.
164 char *hostname, *port, *timestamp, *seq, *p, *t;
170 pthread_mutex_lock( &(st->st_mutex ));
171 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
173 * File doesn't exist, so create it and return.
175 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
176 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
177 sglob->slurpd_status_file, 0, 0 );
178 pthread_mutex_unlock( &(st->st_mutex ));
182 pthread_mutex_unlock( &(st->st_mutex ));
183 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
187 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
190 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
193 if (( t = strchr( p, ':' )) == NULL ) {
199 if (( t = strchr( p, ':' )) == NULL ) {
205 if (( t = strchr( p, ':' )) == NULL ) {
210 if (( t = strchr( seq, '\n' )) != NULL ) {
215 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
216 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
217 atoi( port ) == sglob->st->st_data[ i ]->port ) {
219 strcpy( sglob->st->st_data[ i ]->last, timestamp );
220 sglob->st->st_data[ i ]->seq = atoi( seq );
226 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
228 Debug( LDAP_DEBUG_ARGS,
229 "Retrieved state information for %s\n", tbuf, 0, 0 );
231 Debug( LDAP_DEBUG_ANY,
232 "Warning: saved state for %s:%s, not a known replica\n",
236 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
237 pthread_mutex_unlock( &(st->st_mutex ));
241 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
242 pthread_mutex_unlock( &(st->st_mutex ));
257 return( pthread_mutex_lock( &st->st_mutex ));
271 return( pthread_mutex_unlock( &st->st_mutex ));
278 * Allocate and initialize an St struct.
289 (*st) = (St *) malloc( sizeof( St ));
294 pthread_mutex_init( &((*st)->st_mutex), pthread_mutexattr_default );
295 (*st)->st_data = NULL;
297 (*st)->st_lfp = NULL;
298 (*st)->st_nreplicas = 0;
299 (*st)->st_err_logged = 0;
300 (*st)->st_update = St_update;
301 (*st)->st_add = St_add;
302 (*st)->st_write = St_write;
303 (*st)->st_read = St_read;
304 (*st)->st_lock = St_lock;
305 (*st)->st_unlock = St_unlock;