s4-process_model Fix valgrind-found use of un-initialised variable
[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 /* the list of currently registered process models */
26 static struct process_model {
27         struct model_ops *ops;
28         bool initialised;
29 } *models = NULL;
30 static int num_models;
31
32
33 /*
34   return the operations structure for a named backend of the specified type
35 */
36 static struct process_model *process_model_byname(const char *name)
37 {
38         int i;
39
40         for (i=0;i<num_models;i++) {
41                 if (strcmp(models[i].ops->name, name) == 0) {
42                         return &models[i];
43                 }
44         }
45
46         return NULL;
47 }
48
49
50 /*
51   setup the events for the chosen process model
52 */
53 _PUBLIC_ const struct model_ops *process_model_startup(const char *model)
54 {
55         struct process_model *m;
56
57         m = process_model_byname(model);
58         if (m == NULL) {
59                 DEBUG(0,("Unknown process model '%s'\n", model));
60                 exit(-1);
61         }
62
63         if (!m->initialised) {
64                 m->initialised = true;
65                 m->ops->model_init();
66         }
67
68         return m->ops;
69 }
70
71 /*
72   register a process model. 
73
74   The 'name' can be later used by other backends to find the operations
75   structure for this backend.  
76 */
77 _PUBLIC_ NTSTATUS register_process_model(const void *_ops)
78 {
79         const struct model_ops *ops = _ops;
80
81         if (process_model_byname(ops->name) != NULL) {
82                 /* its already registered! */
83                 DEBUG(0,("PROCESS_MODEL '%s' already registered\n", 
84                          ops->name));
85                 return NT_STATUS_OBJECT_NAME_COLLISION;
86         }
87
88         models = realloc_p(models, struct process_model, num_models+1);
89         if (!models) {
90                 smb_panic("out of memory in register_process_model");
91         }
92
93         models[num_models].ops = smb_xmemdup(ops, sizeof(*ops));
94         models[num_models].ops->name = smb_xstrdup(ops->name);
95         models[num_models].initialised = false;
96
97         num_models++;
98
99         DEBUG(3,("PROCESS_MODEL '%s' registered\n", 
100                  ops->name));
101
102         return NT_STATUS_OK;
103 }
104
105 _PUBLIC_ NTSTATUS process_model_init(struct loadparm_context *lp_ctx)
106 {
107 #define _MODULE_PROTO(init) extern NTSTATUS init(void);
108         STATIC_process_model_MODULES_PROTO;
109         init_module_fn static_init[] = { STATIC_process_model_MODULES };
110         init_module_fn *shared_init;
111         static bool initialised;
112
113         if (initialised) {
114                 return NT_STATUS_OK;
115         }
116         initialised = true;
117
118         shared_init = load_samba_modules(NULL, lp_ctx, "process_model");
119         
120         run_init_functions(static_init);
121         run_init_functions(shared_init);
122
123         talloc_free(shared_init);
124
125         return NT_STATUS_OK;
126 }
127
128 /*
129   return the PROCESS_MODEL module version, and the size of some critical types
130   This can be used by process model modules to either detect compilation errors, or provide
131   multiple implementations for different smbd compilation options in one module
132 */
133 const struct process_model_critical_sizes *process_model_version(void)
134 {
135         static const struct process_model_critical_sizes critical_sizes = {
136                 PROCESS_MODEL_VERSION,
137                 sizeof(struct model_ops)
138         };
139
140         return &critical_sizes;
141 }