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.
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
25 #include <ac/unistd.h>
31 * Add information about replica host specified by Ri to list
42 if ( st == NULL || ri == NULL ) {
46 /* Serialize access to the St struct */
47 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
50 ind = st->st_nreplicas - 1;
51 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
52 ( st->st_nreplicas * sizeof( Stel * )));
53 if ( st->st_data == NULL ) {
54 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
57 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
58 if ( st->st_data[ ind ] == NULL ) {
59 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
63 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
64 st->st_data[ ind ]->port = ri->ri_port;
65 memset( st->st_data[ ind ]->last, 0, sizeof( st->st_data[ ind ]->last ));
66 st->st_data[ ind ]->seq = 0;
68 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
69 return st->st_data[ ind ];
75 * Write the contents of an St to disk.
89 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
90 if ( st->st_fp == NULL ) {
92 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
93 &(st->st_lfp))) < 0 ) {
94 if ( !st->st_err_logged ) {
95 Debug( LDAP_DEBUG_ANY,
96 "Error: cannot open status file \"%s\": %s\n",
97 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
98 st->st_err_logged = 1;
99 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
103 st->st_err_logged = 0;
107 /* Write data to the file */
108 truncate( sglob->slurpd_status_file, 0L );
109 fseek( st->st_fp, 0L, 0 );
110 for ( i = 0; i < st->st_nreplicas; i++ ) {
111 stel = st->st_data[ i ];
112 fprintf( st->st_fp, "%s:%d:%s:%d\n", stel->hostname, stel->port,
113 stel->last, stel->seq );
117 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
126 * Update the entry for a given host.
135 if ( stel == NULL || re == NULL ) {
139 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
140 strcpy( stel->last, re->re_timestamp );
141 stel->seq = re->re_seq;
142 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
150 * Read status information from disk file.
162 char *hostname, *port, *timestamp, *seq, *p, *t;
168 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
169 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
171 * File doesn't exist, so create it and return.
173 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
174 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
175 sglob->slurpd_status_file, 0, 0 );
176 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
180 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
181 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
185 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
186 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
189 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
192 if (( t = strchr( p, ':' )) == NULL ) {
198 if (( t = strchr( p, ':' )) == NULL ) {
204 if (( t = strchr( p, ':' )) == NULL ) {
209 if (( t = strchr( seq, '\n' )) != NULL ) {
214 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
215 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
216 atoi( port ) == sglob->st->st_data[ i ]->port ) {
218 strcpy( sglob->st->st_data[ i ]->last, timestamp );
219 sglob->st->st_data[ i ]->seq = atoi( seq );
225 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
227 Debug( LDAP_DEBUG_ARGS,
228 "Retrieved state information for %s\n", tbuf, 0, 0 );
230 Debug( LDAP_DEBUG_ANY,
231 "Warning: saved state for %s:%s, not a known replica\n",
235 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
236 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
240 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
241 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
256 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
270 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
277 * Allocate and initialize an St struct.
288 (*st) = (St *) malloc( sizeof( St ));
293 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
294 (*st)->st_data = NULL;
296 (*st)->st_lfp = NULL;
297 (*st)->st_nreplicas = 0;
298 (*st)->st_err_logged = 0;
299 (*st)->st_update = St_update;
300 (*st)->st_add = St_add;
301 (*st)->st_write = St_write;
302 (*st)->st_read = St_read;
303 (*st)->st_lock = St_lock;
304 (*st)->st_unlock = St_unlock;