e0e98f644e1c5b51ff120c4051d8a6e97aa70fa0
[samba.git] / source4 / smbd / service_task.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    helper functions for task based servers (nbtd, winbind etc)
5
6    Copyright (C) Andrew Tridgell 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "process_model.h"
24 #include "lib/messaging/irpc.h"
25 #include "param/param.h"
26 #include "librpc/gen_ndr/ndr_irpc_c.h"
27
28 /*
29   terminate a task service
30 */
31 void task_server_terminate(struct task_server *task, const char *reason, bool fatal)
32 {
33         struct tevent_context *event_ctx = task->event_ctx;
34         const struct model_ops *model_ops = task->model_ops;
35         DEBUG(0,("task_server_terminate: [%s]\n", reason));
36
37         if (fatal && task->msg_ctx != NULL) {
38                 struct dcerpc_binding_handle *irpc_handle;
39                 struct samba_terminate r;
40
41                 irpc_handle = irpc_binding_handle_by_name(task, task->msg_ctx,
42                                                           "samba", &ndr_table_irpc);
43                 if (irpc_handle != NULL) {
44                         /* Note: this makes use of nested event loops... */
45                         dcerpc_binding_handle_set_sync_ev(irpc_handle, event_ctx);
46                         r.in.reason = reason;
47                         dcerpc_samba_terminate_r(irpc_handle, task, &r);
48                 }
49         }
50
51         imessaging_cleanup(task->msg_ctx);
52
53         model_ops->terminate(event_ctx, task->lp_ctx, reason);
54         
55         /* don't free this above, it might contain the 'reason' being printed */
56         talloc_free(task);
57 }
58
59 /* used for the callback from the process model code */
60 struct task_state {
61         void (*task_init)(struct task_server *);
62         const struct model_ops *model_ops;
63 };
64
65
66 /*
67   called by the process model code when the new task starts up. This then calls
68   the server specific startup code
69 */
70 static void task_server_callback(struct tevent_context *event_ctx, 
71                                  struct loadparm_context *lp_ctx,
72                                  struct server_id server_id, void *private_data)
73 {
74         struct task_state *state = talloc_get_type(private_data, struct task_state);
75         struct task_server *task;
76
77         task = talloc(event_ctx, struct task_server);
78         if (task == NULL) return;
79
80         task->event_ctx = event_ctx;
81         task->model_ops = state->model_ops;
82         task->server_id = server_id;
83         task->lp_ctx = lp_ctx;
84
85         task->msg_ctx = imessaging_init(task,
86                                         task->lp_ctx,
87                                         task->server_id,
88                                         task->event_ctx);
89         if (!task->msg_ctx) {
90                 task_server_terminate(task, "imessaging_init() failed", true);
91                 return;
92         }
93
94         state->task_init(task);
95 }
96
97 /*
98   startup a task based server
99 */
100 NTSTATUS task_server_startup(struct tevent_context *event_ctx, 
101                              struct loadparm_context *lp_ctx,
102                              const char *service_name, 
103                              const struct model_ops *model_ops, 
104                              void (*task_init)(struct task_server *),
105                              int from_parent_fd)
106 {
107         struct task_state *state;
108
109         state = talloc(event_ctx, struct task_state);
110         NT_STATUS_HAVE_NO_MEMORY(state);
111
112         state->task_init = task_init;
113         state->model_ops = model_ops;
114
115         state->model_ops->new_task(event_ctx, lp_ctx, service_name,
116                                    task_server_callback, state,
117                                    from_parent_fd);
118
119         return NT_STATUS_OK;
120 }
121
122 /*
123   setup a task title 
124 */
125 void task_server_set_title(struct task_server *task, const char *title)
126 {
127         task->model_ops->set_title(task->event_ctx, title);
128 }