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