]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
5523616ed16d0984ef5a41054b69e3968ad2bb27
[openldap] / servers / slurpd / fm.c
1 /*
2  * Copyright (c) 1996 Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13 /*
14  * fm.c - file management routines.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <stdlib.h>                     /* get free() */
21
22 #include <ac/string.h>
23 #include <ac/signal.h>
24
25 #include "slurp.h"
26 #include "globals.h"
27
28
29 /*
30  * Forward references
31  */
32 static char *get_record LDAP_P(( FILE * ));
33 static void populate_queue LDAP_P(( char *f ));
34 static RETSIGTYPE set_shutdown LDAP_P((int));
35
36
37 /*
38  * Main file manager routine.  Watches for new data to be appended to the
39  * slapd replication log.  When new data is appended, fm does the following:
40  *  - appends the data to slurpd's private copy of the replication log.
41  *  - truncates the slapd replog
42  *  - adds items to the internal queue of replication work to do
43  *  - signals the replication threads to let them know new work has arrived.
44  */
45 void *
46 fm(
47     void *arg
48 )
49 {
50     int rc;
51
52     /* Set up our signal handlers:
53      * SIG{TERM,INT,HUP} causes a shutdown
54      * LDAP_SIGUSR1 - does nothing, used to wake up sleeping threads.
55          * LDAP_SIGUSR2 - causes a shutdown
56      */
57     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
58     (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
59     (void) SIGNAL( SIGTERM, set_shutdown );
60     (void) SIGNAL( SIGINT, set_shutdown );
61 #ifdef SIGHUP
62     (void) SIGNAL( SIGHUP, set_shutdown );
63 #endif
64
65     if ( sglob->one_shot_mode ) {
66         if ( file_nonempty( sglob->slapd_replogfile )) {
67             populate_queue( sglob->slapd_replogfile );
68         }
69         printf( "Processing in one-shot mode:\n" );
70         printf( "%d total replication records in file,\n",
71                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
72         printf( "%d replication records to process.\n",
73                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
74         return NULL;
75     }
76     /*
77      * There may be some leftover replication records in our own
78      * copy of the replication log.  If any exist, add them to the
79      * queue.
80      */
81     if ( file_nonempty( sglob->slurpd_replogfile )) {
82         populate_queue( sglob->slurpd_replogfile );
83     }
84
85
86     while ( !sglob->slurpd_shutdown ) {
87         if ( file_nonempty( sglob->slapd_replogfile )) {
88             /* New work found - copy to slurpd replog file */
89             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
90                     sglob->slapd_replogfile, 0, 0 );
91             if (( rc = copy_replog( sglob->slapd_replogfile,
92                     sglob->slurpd_replogfile )) == 0 )  {
93                 populate_queue( sglob->slurpd_replogfile );
94             } else {
95                 if ( rc < 0 ) {
96                     Debug( LDAP_DEBUG_ANY,
97                             "Fatal error while copying replication log\n",
98                             0, 0, 0 );
99                     sglob->slurpd_shutdown = 1;
100                 }
101             }
102         } else {
103             ldap_pvt_thread_sleep( sglob->no_work_interval );
104         }
105
106         /* Garbage-collect queue */
107         sglob->rq->rq_gc( sglob->rq );
108
109         /* Trim replication log file, if needed */
110         if ( sglob->rq->rq_needtrim( sglob->rq )) {
111             FILE *fp, *lfp;
112             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
113                     &lfp )) < 0 ) {
114                 Debug( LDAP_DEBUG_ANY,
115                         "Error: cannot acquire lock on \"%s\" for trimming\n",
116                         sglob->slurpd_replogfile, 0, 0 );
117             } else {
118                 sglob->rq->rq_write( sglob->rq, fp );
119                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
120             }
121         }
122     }
123     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
124     return NULL;
125 }
126
127
128
129
130 /*
131  * Set a global flag which signals that we're shutting down.
132  */
133 static RETSIGTYPE
134 set_shutdown(int sig)
135 {
136     int i;
137
138     sglob->slurpd_shutdown = 1;                         /* set flag */
139     ldap_pvt_thread_kill( sglob->fm_tid, LDAP_SIGUSR1 );        /* wake up file mgr */
140     sglob->rq->rq_lock( sglob->rq );                    /* lock queue */
141     ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) );    /* wake repl threads */
142     for ( i = 0; i < sglob->num_replicas; i++ ) {
143         (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
144     }
145     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
146     (void) SIGNAL( sig, set_shutdown ); /* reinstall handlers */
147 }
148
149
150
151
152 /*
153  * A do-nothing signal handler.
154  */
155 RETSIGTYPE
156 do_nothing(int sig)
157 {
158     (void) SIGNAL( sig, do_nothing );
159 }
160
161
162
163
164 /*
165  * Open the slurpd replication log, seek to our last known position, and
166  * process any pending replication entries.
167  */
168 static void
169 populate_queue(
170     char *f
171 )
172 {
173     FILE        *fp, *lfp;
174     Rq          *rq = sglob->rq;
175     char        *p;
176
177     if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
178         Debug( LDAP_DEBUG_ANY,
179                 "error: can't lock file \"%s\": %s\n",
180                 f, sys_errlist[ errno ], 0 );
181         return;
182     }
183
184     /*
185      * Read replication records from fp and append them the
186      * the queue.
187      */
188     if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
189         Debug( LDAP_DEBUG_ANY,
190                 "error: can't seek to offset %ld in file \"%s\"\n",
191                 sglob->srpos, f, 0 );
192     } else {
193     while (( p = get_record( fp )) != NULL ) {
194         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
195             char *t;
196             /* Print an error message.  Only print first line.  */
197             if (( t = strchr( p, '\n' )) != NULL ) {
198                 *t = '\0';
199             }
200             Debug( LDAP_DEBUG_ANY,
201                     "error: malformed replog entry (begins with \"%s\")\n",
202                     p, 0, 0 );
203         }
204         free( p );
205         ldap_pvt_thread_yield();
206     }
207     sglob->srpos = ftell( fp );
208     }
209     (void) relinquish_lock( f, fp, lfp );
210 }
211     
212
213
214
215 /*
216  * Get the next "record" from the file pointed to by fp.  A "record"
217  * is delimited by two consecutive newlines.  Returns NULL on EOF.
218  */
219 static char *
220 get_record(
221     FILE *fp
222 )
223 {
224     int         len;
225     static char line[BUFSIZ];
226     char        *buf = NULL;
227     static int  lcur, lmax;
228
229     lcur = lmax = 0;
230
231     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
232             (( len = strlen( line )) > 1 )) {
233         while ( lcur + len + 1 > lmax ) {
234             lmax += BUFSIZ;
235             buf = (char *) ch_realloc( buf, lmax );
236         }
237         strcpy( buf + lcur, line );
238         lcur += len;
239     }
240     return( buf );
241 }