]> git.sur5r.net Git - openldap/blob - servers/slurpd/fm.c
Initial revision
[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      * SIGUSR1 - does nothing, used to wake up sleeping threads.
81      * SIGUSR2 - causes slurpd to read its administrative interface file.
82      *           (not yet implemented).
83      */
84     (void) SIGNAL( SIGUSR1, (void *) do_nothing );
85     (void) SIGNAL( SIGUSR2, (void *) do_admin );
86     (void) SIGNAL( SIGTERM, (void *) set_shutdown );
87     (void) SIGNAL( SIGINT, (void *) set_shutdown );
88     (void) SIGNAL( SIGHUP, (void *) set_shutdown );
89
90     if ( sglob->one_shot_mode ) {
91         if ( file_nonempty( sglob->slapd_replogfile )) {
92             populate_queue( sglob->slapd_replogfile );
93         }
94         printf( "Processing in one-shot mode:\n" );
95         printf( "%d total replication records in file,\n",
96                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_ALL ));
97         printf( "%d replication records to process.\n",
98                 sglob->rq->rq_getcount( sglob->rq, RQ_COUNT_NZRC ));
99         return;
100     }
101     /*
102      * There may be some leftover replication records in our own
103      * copy of the replication log.  If any exist, add them to the
104      * queue.
105      */
106     if ( file_nonempty( sglob->slurpd_replogfile )) {
107         populate_queue( sglob->slurpd_replogfile );
108     }
109
110
111     while ( !sglob->slurpd_shutdown ) {
112         if ( file_nonempty( sglob->slapd_replogfile )) {
113             /* New work found - copy to slurpd replog file */
114             Debug( LDAP_DEBUG_ARGS, "new work in %s\n",
115                     sglob->slapd_replogfile, 0, 0 );
116             if (( rc = copy_replog( sglob->slapd_replogfile,
117                     sglob->slurpd_replogfile )) == 0 )  {
118                 populate_queue( sglob->slurpd_replogfile );
119             } else {
120                 if ( rc < 0 ) {
121                     Debug( LDAP_DEBUG_ANY,
122                             "Fatal error while copying replication log\n",
123                             0, 0, 0 );
124                     sglob->slurpd_shutdown = 1;
125                 }
126             }
127         } else {
128             tsleep( sglob->no_work_interval );
129         }
130
131         /* Garbage-collect queue */
132         sglob->rq->rq_gc( sglob->rq );
133
134         /* Trim replication log file, if needed */
135         if ( sglob->rq->rq_needtrim( sglob->rq )) {
136             FILE *fp, *lfp;
137             if (( rc = acquire_lock( sglob->slurpd_replogfile, &fp,
138                     &lfp )) < 0 ) {
139                 Debug( LDAP_DEBUG_ANY,
140                         "Error: cannot acquire lock on \"%s\" for trimming\n",
141                         sglob->slurpd_replogfile, 0, 0 );
142             } else {
143                 sglob->rq->rq_write( sglob->rq, fp );
144                 (void) relinquish_lock( sglob->slurpd_replogfile, fp, lfp );
145             }
146         }
147     }
148     Debug( LDAP_DEBUG_ARGS, "fm: exiting\n", 0, 0, 0 );
149 }
150
151
152
153
154 /*
155  * Set a global flag which signals that we're shutting down.
156  */
157 static void
158 set_shutdown()
159 {
160     int i;
161
162     sglob->slurpd_shutdown = 1;                         /* set flag */
163     pthread_kill( sglob->fm_tid, SIGUSR1 );             /* wake up file mgr */
164     sglob->rq->rq_lock( sglob->rq );                    /* lock queue */
165     pthread_cond_broadcast( &(sglob->rq->rq_more) );    /* wake repl threads */
166     for ( i = 0; i < sglob->num_replicas; i++ ) {
167         (sglob->replicas[ i ])->ri_wake( sglob->replicas[ i ]);
168     }
169     sglob->rq->rq_unlock( sglob->rq );                  /* unlock queue */
170     (void) SIGNAL( SIGTERM, (void *) set_shutdown );    /* reinstall handlers */
171     (void) SIGNAL( SIGINT, (void *) set_shutdown );
172     (void) SIGNAL( SIGHUP, (void *) set_shutdown );
173 }
174
175
176
177
178 /*
179  * A do-nothing signal handler.
180  */
181 void
182 do_nothing()
183 {
184     (void) SIGNAL( SIGUSR1, (void *) do_nothing );
185 }
186
187
188
189
190 /*
191  * Open the slurpd replication log, seek to our last known position, and
192  * process any pending replication entries.
193  */
194 static void
195 populate_queue(
196     char *f
197 )
198 {
199     FILE        *fp, *lfp;
200     Rq          *rq = sglob->rq;
201     char        *p;
202
203     if ( acquire_lock( f, &fp, &lfp ) < 0 ) {
204         Debug( LDAP_DEBUG_ANY,
205                 "error: can't lock file \"%s\": %s\n",
206                 f, sys_errlist[ errno ], 0 );
207         return;
208     }
209
210     /*
211      * Read replication records from fp and append them the
212      * the queue.
213      */
214     if ( fseek( fp, sglob->srpos, 0 ) < 0 ) {
215         Debug( LDAP_DEBUG_ANY,
216                 "error: can't seek to offset %ld in file \"%s\"\n",
217                 sglob->srpos, f, 0 );
218         return;
219     }
220     while (( p = get_record( fp )) != NULL ) {
221         if ( sglob->rq->rq_add( sglob->rq, p ) < 0 ) {
222             char *t;
223             /* Print an error message.  Only print first line.  */
224             if (( t = strchr( p, '\n' )) != NULL ) {
225                 *t = '\0';
226             }
227             Debug( LDAP_DEBUG_ANY,
228                     "error: malformed replog entry (begins with \"%s\")\n",
229                     p, 0, 0 );
230         }
231         free( p );
232         pthread_yield();
233     }
234     sglob->srpos = ftell( fp );
235     (void) relinquish_lock( f, fp, lfp );
236 }
237     
238
239
240
241 /*
242  * Get the next "record" from the file pointed to by fp.  A "record"
243  * is delimited by two consecutive newlines.  Returns NULL on EOF.
244  */
245 static char *
246 get_record(
247     FILE *fp
248 )
249 {
250     int         len;
251     static char line[BUFSIZ];
252     char        *buf = NULL;
253     static int  lcur, lmax;
254
255     lcur = lmax = 0;
256
257     while (( fgets( line, sizeof(line), fp ) != NULL ) &&
258             (( len = strlen( line )) > 1 )) {
259         while ( lcur + len + 1 > lmax ) {
260             lmax += BUFSIZ;
261             buf = (char *) ch_realloc( buf, lmax );
262         }
263         strcpy( buf + lcur, line );
264         lcur += len;
265     }
266     return( buf );
267 }
268