r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[kai/samba-autobuild/.git] / source4 / 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 "lib/events/events.h"
25 #include "smbd/service_task.h"
26 #include "smbd/service.h"
27 #include "librpc/gen_ndr/winsrepl.h"
28 #include "wrepl_server/wrepl_server.h"
29
30 static NTSTATUS wreplsrv_periodic_run(struct wreplsrv_service *service)
31 {
32         NTSTATUS status;
33
34         status = wreplsrv_load_partners(service);
35         NT_STATUS_NOT_OK_RETURN(status);
36
37         status = wreplsrv_scavenging_run(service);
38         NT_STATUS_NOT_OK_RETURN(status);
39
40         status = wreplsrv_out_pull_run(service);
41         NT_STATUS_NOT_OK_RETURN(status);
42
43         status = wreplsrv_out_push_run(service);
44         NT_STATUS_NOT_OK_RETURN(status);
45
46         return NT_STATUS_OK;
47 }
48
49 static void wreplsrv_periodic_handler_te(struct event_context *ev, struct timed_event *te,
50                                          struct timeval t, void *ptr)
51 {
52         struct wreplsrv_service *service = talloc_get_type(ptr, struct wreplsrv_service);
53         NTSTATUS status;
54
55         service->periodic.te = NULL;
56
57         status = wreplsrv_periodic_schedule(service, service->config.periodic_interval);
58         if (!NT_STATUS_IS_OK(status)) {
59                 task_server_terminate(service->task, nt_errstr(status));
60                 return;
61         }
62
63         status = wreplsrv_periodic_run(service);
64         if (!NT_STATUS_IS_OK(status)) {
65                 DEBUG(0,("wresrv_periodic_run() failed: %s\n", nt_errstr(status)));
66         }
67 }
68
69 NTSTATUS wreplsrv_periodic_schedule(struct wreplsrv_service *service, uint32_t next_interval)
70 {
71         TALLOC_CTX *tmp_mem;
72         struct timed_event *new_te;
73         struct timeval next_time;
74
75         /* prevent looping */
76         if (next_interval == 0) next_interval = 1;
77
78         next_time = timeval_current_ofs(next_interval, 5000);
79
80         if (service->periodic.te) {
81                 /*
82                  * if the timestamp of the new event is higher,
83                  * as current next we don't need to reschedule
84                  */
85                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
86                         return NT_STATUS_OK;
87                 }
88         }
89
90         /* reset the next scheduled timestamp */
91         service->periodic.next_event = next_time;
92
93         new_te = event_add_timed(service->task->event_ctx, service,
94                                  service->periodic.next_event,
95                                  wreplsrv_periodic_handler_te, service);
96         NT_STATUS_HAVE_NO_MEMORY(new_te);
97
98         tmp_mem = talloc_new(service);
99         DEBUG(6,("wreplsrv_periodic_schedule(%u) %sscheduled for: %s\n",
100                 next_interval,
101                 (service->periodic.te?"re":""),
102                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
103         talloc_free(tmp_mem);
104
105         talloc_free(service->periodic.te);
106         service->periodic.te = new_te;
107
108         return NT_STATUS_OK;
109 }
110
111 NTSTATUS wreplsrv_setup_periodic(struct wreplsrv_service *service)
112 {
113         NTSTATUS status;
114
115         status = wreplsrv_periodic_schedule(service, 0);
116         NT_STATUS_NOT_OK_RETURN(status);
117
118         return NT_STATUS_OK;
119 }