Merge commit 'release-4-0-0alpha1' into v4-0-test
[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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/process_model.h"
23 #include "build.h"
24 #include "param/param.h"
25
26 /*
27   setup the events for the chosen process model
28 */
29 _PUBLIC_ const struct model_ops *process_model_startup(struct event_context *ev, const char *model)
30 {
31         const struct model_ops *ops;
32
33         ops = process_model_byname(model);
34         if (!ops) {
35                 DEBUG(0,("Unknown process model '%s'\n", model));
36                 exit(-1);
37         }
38
39         ops->model_init(ev);
40
41         return ops;
42 }
43
44 /* the list of currently registered process models */
45 static struct process_model {
46         struct model_ops *ops;
47 } *models = NULL;
48 static int num_models;
49
50 /*
51   register a process model. 
52
53   The 'name' can be later used by other backends to find the operations
54   structure for this backend.  
55 */
56 _PUBLIC_ NTSTATUS register_process_model(const void *_ops)
57 {
58         const struct model_ops *ops = _ops;
59
60         if (process_model_byname(ops->name) != NULL) {
61                 /* its already registered! */
62                 DEBUG(0,("PROCESS_MODEL '%s' already registered\n", 
63                          ops->name));
64                 return NT_STATUS_OBJECT_NAME_COLLISION;
65         }
66
67         models = realloc_p(models, struct process_model, num_models+1);
68         if (!models) {
69                 smb_panic("out of memory in register_process_model");
70         }
71
72         models[num_models].ops = smb_xmemdup(ops, sizeof(*ops));
73         models[num_models].ops->name = smb_xstrdup(ops->name);
74
75         num_models++;
76
77         DEBUG(3,("PROCESS_MODEL '%s' registered\n", 
78                  ops->name));
79
80         return NT_STATUS_OK;
81 }
82
83 NTSTATUS process_model_init(void)
84 {
85         init_module_fn static_init[] = STATIC_process_model_MODULES;
86         init_module_fn *shared_init = load_samba_modules(NULL, global_loadparm, "process_model");
87
88         run_init_functions(static_init);
89         run_init_functions(shared_init);
90
91         talloc_free(shared_init);
92         
93         return NT_STATUS_OK;
94 }
95
96 /*
97   return the operations structure for a named backend of the specified type
98 */
99 const struct model_ops *process_model_byname(const char *name)
100 {
101         int i;
102
103         for (i=0;i<num_models;i++) {
104                 if (strcmp(models[i].ops->name, name) == 0) {
105                         return models[i].ops;
106                 }
107         }
108
109         return NULL;
110 }
111
112 /*
113   return the PROCESS_MODEL module version, and the size of some critical types
114   This can be used by process model modules to either detect compilation errors, or provide
115   multiple implementations for different smbd compilation options in one module
116 */
117 const struct process_model_critical_sizes *process_model_version(void)
118 {
119         static const struct process_model_critical_sizes critical_sizes = {
120                 PROCESS_MODEL_VERSION,
121                 sizeof(struct model_ops)
122         };
123
124         return &critical_sizes;
125 }