]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
Replaced PREEMPTIVE_THREADS with HAVE_YIELDING_SELECT to clarify
[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 slurpd to read its administrative interface file.
56      *           (not yet implemented).
57      */
58     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
59     (void) SIGNAL( LDAP_SIGUSR2, do_admin );
60     (void) SIGNAL( SIGTERM, set_shutdown );
61     (void) SIGNAL( SIGINT, set_shutdown );
62     (void) SIGNAL( SIGHUP, set_shutdown );
63
64     if ( sglob->one_shot_mode ) {
65         if ( file_nonempty( sglob->slapd_replogfile )) {
66             populate_queue( sglob->slapd_replogfile );
67         }
68         printf( "Processing in one-shot mode:\n" );
69         printf( "%d total replication records in file,\n",
70                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
71         printf( "%d replication records to process.\n",
72                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
73         return NULL;
74     }
75     /*
76      * There may be some leftover replication records in our own
77      * copy of the replication log.  If any exist, add them to the
78      * queue.
79      */
80     if ( file_nonempty( sglob->slurpd_replogfile )) {
81         populate_queue( sglob->slurpd_replogfile );
82     }
83
84
85     while ( !sglob->slurpd_shutdown ) {
86         if ( file_nonempty( sglob->slapd_replogfile )) {
87             /* New work found - copy to slurpd replog file */
88             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
89                     sglob->slapd_replogfile, 0, 0 );
90             if (( rc = copy_replog( sglob->slapd_replogfile,
91                     sglob->slurpd_replogfile )) == 0 )  {
92                 populate_queue( sglob->slurpd_replogfile );
93             } else {
94                 if ( rc < 0 ) {
95                     Debug( LDAP_DEBUG_ANY,
96                             "Fatal error while copying replication log\n",
97                             0, 0, 0 );
98                     sglob->slurpd_shutdown = 1;
99                 }
100             }
101         } else {
102             tsleep( sglob->no_work_interval );
103         }
104
105         /* Garbage-collect queue */
106         sglob->rq->rq_gc( sglob->rq );
107
108         /* Trim replication log file, if needed */
109         if ( sglob->rq->rq_needtrim( sglob->rq )) {
110             FILE *fp, *lfp;
111             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
112                     &lfp )) < 0 ) {
113                 Debug( LDAP_DEBUG_ANY,
114                         "Error: cannot acquire lock on \"%s\" for trimming\n",
115                         sglob->slurpd_replogfile, 0, 0 );
116             } else {
117                 sglob->rq->rq_write( sglob->rq, fp );
118                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
119             }
120         }
121     }
122     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
123     return NULL;
124 }
125
126
127
128
129 /*
130  * Set a global flag which signals that we're shutting down.
131  */
132 static RETSIGTYPE
133 set_shutdown(int x)
134 {
135     int i;
136
137     sglob->slurpd_shutdown = 1;                         /* set flag */
138     pthread_kill( sglob->fm_tid, LDAP_SIGUSR1 );        /* wake up file mgr */
139     sglob->rq->rq_lock( sglob->rq );                    /* lock queue */
140     pthread_cond_broadcast( &(sglob->rq->rq_more) );    /* wake repl threads */
141     for ( i = 0; i < sglob->num_replicas; i++ ) {
142         (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
143     }
144     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
145     (void) SIGNAL( SIGTERM, set_shutdown );     /* reinstall handlers */
146     (void) SIGNAL( SIGINT, set_shutdown );
147     (void) SIGNAL( SIGHUP, set_shutdown );
148 }
149
150
151
152
153 /*
154  * A do-nothing signal handler.
155  */
156 RETSIGTYPE
157 do_nothing(int i)
158 {
159     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
160 }
161
162
163
164
165 /*
166  * Open the slurpd replication log, seek to our last known position, and
167  * process any pending replication entries.
168  */
169 static void
170 populate_queue(
171     char *f
172 )
173 {
174     FILE        *fp, *lfp;
175     Rq          *rq = sglob->rq;
176     char        *p;
177
178     if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
179         Debug( LDAP_DEBUG_ANY,
180                 "error: can't lock file \"%s\": %s\n",
181                 f, sys_errlist[ errno ], 0 );
182         return;
183     }
184
185     /*
186      * Read replication records from fp and append them the
187      * the queue.
188      */
189     if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
190         Debug( LDAP_DEBUG_ANY,
191                 "error: can't seek to offset %ld in file \"%s\"\n",
192                 sglob->srpos, f, 0 );
193     } else {
194     while (( p = get_record( fp )) != NULL ) {
195         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
196             char *t;
197             /* Print an error message.  Only print first line.  */
198             if (( t = strchr( p, '\n' )) != NULL ) {
199                 *t = '\0';
200             }
201             Debug( LDAP_DEBUG_ANY,
202                     "error: malformed replog entry (begins with \"%s\")\n",
203                     p, 0, 0 );
204         }
205         free( p );
206         pthread_yield();
207     }
208     sglob->srpos = ftell( fp );
209     }
210     (void) relinquish_lock( f, fp, lfp );
211 }
212     
213
214
215
216 /*
217  * Get the next "record" from the file pointed to by fp.  A "record"
218  * is delimited by two consecutive newlines.  Returns NULL on EOF.
219  */
220 static char *
221 get_record(
222     FILE *fp
223 )
224 {
225     int         len;
226     static char line[BUFSIZ];
227     char        *buf = NULL;
228     static int  lcur, lmax;
229
230     lcur = lmax = 0;
231
232     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
233             (( len = strlen( line )) > 1 )) {
234         while ( lcur + len + 1 > lmax ) {
235             lmax += BUFSIZ;
236             buf = (char *) ch_realloc( buf, lmax );
237         }
238         strcpy( buf + lcur, line );
239         lcur += len;
240     }
241     return( buf );
242 }