r1277: rename struct server_context to smbsrv_ontext
[kai/samba.git] / source4 / smbd / process_model.c
1 /* 
2    Unix SMB/CIFS implementation.
3    process model manager - main loop
4    Copyright (C) Andrew Tridgell 1992-2003
5    Copyright (C) James J Myers 2003 <myersjj@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /*
25   setup the events for the chosen process model
26 */
27 void process_model_startup(struct event_context *events, 
28                                 const char *model)
29 {
30         const struct model_ops *ops;
31
32         ops = process_model_byname(model);
33         if (!ops) {
34                 DEBUG(0,("Unknown process model '%s'\n", model));
35                 exit(-1);
36         }
37
38         ops->model_startup();
39
40         /* now setup the listening sockets, adding 
41            event handlers to the events structure */
42         open_sockets_smbd(events, ops);
43
44         /* setup any sockets we need to listen on for RPC over TCP */
45         open_sockets_rpc(events, ops);
46 }
47
48 /* the list of currently registered process models */
49 static struct {
50         struct model_ops *ops;
51 } *models = NULL;
52 static int num_models;
53
54 /*
55   register a process model. 
56
57   The 'name' can be later used by other backends to find the operations
58   structure for this backend.  
59 */
60 static NTSTATUS register_process_model(void *_ops)
61 {
62         const struct model_ops *ops = _ops;
63
64         if (process_model_byname(ops->name) != NULL) {
65                 /* its already registered! */
66                 DEBUG(0,("PROCESS_MODEL '%s' already registered\n", 
67                          ops->name));
68                 return NT_STATUS_OBJECT_NAME_COLLISION;
69         }
70
71         models = Realloc(models, sizeof(models[0]) * (num_models+1));
72         if (!models) {
73                 smb_panic("out of memory in register_process_model");
74         }
75
76         models[num_models].ops = smb_xmemdup(ops, sizeof(*ops));
77         models[num_models].ops->name = smb_xstrdup(ops->name);
78
79         num_models++;
80
81         DEBUG(3,("PROCESS_MODEL '%s' registered\n", 
82                  ops->name));
83
84         return NT_STATUS_OK;
85 }
86
87 /*
88   return the operations structure for a named backend of the specified type
89 */
90 const struct model_ops *process_model_byname(const char *name)
91 {
92         int i;
93
94         for (i=0;i<num_models;i++) {
95                 if (strcmp(models[i].ops->name, name) == 0) {
96                         return models[i].ops;
97                 }
98         }
99
100         return NULL;
101 }
102
103 /*
104   return the PROCESS_MODEL module version, and the size of some critical types
105   This can be used by process model modules to either detect compilation errors, or provide
106   multiple implementations for different smbd compilation options in one module
107 */
108 const struct process_model_critical_sizes *process_model_version(void)
109 {
110         static const struct process_model_critical_sizes critical_sizes = {
111                 PROCESS_MODEL_VERSION,
112                 sizeof(struct model_ops),
113                 sizeof(struct smbsrv_context),
114                 sizeof(struct event_context),
115                 sizeof(struct fd_event)
116         };
117
118         return &critical_sizes;
119 }
120
121 /*
122   initialise the PROCESS_MODEL subsystem
123 */
124 BOOL process_model_init(void)
125 {
126         NTSTATUS status;
127
128         status = register_subsystem("process_model", register_process_model); 
129         if (!NT_STATUS_IS_OK(status)) {
130                 return False;
131         }
132
133         /* FIXME: Perhaps panic if a basic process model, such as simple, fails to initialise? */
134         static_init_process_model;
135
136         DEBUG(3,("PROCESS subsystem version %d initialised\n", PROCESS_MODEL_VERSION));
137         return True;
138 }