38e9c710aae7fd8a29a07da04923f2af2d665ff3
[metze/samba/wip.git] / source4 / dsdb / repl / drepl_periodic.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB replication service periodic handling
4    
5    Copyright (C) Stefan Metzmacher 2007
6     
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24 #include "dsdb/samdb/samdb.h"
25 #include "auth/auth.h"
26 #include "smbd/service.h"
27 #include "lib/events/events.h"
28 #include "lib/messaging/irpc.h"
29 #include "dsdb/repl/drepl_service.h"
30 #include "lib/ldb/include/ldb_errors.h"
31 #include "lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
35
36 static void dreplsrv_periodic_run(struct dreplsrv_service *service);
37
38 static void dreplsrv_periodic_handler_te(struct event_context *ev, struct timed_event *te,
39                                          struct timeval t, void *ptr)
40 {
41         struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
42         WERROR status;
43
44         service->periodic.te = NULL;
45
46         dreplsrv_periodic_run(service);
47
48         status = dreplsrv_periodic_schedule(service, service->periodic.interval);
49         if (!W_ERROR_IS_OK(status)) {
50                 task_server_terminate(service->task, win_errstr(status));
51                 return;
52         }
53 }
54
55 WERROR dreplsrv_periodic_schedule(struct dreplsrv_service *service, uint32_t next_interval)
56 {
57         TALLOC_CTX *tmp_mem;
58         struct timed_event *new_te;
59         struct timeval next_time;
60
61         /* prevent looping */
62         if (next_interval == 0) next_interval = 1;
63
64         next_time = timeval_current_ofs(next_interval, 50);
65
66         if (service->periodic.te) {
67                 /*
68                  * if the timestamp of the new event is higher,
69                  * as current next we don't need to reschedule
70                  */
71                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
72                         return WERR_OK;
73                 }
74         }
75
76         /* reset the next scheduled timestamp */
77         service->periodic.next_event = next_time;
78
79         new_te = event_add_timed(service->task->event_ctx, service,
80                                  service->periodic.next_event,
81                                  dreplsrv_periodic_handler_te, service);
82         W_ERROR_HAVE_NO_MEMORY(new_te);
83
84         tmp_mem = talloc_new(service);
85         DEBUG(2,("dreplsrv_periodic_schedule(%u) %sscheduled for: %s\n",
86                 next_interval,
87                 (service->periodic.te?"re":""),
88                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
89         talloc_free(tmp_mem);
90
91         talloc_free(service->periodic.te);
92         service->periodic.te = new_te;
93
94         return WERR_OK;
95 }
96
97 static void dreplsrv_periodic_run(struct dreplsrv_service *service)
98 {
99         TALLOC_CTX *mem_ctx;
100
101         DEBUG(2,("dreplsrv_periodic_run(): schedule pull replication\n"));
102
103         mem_ctx = talloc_new(service);
104         dreplsrv_schedule_pull_replication(service, mem_ctx);
105         talloc_free(mem_ctx);
106
107         DEBUG(2,("dreplsrv_periodic_run(): run pending_ops\n"));
108
109         dreplsrv_run_pending_ops(service);
110 }