const char* os_readdir() { #if defined(PLATFORM_IS_WINDOWS) if(opendir_handle == INVALID_HANDLE_VALUE) return NULL; size_t len = strlen(opendir_data.cFileName) + 1; if(skip_entry(opendir_data.cFileName, len - 1)) { if(!FindNextFile(opendir_handle, &opendir_data)) { os_closedir(); return NULL; } return os_readdir(); } char* cstring = (char*)pony_alloc(len); memcpy(cstring, opendir_data.cFileName, len); if(!FindNextFile(opendir_handle, &opendir_data)) os_closedir(); return cstring; #elif defined(PLATFORM_IS_POSIX_BASED) if(opendir_handle == NULL) return NULL; struct dirent entry; struct dirent* result; if(readdir_r(opendir_handle, &entry, &result) != 0) return NULL; if(result == NULL) return NULL; size_t len = strlen(result->d_name) + 1; if(skip_entry(result->d_name, len - 1)) return os_readdir(); char* cstring = pony_alloc(len); memcpy(cstring, result->d_name, len); return cstring; #else return NULL; #endif }
/* * Get a service entry. Steps: * * 1. Parse entry attributes * 2. Determine service id * 3. Insert entry in table */ static void get_service_entry( int fd, pset_h sconfs, const char *name, struct service_config *defaults ) { struct service_config *scp ; const char *func = "get_service_entry" ; scp = sc_alloc( name ) ; if ( scp == NULL ) { skip_entry( fd ) ; return ; } /* Now fill in default attributes if given. */ if ( SC_SPECIFIED( defaults, A_LOG_ON_SUCCESS ) && ! SC_IS_PRESENT( scp, A_LOG_ON_SUCCESS) ) fill_attribute( A_LOG_ON_SUCCESS, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_LOG_ON_FAILURE ) && ! SC_IS_PRESENT( scp, A_LOG_ON_FAILURE ) ) fill_attribute( A_LOG_ON_FAILURE, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_ONLY_FROM ) && ! SC_IS_PRESENT( scp, A_ONLY_FROM ) ) fill_attribute( A_ONLY_FROM, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_NO_ACCESS ) && ! SC_IS_PRESENT( scp, A_NO_ACCESS ) ) fill_attribute( A_NO_ACCESS, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_PASSENV ) && ! SC_IS_PRESENT( scp, A_PASSENV ) ) fill_attribute( A_PASSENV, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_ACCESS_TIMES ) && ! SC_IS_PRESENT( scp, A_ACCESS_TIMES ) ) fill_attribute( A_ACCESS_TIMES, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_BANNER ) && ! SC_IS_PRESENT( scp, A_BANNER ) ) fill_attribute( A_BANNER, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_BANNER_SUCCESS ) && ! SC_IS_PRESENT( scp, A_BANNER_SUCCESS ) ) fill_attribute( A_BANNER_SUCCESS, scp, defaults ) ; if ( SC_SPECIFIED( defaults, A_BANNER_FAIL ) && ! SC_IS_PRESENT( scp, A_BANNER_FAIL ) ) fill_attribute( A_BANNER_FAIL, scp, defaults ) ; if ( parse_entry( SERVICE_ENTRY, fd, scp ) == FAILED ) { sc_free( scp ) ; skip_entry( fd ) ; return ; } /* * If no service id was specified, set it equal to the service name */ if ( ! SC_SPECIFIED( scp, A_ID ) ) { if ( (SC_ID(scp) = new_string( SC_NAME(scp) )) ) SC_PRESENT( scp, A_ID ) ; else { out_of_memory( func ) ; sc_free( scp ) ; return ; } } if ( ! (pset_add( sconfs, scp )) ) { out_of_memory( func ) ; sc_free( scp ) ; return ; } }
/* * Read the configuration file (descriptor fd) and place all * services found there in the configuration. */ void parse_conf_file( int fd, struct configuration *confp, const char *filename) { pset_h sconfs = CNF_SERVICE_CONFS( confp ) ; struct service_config *default_config = CNF_DEFAULTS( confp ) ; boolean_e found_defaults = NO ; struct service_config default_default_config ; const char *func = "parse_conf_file" ; int incfd; line_count = 0 ; current_file = filename; CLEAR( default_default_config ) ; for ( ;; ) { entry_e entry_type ; char *service_name = NULL; /* * if find_next_entry is successful, service_name * will point to malloc'ed memory */ entry_type = find_next_entry( fd, &service_name ) ; switch ( entry_type ) { case INCLUDE_ENTRY: { int saved_line_count = line_count; incfd = open(service_name, O_RDONLY); if( incfd < 0 ) { parsemsg( LOG_ERR, func, "Unable to open included configuration file: %s", service_name); break; } parsemsg( LOG_DEBUG,func, "Reading included configuration file: %s",service_name); parse_conf_file(incfd, confp, service_name); /* * parse_conf_file eventually calls Srdline, try Sclosing it * to unmmap memory. */ Sclose(incfd); /* Restore since we've returned from included file */ current_file = filename; line_count = saved_line_count; } break; case INCLUDEDIR_ENTRY: { int saved_line_count = line_count; handle_includedir(service_name, confp); current_file = filename; line_count = saved_line_count; } break; case SERVICE_ENTRY: get_service_entry( fd, sconfs, service_name, default_config ) ; break ; case DEFAULTS_ENTRY: if ( found_defaults == YES ) { parsemsg( LOG_ERR, func, "only 1 defaults entry is allowed. This entry will be ignored" ) ; skip_entry( fd ) ; } else if ( parse_entry( DEFAULTS_ENTRY, fd, default_config ) == OK ) { found_defaults = YES ; /* * We must check bind_address to see if it was deferred. */ if (SC_SPECIFIED( default_config, A_BIND) && SC_BIND_ADDR(default_config) == NULL) M_CLEAR( default_config->sc_specified_attributes, A_BIND ) ; } break ; case BAD_ENTRY: skip_entry( fd ) ; break ; case NO_ENTRY: return ; } if (service_name) free(service_name); } }