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/string.h>
24 #include <ac/unistd.h>
30 * Add information about replica host specified by Ri to list
41 if ( st == NULL || ri == NULL ) {
45 /* Serialize access to the St struct */
46 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
49 ind = st->st_nreplicas - 1;
50 st->st_data = ( Stel ** ) ch_realloc( st->st_data,
51 ( st->st_nreplicas * sizeof( Stel * )));
52 if ( st->st_data == NULL ) {
53 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
56 st->st_data[ ind ] = ( Stel * ) ch_malloc( sizeof( Stel ) );
57 if ( st->st_data[ ind ] == NULL ) {
58 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
62 st->st_data[ ind ]->hostname = strdup( ri->ri_hostname );
63 st->st_data[ ind ]->port = ri->ri_port;
64 memset( st->st_data[ ind ]->last, 0, sizeof( st->st_data[ ind ]->last ));
65 st->st_data[ ind ]->seq = 0;
67 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
68 return st->st_data[ ind ];
74 * Write the contents of an St to disk.
88 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
89 if ( st->st_fp == NULL ) {
91 if (( rc = acquire_lock( sglob->slurpd_status_file, &(st->st_fp),
92 &(st->st_lfp))) < 0 ) {
93 if ( !st->st_err_logged ) {
94 Debug( LDAP_DEBUG_ANY,
95 "Error: cannot open status file \"%s\": %s\n",
96 sglob->slurpd_status_file, sys_errlist[ errno ], 0 );
97 st->st_err_logged = 1;
98 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
102 st->st_err_logged = 0;
106 /* Write data to the file */
107 truncate( sglob->slurpd_status_file, 0L );
108 fseek( st->st_fp, 0L, 0 );
109 for ( i = 0; i < st->st_nreplicas; i++ ) {
110 stel = st->st_data[ i ];
111 fprintf( st->st_fp, "%s:%d:%s:%d\n", stel->hostname, stel->port,
112 stel->last, stel->seq );
116 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
125 * Update the entry for a given host.
134 if ( stel == NULL || re == NULL ) {
138 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
139 strcpy( stel->last, re->re_timestamp );
140 stel->seq = re->re_seq;
141 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
149 * Read status information from disk file.
161 char *hostname, *port, *timestamp, *seq, *p, *t;
167 ldap_pvt_thread_mutex_lock( &(st->st_mutex ));
168 if ( access( sglob->slurpd_status_file, F_OK ) < 0 ) {
170 * File doesn't exist, so create it and return.
172 if (( fp = fopen( sglob->slurpd_status_file, "w" )) == NULL ) {
173 Debug( LDAP_DEBUG_ANY, "Error: cannot create status file \"%s\"\n",
174 sglob->slurpd_status_file, 0, 0 );
175 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
179 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
180 Debug( LDAP_DEBUG_ARGS, "No status file found, defaulting values\n",
184 if (( rc = acquire_lock( sglob->slurpd_status_file, &fp, &lfp)) < 0 ) {
185 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
188 while ( fgets( buf, sizeof( buf ), fp ) != NULL ) {
191 if (( t = strchr( p, ':' )) == NULL ) {
197 if (( t = strchr( p, ':' )) == NULL ) {
203 if (( t = strchr( p, ':' )) == NULL ) {
208 if (( t = strchr( seq, '\n' )) != NULL ) {
213 for ( i = 0; i < sglob->st->st_nreplicas; i++ ) {
214 if ( !strcmp( hostname, sglob->st->st_data[ i ]->hostname ) &&
215 atoi( port ) == sglob->st->st_data[ i ]->port ) {
217 strcpy( sglob->st->st_data[ i ]->last, timestamp );
218 sglob->st->st_data[ i ]->seq = atoi( seq );
224 sprintf( tbuf, "%s:%s (timestamp %s.%s)", hostname, port,
226 Debug( LDAP_DEBUG_ARGS,
227 "Retrieved state information for %s\n", tbuf, 0, 0 );
229 Debug( LDAP_DEBUG_ANY,
230 "Warning: saved state for %s:%s, not a known replica\n",
234 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
235 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
239 (void) relinquish_lock( sglob->slurpd_status_file, fp, lfp);
240 ldap_pvt_thread_mutex_unlock( &(st->st_mutex ));
255 return( ldap_pvt_thread_mutex_lock( &st->st_mutex ));
269 return( ldap_pvt_thread_mutex_unlock( &st->st_mutex ));
276 * Allocate and initialize an St struct.
287 (*st) = (St *) malloc( sizeof( St ));
292 ldap_pvt_thread_mutex_init( &((*st)->st_mutex) );
293 (*st)->st_data = NULL;
295 (*st)->st_lfp = NULL;
296 (*st)->st_nreplicas = 0;
297 (*st)->st_err_logged = 0;
298 (*st)->st_update = St_update;
299 (*st)->st_add = St_add;
300 (*st)->st_write = St_write;
301 (*st)->st_read = St_read;
302 (*st)->st_lock = St_lock;
303 (*st)->st_unlock = St_unlock;