Merge commit 'release-4-0-0alpha1' into v4-0-test
[kai/samba.git] / source / 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/events/events.h"
25 #include "smbd/service.h"
26 #include "smbd/service_task.h"
27 #include "lib/messaging/irpc.h"
28 #include "param/param.h"
29
30 /*
31   terminate a task service
32 */
33 void task_server_terminate(struct task_server *task, const char *reason)
34 {
35         struct event_context *event_ctx = task->event_ctx;
36         const struct model_ops *model_ops = task->model_ops;
37         DEBUG(0,("task_server_terminate: [%s]\n", reason));
38         model_ops->terminate(event_ctx, reason);
39         
40         /* don't free this above, it might contain the 'reason' being printed */
41         talloc_free(task);
42 }
43
44 /* used for the callback from the process model code */
45 struct task_state {
46         void (*task_init)(struct task_server *);
47         const struct model_ops *model_ops;
48 };
49
50
51 /*
52   called by the process model code when the new task starts up. This then calls
53   the server specific startup code
54 */
55 static void task_server_callback(struct event_context *event_ctx, 
56                                  struct server_id server_id, void *private)
57 {
58         struct task_state *state = talloc_get_type(private, struct task_state);
59         struct task_server *task;
60
61         task = talloc(event_ctx, struct task_server);
62         if (task == NULL) return;
63
64         task->event_ctx = event_ctx;
65         task->model_ops = state->model_ops;
66         task->server_id = server_id;
67
68         task->msg_ctx = messaging_init(task, 
69                                        lp_messaging_path(task, global_loadparm),
70                                        task->server_id, task->event_ctx);
71         if (!task->msg_ctx) {
72                 task_server_terminate(task, "messaging_init() failed");
73                 return;
74         }
75
76         state->task_init(task);
77 }
78
79 /*
80   startup a task based server
81 */
82 NTSTATUS task_server_startup(struct event_context *event_ctx, 
83                              const struct model_ops *model_ops, 
84                              void (*task_init)(struct task_server *))
85 {
86         struct task_state *state;
87
88         state = talloc(event_ctx, struct task_state);
89         NT_STATUS_HAVE_NO_MEMORY(state);
90
91         state->task_init = task_init;
92         state->model_ops = model_ops;
93         
94         model_ops->new_task(event_ctx, task_server_callback, state);
95
96         return NT_STATUS_OK;
97 }
98
99 /*
100   setup a task title 
101 */
102 void task_server_set_title(struct task_server *task, const char *title)
103 {
104         task->model_ops->set_title(task->event_ctx, title);
105 }