r12434: implement database scavenging, the only missing part is the verifying of...
[samba.git] / source / wrepl_server / wrepl_periodic.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "dlinklist.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "lib/messaging/irpc.h"
30 #include "librpc/gen_ndr/ndr_winsrepl.h"
31 #include "wrepl_server/wrepl_server.h"
32 #include "nbt_server/wins/winsdb.h"
33 #include "ldb/include/ldb.h"
34 #include "libcli/composite/composite.h"
35 #include "libcli/wrepl/winsrepl.h"
36 #include "wrepl_server/wrepl_out_helpers.h"
37
38 static NTSTATUS wreplsrv_periodic_run(struct wreplsrv_service *service)
39 {
40         NTSTATUS status;
41
42         status = wreplsrv_scavenging_run(service);
43         NT_STATUS_NOT_OK_RETURN(status);
44
45         status = wreplsrv_out_pull_run(service);
46         NT_STATUS_NOT_OK_RETURN(status);
47
48         status = wreplsrv_out_push_run(service);
49         NT_STATUS_NOT_OK_RETURN(status);
50
51         return NT_STATUS_OK;
52 }
53
54 static void wreplsrv_periodic_handler_te(struct event_context *ev, struct timed_event *te,
55                                          struct timeval t, void *ptr)
56 {
57         struct wreplsrv_service *service = talloc_get_type(ptr, struct wreplsrv_service);
58         NTSTATUS status;
59
60         service->periodic.te = NULL;
61
62         status = wreplsrv_periodic_schedule(service, service->config.periodic_interval);
63         if (!NT_STATUS_IS_OK(status)) {
64                 task_server_terminate(service->task, nt_errstr(status));
65                 return;
66         }
67
68         status = wreplsrv_periodic_run(service);
69         if (!NT_STATUS_IS_OK(status)) {
70                 DEBUG(0,("wresrv_periodic_run() failed: %s\n", nt_errstr(status)));
71         }
72 }
73
74 NTSTATUS wreplsrv_periodic_schedule(struct wreplsrv_service *service, uint32_t next_interval)
75 {
76         TALLOC_CTX *tmp_mem;
77         struct timed_event *new_te;
78         struct timeval next_time;
79
80         /* prevent looping */
81         if (next_interval == 0) next_interval = 1;
82
83         next_time = timeval_current_ofs(next_interval, 5000);
84
85         if (service->periodic.te) {
86                 /*
87                  * if the timestamp of the new event is higher,
88                  * as current next we don't need to reschedule
89                  */
90                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
91                         return NT_STATUS_OK;
92                 }
93         }
94
95         /* reset the next scheduled timestamp */
96         service->periodic.next_event = next_time;
97
98         new_te = event_add_timed(service->task->event_ctx, service,
99                                  service->periodic.next_event,
100                                  wreplsrv_periodic_handler_te, service);
101         NT_STATUS_HAVE_NO_MEMORY(new_te);
102
103         tmp_mem = talloc_new(service);
104         DEBUG(4,("wreplsrv_periodic_schedule(%u) %sscheduled for: %s\n",
105                 next_interval,
106                 (service->periodic.te?"re":""),
107                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
108         talloc_free(tmp_mem);
109
110         talloc_free(service->periodic.te);
111         service->periodic.te = new_te;
112
113         return NT_STATUS_OK;
114 }
115
116 NTSTATUS wreplsrv_setup_periodic(struct wreplsrv_service *service)
117 {
118         NTSTATUS status;
119
120         status = wreplsrv_periodic_schedule(service, 0);
121         NT_STATUS_NOT_OK_RETURN(status);
122
123         return NT_STATUS_OK;
124 }