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