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