2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
11 #include <sys/types.h>
12 #include <netinet/in.h>
25 void cnf_free( struct configuration *confp )
28 pset_h sconfs = confp->cnf_service_confs ;
30 for ( u = 0 ; u < pset_count( sconfs ) ; u++ )
31 sc_free( SCP( pset_pointer( sconfs, u ) ) ) ;
32 pset_destroy( sconfs ) ;
33 if ( confp->cnf_defaults )
34 sc_free( confp->cnf_defaults ) ;
41 * Extract from 'confp' the service that matches 'scp'
43 struct service_config *cnf_extract( struct configuration *confp,
44 struct service_config *scp )
46 pset_h stab = confp->cnf_service_confs ;
49 for ( u = 0 ; u < pset_count( stab ) ; u++ )
51 struct service_config *iscp = SCP( pset_pointer( stab, u ) ) ;
53 if ( ! EQ( SC_ID(scp), SC_ID(iscp) ) || sc_different_confs( scp, iscp ) )
56 pset_remove_index( stab, u ) ;
63 void cnf_dump( struct configuration *confp, int fd )
68 stab = confp->cnf_service_confs;
70 sc_dump( confp->cnf_defaults, fd, 0, TRUE ) ;
71 tabprint( fd, 0, "\n" ) ;
73 for ( u = 0 ; u < pset_count( stab ) ; u++ )
75 sc_dump( SCP( pset_pointer( stab, u ) ), fd, 0, FALSE ) ;
76 Sputchar( fd, '\n' ) ;
81 status_e cnf_init( struct configuration *confp, int *fdp, psi_h *iterp )
86 struct service_config *scp ;
87 const char *func = "cnf_init" ;
90 * Open configuration file
92 fd = open( ps.ros.config_file, O_RDONLY ) ;
95 msg( LOG_ERR, func, "open( %s ) failed: %m", ps.ros.config_file ) ;
99 if ( ( pset = pset_create( 0, 0 ) ) == NULL )
101 msg( LOG_CRIT, func, "can't create service table" ) ;
102 (void) Sclose( fd ) ;
106 if ( ( scp = sc_alloc( CHAR_NULL ) ) == NULL )
108 msg( LOG_ERR, func, "can't allocate defaults service" ) ;
109 pset_destroy( pset ) ;
110 (void) Sclose( fd ) ;
114 if ( ( pset_iter = psi_create( pset ) ) == NULL )
116 msg( LOG_ERR, func, "can't create service table iterator" ) ;
118 pset_destroy( pset ) ;
119 (void) Sclose( fd ) ;
124 confp->cnf_service_confs = pset ;
125 confp->cnf_defaults = scp ;
131 static void destroy_service( struct service *sp )
133 svc_deactivate( sp ) ;
140 * Try to start all services.
141 * Return the # of services started.
143 unsigned cnf_start_services( struct configuration *confp )
145 pset_h sconfs = confp->cnf_service_confs ;
146 unsigned services_started = 0 ;
148 const char *func = "cnf_start_services" ;
150 for ( u = 0 ; u < pset_count( sconfs ) ; u++ )
152 struct service_config *scp = SCP( pset_pointer( sconfs, u ) ) ;
155 if ( ( sp = svc_new( scp ) ) == NULL )
162 if ( svc_activate( sp ) == FAILED )
165 "Service %s failed to start and is deactivated.",
173 * descriptors_free can be negative without a descriptor-allocating
174 * system call failing because some of the descriptors we reserve
177 if ( ps.rws.descriptors_free < 0 )
180 "Service %s disabled because of lack of file descriptors",
182 destroy_service( sp ) ;
187 * Activation successful; add service to service table
189 if ( pset_add( SERVICES( ps ), sp ) == NULL )
191 out_of_memory( func ) ;
192 destroy_service( sp ) ;
201 msg( LOG_DEBUG, func, "Started service: %s", SVC_ID( sp ) ) ;
205 * All the configurations have been linked to their services
206 * so we don't need to hold references to them in the pset.
207 * We need to clear the pset so that the cnf_free will not free the memory.
209 pset_clear( sconfs ) ;
212 msg( LOG_DEBUG, func, "mask_max = %d, services_started = %d",
213 ps.rws.mask_max, services_started ) ;
215 return( services_started ) ;