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