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