r17930: Merge noinclude branch:
[kai/samba.git] / source4 / smbd / service.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SERVER SERVICE code
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) Stefan (metze) Metzmacher      2004
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
25 #include "lib/util/dlinklist.h"
26 #include "smbd/process_model.h"
27
28 /*
29   a linked list of registered servers
30 */
31 static struct registered_server {
32         struct registered_server *next, *prev;
33         const char *service_name;
34         NTSTATUS (*service_init)(struct event_context *, const struct model_ops *);
35 } *registered_servers;
36
37 /*
38   register a server service. 
39 */
40 NTSTATUS register_server_service(const char *name,
41                                  NTSTATUS (*service_init)(struct event_context *, const struct model_ops *))
42 {
43         struct registered_server *srv;
44         srv = talloc(talloc_autofree_context(), struct registered_server);
45         NT_STATUS_HAVE_NO_MEMORY(srv);
46         srv->service_name = name;
47         srv->service_init = service_init;
48         DLIST_ADD_END(registered_servers, srv, struct registered_server *);
49         return NT_STATUS_OK;
50 }
51
52
53 /*
54   initialise a server service
55 */
56 static NTSTATUS server_service_init(const char *name,
57                                     struct event_context *event_ctx,
58                                     const struct model_ops *model_ops)
59 {
60         struct registered_server *srv;
61         for (srv=registered_servers; srv; srv=srv->next) {
62                 if (strcasecmp(name, srv->service_name) == 0) {
63                         return srv->service_init(event_ctx, model_ops);
64                 }
65         }
66         return NT_STATUS_INVALID_SYSTEM_SERVICE;
67 }
68
69
70 /*
71   startup all of our server services
72 */
73 NTSTATUS server_service_startup(struct event_context *event_ctx, 
74                                 const char *model, const char **server_services)
75 {
76         int i;
77         const struct model_ops *model_ops;
78
79         if (!server_services) {
80                 DEBUG(0,("server_service_startup: no endpoint servers configured\n"));
81                 return NT_STATUS_INVALID_PARAMETER;
82         }
83
84         model_ops = process_model_startup(event_ctx, model);
85         if (!model_ops) {
86                 DEBUG(0,("process_model_startup('%s') failed\n", model));
87                 return NT_STATUS_INTERNAL_ERROR;
88         }
89
90         for (i=0;server_services[i];i++) {
91                 NTSTATUS status;
92
93                 status = server_service_init(server_services[i], event_ctx, model_ops);
94                 if (!NT_STATUS_IS_OK(status)) {
95                         DEBUG(0,("Failed to start service '%s' - %s\n", 
96                                  server_services[i], nt_errstr(status)));
97                 }
98                 NT_STATUS_NOT_OK_RETURN(status);
99         }
100
101         return NT_STATUS_OK;
102 }