r5411: make network interface selection a bit saner
[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 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 "process_model.h"
25 #include "lib/events/events.h"
26 #include "smbd/service_task.h"
27
28 /*
29   terminate a task service
30 */
31 void task_terminate(struct task_server *task, const char *reason)
32 {
33         struct event_context *event_ctx = task->event_ctx;
34         const struct model_ops *model_ops = task->model_ops;
35         DEBUG(0,("task_terminate: [%s]\n", reason));
36         talloc_free(task);
37         model_ops->terminate(event_ctx, reason);
38 }
39
40 /* used for the callback from the process model code */
41 struct task_state {
42         void (*task_init)(struct task_server *);
43         const struct model_ops *model_ops;
44 };
45
46
47 /*
48   called by the process model code when the new task starts up. This then calls
49   the server specific startup code
50 */
51 static void task_server_callback(struct event_context *event_ctx, uint32_t server_id, void *private)
52 {
53         struct task_state *state = talloc_get_type(private, struct task_state);
54         struct task_server *task;
55
56         task = talloc(event_ctx, struct task_server);
57         if (task == NULL) return;
58
59         task->event_ctx = event_ctx;
60         task->model_ops = state->model_ops;
61         task->server_id = server_id;
62
63         task->msg_ctx = messaging_init(task, task->server_id, task->event_ctx);
64         if (!task->msg_ctx) {
65                 task_terminate(task, "messaging_init() failed");
66                 return;
67         }
68
69         state->task_init(task);
70 }
71
72 /*
73   startup a task based server
74 */
75 NTSTATUS task_server_startup(struct event_context *event_ctx, 
76                              const struct model_ops *model_ops, 
77                              void (*task_init)(struct task_server *))
78 {
79         struct task_state *state;
80
81         state = talloc(event_ctx, struct task_state);
82         NT_STATUS_HAVE_NO_MEMORY(state);
83
84         state->task_init = task_init;
85         state->model_ops = model_ops;
86         
87         model_ops->new_task(event_ctx, task_server_callback, state);
88
89         return NT_STATUS_OK;
90 }
91