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