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