3 * Copyright (c) 1996 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
16 * st.c - routines for managing the status structure, and for reading and
17 * writing status information to disk.
24 #include <ac/stdlib.h>
25 #include <ac/string.h>
26 #include <ac/unistd.h>
32 * Add information about replica host specified by Ri to list
43 if ( st == NULL || ri == NULL ) {
47 /* Serialize access to the St struct */
48 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
51 ind = st->st_nreplicas - 1;
52 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
53 ( st->st_nreplicas * sizeof( Stel * )));
54 if ( st->st_data == NULL ) {
55 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
58 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
59 if ( st->st_data[ ind ] == NULL ) {
60 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
64 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
65 st->st_data[ ind ]->port = ri->ri_port;
66 memset( st->st_data[ ind ]->last, 0, sizeof( st->st_data[ ind ]->last ));
67 st->st_data[ ind ]->seq = 0;
69 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
70 return st->st_data[ ind ];
76 * Write the contents of an St to disk.
90 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
91 if ( st->st_fp == NULL ) {
93 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
94 &(st->st_lfp))) < 0 ) {
95 if ( !st->st_err_logged ) {
96 Debug( LDAP_DEBUG_ANY,
97 "Error: cannot open status file \"%s\": %s\n",
98 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
99 st->st_err_logged = 1;
100 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
104 st->st_err_logged = 0;
108 /* Write data to the file */
109 truncate( sglob->slurpd_status_file, 0L );
110 fseek( st->st_fp, 0L, 0 );
111 for ( i = 0; i < st->st_nreplicas; i++ ) {
112 stel = st->st_data[ i ];
113 fprintf( st->st_fp, "%s:%d:%s:%d\n", stel->hostname, stel->port,
114 stel->last, stel->seq );
118 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
127 * Update the entry for a given host.
136 if ( stel == NULL || re == NULL ) {
140 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
141 strcpy( stel->last, re->re_timestamp );
142 stel->seq = re->re_seq;
143 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
151 * Read status information from disk file.
163 char *hostname, *port, *timestamp, *seq, *p, *t;
169 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
170 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
172 * File doesn't exist, so create it and return.
174 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
175 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
176 sglob->slurpd_status_file, 0, 0 );
177 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
181 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
182 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
186 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
187 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
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 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
241 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
242 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
257 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
271 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
278 * Allocate and initialize an St struct.
289 (*st) = (St *) malloc( sizeof( St ));
294 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
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;