]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
Update install docs
[openldap] / servers / slurpd / fm.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1996 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  */
13
14 /*
15  * fm.c - file management routines.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/stdlib.h>
23 #include <ac/string.h>
24 #include <ac/signal.h>
25
26 #include "slurp.h"
27 #include "globals.h"
28
29
30 /*
31  * Forward references
32  */
33 static char *get_record LDAP_P(( FILE * ));
34 static void populate_queue LDAP_P(( char *f ));
35 static RETSIGTYPE set_shutdown LDAP_P((int));
36
37
38 /*
39  * Main file manager routine.  Watches for new data to be appended to the
40  * slapd replication log.  When new data is appended, fm does the following:
41  *  - appends the data to slurpd's private copy of the replication log.
42  *  - truncates the slapd replog
43  *  - adds items to the internal queue of replication work to do
44  *  - signals the replication threads to let them know new work has arrived.
45  */
46 void *
47 fm(
48     void *arg
49 )
50 {
51     int rc;
52
53     /* Set up our signal handlers:
54      * SIG{TERM,INT,HUP} causes a shutdown
55      * LDAP_SIGUSR1 - does nothing, used to wake up sleeping threads.
56          * LDAP_SIGUSR2 - causes a shutdown
57      */
58     (void) SIGNAL( LDAP_SIGUSR1, do_nothing );
59     (void) SIGNAL( LDAP_SIGUSR2, set_shutdown );
60     (void) SIGNAL( SIGTERM, set_shutdown );
61     (void) SIGNAL( SIGINT, set_shutdown );
62 #ifdef SIGHUP
63     (void) SIGNAL( SIGHUP, set_shutdown );
64 #endif
65
66     if ( sglob->one_shot_mode ) {
67         if ( file_nonempty( sglob->slapd_replogfile )) {
68             populate_queue( sglob->slapd_replogfile );
69         }
70         printf( "Processing in one-shot mode:\n" );
71         printf( "%d total replication records in file,\n",
72                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
73         printf( "%d replication records to process.\n",
74                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
75         return NULL;
76     }
77     /*
78      * There may be some leftover replication records in our own
79      * copy of the replication log.  If any exist, add them to the
80      * queue.
81      */
82     if ( file_nonempty( sglob->slurpd_replogfile )) {
83         populate_queue( sglob->slurpd_replogfile );
84     }
85
86
87     while ( !sglob->slurpd_shutdown ) {
88         if ( file_nonempty( sglob->slapd_replogfile )) {
89             /* New work found - copy to slurpd replog file */
90             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
91                     sglob->slapd_replogfile, 0, 0 );
92             if (( rc = copy_replog( sglob->slapd_replogfile,
93                     sglob->slurpd_replogfile )) == 0 )  {
94                 populate_queue( sglob->slurpd_replogfile );
95             } else {
96                 if ( rc < 0 ) {
97                     Debug( LDAP_DEBUG_ANY,
98                             "Fatal error while copying replication log\n",
99                             0, 0, 0 );
100                     sglob->slurpd_shutdown = 1;
101                 }
102             }
103         } else {
104             ldap_pvt_thread_sleep( sglob->no_work_interval );
105         }
106
107         /* Garbage-collect queue */
108         sglob->rq->rq_gc( sglob->rq );
109
110         /* Trim replication log file, if needed */
111         if ( sglob->rq->rq_needtrim( sglob->rq )) {
112             FILE *fp, *lfp;
113             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
114                     &lfp )) < 0 ) {
115                 Debug( LDAP_DEBUG_ANY,
116                         "Error: cannot acquire lock on \"%s\" for trimming\n",
117                         sglob->slurpd_replogfile, 0, 0 );
118             } else {
119                 sglob->rq->rq_write( sglob->rq, fp );
120                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
121             }
122         }
123     }
124     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
125     return NULL;
126 }
127
128
129
130
131 /*
132  * Set a global flag which signals that we're shutting down.
133  */
134 static RETSIGTYPE
135 set_shutdown(int sig)
136 {
137     int i;
138
139     sglob->slurpd_shutdown = 1;                         /* set flag */
140     ldap_pvt_thread_kill( sglob->fm_tid, LDAP_SIGUSR1 );        /* wake up file mgr */
141     sglob->rq->rq_lock( sglob->rq );                    /* lock queue */
142     ldap_pvt_thread_cond_broadcast( &(sglob->rq->rq_more) );    /* wake repl threads */
143     for ( i = 0; i < sglob->num_replicas; i++ ) {
144         (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
145     }
146     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
147     (void) SIGNAL_REINSTALL( sig, set_shutdown );       /* reinstall handlers */
148 }
149
150
151
152
153 /*
154  * A do-nothing signal handler.
155  */
156 RETSIGTYPE
157 do_nothing(int sig)
158 {
159     (void) SIGNAL_REINSTALL( sig, 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     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
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 }