summaryrefslogtreecommitdiffstats
path: root/source4/samba/service.c
blob: 2ad6c8f371592bb928767caecfe00635b15e7ffe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* 
   Unix SMB/CIFS implementation.

   SERVER SERVICE code

   Copyright (C) Andrew Tridgell 2003-2005
   Copyright (C) Stefan (metze) Metzmacher	2004
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "../lib/util/dlinklist.h"
#include "samba/process_model.h"
#include "lib/util/samba_modules.h"

#undef strcasecmp

/*
  a linked list of registered servers
*/
static struct registered_server {
	struct registered_server *next, *prev;
	const char *service_name;
	const struct service_details *service_details;
} *registered_servers;

/*
  register a server service. 
*/
NTSTATUS register_server_service(TALLOC_CTX *ctx,
				const char *name,
				const struct service_details *details)
{
	struct registered_server *srv;
	srv = talloc(ctx, struct registered_server);
	NT_STATUS_HAVE_NO_MEMORY(srv);
	srv->service_name = name;
	srv->service_details =
		talloc_memdup(ctx, details, sizeof(struct service_details));
	NT_STATUS_HAVE_NO_MEMORY(srv->service_details);
	DLIST_ADD_END(registered_servers, srv);
	return NT_STATUS_OK;
}


/*
  initialise a server service
*/
static NTSTATUS server_service_init(const char *name,
				    struct tevent_context *event_context,
				    struct loadparm_context *lp_ctx,
				    const struct model_ops *model_ops,
				    int from_parent_fd)
{
	struct registered_server *srv;
	for (srv=registered_servers; srv; srv=srv->next) {
		if (strcasecmp(name, srv->service_name) == 0) {
			return task_server_startup(event_context, lp_ctx,
						   srv->service_name,
						   model_ops,
						   srv->service_details,
						   from_parent_fd);
		}
	}
	return NT_STATUS_INVALID_SYSTEM_SERVICE;
}


/*
  startup all of our server services
*/
NTSTATUS server_service_startup(struct tevent_context *event_ctx,
				struct loadparm_context *lp_ctx,
				const char *model, const char **server_services,
				int from_parent_fd)
{
	int i;
	const struct model_ops *model_ops;

	if (!server_services) {
		DBG_ERR("server_service_startup: "
			"no endpoint servers configured\n");
		return NT_STATUS_INVALID_PARAMETER;
	}

	model_ops = process_model_startup(model);
	if (!model_ops) {
		DBG_ERR("process_model_startup('%s') failed\n", model);
		return NT_STATUS_INTERNAL_ERROR;
	}

	for (i=0;server_services[i];i++) {
		NTSTATUS status;

		status = server_service_init(server_services[i], event_ctx,
					     lp_ctx, model_ops, from_parent_fd);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_ERR("Failed to start service '%s' - %s\n",
				 server_services[i], nt_errstr(status));
		}
		NT_STATUS_NOT_OK_RETURN(status);
	}

	return NT_STATUS_OK;
}

_PUBLIC_ NTSTATUS samba_service_init(void)
{
#define _MODULE_PROTO(init) extern NTSTATUS init(TALLOC_CTX *);
	STATIC_service_MODULES_PROTO;
	init_module_fn static_init[] = { STATIC_service_MODULES };
	init_module_fn *shared_init = NULL;
	static bool initialised;

	if (initialised) {
		return NT_STATUS_OK;
	}
	initialised = true;

	shared_init = load_samba_modules(NULL, "service");

	run_init_functions(NULL, static_init);
	run_init_functions(NULL, shared_init);

	TALLOC_FREE(shared_init);

	return NT_STATUS_OK;
}