4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
30 #include "nscd_common.h"
31 #include "nscd_config.h"
33 #include "nscd_switch.h"
34 #include "nscd_frontend.h"
36 static char *cfgfile_save
= NULL
;
37 static mutex_t time_mutex
= DEFAULTMUTEX
;
38 static time_t start_time
= 0;
41 _nscd_set_start_time(int reset
)
43 (void) mutex_lock(&time_mutex
);
44 if (start_time
== 0 || reset
== 1)
45 start_time
= time(NULL
);
46 (void) mutex_unlock(&time_mutex
);
50 _nscd_get_start_time()
59 char *me
= "nscd_init";
61 nscd_cfg_error_t
*err
;
64 * remember when main or forker nscd starts.
66 _nscd_set_start_time(0);
69 * allocate the space for tables
71 if ((rc
= _nscd_alloc_nsw_config()) != NSCD_SUCCESS
||
72 (rc
= _nscd_alloc_service_state_table()) != NSCD_SUCCESS
||
73 (rc
= _nscd_alloc_nsw_state_base()) != NSCD_SUCCESS
||
74 (rc
= _nscd_alloc_nsw_be_info_db()) != NSCD_SUCCESS
||
75 (rc
= _nscd_alloc_getent_ctx_base()) != NSCD_SUCCESS
)
79 * allocate the space for local configuration
82 if ((rc
= _nscd_alloc_switch_cfg()) != NSCD_SUCCESS
||
83 (rc
= _nscd_alloc_frontend_cfg()) != NSCD_SUCCESS
||
84 (rc
= _nscd_alloc_switch_stats()) != NSCD_SUCCESS
)
88 * Create and init the internal address database to keep
89 * track of the memory allocated by _nscd_alloc
91 if (_nscd_create_int_addrDB() == NULL
) {
92 _NSCD_LOG(NSCD_LOG_INT_ADDR
, NSCD_LOG_LEVEL_ERROR
)
93 (me
, "_nscd_create_int_addrDB failed\n");
94 return (NSCD_NO_MEMORY
);
98 * Create and init the internal context database to keep
99 * track of the getent context currently being used
101 if (_nscd_create_getent_ctxDB() == NULL
) {
102 _NSCD_LOG(NSCD_LOG_GETENT_CTX
, NSCD_LOG_LEVEL_ERROR
)
103 (me
, "_nscd_create_getent_ctx_addrDB failed\n");
104 return (NSCD_NO_MEMORY
);
108 * Create the backend info database for each possible source
110 if ((rc
= _nscd_init_all_nsw_be_info_db()) != NSCD_SUCCESS
) {
111 _NSCD_LOG(NSCD_LOG_NSW_STATE
, NSCD_LOG_LEVEL_ERROR
)
112 (me
, "_nscd_init_all_nsw_be_info_db failed (rc = %d)\n",
118 * Create the nscd_nsw_config_t for each possible nss database
120 if ((rc
= _nscd_init_all_nsw_config()) != NSCD_SUCCESS
) {
121 _NSCD_LOG(NSCD_LOG_NSW_STATE
, NSCD_LOG_LEVEL_ERROR
)
122 (me
, "_nscd_init_all_nsw_config failed (rc = %d)\n", rc
);
127 * initialize config/stats management
129 rc
= _nscd_cfg_init(&err
);
130 if (rc
!= NSCD_SUCCESS
) {
132 _nscd_cfg_free_error(err
);
137 * read in the nsswitch configuration
139 rc
= _nscd_cfg_read_nsswitch_file("/etc/nsswitch.conf", &err
);
140 if (rc
!= NSCD_SUCCESS
) {
142 gettext("reading config file %s failed with rc = %d, %s\n"),
143 "/etc/nsswitch.conf", rc
, NSCD_ERR2MSG(err
));
145 _nscd_cfg_free_error(err
);
147 _NSCD_LOG(NSCD_LOG_FRONT_END
, NSCD_LOG_LEVEL_ERROR
)
148 (me
, "unable to read /etc/nsswitch.conf (rc = %d)\n", rc
);
152 * remember which version of /etc/nsswitch.conf that was read
154 _nscd_restart_if_cfgfile_changed();
157 * read in the nscd configuration
159 if (cfgfile
== NULL
) {
160 cfgfile
= "/etc/nscd.conf";
161 if (access(cfgfile
, R_OK
) != 0) {
162 _NSCD_LOG(NSCD_LOG_FRONT_END
, NSCD_LOG_LEVEL_ERROR
)
163 (me
, "unable to read /etc/nscd.conf (rc = %d)\n", rc
);
165 return (NSCD_CFG_FILE_ACCESS_ERROR
);
168 rc
= _nscd_cfg_read_file(cfgfile
, &err
);
169 if (rc
!= NSCD_SUCCESS
) {
171 gettext("reading config file %s failed with rc = %d, %s\n"),
172 cfgfile
, rc
, NSCD_ERR2MSG(err
));
174 _nscd_cfg_free_error(err
);
176 _NSCD_LOG(NSCD_LOG_FRONT_END
, NSCD_LOG_LEVEL_ERROR
)
177 (me
, "unable to read configuration from %s (rc = %d)\n",
183 * remember the name of the config file
184 * in case refresh is requested later
186 if (cfgfile
!= NULL
) {
187 cfgfile_save
= strdup(cfgfile
);
188 if (cfgfile_save
== NULL
)
189 return (NSCD_NO_MEMORY
);
192 return (NSCD_SUCCESS
);
198 char *me
= "nscd_refresh";
201 nscd_cfg_error_t
*err
;
205 * re-read the nscd configuration
207 if (cfgfile_save
== NULL
)
208 cfgfile
= "/etc/nscd.conf";
210 cfgfile
= cfgfile_save
;
212 if (access(cfgfile
, R_OK
) != 0) {
213 (void) snprintf(errmsg
, sizeof (errmsg
),
214 "unable to read the config file %s (rc = %d), %s\n",
215 cfgfile
, NSCD_CFG_FILE_ACCESS_ERROR
,
221 rc
= _nscd_cfg_read_file(cfgfile
, &err
);
222 if (rc
!= NSCD_SUCCESS
) {
223 (void) snprintf(errmsg
, sizeof (errmsg
),
224 "unable to parse the config file %s (rc = %d), %s\n",
225 cfgfile
, rc
, NSCD_ERR2MSG(err
));
230 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ALL
)
231 (me
, "nscd configuration refreshed successfully\n");
233 return (NSCD_SUCCESS
);
238 _nscd_cfg_free_error(err
);
240 _NSCD_LOG(NSCD_LOG_CONFIG
, NSCD_LOG_LEVEL_ERROR
)
241 (me
, "%s\n", errmsg
);