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